Skip to content

Commit 20fee0c

Browse files
committed
improve custom metadata option for 1.8.8
1 parent 449c2ca commit 20fee0c

File tree

6 files changed

+192
-49
lines changed

6 files changed

+192
-49
lines changed

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/ItemCreator.java

Lines changed: 128 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
import org.broken.arrow.library.itemcreator.utility.ConvertToItemStack;
66
import org.broken.arrow.library.itemcreator.utility.ServerVersion;
77
import org.broken.arrow.library.itemcreator.utility.builders.ItemBuilder;
8+
import org.broken.arrow.library.logging.Validate;
89
import org.broken.arrow.library.logging.Validate.ValidateExceptions;
910
import org.broken.arrow.library.nbt.RegisterNbtAPI;
1011
import org.bukkit.Material;
12+
import org.bukkit.NamespacedKey;
13+
import org.bukkit.enchantments.Enchantment;
1114
import org.bukkit.inventory.ItemStack;
1215
import org.bukkit.plugin.Plugin;
1316

17+
import javax.annotation.Nonnull;
1418
import javax.annotation.Nullable;
1519
import java.util.Arrays;
1620
import java.util.List;
@@ -48,7 +52,7 @@ public ItemCreator(final Plugin plugin) {
4852
/**
4953
* Constructs an ItemCreator instance associated with the given plugin.
5054
*
51-
* @param plugin the plugin instance
55+
* @param plugin the plugin instance
5256
* @param turnOffLogger whether to disable logging for NBT manager
5357
*/
5458
public ItemCreator(final Plugin plugin, boolean turnOffLogger) {
@@ -63,8 +67,6 @@ public ItemCreator(final Plugin plugin, boolean turnOffLogger) {
6367
} catch (NoClassDefFoundError ignore) {
6468
haveTextTranslator = false;
6569
}
66-
67-
6870
}
6971

7072
/**
@@ -245,15 +247,6 @@ public void setEnableColorTranslation(boolean enableColorTranslation) {
245247
this.enableColorTranslation = enableColorTranslation;
246248
}
247249

248-
/**
249-
* Returns the current server version as a float.
250-
*
251-
* @return the server version
252-
*/
253-
public static float getServerVersion() {
254-
return serverVersion.getServerVersion();
255-
}
256-
257250
/**
258251
* Gets the NBTManager instance used to manipulate item NBT data.
259252
*
@@ -281,6 +274,7 @@ public ConvertToItemStack getConvertItems() {
281274
public Plugin getPlugin() {
282275
return plugin;
283276
}
277+
284278
/**
285279
* Returns true if the correct module is imported
286280
* for color translations, it will use bukkits methods
@@ -292,6 +286,88 @@ public boolean isHaveTextTranslator() {
292286
return haveTextTranslator;
293287
}
294288

289+
290+
/**
291+
* Returns the current server version as a float.
292+
*
293+
* @return the server version
294+
*/
295+
public static float getServerVersion() {
296+
return serverVersion.getServerVersion();
297+
}
298+
299+
/**
300+
* Return the enchantment's name across Minecraft versions.
301+
*
302+
* <p>On Minecraft 1.13 and newer this returns the NamespacedKey key
303+
* (enchantment.getKey().getKey()). On older versions this returns
304+
* {@link Enchantment#getName()}, which on legacy servers may be a numeric id string
305+
* or a legacy name. {@link Enchantment#getByName(String)} can resolve either form.
306+
*
307+
* @param enchantment the enchantment to get the name for
308+
* @return the enchantment name ({@link NamespacedKey#getKey()} on 1.13+, otherwise {@link Enchantment#getName()}
309+
* @throws NullPointerException if {@code enchantment} is null
310+
*/
311+
@Nonnull
312+
public static String getEnchantmentName(@Nonnull final Enchantment enchantment) {
313+
Validate.checkNotNull(enchantment, "Enchantment must be set to use this method.");
314+
315+
final String enhancementName;
316+
if (ItemCreator.getServerVersion() > 12.2F)
317+
enhancementName = enchantment.getKey().getKey();
318+
else
319+
enhancementName = enchantment.getName();
320+
321+
return enhancementName;
322+
}
323+
324+
/**
325+
* Resolve an Enchantment by name, using a NamespacedKey on modern servers and legacy names on older servers.
326+
*
327+
* <p>Resolution order:
328+
* <ol>
329+
* <li>On Minecraft 1.13+ this tries {@link Enchantment#getByKey(NamespacedKey)} with
330+
* {@link NamespacedKey#minecraft(String)} constructed from {@code name}.</li>
331+
* <li>If unresolved (or on older servers) this tries {@link Enchantment#getByName(String)} using {@code name}.</li>
332+
* <li>If still unresolved returns {@link Enchantment#VANISHING_CURSE} as a safe default.</li>
333+
* </ol>
334+
*
335+
* <p>Notes:
336+
* <ul>
337+
* <li>The {@code enhancementName} parameter should be the NamespacedKey key for modern servers (e.g. "sharpness")
338+
* or the legacy enchantment name/id for older servers. Use {@link #getEnhancement(NamespacedKey)} on modern
339+
* Minecraft versions if you want access to enchantments outside the Minecraft-provided ones.</li>
340+
* </ul>
341+
*
342+
* @param enhancementName the enchantment name or key (non-null)
343+
* @return the resolved Enchantment, or {@link Enchantment#VANISHING_CURSE} if none found
344+
* @throws NullPointerException if {@code enhancementName} is null
345+
*/
346+
@Nonnull
347+
public static Enchantment getEnhancement(@Nonnull final String enhancementName) {
348+
return getEnchantment(null, enhancementName);
349+
}
350+
351+
/**
352+
* Resolve an Enchantment using a provided {@link NamespacedKey}.
353+
*
354+
* <p>Resolution order:
355+
* <ol>
356+
* <li>Try {@link Enchantment#getByKey(NamespacedKey)} with {@code key}.</li>
357+
* <li>If unresolved, returns {@link Enchantment#VANISHING_CURSE} as a safe default.</li>
358+
* </ol>
359+
*
360+
* @param key the NamespacedKey to try first (non-null)
361+
* @return the resolved Enchantment, or {@link Enchantment#VANISHING_CURSE} if none found
362+
* @throws NullPointerException if {@code key} is null
363+
*/
364+
@Nonnull
365+
public static Enchantment getEnhancement(@Nonnull final NamespacedKey key) {
366+
Validate.checkNotNull(key, "key must not be null");
367+
return getEnchantment(key, null);
368+
}
369+
370+
295371
/**
296372
* Sets the server version based on the plugin's server version.
297373
*
@@ -301,4 +377,44 @@ private static void setServerVersion(Plugin plugin) {
301377
serverVersion = new ServerVersion(plugin);
302378
}
303379

380+
/**
381+
* Resolve an Enchantment with an optional {@link NamespacedKey} and a name fallback.
382+
*
383+
* <p>Resolution order:
384+
* <ol>
385+
* <li>If {@code key} is non-null, try {@link Enchantment#getByKey(NamespacedKey)} with {@code key}.</li>
386+
* <li>If that fails and the server is 1.13+, try {@link Enchantment#getByKey(NamespacedKey)}
387+
* with {@link NamespacedKey#minecraft(String)} constructed from {@code name}.</li>
388+
* <li>If still unresolved (or on older servers) try {@link Enchantment#getByName(String)} with {@code name}.</li>
389+
* <li>If unresolved return {@link Enchantment#VANISHING_CURSE} as a safe default.</li>
390+
* </ol>
391+
*
392+
* <p>Notes:
393+
* <ul>
394+
* <li>The {@code name} parameter should be the NamespacedKey key for modern servers (e.g. "sharpness")
395+
* or the legacy enchantment name/id for older servers.</li>
396+
* </ul>
397+
*
398+
* @param key the NamespacedKey to try first (maybe null)
399+
* @param name the enchantment name or key (maybe null)
400+
* @return the resolved Enchantment, or {@link Enchantment#VANISHING_CURSE} if none found
401+
*/
402+
@Nonnull
403+
private static Enchantment getEnchantment(@Nullable final NamespacedKey key, @Nullable final String name) {
404+
if (key != null) {
405+
Enchantment enchantment = Enchantment.getByKey(key);
406+
if (enchantment != null) return enchantment;
407+
else return Enchantment.VANISHING_CURSE;
408+
}
409+
if (name == null) return Enchantment.VANISHING_CURSE;
410+
411+
if (ItemCreator.getServerVersion() > 12.2F) {
412+
Enchantment enchantment = Enchantment.getByKey(NamespacedKey.minecraft(name));
413+
if (enchantment != null) return enchantment;
414+
}
415+
416+
Enchantment enchantment = Enchantment.getByName(name);
417+
return enchantment == null ? Enchantment.VANISHING_CURSE : enchantment;
418+
}
419+
304420
}

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/BookMeta.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.broken.arrow.library.itemcreator.meta;
22

