SPIGOT-1638 / SPIGOT-1673: Rework Potions API

By: t00thpick1 <t00thpick1dirko@gmail.com>
This commit is contained in:
Bukkit/Spigot
2016-03-02 20:45:27 -05:00
parent a7e0658387
commit 949890d29d
16 changed files with 407 additions and 312 deletions

View File

@@ -402,9 +402,6 @@ public enum Material {
GHAST_TEAR(370), GHAST_TEAR(370),
GOLD_NUGGET(371), GOLD_NUGGET(371),
NETHER_STALK(372), NETHER_STALK(372),
/**
* @see Potion
*/
POTION(373, 1, MaterialData.class), POTION(373, 1, MaterialData.class),
GLASS_BOTTLE(374), GLASS_BOTTLE(374),
SPIDER_EYE(375), SPIDER_EYE(375),
@@ -473,10 +470,7 @@ public enum Material {
SPLASH_POTION(438, 1), SPLASH_POTION(438, 1),
SPECTRAL_ARROW(439), SPECTRAL_ARROW(439),
TIPPED_ARROW(440), TIPPED_ARROW(440),
/** LINGERING_POTION(441, 1),
* @see Potion
*/
LINGERING_POTION(441, 1, MaterialData.class),
SHIELD(442, 1, 336), SHIELD(442, 1, 336),
ELYTRA(443, 1), ELYTRA(443, 1),
BOAT_SPRUCE(444, 1), BOAT_SPRUCE(444, 1),

View File

@@ -318,6 +318,20 @@ public interface World extends PluginMessageRecipient, Metadatable {
*/ */
public Arrow spawnArrow(Location location, Vector direction, float speed, float spread); public Arrow spawnArrow(Location location, Vector direction, float speed, float spread);
/**
* Creates an arrow entity of the given class at the given {@link Location}
*
* @param <T> type of arrow to spawn
* @param location Location to spawn the arrow
* @param direction Direction to shoot the arrow in
* @param speed Speed of the arrow. A recommend speed is 0.6
* @param spread Spread of the arrow. A recommend spread is 12
* @param clazz the Entity class for the arrow
* {@link org.bukkit.entity.SpectralArrow},{@link org.bukkit.entity.Arrow},{@link org.bukkit.entity.TippedArrow}
* @return Arrow entity spawned as a result of this method
*/
public <T extends Arrow> T spawnArrow(Location location, Vector direction, float speed, float spread, Class<T> clazz);
/** /**
* Creates a tree at the given {@link Location} * Creates a tree at the given {@link Location}
* *

View File

@@ -2,9 +2,10 @@ package org.bukkit.entity;
import java.util.List; import java.util.List;
import org.bukkit.Color; import org.bukkit.Color;
import org.bukkit.Effect;
import org.bukkit.Particle; import org.bukkit.Particle;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
/** /**
* Represents an area effect cloud which will imbue a potion effect onto * Represents an area effect cloud which will imbue a potion effect onto
@@ -71,6 +72,7 @@ public interface AreaEffectCloud extends Entity {
* @param duration duration on use delta * @param duration duration on use delta
*/ */
void setDurationOnUse(int duration); void setDurationOnUse(int duration);
/** /**
* Gets the initial radius of the cloud. * Gets the initial radius of the cloud.
* *
@@ -130,33 +132,67 @@ public interface AreaEffectCloud extends Entity {
void setParticle(Particle particle); void setParticle(Particle particle);
/** /**
* Get a copy of all effects which can be applied by this cloud. No * Sets the underlying potion data
* guarantees are made about the nature of the returned list.
* *
* @return the list of all effects * @param data PotionData to set the base potion state to
*/ */
List<PotionEffect> getEffects(); void setBasePotionData(PotionData data);
/** /**
* Add an effect which can be applied by this cloud. * Returns the potion data about the base potion
* *
* @param effect the effect to add * @return a PotionData object
*/ */
void addEffect(PotionEffect effect); PotionData getBasePotionData();
/** /**
* Remove an effect from this cloud. * Checks for the presence of custom potion effects.
* *
* @param effect the effect to remove * @return true if custom potion effects are applied
*/ */
void removeEffect(PotionEffect effect); boolean hasCustomEffects();
/** /**
* Set the effects of this cloud. Will remove all existing effects. * Gets an immutable list containing all custom potion effects applied to
* this cloud.
* <p>
* Plugins should check that hasCustomEffects() returns true before calling
* this method.
* *
* @param effects the new effects to set * @return the immutable list of custom potion effects
*/ */
void setEffects(List<PotionEffect> effects); List<PotionEffect> getCustomEffects();
/**
* Adds a custom potion effect to this cloud.
*
* @param effect the potion effect to add
* @param overwrite true if any existing effect of the same type should be
* overwritten
* @return true if the effect was added as a result of this call
*/
boolean addCustomEffect(PotionEffect effect, boolean overwrite);
/**
* Removes a custom potion effect from this cloud.
*
* @param type the potion effect type to remove
* @return true if the an effect was removed as a result of this call
*/
boolean removeCustomEffect(PotionEffectType type);
/**
* Checks for a specific custom potion effect type on this cloud.
*
* @param type the potion effect type to check for
* @return true if the potion has this effect
*/
boolean hasCustomEffect(PotionEffectType type);
/**
* Removes all custom potion effects from this cloud.
*/
void clearCustomEffects();
/** /**
* Gets the color of this cloud. Will be applied as a tint to its particles. * Gets the color of this cloud. Will be applied as a tint to its particles.

View File

@@ -173,9 +173,13 @@ public enum EntityType {
ENDER_CRYSTAL("EnderCrystal", EnderCrystal.class, 200), ENDER_CRYSTAL("EnderCrystal", EnderCrystal.class, 200),
// These don't have an entity ID in nms.EntityTypes. // These don't have an entity ID in nms.EntityTypes.
/** /**
* A flying splash potion. * A flying splash potion
*/ */
SPLASH_POTION(null, ThrownPotion.class, -1, false), SPLASH_POTION(null, SplashPotion.class, -1, false),
/**
* A flying lingering potion
*/
LINGERING_POTION(null, LingeringPotion.class, -1, false),
AREA_EFFECT_CLOUD(null, AreaEffectCloud.class, -1), AREA_EFFECT_CLOUD(null, AreaEffectCloud.class, -1),
/** /**
* A flying chicken egg. * A flying chicken egg.

View File

@@ -0,0 +1,6 @@
package org.bukkit.entity;
/**
* Represents a thrown lingering potion bottle
*/
public interface LingeringPotion extends ThrownPotion { }

View File

@@ -0,0 +1,6 @@
package org.bukkit.entity;
/**
* Represents a thrown splash potion bottle
*/
public interface SplashPotion extends ThrownPotion { }

View File

@@ -20,8 +20,8 @@ public interface ThrownPotion extends Projectile {
/** /**
* Returns a copy of the ItemStack for this thrown potion. * Returns a copy of the ItemStack for this thrown potion.
* <p> * <p>
* Altering this copy will not alter the thrown potion directly. If you * Altering this copy will not alter the thrown potion directly. If you want
* want to alter the thrown potion, you must use the {@link * to alter the thrown potion, you must use the {@link
* #setItem(ItemStack) setItemStack} method. * #setItem(ItemStack) setItemStack} method.
* *
* @return A copy of the ItemStack for this thrown potion. * @return A copy of the ItemStack for this thrown potion.
@@ -31,7 +31,9 @@ public interface ThrownPotion extends Projectile {
/** /**
* Set the ItemStack for this thrown potion. * Set the ItemStack for this thrown potion.
* <p> * <p>
* The ItemStack must be a potion, otherwise an exception is thrown. * The ItemStack must be of type {@link org.bukkit.Material#SPLASH_POTION}
* or {@link org.bukkit.Material#LINGERING_POTION}, otherwise an exception
* is thrown.
* *
* @param item New ItemStack * @param item New ItemStack
*/ */

View File

@@ -1,3 +1,78 @@
package org.bukkit.entity; package org.bukkit.entity;
public interface TippedArrow extends Arrow {} import java.util.List;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
public interface TippedArrow extends Arrow {
/**
* Sets the underlying potion data
*
* @param data PotionData to set the base potion state to
*/
void setBasePotionData(PotionData data);
/**
* Returns the potion data about the base potion
*
* @return a PotionData object
*/
PotionData getBasePotionData();
/**
* Checks for the presence of custom potion effects.
*
* @return true if custom potion effects are applied
*/
boolean hasCustomEffects();
/**
* Gets an immutable list containing all custom potion effects applied to
* this arrow.
* <p>
* Plugins should check that hasCustomEffects() returns true before calling
* this method.
*
* @return the immutable list of custom potion effects
*/
List<PotionEffect> getCustomEffects();
/**
* Adds a custom potion effect to this arrow.
*
* @param effect the potion effect to add
* @param overwrite true if any existing effect of the same type should be
* overwritten
* @return true if the effect was added as a result of this call
*/
boolean addCustomEffect(PotionEffect effect, boolean overwrite);
/**
* Removes a custom potion effect from this arrow.
*
* @param type the potion effect type to remove
* @return true if the an effect was removed as a result of this call
* @throws IllegalArgumentException if this operation would leave the Arrow
* in a state with no Custom Effects and PotionType.UNCRAFTABLE
*/
boolean removeCustomEffect(PotionEffectType type);
/**
* Checks for a specific custom potion effect type on this arrow.
*
* @param type the potion effect type to check for
* @return true if the potion has this effect
*/
boolean hasCustomEffect(PotionEffectType type);
/**
* Removes all custom potion effects from this arrow.
*
* @throws IllegalArgumentException if this operation would leave the Arrow
* in a state with no Custom Effects and PotionType.UNCRAFTABLE
*/
void clearCustomEffects();
}

View File

@@ -1,16 +1,31 @@
package org.bukkit.inventory.meta; package org.bukkit.inventory.meta;
import org.bukkit.Material;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionType;
import java.util.List; import java.util.List;
/** /**
* Represents a potion ({@link Material#POTION}) that can have custom effects. * Represents a potion or item that can have custom effects.
*/ */
public interface PotionMeta extends ItemMeta { public interface PotionMeta extends ItemMeta {
/**
* Sets the underlying potion data
*
* @param data PotionData to set the base potion state to
*/
void setBasePotionData(PotionData data);
/**
* Returns the potion data about the base potion
*
* @return a PotionData object
*/
PotionData getBasePotionData();
/** /**
* Checks for the presence of custom potion effects. * Checks for the presence of custom potion effects.
* *
@@ -22,8 +37,8 @@ public interface PotionMeta extends ItemMeta {
* Gets an immutable list containing all custom potion effects applied to * Gets an immutable list containing all custom potion effects applied to
* this potion. * this potion.
* <p> * <p>
* Plugins should check that hasCustomEffects() returns true before * Plugins should check that hasCustomEffects() returns true before calling
* calling this method. * this method.
* *
* @return the immutable list of custom potion effects * @return the immutable list of custom potion effects
*/ */
@@ -58,12 +73,13 @@ public interface PotionMeta extends ItemMeta {
/** /**
* Moves a potion effect to the top of the potion effect list. * Moves a potion effect to the top of the potion effect list.
* <p> * <p>
* This causes the client to display the potion effect in the potion's * This causes the client to display the potion effect in the potion's name.
* name.
* *
* @param type the potion effect type to move * @param type the potion effect type to move
* @return true if the potion meta changed as a result of this call * @return true if the potion meta changed as a result of this call
* @deprecated use {@link #setType(PotionType)}
*/ */
@Deprecated
boolean setMainEffect(PotionEffectType type); boolean setMainEffect(PotionEffectType type);
/** /**
@@ -73,5 +89,6 @@ public interface PotionMeta extends ItemMeta {
*/ */
boolean clearCustomEffects(); boolean clearCustomEffects();
@Override
PotionMeta clone(); PotionMeta clone();
} }

View File

@@ -6,17 +6,17 @@ import org.apache.commons.lang.Validate;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import com.google.common.collect.ImmutableList;
/** /**
* Represents a minecraft potion * Potion Adapter for pre-1.9 data values
* see @PotionMeta for 1.9+
*/ */
@Deprecated
public class Potion { public class Potion {
private boolean extended = false; private boolean extended = false;
private boolean splash = false; private boolean splash = false;
private int level = 1; private int level = 1;
private int name = -1;
private PotionType type; private PotionType type;
/** /**
@@ -29,18 +29,11 @@ public class Potion {
* @see #Potion(int) * @see #Potion(int)
*/ */
public Potion(PotionType type) { public Potion(PotionType type) {
Validate.notNull(type, "Null PotionType");
this.type = type; this.type = type;
if (type != null) {
this.name = type.getDamageValue();
}
if (type == null || type == PotionType.WATER) {
this.level = 0;
}
} }
/** /**
* @param type the type of the potion
* @param tier the tier of the potion
* @deprecated In favour of {@link #Potion(PotionType, int)} * @deprecated In favour of {@link #Potion(PotionType, int)}
*/ */
@Deprecated @Deprecated
@@ -50,9 +43,6 @@ public class Potion {
} }
/** /**
* @param type the type of the potion
* @param tier the tier of the potion
* @param splash whether the potion is a splash potion
* @deprecated In favour of {@link #Potion(PotionType, int, boolean)} * @deprecated In favour of {@link #Potion(PotionType, int, boolean)}
*/ */
@Deprecated @Deprecated
@@ -61,10 +51,6 @@ public class Potion {
} }
/** /**
* @param type the type of the potion
* @param tier the tier of the potion
* @param splash whether the potion is a splash potion
* @param extended whether the potion has an extended duration
* @deprecated In favour of {@link #Potion(PotionType, int, boolean, * @deprecated In favour of {@link #Potion(PotionType, int, boolean,
* boolean)} * boolean)}
*/ */
@@ -83,7 +69,6 @@ public class Potion {
public Potion(PotionType type, int level) { public Potion(PotionType type, int level) {
this(type); this(type);
Validate.notNull(type, "Type cannot be null"); Validate.notNull(type, "Type cannot be null");
Validate.isTrue(type != PotionType.WATER, "Water bottles don't have a level!");
Validate.isTrue(level > 0 && level < 3, "Level must be 1 or 2"); Validate.isTrue(level > 0 && level < 3, "Level must be 1 or 2");
this.level = level; this.level = level;
} }
@@ -120,17 +105,11 @@ public class Potion {
} }
/** /**
* Create a potion with a specific name. * @Deprecated
*
* @param name The name index (0-63)
*/ */
@Deprecated
public Potion(int name) { public Potion(int name) {
this(PotionType.getByDamageValue(name & POTION_BIT)); this(PotionType.WATER);
this.name = name & NAME_BIT;
if ((name & POTION_BIT) == 0) {
// If it's 0 it would've become PotionType.WATER, but it should actually be mundane potion
this.type = null;
}
} }
/** /**
@@ -161,8 +140,11 @@ public class Potion {
*/ */
public void apply(ItemStack to) { public void apply(ItemStack to) {
Validate.notNull(to, "itemstack cannot be null"); Validate.notNull(to, "itemstack cannot be null");
Validate.isTrue(to.getType() == Material.POTION, "given itemstack is not a potion"); Validate.isTrue(to.hasItemMeta(), "given itemstack is not a potion");
to.setDurability(toDamageValue()); Validate.isTrue(to.getItemMeta() instanceof PotionMeta, "given itemstack is not a potion");
PotionMeta meta = (PotionMeta) to.getItemMeta();
meta.setBasePotionData(new PotionData(type, level == 2, extended));
to.setItemMeta(meta);
} }
/** /**
@@ -198,8 +180,7 @@ public class Potion {
* @return The effects that this potion applies * @return The effects that this potion applies
*/ */
public Collection<PotionEffect> getEffects() { public Collection<PotionEffect> getEffects() {
if (type == null) return ImmutableList.<PotionEffect>of(); return getBrewer().getEffects(type, level == 2, extended);
return getBrewer().getEffectsFromDamage(toDamageValue());
} }
/** /**
@@ -215,6 +196,7 @@ public class Potion {
* Returns the {@link Tier} of this potion. * Returns the {@link Tier} of this potion.
* *
* @return The tier of this potion * @return The tier of this potion
* @deprecated
*/ */
@Deprecated @Deprecated
public Tier getTier() { public Tier getTier() {
@@ -307,8 +289,7 @@ public class Potion {
*/ */
public void setLevel(int level) { public void setLevel(int level) {
Validate.notNull(this.type, "No-effect potions don't have a level."); Validate.notNull(this.type, "No-effect potions don't have a level.");
int max = type.getMaxLevel(); Validate.isTrue(level > 0 && level <= 2, "Level must be between 1 and 2 for this potion");
Validate.isTrue(level > 0 && level <= max, "Level must be " + (max == 1 ? "" : "between 1 and ") + max + " for this potion");
this.level = level; this.level = level;
} }
@@ -317,28 +298,11 @@ public class Potion {
* item stacks. * item stacks.
* *
* @return The damage value of this potion * @return The damage value of this potion
* @deprecated Magic value * @deprecated Non-functional
*/ */
@Deprecated @Deprecated
public short toDamageValue() { public short toDamageValue() {
short damage;
if (type == PotionType.WATER) {
return 0; return 0;
} else if (type == null) {
// Without this, mundanePotion.toDamageValue() would return 0
damage = (short) (name == 0 ? 8192 : name);
} else {
damage = (short) (level - 1);
damage <<= TIER_SHIFT;
damage |= (short) type.getDamageValue();
}
if (splash) {
damage |= SPLASH_BIT;
}
if (extended) {
damage |= EXTENDED_BIT;
}
return damage;
} }
/** /**
@@ -349,7 +313,17 @@ public class Potion {
* @return The created ItemStack * @return The created ItemStack
*/ */
public ItemStack toItemStack(int amount) { public ItemStack toItemStack(int amount) {
return new ItemStack(Material.POTION, amount, toDamageValue()); Material material;
if (isSplash()) {
material = Material.SPLASH_POTION;
} else {
material = Material.POTION;
}
ItemStack itemStack = new ItemStack(material, amount);
PotionMeta meta = (PotionMeta) itemStack.getItemMeta();
meta.setBasePotionData(new PotionData(type, level == 2, extended));
itemStack.setItemMeta(meta);
return itemStack;
} }
@Deprecated @Deprecated
@@ -383,20 +357,63 @@ public class Potion {
private static final int SPLASH_BIT = 0x4000; private static final int SPLASH_BIT = 0x4000;
private static final int TIER_BIT = 0x20; private static final int TIER_BIT = 0x20;
private static final int TIER_SHIFT = 5; private static final int TIER_SHIFT = 5;
private static final int NAME_BIT = 0x3F;
/** /**
* *
* @param damage the damage value * @param damage the damage value
* @return the produced potion * @return the produced potion
* @deprecated Magic value
*/ */
@Deprecated
public static Potion fromDamage(int damage) { public static Potion fromDamage(int damage) {
PotionType type = PotionType.getByDamageValue(damage & POTION_BIT); PotionType type;
switch (damage & POTION_BIT) {
case 0:
type = PotionType.WATER;
break;
case 1:
type = PotionType.REGEN;
break;
case 2:
type = PotionType.SPEED;
break;
case 3:
type = PotionType.FIRE_RESISTANCE;
break;
case 4:
type = PotionType.POISON;
break;
case 5:
type = PotionType.INSTANT_HEAL;
break;
case 6:
type = PotionType.NIGHT_VISION;
break;
case 8:
type = PotionType.WEAKNESS;
break;
case 9:
type = PotionType.STRENGTH;
break;
case 10:
type = PotionType.SLOWNESS;
break;
case 11:
type = PotionType.JUMP;
break;
case 12:
type = PotionType.INSTANT_DAMAGE;
break;
case 13:
type = PotionType.WATER_BREATHING;
break;
case 14:
type = PotionType.INVISIBILITY;
break;
default:
type = PotionType.WATER;
}
Potion potion; Potion potion;
if (type == null || type == PotionType.WATER) { if (type == null || type == PotionType.WATER) {
potion = new Potion(damage & NAME_BIT); potion = new Potion(PotionType.WATER);
} else { } else {
int level = (damage & TIER_BIT) >> TIER_SHIFT; int level = (damage & TIER_BIT) >> TIER_SHIFT;
level++; level++;
@@ -405,7 +422,7 @@ public class Potion {
if ((damage & SPLASH_BIT) > 0) { if ((damage & SPLASH_BIT) > 0) {
potion = potion.splash(); potion = potion.splash();
} }
if ((type == null || !type.isInstant()) && (damage & EXTENDED_BIT) > 0) { if ((damage & EXTENDED_BIT) > 0) {
potion = potion.extend(); potion = potion.extend();
} }
return potion; return potion;
@@ -442,10 +459,10 @@ public class Potion {
/** /**
* *
* @return the name id * @return the name id
* @deprecated Magic value * @deprecated Non-functional
*/ */
@Deprecated @Deprecated
public int getNameId() { public int getNameId() {
return name; return 0;
} }
} }

View File

@@ -24,8 +24,17 @@ public interface PotionBrewer {
* *
* @param damage The data value of the potion * @param damage The data value of the potion
* @return The list of effects * @return The list of effects
* @deprecated Magic value * @deprecated Non-Functional
*/ */
@Deprecated @Deprecated
public Collection<PotionEffect> getEffectsFromDamage(int damage); public Collection<PotionEffect> getEffectsFromDamage(int damage);
/**
* Returns a collection of {@link PotionEffect} that would be applied from
* a potion with the given type.
*
* @param type The type of the potion
* @return The list of effects
*/
public Collection<PotionEffect> getEffects(PotionType type, boolean upgraded, boolean extended);
} }

View File

@@ -0,0 +1,64 @@
package org.bukkit.potion;
import org.apache.commons.lang.Validate;
public final class PotionData {
private final PotionType type;
private final boolean extended;
private final boolean upgraded;
/**
* Instantiates a final PotionData object to contain information about a
* Potion
*
* @param type the type of the Potion
* @param extended whether the potion is extended PotionType#isExtendable()
* must be true
* @param upgraded whether the potion is upgraded PotionType#isUpgradable()
* must be true
*/
public PotionData(PotionType type, boolean extended, boolean upgraded) {
Validate.notNull(type, "Potion Type must not be null");
Validate.isTrue(!upgraded || type.isUpgradeable(), "Potion Type is not upgradable");
Validate.isTrue(!extended || type.isExtendable(), "Potion Type is not extendable");
Validate.isTrue(!upgraded || !extended, "Potion cannot be both extended and upgraded");
this.type = type;
this.extended = extended;
this.upgraded = upgraded;
}
public PotionData(PotionType type) {
this(type, false, false);
}
/**
* Gets the type of the potion, Type matches up with each kind of craftable
* potion
*
* @return the potion type
*/
public PotionType getType() {
return type;
}
/**
* Checks if the potion is in an upgraded state. This refers to whether or
* not the potion is Tier 2, such as Potion of Fire Resistance II.
*
* @return true if the potion is upgraded;
*/
public boolean isUpgraded() {
return upgraded;
}
/**
* Checks if the potion is in an extended state. This refers to the extended
* duration potions
*
* @return true if the potion is extended
*/
public boolean isExtended() {
return extended;
}
}

View File

@@ -62,7 +62,7 @@ public class PotionEffect implements ConfigurationSerializable {
* @param particles the particle status, see {@link PotionEffect#hasParticles()} * @param particles the particle status, see {@link PotionEffect#hasParticles()}
*/ */
public PotionEffect(PotionEffectType type, int duration, int amplifier, boolean ambient, boolean particles) { public PotionEffect(PotionEffectType type, int duration, int amplifier, boolean ambient, boolean particles) {
this(type, duration, amplifier, ambient, true, null); this(type, duration, amplifier, ambient, particles, null);
} }
/** /**

View File

@@ -162,7 +162,7 @@ public abstract class PotionEffectType {
* @return a resulting potion effect * @return a resulting potion effect
*/ */
public PotionEffect createEffect(int duration, int amplifier) { public PotionEffect createEffect(int duration, int amplifier) {
return Potion.getBrewer().createEffect(this, duration, amplifier); return new PotionEffect(this, isInstant() ? 1 : (int) (duration * getDurationModifier()), amplifier);
} }
/** /**

View File

@@ -1,73 +1,94 @@
package org.bukkit.potion; package org.bukkit.potion;
public enum PotionType { /**
WATER(0, null, 0), * This enum reflects and matches each potion state that can be obtained from
REGEN(1, PotionEffectType.REGENERATION, 2), * the Creative mode inventory
SPEED(2, PotionEffectType.SPEED, 2),
FIRE_RESISTANCE(3, PotionEffectType.FIRE_RESISTANCE, 1),
POISON(4, PotionEffectType.POISON, 2),
INSTANT_HEAL(5, PotionEffectType.HEAL, 2),
NIGHT_VISION(6, PotionEffectType.NIGHT_VISION, 1),
WEAKNESS(8, PotionEffectType.WEAKNESS, 1),
STRENGTH(9, PotionEffectType.INCREASE_DAMAGE, 2),
SLOWNESS(10, PotionEffectType.SLOW, 1),
JUMP(11, PotionEffectType.JUMP, 2),
INSTANT_DAMAGE(12, PotionEffectType.HARM, 2),
WATER_BREATHING(13, PotionEffectType.WATER_BREATHING, 1),
INVISIBILITY(14, PotionEffectType.INVISIBILITY, 1),
/**
* @deprecated not representable with a damage value!
*/ */
@Deprecated public enum PotionType {
LUCK(-1, PotionEffectType.LUCK, 1), UNCRAFTABLE(null, false, false),
WATER(null, false, false),
MUNDANE(null, false, false),
THICK(null, false, false),
AWKWARD(null, false, false),
NIGHT_VISION(PotionEffectType.NIGHT_VISION, false, true),
INVISIBILITY(PotionEffectType.INVISIBILITY, false, true),
JUMP(PotionEffectType.JUMP, true, true),
FIRE_RESISTANCE(PotionEffectType.FIRE_RESISTANCE, false, true),
SPEED(PotionEffectType.SPEED, true, true),
SLOWNESS(PotionEffectType.SLOW, false, true),
WATER_BREATHING(PotionEffectType.WATER_BREATHING, false, true),
INSTANT_HEAL(PotionEffectType.HEAL, true, false),
INSTANT_DAMAGE(PotionEffectType.HARM, true, false),
POISON(PotionEffectType.POISON, true, true),
REGEN(PotionEffectType.REGENERATION, true, true),
STRENGTH(PotionEffectType.INCREASE_DAMAGE, true, true),
WEAKNESS(PotionEffectType.WEAKNESS, false, true),
LUCK(PotionEffectType.LUCK, false, false);
; ;
private final int damageValue, maxLevel;
private final PotionEffectType effect; private final PotionEffectType effect;
private final boolean upgradeable;
private final boolean extendable;
PotionType(int damageValue, PotionEffectType effect, int maxLevel) { PotionType(PotionEffectType effect, boolean upgradeable, boolean extendable) {
this.damageValue = damageValue;
this.effect = effect; this.effect = effect;
this.maxLevel = maxLevel; this.upgradeable = upgradeable;
this.extendable = extendable;
} }
public PotionEffectType getEffectType() { public PotionEffectType getEffectType() {
return effect; return effect;
} }
public boolean isInstant() {
return effect != null && effect.isInstant();
}
/** /**
* Checks if the potion type has an upgraded state.
* This refers to whether or not the potion type can be Tier 2,
* such as Potion of Fire Resistance II.
* *
* @return the damage value * @return true if the potion type can be upgraded;
* @deprecated Magic value */
public boolean isUpgradeable() {
return upgradeable;
}
/**
* Checks if the potion type has an extended state.
* This refers to the extended duration potions
*
* @return true if the potion type can be extended
*/
public boolean isExtendable() {
return extendable;
}
/**
* @deprecated Non-functional
*/ */
@Deprecated @Deprecated
public int getDamageValue() { public int getDamageValue() {
return damageValue; return 0;
} }
public int getMaxLevel() { public int getMaxLevel() {
return maxLevel; return upgradeable ? 2 : 1;
}
public boolean isInstant() {
return effect == null ? true : effect.isInstant();
} }
/** /**
* * @deprecated Non-functional
* @param damage the damage value
* @return the matching potion type or null
* @deprecated Magic value
*/ */
@Deprecated @Deprecated
public static PotionType getByDamageValue(int damage) { public static PotionType getByDamageValue(int damage) {
for (PotionType type : PotionType.values()) {
if (type.damageValue == damage)
return type;
}
return null; return null;
} }
/**
* @deprecated Misleading
*/
@Deprecated
public static PotionType getByEffect(PotionEffectType effectType) { public static PotionType getByEffect(PotionEffectType effectType) {
if (effectType == null) if (effectType == null)
return WATER; return WATER;

View File

@@ -1,170 +0,0 @@
package org.bukkit.potion;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.is;
import org.bukkit.Material;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.ItemStack;
import org.junit.BeforeClass;
import org.junit.Test;
public class PotionTest {
@BeforeClass
public static void setup() {
PotionEffectType.registerPotionEffectType(new PotionEffectType(19){
@Override
public double getDurationModifier() {
return 1;
}
@Override
public String getName() {
return "Poison";
}
@Override
public boolean isInstant() {
return false;
}
});
PotionEffectType.registerPotionEffectType(new PotionEffectType(6){
@Override
public double getDurationModifier() {
return 1;
}
@Override
public String getName() {
return "Heal";
}
@Override
public boolean isInstant() {
return false;
}
});
}
@Test
public void applyToItemStack() {
Potion potion = new Potion(PotionType.POISON);
ItemStack stack = new ItemStack(Material.POTION, 1);
potion.apply(stack);
assertTrue(stack.getDurability() == potion.toDamageValue());
}
@Test
public void fromDamage() {
Potion potion = Potion.fromDamage(PotionType.POISON.getDamageValue());
assertTrue(potion.getType() == PotionType.POISON);
potion = Potion.fromDamage(PotionType.POISON.getDamageValue() | SPLASH_BIT);
assertTrue(potion.getType() == PotionType.POISON && potion.isSplash());
potion = Potion.fromDamage(0x25 /* Potion of Healing II */);
assertTrue(potion.getType() == PotionType.INSTANT_HEAL && potion.getLevel() == 2);
}
@Test(expected = IllegalArgumentException.class)
public void illegalApplyToItemStack() {
Potion potion = new Potion(PotionType.POISON);
potion.apply(new ItemStack(Material.AIR, 1));
}
@Test
public void ItemStackConversion() {
Potion potion = new Potion(PotionType.POISON);
ItemStack itemstack = potion.toItemStack(1);
assertThat(itemstack.getType(), is(Material.POTION));
assertTrue(itemstack.getAmount() == 1);
assertTrue(itemstack.getDurability() == potion.toDamageValue());
}
@Test
public void setExtended() {
Potion potion = new Potion(PotionType.POISON);
assertFalse(potion.hasExtendedDuration());
potion.setHasExtendedDuration(true);
assertTrue(potion.hasExtendedDuration());
assertTrue((potion.toDamageValue() & EXTENDED_BIT) != 0);
}
@Test
public void setSplash() {
Potion potion = new Potion(PotionType.POISON);
assertFalse(potion.isSplash());
potion.setSplash(true);
assertTrue(potion.isSplash());
assertTrue((potion.toDamageValue() & SPLASH_BIT) != 0);
}
@Test
public void setLevel() {
Potion potion = new Potion(PotionType.POISON);
assertEquals(1, potion.getLevel());
potion.setLevel(2);
assertEquals(2, potion.getLevel());
assertTrue((potion.toDamageValue() & 0x3F) == (PotionType.POISON.getDamageValue() | 0x20));
}
@Test(expected=IllegalArgumentException.class)
public void nullType() {
new Potion(null, 2);
}
@Test(expected=IllegalArgumentException.class)
public void maxLevelConstruct() {
new Potion(PotionType.POISON, 3);
}
@Test(expected=IllegalArgumentException.class)
public void maxLevelSet() {
Potion potion = new Potion(PotionType.POISON);
potion.setLevel(3);
}
@Test(expected=IllegalArgumentException.class)
public void nullStack() {
Potion potion = new Potion(PotionType.POISON);
potion.apply((ItemStack) null);
}
@Test(expected=IllegalArgumentException.class)
public void nullEntity() {
Potion potion = new Potion(PotionType.POISON);
potion.apply((LivingEntity) null);
}
@Test
public void water() {
Potion potion = new Potion(PotionType.WATER);
assertEquals(0, potion.getLevel());
assertFalse(potion.isSplash());
assertFalse(potion.hasExtendedDuration());
assertEquals(0, potion.toDamageValue());
}
@Test
public void mundane() {
Potion potion = new Potion(0);
assertFalse(potion.getType() == PotionType.WATER);
assertFalse(potion.toDamageValue() == 0);
assertEquals(8192, potion.toDamageValue());
Potion potion2 = Potion.fromDamage(8192);
assertEquals(potion, potion2);
assertEquals(0, potion.getLevel());
}
@Test
public void awkward() {
Potion potion = new Potion(16);
assertEquals(16, potion.getNameId());
assertFalse(potion.isSplash());
assertFalse(potion.hasExtendedDuration());
assertNull(potion.getType());
assertEquals(16, potion.toDamageValue());
}
private static final int EXTENDED_BIT = 0x40;
private static final int SPLASH_BIT = 0x4000;
}