Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
PotionEffect.java
1 package org.bukkit.potion;
2 
3 import java.util.Map;
4 import java.util.NoSuchElementException;
5 
6 import org.apache.commons.lang.Validate;
10 
11 import com.google.common.collect.ImmutableMap;
12 
13 /**
14  * Represents a potion effect, that can be added to a {@link LivingEntity}. A
15  * potion effect has a duration that it will last for, an amplifier that will
16  * enhance its effects, and a {@link PotionEffectType}, that represents its
17  * effect on an entity.
18  */
19 @SerializableAs("PotionEffect")
20 public class PotionEffect implements ConfigurationSerializable {
21  private static final String AMPLIFIER = "amplifier";
22  private static final String DURATION = "duration";
23  private static final String TYPE = "effect";
24  private static final String AMBIENT = "ambient";
25  private final int amplifier;
26  private final int duration;
27  private final PotionEffectType type;
28  private final boolean ambient;
29 
30  /**
31  * Creates a potion effect.
32  *
33  * @param type effect type
34  * @param duration measured in ticks, see {@link
35  * PotionEffect#getDuration()}
36  * @param amplifier the amplifier, see {@link PotionEffect#getAmplifier()}
37  * @param ambient the ambient status, see {@link PotionEffect#isAmbient()}
38  */
39  public PotionEffect(PotionEffectType type, int duration, int amplifier, boolean ambient) {
40  Validate.notNull(type, "effect type cannot be null");
41  this.type = type;
42  this.duration = duration;
43  this.amplifier = amplifier;
44  this.ambient = ambient;
45  }
46 
47  /**
48  * Creates a potion effect. Assumes ambient is true.
49  *
50  * @param type Effect type
51  * @param duration measured in ticks
52  * @param amplifier the amplifier for the effect
53  * @see PotionEffect#PotionEffect(PotionEffectType, int, int, boolean)
54  */
55  public PotionEffect(PotionEffectType type, int duration, int amplifier) {
56  this(type, duration, amplifier, true);
57  }
58 
59  /**
60  * Constructor for deserialization.
61  *
62  * @param map the map to deserialize from
63  */
64  public PotionEffect(Map<String, Object> map) {
65  this(getEffectType(map), getInt(map, DURATION), getInt(map, AMPLIFIER), getBool(map, AMBIENT));
66  }
67 
68  private static PotionEffectType getEffectType(Map<?,?> map) {
69  int type = getInt(map, TYPE);
71  if (effect != null) {
72  return effect;
73  }
74  throw new NoSuchElementException(map + " does not contain " + TYPE);
75  }
76 
77  private static int getInt(Map<?,?> map, Object key) {
78  Object num = map.get(key);
79  if (num instanceof Integer) {
80  return (Integer) num;
81  }
82  throw new NoSuchElementException(map + " does not contain " + key);
83  }
84 
85  private static boolean getBool(Map<?,?> map, Object key) {
86  Object bool = map.get(key);
87  if (bool instanceof Boolean) {
88  return (Boolean) bool;
89  }
90  throw new NoSuchElementException(map + " does not contain " + key);
91  }
92 
93  public Map<String, Object> serialize() {
94  return ImmutableMap.<String, Object>of(
95  TYPE, type.getId(),
96  DURATION, duration,
97  AMPLIFIER, amplifier,
98  AMBIENT, ambient
99  );
100  }
101 
102  /**
103  * Attempts to add the effect represented by this object to the given
104  * {@link LivingEntity}.
105  *
106  * @see LivingEntity#addPotionEffect(PotionEffect)
107  * @param entity The entity to add this effect to
108  * @return Whether the effect could be added
109  */
110  public boolean apply(LivingEntity entity) {
111  return entity.addPotionEffect(this);
112  }
113 
114  @Override
115  public boolean equals(Object obj) {
116  if (this == obj) {
117  return true;
118  }
119  if (!(obj instanceof PotionEffect)) {
120  return false;
121  }
122  PotionEffect that = (PotionEffect) obj;
123  return this.type.equals(that.type) && this.ambient == that.ambient && this.amplifier == that.amplifier && this.duration == that.duration;
124  }
125 
126  /**
127  * Returns the amplifier of this effect. A higher amplifier means the
128  * potion effect happens more often over its duration and in some cases
129  * has more effect on its target.
130  *
131  * @return The effect amplifier
132  */
133  public int getAmplifier() {
134  return amplifier;
135  }
136 
137  /**
138  * Returns the duration (in ticks) that this effect will run for when
139  * applied to a {@link LivingEntity}.
140  *
141  * @return The duration of the effect
142  */
143  public int getDuration() {
144  return duration;
145  }
146 
147  /**
148  * Returns the {@link PotionEffectType} of this effect.
149  *
150  * @return The potion type of this effect
151  */
153  return type;
154  }
155 
156  /**
157  * Makes potion effect produce more, translucent, particles.
158  *
159  * @return if this effect is ambient
160  */
161  public boolean isAmbient() {
162  return ambient;
163  }
164 
165  @Override
166  public int hashCode() {
167  int hash = 1;
168  hash = hash * 31 + type.hashCode();
169  hash = hash * 31 + amplifier;
170  hash = hash * 31 + duration;
171  hash ^= 0x22222222 >> (ambient ? 1 : -1);
172  return hash;
173  }
174 
175  @Override
176  public String toString() {
177  return type.getName() + (ambient ? ":(" : ":") + duration + "t-x" + amplifier + (ambient ? ")" : "");
178  }
179 }
PotionEffect(PotionEffectType type, int duration, int amplifier)
static PotionEffectType getById(int id)
PotionEffect(Map< String, Object > map)
PotionEffect(PotionEffectType type, int duration, int amplifier, boolean ambient)
Map< String, Object > serialize()
boolean apply(LivingEntity entity)
boolean addPotionEffect(PotionEffect effect)