Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
PotionEffectType.java
1 package org.bukkit.potion;
2 
3 import java.util.HashMap;
4 import java.util.Map;
5 
6 import org.apache.commons.lang.Validate;
7 
8 /**
9  * Represents a type of potion and its effect on an entity.
10  */
11 public abstract class PotionEffectType {
12  /**
13  * Increases movement speed.
14  */
15  public static final PotionEffectType SPEED = new PotionEffectTypeWrapper(1);
16 
17  /**
18  * Decreases movement speed.
19  */
20  public static final PotionEffectType SLOW = new PotionEffectTypeWrapper(2);
21 
22  /**
23  * Increases dig speed.
24  */
26 
27  /**
28  * Decreases dig speed.
29  */
31 
32  /**
33  * Increases damage dealt.
34  */
36 
37  /**
38  * Heals an entity.
39  */
40  public static final PotionEffectType HEAL = new PotionEffectTypeWrapper(6);
41 
42  /**
43  * Hurts an entity.
44  */
45  public static final PotionEffectType HARM = new PotionEffectTypeWrapper(7);
46 
47  /**
48  * Increases jump height.
49  */
50  public static final PotionEffectType JUMP = new PotionEffectTypeWrapper(8);
51 
52  /**
53  * Warps vision on the client.
54  */
55  public static final PotionEffectType CONFUSION = new PotionEffectTypeWrapper(9);
56 
57  /**
58  * Regenerates health.
59  */
60  public static final PotionEffectType REGENERATION = new PotionEffectTypeWrapper(10);
61 
62  /**
63  * Decreases damage dealt to an entity.
64  */
66 
67  /**
68  * Stops fire damage.
69  */
71 
72  /**
73  * Allows breathing underwater.
74  */
76 
77  /**
78  * Grants invisibility.
79  */
80  public static final PotionEffectType INVISIBILITY = new PotionEffectTypeWrapper(14);
81 
82  /**
83  * Blinds an entity.
84  */
85  public static final PotionEffectType BLINDNESS = new PotionEffectTypeWrapper(15);
86 
87  /**
88  * Allows an entity to see in the dark.
89  */
90  public static final PotionEffectType NIGHT_VISION = new PotionEffectTypeWrapper(16);
91 
92  /**
93  * Increases hunger.
94  */
95  public static final PotionEffectType HUNGER = new PotionEffectTypeWrapper(17);
96 
97  /**
98  * Decreases damage dealt by an entity.
99  */
100  public static final PotionEffectType WEAKNESS = new PotionEffectTypeWrapper(18);
101 
102  /**
103  * Deals damage to an entity over time.
104  */
105  public static final PotionEffectType POISON = new PotionEffectTypeWrapper(19);
106 
107  /**
108  * Deals damage to an entity over time and gives the health to the
109  * shooter.
110  */
111  public static final PotionEffectType WITHER = new PotionEffectTypeWrapper(20);
112 
113  /**
114  * Increases the maximum health of an entity.
115  */
117 
118  /**
119  * Increases the maximum health of an entity with health that cannot be
120  * regenerated, but is refilled every 30 seconds.
121  */
122  public static final PotionEffectType ABSORPTION = new PotionEffectTypeWrapper(22);
123 
124  /**
125  * Increases the food level of an entity each tick.
126  */
127  public static final PotionEffectType SATURATION = new PotionEffectTypeWrapper(23);
128 
129  private final int id;
130 
131  protected PotionEffectType(int id) {
132  this.id = id;
133  }
134 
135  /**
136  * Creates a PotionEffect from this PotionEffectType, applying duration
137  * modifiers and checks.
138  *
139  * @see PotionBrewer#createEffect(PotionEffectType, int, int)
140  * @param duration time in ticks
141  * @param amplifier the effect's amplifier
142  * @return a resulting potion effect
143  */
144  public PotionEffect createEffect(int duration, int amplifier) {
145  return Potion.getBrewer().createEffect(this, duration, amplifier);
146  }
147 
148  /**
149  * Returns the duration modifier applied to effects of this type.
150  *
151  * @return duration modifier
152  */
153  public abstract double getDurationModifier();
154 
155  /**
156  * Returns the unique ID of this type.
157  *
158  * @return Unique ID
159  * @deprecated Magic value
160  */
161  @Deprecated
162  public int getId() {
163  return id;
164  }
165 
166  /**
167  * Returns the name of this effect type.
168  *
169  * @return The name of this effect type
170  */
171  public abstract String getName();
172 
173  /**
174  * Returns whether the effect of this type happens once, immediately.
175  *
176  * @return whether this type is normally instant
177  */
178  public abstract boolean isInstant();
179 
180  @Override
181  public boolean equals(Object obj) {
182  if (obj == null) {
183  return false;
184  }
185  if (!(obj instanceof PotionEffectType)) {
186  return false;
187  }
188  final PotionEffectType other = (PotionEffectType) obj;
189  if (this.id != other.id) {
190  return false;
191  }
192  return true;
193  }
194 
195  @Override
196  public int hashCode() {
197  return id;
198  }
199 
200  @Override
201  public String toString() {
202  return "PotionEffectType[" + id + ", " + getName() + "]";
203  }
204 
205  private static final PotionEffectType[] byId = new PotionEffectType[24];
206  private static final Map<String, PotionEffectType> byName = new HashMap<String, PotionEffectType>();
207  // will break on updates.
208  private static boolean acceptingNew = true;
209 
210  /**
211  * Gets the effect type specified by the unique id.
212  *
213  * @param id Unique ID to fetch
214  * @return Resulting type, or null if not found.
215  * @deprecated Magic value
216  */
217  @Deprecated
218  public static PotionEffectType getById(int id) {
219  if (id >= byId.length || id < 0)
220  return null;
221  return byId[id];
222  }
223 
224  /**
225  * Gets the effect type specified by the given name.
226  *
227  * @param name Name of PotionEffectType to fetch
228  * @return Resulting PotionEffectType, or null if not found.
229  */
230  public static PotionEffectType getByName(String name) {
231  Validate.notNull(name, "name cannot be null");
232  return byName.get(name.toLowerCase());
233  }
234 
235  /**
236  * Registers an effect type with the given object.
237  * <p>
238  * Generally not to be used from within a plugin.
239  *
240  * @param type PotionType to register
241  */
242  public static void registerPotionEffectType(PotionEffectType type) {
243  if (byId[type.id] != null || byName.containsKey(type.getName().toLowerCase())) {
244  throw new IllegalArgumentException("Cannot set already-set type");
245  } else if (!acceptingNew) {
246  throw new IllegalStateException(
247  "No longer accepting new potion effect types (can only be done by the server implementation)");
248  }
249 
250  byId[type.id] = type;
251  byName.put(type.getName().toLowerCase(), type);
252  }
253 
254  /**
255  * Stops accepting any effect type registrations.
256  */
257  public static void stopAcceptingRegistrations() {
258  acceptingNew = false;
259  }
260 
261  /**
262  * Returns an array of all the registered {@link PotionEffectType}s.
263  *
264  * @return Array of types.
265  */
266  public static PotionEffectType[] values() {
267  return byId.clone();
268  }
269 }
static final PotionEffectType HEAL
static final PotionEffectType BLINDNESS
static final PotionEffectType FAST_DIGGING
static final PotionEffectType DAMAGE_RESISTANCE
static final PotionEffectType SATURATION
static final PotionEffectType WEAKNESS
static void registerPotionEffectType(PotionEffectType type)
static final PotionEffectType WATER_BREATHING
static PotionEffectType getById(int id)
static final PotionEffectType SLOW_DIGGING
PotionEffect createEffect(int duration, int amplifier)
static final PotionEffectType ABSORPTION
static final PotionEffectType NIGHT_VISION
static final PotionEffectType HUNGER
static final PotionEffectType INVISIBILITY
static final PotionEffectType WITHER
static final PotionEffectType FIRE_RESISTANCE
static final PotionEffectType HEALTH_BOOST
static final PotionEffectType JUMP
static final PotionEffectType INCREASE_DAMAGE
static PotionBrewer getBrewer()
Definition: Potion.java:418
static PotionEffectType[] values()
static final PotionEffectType SLOW
PotionEffect createEffect(PotionEffectType potion, int duration, int amplifier)
abstract double getDurationModifier()
static final PotionEffectType SPEED
static final PotionEffectType CONFUSION
static final PotionEffectType POISON
static final PotionEffectType HARM
static final PotionEffectType REGENERATION
static PotionEffectType getByName(String name)