From bfdd94209d1d54fd8bdf8032fbf6cd677055b7e0 Mon Sep 17 00:00:00 2001 From: MemencioPerez Date: Sun, 24 Nov 2024 02:16:36 -0400 Subject: [PATCH 1/2] feat: add options for NBT Short tags --- .../deluxemenus/config/DeluxeMenusConfig.java | 2 ++ .../deluxemenus/menu/MenuItem.java | 16 +++++++++++ .../menu/options/MenuItemOptions.java | 26 +++++++++++++++++ .../deluxemenus/nbt/NbtProvider.java | 28 +++++++++++++++++++ 4 files changed, 72 insertions(+) diff --git a/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java b/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java index c6ddc80..46a7d50 100644 --- a/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java +++ b/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java @@ -759,8 +759,10 @@ private Map> loadMenuItems(FileConfiguration .hideUnbreakable(c.getBoolean(currentPath + "hide_unbreakable", false)) .hideEnchants(c.getBoolean(currentPath + "hide_enchantments", false)) .nbtString(c.getString(currentPath + "nbt_string", null)) + .nbtShort(c.getString(currentPath + "nbt_short", null)) .nbtInt(c.getString(currentPath + "nbt_int", null)) .nbtStrings(c.getStringList(currentPath + "nbt_strings")) + .nbtShorts(c.getStringList(currentPath + "nbt_shorts")) .nbtInts(c.getStringList(currentPath + "nbt_ints")) .priority(c.getInt(currentPath + "priority", 1)); diff --git a/src/main/java/com/extendedclip/deluxemenus/menu/MenuItem.java b/src/main/java/com/extendedclip/deluxemenus/menu/MenuItem.java index be8224f..88178e5 100644 --- a/src/main/java/com/extendedclip/deluxemenus/menu/MenuItem.java +++ b/src/main/java/com/extendedclip/deluxemenus/menu/MenuItem.java @@ -413,6 +413,14 @@ public ItemStack getItemStack(@NotNull final MenuHolder holder) { } } + if (this.options.nbtShort().isPresent()) { + final String tag = holder.setPlaceholdersAndArguments(this.options.nbtShort().get()); + if (tag.contains(":")) { + final String[] parts = tag.split(":"); + itemStack = NbtProvider.setShort(itemStack, parts[0], Short.parseShort(parts[1])); + } + } + if (this.options.nbtInt().isPresent()) { final String tag = holder.setPlaceholdersAndArguments(this.options.nbtInt().get()); if (tag.contains(":")) { @@ -429,6 +437,14 @@ public ItemStack getItemStack(@NotNull final MenuHolder holder) { } } + for (String nbtTag : this.options.nbtShorts()) { + final String tag = holder.setPlaceholdersAndArguments(nbtTag); + if (tag.contains(":")) { + final String[] parts = tag.split(":"); + itemStack = NbtProvider.setShort(itemStack, parts[0], Short.parseShort(parts[1])); + } + } + for (String nbtTag : this.options.nbtInts()) { final String tag = holder.setPlaceholdersAndArguments(nbtTag); if (tag.contains(":")) { diff --git a/src/main/java/com/extendedclip/deluxemenus/menu/options/MenuItemOptions.java b/src/main/java/com/extendedclip/deluxemenus/menu/options/MenuItemOptions.java index 3c0400e..da6977d 100644 --- a/src/main/java/com/extendedclip/deluxemenus/menu/options/MenuItemOptions.java +++ b/src/main/java/com/extendedclip/deluxemenus/menu/options/MenuItemOptions.java @@ -50,8 +50,10 @@ public class MenuItemOptions { private final LoreAppendMode loreAppendMode; private final String nbtString; + private final String nbtShort; private final String nbtInt; private final List nbtStrings; + private final List nbtShorts; private final List nbtInts; private final int slot; @@ -97,8 +99,10 @@ private MenuItemOptions(final @NotNull MenuItemOptionsBuilder builder) { this.displayNameHasPlaceholders = builder.displayNameHasPlaceholders; this.loreHasPlaceholders = builder.loreHasPlaceholders; this.nbtString = builder.nbtString; + this.nbtShort = builder.nbtShort; this.nbtInt = builder.nbtInt; this.nbtStrings = builder.nbtStrings; + this.nbtShorts = builder.nbtShorts; this.nbtInts = builder.nbtInts; this.slot = builder.slot; this.priority = builder.priority; @@ -218,6 +222,10 @@ public boolean hasLore() { return Optional.ofNullable(nbtString); } + public @NotNull Optional nbtShort() { + return Optional.ofNullable(nbtShort); + } + public @NotNull Optional nbtInt() { return Optional.ofNullable(nbtInt); } @@ -226,6 +234,10 @@ public boolean hasLore() { return nbtStrings; } + public @NotNull List nbtShorts() { + return nbtShorts; + } + public @NotNull List nbtInts() { return nbtInts; } @@ -317,8 +329,10 @@ public boolean updatePlaceholders() { .itemFlags(this.itemFlags) .unbreakable(this.unbreakable) .nbtString(this.nbtString) + .nbtShort(this.nbtShort) .nbtInt(this.nbtInt) .nbtStrings(this.nbtStrings) + .nbtShorts(this.nbtShorts) .nbtInts(this.nbtInts) .slot(this.slot) .priority(this.priority) @@ -368,8 +382,10 @@ public static class MenuItemOptionsBuilder { private LoreAppendMode loreAppendMode; private String nbtString; + private String nbtShort; private String nbtInt; private List nbtStrings = Collections.emptyList(); + private List nbtShorts = Collections.emptyList(); private List nbtInts = Collections.emptyList(); private int slot; @@ -526,6 +542,11 @@ public MenuItemOptionsBuilder nbtString(final @Nullable String nbtString) { return this; } + public MenuItemOptionsBuilder nbtShort(final @Nullable String nbtShort) { + this.nbtShort = nbtShort; + return this; + } + public MenuItemOptionsBuilder nbtInt(final @Nullable String nbtInt) { this.nbtInt = nbtInt; return this; @@ -536,6 +557,11 @@ public MenuItemOptionsBuilder nbtStrings(final @NotNull List nbtStrings) return this; } + public MenuItemOptionsBuilder nbtShorts(final @NotNull List nbtShorts) { + this.nbtShorts = nbtShorts; + return this; + } + public MenuItemOptionsBuilder nbtInts(final @NotNull List nbtInts) { this.nbtInts = nbtInts; return this; diff --git a/src/main/java/com/extendedclip/deluxemenus/nbt/NbtProvider.java b/src/main/java/com/extendedclip/deluxemenus/nbt/NbtProvider.java index 418f913..bb37d2a 100644 --- a/src/main/java/com/extendedclip/deluxemenus/nbt/NbtProvider.java +++ b/src/main/java/com/extendedclip/deluxemenus/nbt/NbtProvider.java @@ -15,6 +15,7 @@ public final class NbtProvider { private static Method getStringMethod; private static Method setStringMethod; private static Method setBooleanMethod; + private static Method setShortMethod; private static Method setIntMethod; private static Method removeTagMethod; private static Method hasTagMethod; @@ -36,6 +37,7 @@ public final class NbtProvider { getStringMethod = compoundClass.getMethod(VersionConstants.GET_STRING_METHOD_NAME, String.class); setStringMethod = compoundClass.getMethod(VersionConstants.SET_STRING_METHOD_NAME, String.class, String.class); setBooleanMethod = compoundClass.getMethod(VersionConstants.SET_BOOLEAN_METHOD_NAME, String.class, boolean.class); + setShortMethod = compoundClass.getMethod(VersionConstants.SET_SHORT_METHOD_NAME, String.class, short.class); setIntMethod = compoundClass.getMethod(VersionConstants.SET_INTEGER_METHOD_NAME, String.class, int.class); removeTagMethod = compoundClass.getMethod(VersionConstants.REMOVE_TAG_METHOD_NAME, String.class); hasTagMethod = itemStackClass.getMethod(VersionConstants.HAS_TAG_METHOD_NAME); @@ -116,6 +118,19 @@ public static String getString(final ItemStack itemStack, final String key) { return getString(itemCompound, key); } + public static ItemStack setShort(final ItemStack itemStack, final String key, final short value) { + if (itemStack == null) return null; + if (itemStack.getType() == Material.AIR) return null; + + Object nmsItemStack = asNMSCopy(itemStack); + Object itemCompound = hasTag(nmsItemStack) ? getTag(nmsItemStack) : newNBTTagCompound(); + + setShort(itemCompound, key, value); + setTag(nmsItemStack, itemCompound); + + return asBukkitCopy(nmsItemStack); + } + public static ItemStack setInt(final ItemStack itemStack, final String key, final int value) { if (itemStack == null) return null; if (itemStack.getType() == Material.AIR) return null; @@ -176,6 +191,13 @@ private static void setBoolean(final Object itemCompound, final String key, fina } } + private static void setShort(final Object itemCompound, final String key, final short value) { + try { + setShortMethod.invoke(itemCompound, key, value); + } catch (IllegalAccessException | InvocationTargetException ignored) { + } + } + private static void setInt(final Object itemCompound, final String key, final int value) { try { setIntMethod.invoke(itemCompound, key, value); @@ -299,6 +321,7 @@ private static class VersionConstants { private final static String GET_STRING_METHOD_NAME = getStringMethodName(); private final static String SET_STRING_METHOD_NAME = setStringMethodName(); private final static String SET_BOOLEAN_METHOD_NAME = setBooleanMethodName(); + private final static String SET_SHORT_METHOD_NAME = setShortMethodName(); private final static String SET_INTEGER_METHOD_NAME = setIntegerMethodName(); private final static String REMOVE_TAG_METHOD_NAME = removeTagMethodName(); private final static String HAS_TAG_METHOD_NAME = hasTagMethodName(); @@ -320,6 +343,11 @@ private static String setBooleanMethodName() { return "setBoolean"; } + private static String setShortMethodName() { + if (VersionHelper.HAS_OBFUSCATED_NAMES) return "a"; + return "setShort"; + } + private static String setIntegerMethodName() { if (VersionHelper.HAS_OBFUSCATED_NAMES) return "a"; return "setInt"; From 77eca8665c0df07292369ab9f870e58d3bd1f781 Mon Sep 17 00:00:00 2001 From: MemencioPerez Date: Sun, 24 Nov 2024 04:33:10 -0400 Subject: [PATCH 2/2] feat: add options for NBT Byte tags --- .../deluxemenus/config/DeluxeMenusConfig.java | 2 ++ .../deluxemenus/menu/MenuItem.java | 16 +++++++++++ .../menu/options/MenuItemOptions.java | 26 +++++++++++++++++ .../deluxemenus/nbt/NbtProvider.java | 28 +++++++++++++++++++ 4 files changed, 72 insertions(+) diff --git a/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java b/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java index 46a7d50..dae137e 100644 --- a/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java +++ b/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java @@ -759,9 +759,11 @@ private Map> loadMenuItems(FileConfiguration .hideUnbreakable(c.getBoolean(currentPath + "hide_unbreakable", false)) .hideEnchants(c.getBoolean(currentPath + "hide_enchantments", false)) .nbtString(c.getString(currentPath + "nbt_string", null)) + .nbtByte(c.getString(currentPath + "nbt_byte", null)) .nbtShort(c.getString(currentPath + "nbt_short", null)) .nbtInt(c.getString(currentPath + "nbt_int", null)) .nbtStrings(c.getStringList(currentPath + "nbt_strings")) + .nbtBytes(c.getStringList(currentPath + "nbt_bytes")) .nbtShorts(c.getStringList(currentPath + "nbt_shorts")) .nbtInts(c.getStringList(currentPath + "nbt_ints")) .priority(c.getInt(currentPath + "priority", 1)); diff --git a/src/main/java/com/extendedclip/deluxemenus/menu/MenuItem.java b/src/main/java/com/extendedclip/deluxemenus/menu/MenuItem.java index 88178e5..82f1700 100644 --- a/src/main/java/com/extendedclip/deluxemenus/menu/MenuItem.java +++ b/src/main/java/com/extendedclip/deluxemenus/menu/MenuItem.java @@ -413,6 +413,14 @@ public ItemStack getItemStack(@NotNull final MenuHolder holder) { } } + if (this.options.nbtByte().isPresent()) { + final String tag = holder.setPlaceholdersAndArguments(this.options.nbtByte().get()); + if (tag.contains(":")) { + final String[] parts = tag.split(":"); + itemStack = NbtProvider.setByte(itemStack, parts[0], Byte.parseByte(parts[1])); + } + } + if (this.options.nbtShort().isPresent()) { final String tag = holder.setPlaceholdersAndArguments(this.options.nbtShort().get()); if (tag.contains(":")) { @@ -437,6 +445,14 @@ public ItemStack getItemStack(@NotNull final MenuHolder holder) { } } + for (String nbtTag : this.options.nbtBytes()) { + final String tag = holder.setPlaceholdersAndArguments(nbtTag); + if (tag.contains(":")) { + final String[] parts = tag.split(":"); + itemStack = NbtProvider.setByte(itemStack, parts[0], Byte.parseByte(parts[1])); + } + } + for (String nbtTag : this.options.nbtShorts()) { final String tag = holder.setPlaceholdersAndArguments(nbtTag); if (tag.contains(":")) { diff --git a/src/main/java/com/extendedclip/deluxemenus/menu/options/MenuItemOptions.java b/src/main/java/com/extendedclip/deluxemenus/menu/options/MenuItemOptions.java index da6977d..fb1c45d 100644 --- a/src/main/java/com/extendedclip/deluxemenus/menu/options/MenuItemOptions.java +++ b/src/main/java/com/extendedclip/deluxemenus/menu/options/MenuItemOptions.java @@ -50,9 +50,11 @@ public class MenuItemOptions { private final LoreAppendMode loreAppendMode; private final String nbtString; + private final String nbtByte; private final String nbtShort; private final String nbtInt; private final List nbtStrings; + private final List nbtBytes; private final List nbtShorts; private final List nbtInts; @@ -99,9 +101,11 @@ private MenuItemOptions(final @NotNull MenuItemOptionsBuilder builder) { this.displayNameHasPlaceholders = builder.displayNameHasPlaceholders; this.loreHasPlaceholders = builder.loreHasPlaceholders; this.nbtString = builder.nbtString; + this.nbtByte = builder.nbtByte; this.nbtShort = builder.nbtShort; this.nbtInt = builder.nbtInt; this.nbtStrings = builder.nbtStrings; + this.nbtBytes = builder.nbtBytes; this.nbtShorts = builder.nbtShorts; this.nbtInts = builder.nbtInts; this.slot = builder.slot; @@ -222,6 +226,10 @@ public boolean hasLore() { return Optional.ofNullable(nbtString); } + public @NotNull Optional nbtByte() { + return Optional.ofNullable(nbtByte); + } + public @NotNull Optional nbtShort() { return Optional.ofNullable(nbtShort); } @@ -234,6 +242,10 @@ public boolean hasLore() { return nbtStrings; } + public @NotNull List nbtBytes() { + return nbtBytes; + } + public @NotNull List nbtShorts() { return nbtShorts; } @@ -329,9 +341,11 @@ public boolean updatePlaceholders() { .itemFlags(this.itemFlags) .unbreakable(this.unbreakable) .nbtString(this.nbtString) + .nbtByte(this.nbtByte) .nbtShort(this.nbtShort) .nbtInt(this.nbtInt) .nbtStrings(this.nbtStrings) + .nbtBytes(this.nbtBytes) .nbtShorts(this.nbtShorts) .nbtInts(this.nbtInts) .slot(this.slot) @@ -382,9 +396,11 @@ public static class MenuItemOptionsBuilder { private LoreAppendMode loreAppendMode; private String nbtString; + private String nbtByte; private String nbtShort; private String nbtInt; private List nbtStrings = Collections.emptyList(); + private List nbtBytes = Collections.emptyList(); private List nbtShorts = Collections.emptyList(); private List nbtInts = Collections.emptyList(); @@ -542,6 +558,11 @@ public MenuItemOptionsBuilder nbtString(final @Nullable String nbtString) { return this; } + public MenuItemOptionsBuilder nbtByte(final @Nullable String nbtByte) { + this.nbtByte = nbtByte; + return this; + } + public MenuItemOptionsBuilder nbtShort(final @Nullable String nbtShort) { this.nbtShort = nbtShort; return this; @@ -557,6 +578,11 @@ public MenuItemOptionsBuilder nbtStrings(final @NotNull List nbtStrings) return this; } + public MenuItemOptionsBuilder nbtBytes(final @NotNull List nbtBytes) { + this.nbtBytes = nbtBytes; + return this; + } + public MenuItemOptionsBuilder nbtShorts(final @NotNull List nbtShorts) { this.nbtShorts = nbtShorts; return this; diff --git a/src/main/java/com/extendedclip/deluxemenus/nbt/NbtProvider.java b/src/main/java/com/extendedclip/deluxemenus/nbt/NbtProvider.java index bb37d2a..3e0c426 100644 --- a/src/main/java/com/extendedclip/deluxemenus/nbt/NbtProvider.java +++ b/src/main/java/com/extendedclip/deluxemenus/nbt/NbtProvider.java @@ -15,6 +15,7 @@ public final class NbtProvider { private static Method getStringMethod; private static Method setStringMethod; private static Method setBooleanMethod; + private static Method setByteMethod; private static Method setShortMethod; private static Method setIntMethod; private static Method removeTagMethod; @@ -37,6 +38,7 @@ public final class NbtProvider { getStringMethod = compoundClass.getMethod(VersionConstants.GET_STRING_METHOD_NAME, String.class); setStringMethod = compoundClass.getMethod(VersionConstants.SET_STRING_METHOD_NAME, String.class, String.class); setBooleanMethod = compoundClass.getMethod(VersionConstants.SET_BOOLEAN_METHOD_NAME, String.class, boolean.class); + setByteMethod = compoundClass.getMethod(VersionConstants.SET_BYTE_METHOD_NAME, String.class, byte.class); setShortMethod = compoundClass.getMethod(VersionConstants.SET_SHORT_METHOD_NAME, String.class, short.class); setIntMethod = compoundClass.getMethod(VersionConstants.SET_INTEGER_METHOD_NAME, String.class, int.class); removeTagMethod = compoundClass.getMethod(VersionConstants.REMOVE_TAG_METHOD_NAME, String.class); @@ -118,6 +120,19 @@ public static String getString(final ItemStack itemStack, final String key) { return getString(itemCompound, key); } + public static ItemStack setByte(final ItemStack itemStack, final String key, final byte value) { + if (itemStack == null) return null; + if (itemStack.getType() == Material.AIR) return null; + + Object nmsItemStack = asNMSCopy(itemStack); + Object itemCompound = hasTag(nmsItemStack) ? getTag(nmsItemStack) : newNBTTagCompound(); + + setByte(itemCompound, key, value); + setTag(nmsItemStack, itemCompound); + + return asBukkitCopy(nmsItemStack); + } + public static ItemStack setShort(final ItemStack itemStack, final String key, final short value) { if (itemStack == null) return null; if (itemStack.getType() == Material.AIR) return null; @@ -191,6 +206,13 @@ private static void setBoolean(final Object itemCompound, final String key, fina } } + private static void setByte(final Object itemCompound, final String key, final byte value) { + try { + setByteMethod.invoke(itemCompound, key, value); + } catch (IllegalAccessException | InvocationTargetException ignored) { + } + } + private static void setShort(final Object itemCompound, final String key, final short value) { try { setShortMethod.invoke(itemCompound, key, value); @@ -321,6 +343,7 @@ private static class VersionConstants { private final static String GET_STRING_METHOD_NAME = getStringMethodName(); private final static String SET_STRING_METHOD_NAME = setStringMethodName(); private final static String SET_BOOLEAN_METHOD_NAME = setBooleanMethodName(); + private final static String SET_BYTE_METHOD_NAME = setByteMethodName(); private final static String SET_SHORT_METHOD_NAME = setShortMethodName(); private final static String SET_INTEGER_METHOD_NAME = setIntegerMethodName(); private final static String REMOVE_TAG_METHOD_NAME = removeTagMethodName(); @@ -343,6 +366,11 @@ private static String setBooleanMethodName() { return "setBoolean"; } + private static String setByteMethodName() { + if (VersionHelper.HAS_OBFUSCATED_NAMES) return "a"; + return "setByte"; + } + private static String setShortMethodName() { if (VersionHelper.HAS_OBFUSCATED_NAMES) return "a"; return "setShort";