From a8663e41a713baff5ffebf3aa87abceb5badb30b Mon Sep 17 00:00:00 2001 From: rockyhawk64 Date: Sun, 16 Jun 2024 21:36:21 +1000 Subject: [PATCH] NBT Improvements --- pom.xml | 2 +- src/me/rockyhawk/commandpanels/api/Panel.java | 2 +- .../classresources/ItemCreation.java | 6 +- .../ioclasses/nbt/NBTManager.java | 168 ++++++++++++------ 4 files changed, 124 insertions(+), 54 deletions(-) diff --git a/pom.xml b/pom.xml index c256037..b44b198 100644 --- a/pom.xml +++ b/pom.xml @@ -126,7 +126,7 @@ de.tr7zw item-nbt-api - 2.12.4-SNAPSHOT + 2.13.0 compile diff --git a/src/me/rockyhawk/commandpanels/api/Panel.java b/src/me/rockyhawk/commandpanels/api/Panel.java index 9edad2f..d2e626b 100644 --- a/src/me/rockyhawk/commandpanels/api/Panel.java +++ b/src/me/rockyhawk/commandpanels/api/Panel.java @@ -99,7 +99,7 @@ public class Panel{ } try { //add NBT to item and return the ItemStack - return plugin.nbt.setNBT(s, "CommandPanelsHotbar", "string", panelName + ":" + slot); + return plugin.nbt.setNBT(s, "CommandPanelsHotbar", "string_" + panelName + ":" + slot); }catch(Exception e) { //return air if null return new ItemStack(Material.AIR); diff --git a/src/me/rockyhawk/commandpanels/classresources/ItemCreation.java b/src/me/rockyhawk/commandpanels/classresources/ItemCreation.java index 63a6dac..24ed7a2 100644 --- a/src/me/rockyhawk/commandpanels/classresources/ItemCreation.java +++ b/src/me/rockyhawk/commandpanels/classresources/ItemCreation.java @@ -191,10 +191,12 @@ public class ItemCreation { } if(itemSection.contains("nbt")){ - plugin.nbt.applyNBTRecursively("", itemSection.getConfigurationSection("nbt"), s, p, panel, position); + //ItemStack item, ConfigurationSection section, Player player, Panel panel, PanelPosition position + plugin.nbt.applyNBTRecursively(s, itemSection.getConfigurationSection("nbt"), p, panel, position); + //plugin.nbt.applyNBTRecursively("", itemSection.getConfigurationSection("nbt"), s, p, panel, position); } if(addNBT){ - plugin.nbt.setNBT(s, "CommandPanelsItem","boolean", "true"); + plugin.nbt.setNBT(s, "CommandPanelsItem","boolean_" + "true"); } if (itemSection.contains("enchanted")) { diff --git a/src/me/rockyhawk/commandpanels/ioclasses/nbt/NBTManager.java b/src/me/rockyhawk/commandpanels/ioclasses/nbt/NBTManager.java index d618786..39e04fa 100644 --- a/src/me/rockyhawk/commandpanels/ioclasses/nbt/NBTManager.java +++ b/src/me/rockyhawk/commandpanels/ioclasses/nbt/NBTManager.java @@ -1,9 +1,11 @@ package me.rockyhawk.commandpanels.ioclasses.nbt; +import de.tr7zw.changeme.nbtapi.NBTCompound; import de.tr7zw.changeme.nbtapi.NBTItem; import me.rockyhawk.commandpanels.CommandPanels; import me.rockyhawk.commandpanels.api.Panel; import me.rockyhawk.commandpanels.openpanelsmanager.PanelPosition; +import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -21,45 +23,6 @@ public class NBTManager { return nbtitem1.equals(nbtitem2); } - public ItemStack setNBT(ItemStack item, String key, String type, String value) { - type = type.toLowerCase(); - if (item == null) { - throw new IllegalArgumentException("ItemStack cannot be null"); - } - NBTItem nbtItem = new NBTItem(item); - - switch (type.toLowerCase()) { - case "byte": - nbtItem.setByte(key, Byte.valueOf(value)); - break; - case "boolean": - nbtItem.setBoolean(key, Boolean.valueOf(value)); - break; - case "short": - nbtItem.setShort(key, Short.valueOf(value)); - break; - case "integer": - nbtItem.setInteger(key, Integer.valueOf(value)); - break; - case "long": - nbtItem.setLong(key, Long.valueOf(value)); - break; - case "float": - nbtItem.setFloat(key, Float.valueOf(value)); - break; - case "double": - nbtItem.setDouble(key, Double.valueOf(value)); - break; - case "string": - nbtItem.setString(key, value); - break; - default: - throw new IllegalArgumentException("Unsupported NBT type: " + type); - } - item.setItemMeta(nbtItem.getItem().getItemMeta()); - return item; - } - public Object getNBT(ItemStack item, String key, String type) { type = type.toLowerCase(); NBTItem nbtItem = new NBTItem(item); @@ -92,20 +55,125 @@ public class NBTManager { //nbt will be assigned with the value "string_value_1 //which uses the type "String" and value "value_1" - public void applyNBTRecursively(String path, ConfigurationSection section, ItemStack s, Player p, Panel panel, PanelPosition position) { - for (String key : section.getKeys(true)) { - String fullPath = path.isEmpty() ? key : path + "." + key; + // Method to apply NBT recursively + public ItemStack setNBT(ItemStack item, String key, String value) { + if (item == null || item.getType() == Material.AIR) { + return item; + } + NBTItem nbtItem = new NBTItem(item); + setNBTDirectlyOnItem(nbtItem, key, value); + + item.setItemMeta(nbtItem.getItem().getItemMeta()); + return item; + } + + // Method to apply NBT recursively + public void applyNBTRecursively(ItemStack item, ConfigurationSection section, Player player, Panel panel, PanelPosition position) { + NBTItem nbtItem = new NBTItem(item); + + // Iterate over each key in the root of the ConfigurationSection + for (String key : section.getKeys(false)) { + // Check if the key represents a ConfigurationSection if (section.isConfigurationSection(key)) { - // Recursive call for nested sections - applyNBTRecursively(fullPath, section.getConfigurationSection(key), s, p, panel, position); + // Create a compound for each ConfigurationSection + NBTCompound compound = nbtItem.addCompound(key); + convertSectionToNBT(compound, section.getConfigurationSection(key), player, panel, position); } else { - // Set the NBT tag at this level - String wholeValue = plugin.tex.attachPlaceholders(panel, position, p, section.getString(key)); - int firstUnderscore = wholeValue.indexOf("_"); - String type = wholeValue.substring(0, firstUnderscore); - String value = wholeValue.substring(firstUnderscore + 1); - s = plugin.nbt.setNBT(s, fullPath, type, value); + // Handle non-section values directly on the NBTItem if necessary + String value = plugin.tex.attachPlaceholders(panel, position, player, section.getString(key)); + setNBTDirectlyOnItem(nbtItem, key, value); + } + } + item.setItemMeta(nbtItem.getItem().getItemMeta()); + } + + // Convert ConfigurationSection to NBTCompound recursively + private void convertSectionToNBT(NBTCompound compound, ConfigurationSection section, Player player, Panel panel, PanelPosition position) { + for (String key : section.getKeys(false)) { + if (section.isConfigurationSection(key)) { + // Recursively convert sub-sections to their own compounds + NBTCompound subCompound = compound.addCompound(key); + convertSectionToNBT(subCompound, section.getConfigurationSection(key), player, panel, position); + } else { + // Handle scalar values within each compound + String value = plugin.tex.attachPlaceholders(panel, position, player, section.getString(key)); + setNBTOnCompound(compound, key, value); } } } + + private void setNBTDirectlyOnItem(NBTItem nbtItem, String key, String value) { + int underscoreIndex = value.indexOf("_"); + if (underscoreIndex == -1) return; // Skip if format is invalid + + String type = value.substring(0, underscoreIndex); + String val = value.substring(underscoreIndex + 1); + + switch (type.toLowerCase()) { + case "byte": + nbtItem.setByte(key, Byte.parseByte(val)); + break; + case "boolean": + nbtItem.setBoolean(key, Boolean.parseBoolean(val)); + break; + case "short": + nbtItem.setShort(key, Short.parseShort(val)); + break; + case "integer": + nbtItem.setInteger(key, Integer.parseInt(val)); + break; + case "long": + nbtItem.setLong(key, Long.parseLong(val)); + break; + case "float": + nbtItem.setFloat(key, Float.parseFloat(val)); + break; + case "double": + nbtItem.setDouble(key, Double.parseDouble(val)); + break; + case "string": + nbtItem.setString(key, val); + break; + default: + throw new IllegalArgumentException("Unsupported NBT type: " + type); + } + } + + // Set typed value on an NBTCompound + private void setNBTOnCompound(NBTCompound compound, String key, String value) { + int underscoreIndex = value.indexOf("_"); + if (underscoreIndex == -1) return; // Invalid format, skip + + String type = value.substring(0, underscoreIndex); + String val = value.substring(underscoreIndex + 1); + + switch (type.toLowerCase()) { + case "byte": + compound.setByte(key, Byte.parseByte(val)); + break; + case "boolean": + compound.setBoolean(key, Boolean.parseBoolean(val)); + break; + case "short": + compound.setShort(key, Short.parseShort(val)); + break; + case "integer": + compound.setInteger(key, Integer.parseInt(val)); + break; + case "long": + compound.setLong(key, Long.parseLong(val)); + break; + case "float": + compound.setFloat(key, Float.parseFloat(val)); + break; + case "double": + compound.setDouble(key, Double.parseDouble(val)); + break; + case "string": + compound.setString(key, val); + break; + default: + throw new IllegalArgumentException("Unsupported NBT type: " + type); + } + } } \ No newline at end of file