3+
import org.broken.arrow.library.itemcreator.ItemCreator;
34
import org.bukkit.inventory.meta.ItemMeta;
45

56
import javax.annotation.Nonnull;
@@ -27,8 +28,9 @@
2728
* </p>
2829
*/
2930
public class BookMeta {
31+
private static final boolean modernVersion = ItemCreator.getServerVersion() > 12.2F;
3032
private List<String> pages = new ArrayList<>();
31-
private org.bukkit.inventory.meta.BookMeta.Generation generation;
33+
private String generation;
3234
private String title;
3335
private String author;
3436
static final int MAX_PAGES = Integer.MAX_VALUE;
@@ -46,7 +48,10 @@ public static BookMeta setBookMeta(@Nonnull final org.bukkit.inventory.meta.Book
4648
BookMeta bookMeta = new BookMeta();
4749
bookMeta.setAuthor(bukkitBookMeta.getAuthor());
4850
bookMeta.setTitle(bukkitBookMeta.getTitle());
49-
bookMeta.setGeneration(bukkitBookMeta.getGeneration());
51+
52+
if (modernVersion && bukkitBookMeta.getGeneration() != null)
53+
bookMeta.setGeneration(bukkitBookMeta.getGeneration());
54+
5055
bookMeta.setPages(bukkitBookMeta.getPages());
5156

5257
return bookMeta;
@@ -98,16 +103,16 @@ public void setAuthor(@Nullable final String author) {
98103
* @return the generation of the book
99104
*/
100105
public org.bukkit.inventory.meta.BookMeta.Generation getGeneration() {
101-
return generation;
106+
return org.bukkit.inventory.meta.BookMeta.Generation.valueOf(generation);
102107
}
103108

104109
/**
105110
* Sets the generation of the book. Removes generation when given null.
106111
*
107112
* @param generation the generation to set
108113
*/
109-
public void setGeneration(org.bukkit.inventory.meta.BookMeta.Generation generation) {
110-
this.generation = generation;
114+
public void setGeneration(@Nullable final org.bukkit.inventory.meta.BookMeta.Generation generation) {
115+
this.generation =generation == null ? "COPY_OF_COPY": generation.name();
111116
}
112117

113118
/**
@@ -222,7 +227,8 @@ public void applyBookMenta(final ItemMeta bookMeta) {
222227
final org.bukkit.inventory.meta.BookMeta meta = (org.bukkit.inventory.meta.BookMeta) bookMeta;
223228
meta.setTitle(this.getTitle());
224229
meta.setAuthor(this.getAuthor());
225-
meta.setGeneration(this.getGeneration());
230+
if (modernVersion)
231+
meta.setGeneration(this.getGeneration());
226232
meta.setPages(this.getPages());
227233
}
228234

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/enhancement/EnhancementWrapper.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.broken.arrow.library.itemcreator.meta.enhancement;
22

3+
import org.broken.arrow.library.itemcreator.ItemCreator;
34
import org.bukkit.enchantments.Enchantment;
45

56
import javax.annotation.Nonnull;
@@ -11,7 +12,7 @@
1112
public class EnhancementWrapper {
1213

1314
@Nonnull
14-
private final Enchantment enchantment;
15+
private final String enhancementName;
1516
private int level;
1617
private boolean ignoreLevelRestriction;
1718

@@ -35,7 +36,7 @@ public EnhancementWrapper(@Nonnull final Enchantment enchantment, final int leve
3536
* @param ignoreLevelRestriction whether to bypass the enchantment's level restrictions
3637
*/
3738
public EnhancementWrapper(@Nonnull final Enchantment enchantment, final int level, final boolean ignoreLevelRestriction) {
38-
this.enchantment = enchantment;
39+
this.enhancementName = ItemCreator.getEnchantmentName(enchantment);
3940
this.level = level;
4041
this.ignoreLevelRestriction = ignoreLevelRestriction;
4142
}
@@ -47,7 +48,7 @@ public EnhancementWrapper(@Nonnull final Enchantment enchantment, final int leve
4748
*/
4849
@Nonnull
4950
public Enchantment getEnchantment() {
50-
return enchantment;
51+
return ItemCreator.getEnhancement(enhancementName);
5152
}
5253

5354
/**
@@ -93,11 +94,8 @@ public EnhancementWrapper setIgnoreLevelRestriction(boolean ignoreLevelRestricti
9394

9495
@Override
9596
public String toString() {
96-
return "enchantment= " +
97-
enchantment +
98-
" level= " +
99-
level +
100-
" ignoreLevel= " +
101-
ignoreLevelRestriction;
97+
return "[enchantment= " + enhancementName +
98+
", level= " + level +
99+
", ignoreLevel= " + ignoreLevelRestriction+"]";
102100
}
103101
}

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/serialization/itemstack/ItemStacksSerializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public String toJson() {
114114
.registerTypeAdapter(BottleEffectMeta.class, new BottleEffectMetaAdapter())
115115
.registerTypeAdapter(MapWrapperMeta.class, new MapMetaAdapter())
116116
.registerTypeAdapter(ColorMetaAdapter.class, new ColorMetaAdapter())
117-
.registerTypeAdapter(ColorMetaAdapter.class, new EnhancementWrapperAdapter())
117+
.registerTypeAdapter(EnhancementWrapperAdapter.class, new EnhancementWrapperAdapter())
118118
.create()
119119
.toJson(this);
120120
}
@@ -131,7 +131,7 @@ public static ItemStacksSerializer fromJson(String json) {
131131
.registerTypeAdapter(BottleEffectMeta.class, new BottleEffectMetaAdapter())
132132
.registerTypeAdapter(MapWrapperMeta.class, new MapMetaAdapter())
133133
.registerTypeAdapter(ColorMetaAdapter.class, new ColorMetaAdapter())
134-
.registerTypeAdapter(ColorMetaAdapter.class, new EnhancementWrapperAdapter())
134+
.registerTypeAdapter(EnhancementWrapperAdapter.class, new EnhancementWrapperAdapter())
135135
.create()
136136
.fromJson(json, ItemStacksSerializer.class);
137137
serializer.itemStacks.addAll(serializer.items.stream()

0 commit comments

Comments
 (0)