forked from Upstream/CommandPanels
NBT Improvements
This commit is contained in:
parent
843ba8b393
commit
a8663e41a7
2
pom.xml
2
pom.xml
@ -126,7 +126,7 @@
|
||||
<dependency>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-api</artifactId>
|
||||
<version>2.12.4-SNAPSHOT</version>
|
||||
<version>2.13.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -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);
|
||||
|
@ -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")) {
|
||||
|
@ -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;
|
||||
if (section.isConfigurationSection(key)) {
|
||||
// Recursive call for nested sections
|
||||
applyNBTRecursively(fullPath, section.getConfigurationSection(key), s, p, 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);
|
||||
// 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)) {
|
||||
// Create a compound for each ConfigurationSection
|
||||
NBTCompound compound = nbtItem.addCompound(key);
|
||||
convertSectionToNBT(compound, section.getConfigurationSection(key), player, panel, position);
|
||||
} else {
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user