Commits

DerFrZocker authored and md_5 committed 977a1fb45ff
SPIGOT-6026: Pull PotionEffectType and Enchantment from registry
No tags

src/main/java/org/bukkit/enchantments/Enchantment.java

Modified
1 1 package org.bukkit.enchantments;
2 2
3 -import java.util.HashMap;
4 -import java.util.Map;
3 +import com.google.common.base.Preconditions;
4 +import com.google.common.collect.Lists;
5 5 import org.bukkit.Keyed;
6 6 import org.bukkit.NamespacedKey;
7 +import org.bukkit.Registry;
7 8 import org.bukkit.inventory.ItemStack;
8 9 import org.jetbrains.annotations.Contract;
9 10 import org.jetbrains.annotations.NotNull;
10 11 import org.jetbrains.annotations.Nullable;
11 12
12 13 /**
13 14 * The various type of enchantments that may be added to armour or weapons
14 15 */
15 16 public abstract class Enchantment implements Keyed {
16 17 /**
17 18 * Provides protection against environmental damage
18 19 */
19 - public static final Enchantment PROTECTION_ENVIRONMENTAL = new EnchantmentWrapper("protection");
20 + public static final Enchantment PROTECTION_ENVIRONMENTAL = getEnchantment("protection");
20 21
21 22 /**
22 23 * Provides protection against fire damage
23 24 */
24 - public static final Enchantment PROTECTION_FIRE = new EnchantmentWrapper("fire_protection");
25 + public static final Enchantment PROTECTION_FIRE = getEnchantment("fire_protection");
25 26
26 27 /**
27 28 * Provides protection against fall damage
28 29 */
29 - public static final Enchantment PROTECTION_FALL = new EnchantmentWrapper("feather_falling");
30 + public static final Enchantment PROTECTION_FALL = getEnchantment("feather_falling");
30 31
31 32 /**
32 33 * Provides protection against explosive damage
33 34 */
34 - public static final Enchantment PROTECTION_EXPLOSIONS = new EnchantmentWrapper("blast_protection");
35 + public static final Enchantment PROTECTION_EXPLOSIONS = getEnchantment("blast_protection");
35 36
36 37 /**
37 38 * Provides protection against projectile damage
38 39 */
39 - public static final Enchantment PROTECTION_PROJECTILE = new EnchantmentWrapper("projectile_protection");
40 + public static final Enchantment PROTECTION_PROJECTILE = getEnchantment("projectile_protection");
40 41
41 42 /**
42 43 * Decreases the rate of air loss whilst underwater
43 44 */
44 - public static final Enchantment OXYGEN = new EnchantmentWrapper("respiration");
45 + public static final Enchantment OXYGEN = getEnchantment("respiration");
45 46
46 47 /**
47 48 * Increases the speed at which a player may mine underwater
48 49 */
49 - public static final Enchantment WATER_WORKER = new EnchantmentWrapper("aqua_affinity");
50 + public static final Enchantment WATER_WORKER = getEnchantment("aqua_affinity");
50 51
51 52 /**
52 53 * Damages the attacker
53 54 */
54 - public static final Enchantment THORNS = new EnchantmentWrapper("thorns");
55 + public static final Enchantment THORNS = getEnchantment("thorns");
55 56
56 57 /**
57 58 * Increases walking speed while in water
58 59 */
59 - public static final Enchantment DEPTH_STRIDER = new EnchantmentWrapper("depth_strider");
60 + public static final Enchantment DEPTH_STRIDER = getEnchantment("depth_strider");
60 61
61 62 /**
62 63 * Freezes any still water adjacent to ice / frost which player is walking on
63 64 */
64 - public static final Enchantment FROST_WALKER = new EnchantmentWrapper("frost_walker");
65 + public static final Enchantment FROST_WALKER = getEnchantment("frost_walker");
65 66
66 67 /**
67 68 * Item cannot be removed
68 69 */
69 - public static final Enchantment BINDING_CURSE = new EnchantmentWrapper("binding_curse");
70 + public static final Enchantment BINDING_CURSE = getEnchantment("binding_curse");
70 71
71 72 /**
72 73 * Increases damage against all targets
73 74 */
74 - public static final Enchantment DAMAGE_ALL = new EnchantmentWrapper("sharpness");
75 + public static final Enchantment DAMAGE_ALL = getEnchantment("sharpness");
75 76
76 77 /**
77 78 * Increases damage against undead targets
78 79 */
79 - public static final Enchantment DAMAGE_UNDEAD = new EnchantmentWrapper("smite");
80 + public static final Enchantment DAMAGE_UNDEAD = getEnchantment("smite");
80 81
81 82 /**
82 83 * Increases damage against arthropod targets
83 84 */
84 - public static final Enchantment DAMAGE_ARTHROPODS = new EnchantmentWrapper("bane_of_arthropods");
85 + public static final Enchantment DAMAGE_ARTHROPODS = getEnchantment("bane_of_arthropods");
85 86
86 87 /**
87 88 * All damage to other targets will knock them back when hit
88 89 */
89 - public static final Enchantment KNOCKBACK = new EnchantmentWrapper("knockback");
90 + public static final Enchantment KNOCKBACK = getEnchantment("knockback");
90 91
91 92 /**
92 93 * When attacking a target, has a chance to set them on fire
93 94 */
94 - public static final Enchantment FIRE_ASPECT = new EnchantmentWrapper("fire_aspect");
95 + public static final Enchantment FIRE_ASPECT = getEnchantment("fire_aspect");
95 96
96 97 /**
97 98 * Provides a chance of gaining extra loot when killing monsters
98 99 */
99 - public static final Enchantment LOOT_BONUS_MOBS = new EnchantmentWrapper("looting");
100 + public static final Enchantment LOOT_BONUS_MOBS = getEnchantment("looting");
100 101
101 102 /**
102 103 * Increases damage against targets when using a sweep attack
103 104 */
104 - public static final Enchantment SWEEPING_EDGE = new EnchantmentWrapper("sweeping");
105 + public static final Enchantment SWEEPING_EDGE = getEnchantment("sweeping");
105 106
106 107 /**
107 108 * Increases the rate at which you mine/dig
108 109 */
109 - public static final Enchantment DIG_SPEED = new EnchantmentWrapper("efficiency");
110 + public static final Enchantment DIG_SPEED = getEnchantment("efficiency");
110 111
111 112 /**
112 113 * Allows blocks to drop themselves instead of fragments (for example,
113 114 * stone instead of cobblestone)
114 115 */
115 - public static final Enchantment SILK_TOUCH = new EnchantmentWrapper("silk_touch");
116 + public static final Enchantment SILK_TOUCH = getEnchantment("silk_touch");
116 117
117 118 /**
118 119 * Decreases the rate at which a tool looses durability
119 120 */
120 - public static final Enchantment DURABILITY = new EnchantmentWrapper("unbreaking");
121 + public static final Enchantment DURABILITY = getEnchantment("unbreaking");
121 122
122 123 /**
123 124 * Provides a chance of gaining extra loot when destroying blocks
124 125 */
125 - public static final Enchantment LOOT_BONUS_BLOCKS = new EnchantmentWrapper("fortune");
126 + public static final Enchantment LOOT_BONUS_BLOCKS = getEnchantment("fortune");
126 127
127 128 /**
128 129 * Provides extra damage when shooting arrows from bows
129 130 */
130 - public static final Enchantment ARROW_DAMAGE = new EnchantmentWrapper("power");
131 + public static final Enchantment ARROW_DAMAGE = getEnchantment("power");
131 132
132 133 /**
133 134 * Provides a knockback when an entity is hit by an arrow from a bow
134 135 */
135 - public static final Enchantment ARROW_KNOCKBACK = new EnchantmentWrapper("punch");
136 + public static final Enchantment ARROW_KNOCKBACK = getEnchantment("punch");
136 137
137 138 /**
138 139 * Sets entities on fire when hit by arrows shot from a bow
139 140 */
140 - public static final Enchantment ARROW_FIRE = new EnchantmentWrapper("flame");
141 + public static final Enchantment ARROW_FIRE = getEnchantment("flame");
141 142
142 143 /**
143 144 * Provides infinite arrows when shooting a bow
144 145 */
145 - public static final Enchantment ARROW_INFINITE = new EnchantmentWrapper("infinity");
146 + public static final Enchantment ARROW_INFINITE = getEnchantment("infinity");
146 147
147 148 /**
148 149 * Decreases odds of catching worthless junk
149 150 */
150 - public static final Enchantment LUCK = new EnchantmentWrapper("luck_of_the_sea");
151 + public static final Enchantment LUCK = getEnchantment("luck_of_the_sea");
151 152
152 153 /**
153 154 * Increases rate of fish biting your hook
154 155 */
155 - public static final Enchantment LURE = new EnchantmentWrapper("lure");
156 + public static final Enchantment LURE = getEnchantment("lure");
156 157
157 158 /**
158 159 * Causes a thrown trident to return to the player who threw it
159 160 */
160 - public static final Enchantment LOYALTY = new EnchantmentWrapper("loyalty");
161 + public static final Enchantment LOYALTY = getEnchantment("loyalty");
161 162
162 163 /**
163 164 * Deals more damage to mobs that live in the ocean
164 165 */
165 - public static final Enchantment IMPALING = new EnchantmentWrapper("impaling");
166 + public static final Enchantment IMPALING = getEnchantment("impaling");
166 167
167 168 /**
168 169 * When it is rainy, launches the player in the direction their trident is thrown
169 170 */
170 - public static final Enchantment RIPTIDE = new EnchantmentWrapper("riptide");
171 + public static final Enchantment RIPTIDE = getEnchantment("riptide");
171 172
172 173 /**
173 174 * Strikes lightning when a mob is hit with a trident if conditions are
174 175 * stormy
175 176 */
176 - public static final Enchantment CHANNELING = new EnchantmentWrapper("channeling");
177 + public static final Enchantment CHANNELING = getEnchantment("channeling");
177 178
178 179 /**
179 180 * Shoot multiple arrows from crossbows
180 181 */
181 - public static final Enchantment MULTISHOT = new EnchantmentWrapper("multishot");
182 + public static final Enchantment MULTISHOT = getEnchantment("multishot");
182 183
183 184 /**
184 185 * Charges crossbows quickly
185 186 */
186 - public static final Enchantment QUICK_CHARGE = new EnchantmentWrapper("quick_charge");
187 + public static final Enchantment QUICK_CHARGE = getEnchantment("quick_charge");
187 188
188 189 /**
189 190 * Crossbow projectiles pierce entities
190 191 */
191 - public static final Enchantment PIERCING = new EnchantmentWrapper("piercing");
192 + public static final Enchantment PIERCING = getEnchantment("piercing");
192 193
193 194 /**
194 195 * Allows mending the item using experience orbs
195 196 */
196 - public static final Enchantment MENDING = new EnchantmentWrapper("mending");
197 + public static final Enchantment MENDING = getEnchantment("mending");
197 198
198 199 /**
199 200 * Item disappears instead of dropping
200 201 */
201 - public static final Enchantment VANISHING_CURSE = new EnchantmentWrapper("vanishing_curse");
202 + public static final Enchantment VANISHING_CURSE = getEnchantment("vanishing_curse");
202 203
203 204 /**
204 205 * Walk quicker on soul blocks
205 206 */
206 - public static final Enchantment SOUL_SPEED = new EnchantmentWrapper("soul_speed");
207 + public static final Enchantment SOUL_SPEED = getEnchantment("soul_speed");
207 208
208 209 /**
209 210 * Walk quicker while sneaking
210 211 */
211 - public static final Enchantment SWIFT_SNEAK = new EnchantmentWrapper("swift_sneak");
212 + public static final Enchantment SWIFT_SNEAK = getEnchantment("swift_sneak");
212 213
213 - private static final Map<NamespacedKey, Enchantment> byKey = new HashMap<NamespacedKey, Enchantment>();
214 - private static final Map<String, Enchantment> byName = new HashMap<String, Enchantment>();
215 - private static boolean acceptingNew = true;
216 - private final NamespacedKey key;
214 + @NotNull
215 + private static Enchantment getEnchantment(@NotNull String key) {
216 + NamespacedKey namespacedKey = NamespacedKey.minecraft(key);
217 + Enchantment enchantment = Registry.ENCHANTMENT.get(namespacedKey);
217 218
218 - public Enchantment(@NotNull NamespacedKey key) {
219 - this.key = key;
220 - }
219 + Preconditions.checkNotNull(enchantment, "No Enchantment found for %s. This is a bug.", namespacedKey);
221 220
222 - @NotNull
223 - @Override
224 - public NamespacedKey getKey() {
225 - return key;
221 + return enchantment;
226 222 }
227 223
228 224 /**
229 225 * Gets the unique name of this enchantment
230 226 *
231 227 * @return Unique name
232 228 * @deprecated enchantments are badly named, use {@link #getKey()}.
233 229 */
234 230 @NotNull
235 231 @Deprecated
293 289 * ItemStack}.
294 290 * <p>
295 291 * This does not check if it conflicts with any enchantments already
296 292 * applied to the item.
297 293 *
298 294 * @param item Item to test
299 295 * @return True if the enchantment may be applied, otherwise False
300 296 */
301 297 public abstract boolean canEnchantItem(@NotNull ItemStack item);
302 298
303 - @Override
304 - public boolean equals(Object obj) {
305 - if (obj == null) {
306 - return false;
307 - }
308 - if (!(obj instanceof Enchantment)) {
309 - return false;
310 - }
311 - final Enchantment other = (Enchantment) obj;
312 - if (!this.key.equals(other.key)) {
313 - return false;
314 - }
315 - return true;
316 - }
317 -
318 - @Override
319 - public int hashCode() {
320 - return key.hashCode();
321 - }
322 -
323 - @Override
324 - public String toString() {
325 - return "Enchantment[" + key + ", " + getName() + "]";
326 - }
327 -
328 - /**
329 - * Registers an enchantment with the given ID and object.
330 - * <p>
331 - * Generally not to be used from within a plugin.
332 - *
333 - * @param enchantment Enchantment to register
334 - */
335 - public static void registerEnchantment(@NotNull Enchantment enchantment) {
336 - if (byKey.containsKey(enchantment.key) || byName.containsKey(enchantment.getName())) {
337 - throw new IllegalArgumentException("Cannot set already-set enchantment");
338 - } else if (!isAcceptingRegistrations()) {
339 - throw new IllegalStateException("No longer accepting new enchantments (can only be done by the server implementation)");
340 - }
341 -
342 - byKey.put(enchantment.key, enchantment);
343 - byName.put(enchantment.getName(), enchantment);
344 - }
345 -
346 - /**
347 - * Checks if this is accepting Enchantment registrations.
348 - *
349 - * @return True if the server Implementation may add enchantments
350 - */
351 - public static boolean isAcceptingRegistrations() {
352 - return acceptingNew;
353 - }
354 -
355 - /**
356 - * Stops accepting any enchantment registrations
357 - */
358 - public static void stopAcceptingRegistrations() {
359 - acceptingNew = false;
360 - }
361 -
362 299 /**
363 300 * Gets the Enchantment at the specified key
364 301 *
365 302 * @param key key to fetch
366 303 * @return Resulting Enchantment, or null if not found
304 + * @deprecated only for backwards compatibility, use {@link Registry#get(NamespacedKey)} instead
367 305 */
368 306 @Contract("null -> null")
369 307 @Nullable
308 + @Deprecated
370 309 public static Enchantment getByKey(@Nullable NamespacedKey key) {
371 - return byKey.get(key);
310 + if (key == null) {
311 + return null;
312 + }
313 + return Registry.ENCHANTMENT.get(key);
372 314 }
373 315
374 316 /**
375 317 * Gets the Enchantment at the specified name
376 318 *
377 319 * @param name Name to fetch
378 320 * @return Resulting Enchantment, or null if not found
379 321 * @deprecated enchantments are badly named, use {@link #getByKey(org.bukkit.NamespacedKey)}.
380 322 */
381 323 @Deprecated
382 324 @Contract("null -> null")
383 325 @Nullable
384 326 public static Enchantment getByName(@Nullable String name) {
385 - return byName.get(name);
327 + if (name == null) {
328 + return null;
329 + }
330 +
331 + name = convertLegacy(name);
332 + return getByKey(NamespacedKey.fromString(name.toLowerCase()));
386 333 }
387 334
388 335 /**
389 336 * Gets an array of all the registered {@link Enchantment}s
390 337 *
391 338 * @return Array of enchantments
339 + * @deprecated use {@link Registry#iterator()}
392 340 */
393 341 @NotNull
342 + @Deprecated
394 343 public static Enchantment[] values() {
395 - return byName.values().toArray(new Enchantment[byName.size()]);
344 + return Lists.newArrayList(Registry.ENCHANTMENT).toArray(new Enchantment[0]);
345 + }
346 +
347 + private static String convertLegacy(String from) {
348 + if (from == null) {
349 + return null;
350 + }
351 +
352 + switch (from.toLowerCase()) {
353 + case "protection_environmental":
354 + return "protection";
355 + case "protection_fire":
356 + return "fire_protection";
357 + case "protection_fall":
358 + return "feather_falling";
359 + case "protection_explosions":
360 + return "blast_protection";
361 + case "protection_projectile":
362 + return "projectile_protection";
363 + case "oxygen":
364 + return "respiration";
365 + case "water_worker":
366 + return "aqua_affinity";
367 + case "damage_all":
368 + return "sharpness";
369 + case "damage_undead":
370 + return "smite";
371 + case "damage_arthropods":
372 + return "bane_of_arthropods";
373 + case "loot_bonus_mobs":
374 + return "looting";
375 + case "sweeping_edge":
376 + return "sweeping";
377 + case "dig_speed":
378 + return "efficiency";
379 + case "durability":
380 + return "unbreaking";
381 + case "loot_bonus_blocks":
382 + return "fortune";
383 + case "arrow_damage":
384 + return "power";
385 + case "arrow_knockback":
386 + return "punch";
387 + case "arrow_fire":
388 + return "flame";
389 + case "arrow_infinite":
390 + return "infinity";
391 + case "luck":
392 + return "luck_of_the_sea";
393 + }
394 +
395 + return from;
396 396 }
397 397 }

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut