From 6bfbe3338a13f241c42560afb8926e29c742eec0 Mon Sep 17 00:00:00 2001 From: TinyTank800 Date: Sat, 3 Feb 2024 12:43:46 -0800 Subject: [PATCH] Updated NBT system using NBTAPI. Rocky will need to update pom.xml --- .../classresources/ItemCreation.java | 40 +++++++++-- .../placeholders/Placeholders.java | 2 +- .../commandtags/CommandTags.java | 2 +- .../ioclasses/nbt/NBTManager.java | 68 ++++++++++++------- .../openwithitem/HotbarItemLoader.java | 5 +- 5 files changed, 85 insertions(+), 32 deletions(-) diff --git a/src/me/rockyhawk/commandpanels/classresources/ItemCreation.java b/src/me/rockyhawk/commandpanels/classresources/ItemCreation.java index 3adade2..cd7eea7 100644 --- a/src/me/rockyhawk/commandpanels/classresources/ItemCreation.java +++ b/src/me/rockyhawk/commandpanels/classresources/ItemCreation.java @@ -1,5 +1,8 @@ package me.rockyhawk.commandpanels.classresources; +import de.tr7zw.changeme.nbtapi.NBT; +import de.tr7zw.changeme.nbtapi.NBTItem; +import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT; import me.arcaniax.hdb.api.HeadDatabaseAPI; import me.rockyhawk.commandpanels.CommandPanels; import me.rockyhawk.commandpanels.api.Panel; @@ -195,7 +198,14 @@ public class ItemCreation { } if(addNBT){ - s = plugin.nbt.setNBT(s); + if (itemSection.contains("nbt")) { + for(String key : Objects.requireNonNull(itemSection.getConfigurationSection("nbt")).getKeys(true)){ + if(itemSection.isConfigurationSection("nbt." + key)){ + continue; + } + s = plugin.nbt.setNBT(s,key,plugin.tex.attachPlaceholders(panel, position, p, Objects.requireNonNull(itemSection.getString("nbt." + key)))); //itemSection.getString("nbt." + key)); + } + } } if (itemSection.contains("enchanted")) { try { @@ -333,8 +343,11 @@ public class ItemCreation { } } if (itemSection.contains("nbt")) { - for(String key : itemSection.getConfigurationSection("nbt").getKeys(false)){ - s = plugin.nbt.setNBT(s,key,itemSection.getString("nbt." + key)); + for(String key : Objects.requireNonNull(itemSection.getConfigurationSection("nbt")).getKeys(true)){ + if(itemSection.isConfigurationSection("nbt." + key)){ + continue; + } + s = plugin.nbt.setNBT(s,key,plugin.tex.attachPlaceholders(panel, position, p, Objects.requireNonNull(itemSection.getString("nbt." + key)))); //itemSection.getString("nbt." + key)); } } // 1.20 Trim Feature for Player Armor @@ -474,6 +487,13 @@ public class ItemCreation { if(plugin.legacy.LOCAL_VERSION.greaterThanOrEqualTo(MinecraftVersions.v1_14)){ file.set("panels." + panelName + ".item." + i + ".customdata", Objects.requireNonNull(cont.getItemMeta()).getCustomModelData()); } + try { + ReadWriteNBT nbt = NBT.itemStackToNBT(cont); + file.set("panels." + panelName + ".item." + i + ".nbt", nbt.toString()); + }catch(Exception ignore){ + //no nbt or error + file.set("panels." + panelName + ".item." + i + ".nbt", null); + } }catch(Exception n){ //skip over an item that spits an error } @@ -487,7 +507,7 @@ public class ItemCreation { Material, Name, Lore, Enchanted, Potion */ @SuppressWarnings("deprecation") - public boolean isIdentical(ItemStack one, ItemStack two){ + public boolean isIdentical(ItemStack one, ItemStack two, Boolean nbtCheck){ //check material if (one.getType() != two.getType()) { return false; @@ -511,6 +531,18 @@ public class ItemCreation { } } }catch(Exception ignore){} + //check for nbt + if(nbtCheck) { + try { + NBTItem nbtitem1 = new NBTItem(one); + NBTItem nbtitem2 = new NBTItem(two); + + if (!nbtitem1.equals(nbtitem2)) { + return false; + + } + } catch (Exception ignore) {} + } //check for damage try { if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_12)){ diff --git a/src/me/rockyhawk/commandpanels/classresources/placeholders/Placeholders.java b/src/me/rockyhawk/commandpanels/classresources/placeholders/Placeholders.java index fa13061..5bc1554 100644 --- a/src/me/rockyhawk/commandpanels/classresources/placeholders/Placeholders.java +++ b/src/me/rockyhawk/commandpanels/classresources/placeholders/Placeholders.java @@ -275,7 +275,7 @@ public class Placeholders { try { //if it is a regular custom item ItemStack confItm = plugin.itemCreate.makeItemFromConfig(panel,position,panel.getConfig().getConfigurationSection("custom-item." + matLoc),p,true,true, false); - if(plugin.itemCreate.isIdentical(confItm,itm)){ + if(plugin.itemCreate.isIdentical(confItm,itm, Objects.requireNonNull(panel.getConfig().getConfigurationSection("custom-item." + matLoc)).contains("nbt"))){ isIdentical = true; } diff --git a/src/me/rockyhawk/commandpanels/commandtags/CommandTags.java b/src/me/rockyhawk/commandpanels/commandtags/CommandTags.java index 87380ad..d300aca 100644 --- a/src/me/rockyhawk/commandpanels/commandtags/CommandTags.java +++ b/src/me/rockyhawk/commandpanels/commandtags/CommandTags.java @@ -265,7 +265,7 @@ public class CommandTags { if (Material.matchMaterial(args[1]) == null) { //item-paywall is a custom item as it is not a material - if (plugin.itemCreate.isIdentical(sellItem, itm)) { + if (plugin.itemCreate.isIdentical(sellItem, itm, Objects.requireNonNull(panel.getConfig().getConfigurationSection("custom-item." + args[1])).contains("nbt"))) { ItemStack add = new ItemStack(p.getInventory().getItem(f).getType(), p.getInventory().getItem(f).getAmount()); remainingAmount -= add.getAmount(); if (removal) remCont.put(f,add); diff --git a/src/me/rockyhawk/commandpanels/ioclasses/nbt/NBTManager.java b/src/me/rockyhawk/commandpanels/ioclasses/nbt/NBTManager.java index daa57c0..4bb83fc 100644 --- a/src/me/rockyhawk/commandpanels/ioclasses/nbt/NBTManager.java +++ b/src/me/rockyhawk/commandpanels/ioclasses/nbt/NBTManager.java @@ -1,5 +1,7 @@ package me.rockyhawk.commandpanels.ioclasses.nbt; +import de.tr7zw.changeme.nbtapi.NBT; +import de.tr7zw.changeme.nbtapi.NBTItem; import me.rockyhawk.commandpanels.CommandPanels; import me.rockyhawk.commandpanels.ioclasses.legacy.MinecraftVersions; import org.bukkit.inventory.ItemStack; @@ -12,39 +14,57 @@ public class NBTManager { //commandpanel item NBT public boolean hasNBT(ItemStack item){ - if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_13)){ - return new NBT_1_13().contains(item, "CommandPanelsItem"); - }else{ - return new NBT_1_14(plugin).hasNBT(item,"CommandPanelsItem"); - } + NBTItem nbti = new NBTItem(item); + return nbti.hasTag("CommandPanelsItem"); + +// if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_13)){ +// return new NBT_1_13().contains(item, "CommandPanelsItem"); +// }else{ +// return new NBT_1_14(plugin).hasNBT(item,"CommandPanelsItem"); +// } } public ItemStack setNBT(ItemStack item){ - if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_13)){ - return new NBT_1_13().set(item,1,"CommandPanelsItem"); - }else{ - return new NBT_1_14(plugin).addNBT(item,"CommandPanelsItem","1"); - } + NBT.modify(item, nbt -> { + nbt.setString("CommandPanelsItem", "1"); + }); + + return item; + +// if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_13)){ +// return new NBT_1_13().set(item,1,"CommandPanelsItem"); +// }else{ +// return new NBT_1_14(plugin).addNBT(item,"CommandPanelsItem","1"); +// } } //custom key NBT public String getNBT(ItemStack item, String key){ - String output = ""; - if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_13)){ - try{ - output = new NBT_1_13().getString(item, key); - }catch(NullPointerException ignore){} - }else{ - output = new NBT_1_14(plugin).getNBT(item, key); - } - return output; + NBTItem nbti = new NBTItem(item); + if(!nbti.hasNBTData()) return ""; + return nbti.getString(key); + +// String output = ""; +// if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_13)){ +// try{ +// output = new NBT_1_13().getString(item, key); +// }catch(NullPointerException ignore){} +// }else{ +// output = new NBT_1_14(plugin).getNBT(item, key); +// } +// return output; } public ItemStack setNBT(ItemStack item, String key, String value){ - if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_13)){ - return new NBT_1_13().set(item,value,key); - }else{ - return new NBT_1_14(plugin).addNBT(item,key,value); - } + NBT.modify(item, nbt -> { + nbt.setString(key, value); + }); + + return item; + //if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_13)){ + // return new NBT_1_13().set(item,value,key); + //}else{ + // return new NBT_1_14(plugin).addNBT(item,key,value); + //} } } diff --git a/src/me/rockyhawk/commandpanels/openwithitem/HotbarItemLoader.java b/src/me/rockyhawk/commandpanels/openwithitem/HotbarItemLoader.java index a15a790..035a941 100644 --- a/src/me/rockyhawk/commandpanels/openwithitem/HotbarItemLoader.java +++ b/src/me/rockyhawk/commandpanels/openwithitem/HotbarItemLoader.java @@ -10,6 +10,7 @@ import org.bukkit.event.inventory.ClickType; import org.bukkit.inventory.ItemStack; import java.util.HashMap; +import java.util.Objects; import java.util.UUID; public class HotbarItemLoader { @@ -63,7 +64,7 @@ public class HotbarItemLoader { //return true if found public boolean itemCheckExecute(ItemStack invItem, Player p, boolean openPanel, boolean stationaryOnly){ try { - if (plugin.nbt.getNBT(invItem, "CommandPanelsHotbar") == null) { + if (Objects.equals(plugin.nbt.getNBT(invItem, "CommandPanelsHotbar"), "")) { return false; } }catch(IllegalArgumentException | NullPointerException nu){ @@ -119,7 +120,7 @@ public class HotbarItemLoader { stationaryItems.put(p.getUniqueId(),new HotbarPlayerManager()); for(int i = 0; i <= 35; i++){ try { - if (plugin.nbt.getNBT(p.getInventory().getItem(i), "CommandPanelsHotbar") != null) { + if (!Objects.equals(plugin.nbt.getNBT(p.getInventory().getItem(i), "CommandPanelsHotbar"), "")) { //do not remove items that are not stationary if(!plugin.nbt.getNBT(p.getInventory().getItem(i), "CommandPanelsHotbar").endsWith("-1")) { p.getInventory().setItem(i, new ItemStack(Material.AIR));