Skip to content

Commit

Permalink
Merge pull request #153 from MemencioPerez/main
Browse files Browse the repository at this point in the history
  • Loading branch information
BlitzOffline authored Nov 27, 2024
2 parents 2e90538 + 77eca86 commit abd23c9
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,12 @@ private Map<Integer, TreeMap<Integer, MenuItem>> 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))
.hideTooltip(c.getString(currentPath + "hide_tooltip", null))
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/extendedclip/deluxemenus/menu/MenuItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,22 @@ 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(":")) {
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(":")) {
Expand All @@ -464,6 +480,22 @@ 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(":")) {
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(":")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ 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<String> nbtStrings;
private final List<String> nbtBytes;
private final List<String> nbtShorts;
private final List<String> nbtInts;

private final int slot;
Expand Down Expand Up @@ -108,8 +112,12 @@ 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;
this.priority = builder.priority;
Expand Down Expand Up @@ -249,6 +257,14 @@ public boolean hasLore() {
return Optional.ofNullable(nbtString);
}

public @NotNull Optional<String> nbtByte() {
return Optional.ofNullable(nbtByte);
}

public @NotNull Optional<String> nbtShort() {
return Optional.ofNullable(nbtShort);
}

public @NotNull Optional<String> nbtInt() {
return Optional.ofNullable(nbtInt);
}
Expand All @@ -257,6 +273,14 @@ public boolean hasLore() {
return nbtStrings;
}

public @NotNull List<String> nbtBytes() {
return nbtBytes;
}

public @NotNull List<String> nbtShorts() {
return nbtShorts;
}

public @NotNull List<String> nbtInts() {
return nbtInts;
}
Expand Down Expand Up @@ -353,8 +377,12 @@ 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)
.priority(this.priority)
Expand Down Expand Up @@ -410,8 +438,12 @@ public static class MenuItemOptionsBuilder {
private LoreAppendMode loreAppendMode;

private String nbtString;
private String nbtByte;
private String nbtShort;
private String nbtInt;
private List<String> nbtStrings = Collections.emptyList();
private List<String> nbtBytes = Collections.emptyList();
private List<String> nbtShorts = Collections.emptyList();
private List<String> nbtInts = Collections.emptyList();

private int slot;
Expand Down Expand Up @@ -593,6 +625,16 @@ 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;
}

public MenuItemOptionsBuilder nbtInt(final @Nullable String nbtInt) {
this.nbtInt = nbtInt;
return this;
Expand All @@ -603,6 +645,16 @@ public MenuItemOptionsBuilder nbtStrings(final @NotNull List<String> nbtStrings)
return this;
}

public MenuItemOptionsBuilder nbtBytes(final @NotNull List<String> nbtBytes) {
this.nbtBytes = nbtBytes;
return this;
}

public MenuItemOptionsBuilder nbtShorts(final @NotNull List<String> nbtShorts) {
this.nbtShorts = nbtShorts;
return this;
}

public MenuItemOptionsBuilder nbtInts(final @NotNull List<String> nbtInts) {
this.nbtInts = nbtInts;
return this;
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/extendedclip/deluxemenus/nbt/NbtProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ 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;
private static Method hasTagMethod;
Expand All @@ -36,6 +38,8 @@ 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);
hasTagMethod = itemStackClass.getMethod(VersionConstants.HAS_TAG_METHOD_NAME);
Expand Down Expand Up @@ -116,6 +120,32 @@ 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;

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;
Expand Down Expand Up @@ -176,6 +206,20 @@ 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);
} catch (IllegalAccessException | InvocationTargetException ignored) {
}
}

private static void setInt(final Object itemCompound, final String key, final int value) {
try {
setIntMethod.invoke(itemCompound, key, value);
Expand Down Expand Up @@ -299,6 +343,8 @@ 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();
private final static String HAS_TAG_METHOD_NAME = hasTagMethodName();
Expand All @@ -320,6 +366,16 @@ 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";
}

private static String setIntegerMethodName() {
if (VersionHelper.HAS_OBFUSCATED_NAMES) return "a";
return "setInt";
Expand Down

1 comment on commit abd23c9

@GG-MD
Copy link

@GG-MD GG-MD commented on abd23c9 Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BlitzOffline error:

[03:32:22 WARN]: [DeluxeMenus] Task #18802 for DeluxeMenus v1.14.1-DEV-PR--abd23c94 generated an exception
java.lang.IncompatibleClassChangeError: Method 'org.bukkit.Sound org.bukkit.Sound.valueOf(java.lang.String)' must be Methodref constant
        at DeluxeMenus-1.14.1-DEV-PR--abd23c94.jar/com.extendedclip.deluxemenus.action.ClickActionTask.run(ClickActionTask.java:363) ~[DeluxeMenus-1.14.1-DEV-PR--abd23c94.jar:?]
        at org.bukkit.craftbukkit.scheduler.CraftTask.run(CraftTask.java:86) ~[purpur-1.21.1.jar:1.21.1-2329-803bf62]
        at org.bukkit.craftbukkit.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:475) ~[purpur-1.21.1.jar:1.21.1-2329-803bf62]
        at net.minecraft.server.MinecraftServer.tickChildren(MinecraftServer.java:1770) ~[purpur-1.21.1.jar:1.21.1-2329-803bf62]
        at net.minecraft.server.dedicated.DedicatedServer.tickChildren(DedicatedServer.java:513) ~[purpur-1.21.1.jar:1.21.1-2329-803bf62]
        at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:1642) ~[purpur-1.21.1.jar:1.21.1-2329-803bf62]
        at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1342) ~[purpur-1.21.1.jar:1.21.1-2329-803bf62]
        at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:333) ~[purpur-1.21.1.jar:1.21.1-2329-803bf62]
        at java.base/java.lang.Thread.run(Thread.java:1583) ~[?:?]

Please sign in to comment.