Updated NBT system using NBTAPI. Rocky will need to update pom.xml

This commit is contained in:
TinyTank800 2024-02-03 12:43:46 -08:00
parent 569a8172b9
commit 6bfbe3338a
5 changed files with 85 additions and 32 deletions

View File

@ -1,5 +1,8 @@
package me.rockyhawk.commandpanels.classresources; 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.arcaniax.hdb.api.HeadDatabaseAPI;
import me.rockyhawk.commandpanels.CommandPanels; import me.rockyhawk.commandpanels.CommandPanels;
import me.rockyhawk.commandpanels.api.Panel; import me.rockyhawk.commandpanels.api.Panel;
@ -195,7 +198,14 @@ public class ItemCreation {
} }
if(addNBT){ 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")) { if (itemSection.contains("enchanted")) {
try { try {
@ -333,8 +343,11 @@ public class ItemCreation {
} }
} }
if (itemSection.contains("nbt")) { if (itemSection.contains("nbt")) {
for(String key : itemSection.getConfigurationSection("nbt").getKeys(false)){ for(String key : Objects.requireNonNull(itemSection.getConfigurationSection("nbt")).getKeys(true)){
s = plugin.nbt.setNBT(s,key,itemSection.getString("nbt." + key)); 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 // 1.20 Trim Feature for Player Armor
@ -474,6 +487,13 @@ public class ItemCreation {
if(plugin.legacy.LOCAL_VERSION.greaterThanOrEqualTo(MinecraftVersions.v1_14)){ if(plugin.legacy.LOCAL_VERSION.greaterThanOrEqualTo(MinecraftVersions.v1_14)){
file.set("panels." + panelName + ".item." + i + ".customdata", Objects.requireNonNull(cont.getItemMeta()).getCustomModelData()); 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){ }catch(Exception n){
//skip over an item that spits an error //skip over an item that spits an error
} }
@ -487,7 +507,7 @@ public class ItemCreation {
Material, Name, Lore, Enchanted, Potion Material, Name, Lore, Enchanted, Potion
*/ */
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public boolean isIdentical(ItemStack one, ItemStack two){ public boolean isIdentical(ItemStack one, ItemStack two, Boolean nbtCheck){
//check material //check material
if (one.getType() != two.getType()) { if (one.getType() != two.getType()) {
return false; return false;
@ -511,6 +531,18 @@ public class ItemCreation {
} }
} }
}catch(Exception ignore){} }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 //check for damage
try { try {
if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_12)){ if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_12)){

View File

@ -275,7 +275,7 @@ public class Placeholders {
try { try {
//if it is a regular custom item //if it is a regular custom item
ItemStack confItm = plugin.itemCreate.makeItemFromConfig(panel,position,panel.getConfig().getConfigurationSection("custom-item." + matLoc),p,true,true, false); 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; isIdentical = true;
} }

View File

@ -265,7 +265,7 @@ public class CommandTags {
if (Material.matchMaterial(args[1]) == null) { if (Material.matchMaterial(args[1]) == null) {
//item-paywall is a custom item as it is not a material //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()); ItemStack add = new ItemStack(p.getInventory().getItem(f).getType(), p.getInventory().getItem(f).getAmount());
remainingAmount -= add.getAmount(); remainingAmount -= add.getAmount();
if (removal) remCont.put(f,add); if (removal) remCont.put(f,add);

View File

@ -1,5 +1,7 @@
package me.rockyhawk.commandpanels.ioclasses.nbt; 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.CommandPanels;
import me.rockyhawk.commandpanels.ioclasses.legacy.MinecraftVersions; import me.rockyhawk.commandpanels.ioclasses.legacy.MinecraftVersions;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -12,39 +14,57 @@ public class NBTManager {
//commandpanel item NBT //commandpanel item NBT
public boolean hasNBT(ItemStack item){ public boolean hasNBT(ItemStack item){
if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_13)){ NBTItem nbti = new NBTItem(item);
return new NBT_1_13().contains(item, "CommandPanelsItem"); return nbti.hasTag("CommandPanelsItem");
}else{
return new NBT_1_14(plugin).hasNBT(item,"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){ public ItemStack setNBT(ItemStack item){
if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_13)){ NBT.modify(item, nbt -> {
return new NBT_1_13().set(item,1,"CommandPanelsItem"); nbt.setString("CommandPanelsItem", "1");
}else{ });
return new NBT_1_14(plugin).addNBT(item,"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 //custom key NBT
public String getNBT(ItemStack item, String key){ public String getNBT(ItemStack item, String key){
String output = ""; NBTItem nbti = new NBTItem(item);
if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_13)){ if(!nbti.hasNBTData()) return "";
try{ return nbti.getString(key);
output = new NBT_1_13().getString(item, key);
}catch(NullPointerException ignore){} // String output = "";
}else{ // if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_13)){
output = new NBT_1_14(plugin).getNBT(item, key); // try{
} // output = new NBT_1_13().getString(item, key);
return output; // }catch(NullPointerException ignore){}
// }else{
// output = new NBT_1_14(plugin).getNBT(item, key);
// }
// return output;
} }
public ItemStack setNBT(ItemStack item, String key, String value){ public ItemStack setNBT(ItemStack item, String key, String value){
if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_13)){ NBT.modify(item, nbt -> {
return new NBT_1_13().set(item,value,key); nbt.setString(key, value);
}else{ });
return new NBT_1_14(plugin).addNBT(item,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);
//}
} }
} }

View File

@ -10,6 +10,7 @@ import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.HashMap; import java.util.HashMap;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
public class HotbarItemLoader { public class HotbarItemLoader {
@ -63,7 +64,7 @@ public class HotbarItemLoader {
//return true if found //return true if found
public boolean itemCheckExecute(ItemStack invItem, Player p, boolean openPanel, boolean stationaryOnly){ public boolean itemCheckExecute(ItemStack invItem, Player p, boolean openPanel, boolean stationaryOnly){
try { try {
if (plugin.nbt.getNBT(invItem, "CommandPanelsHotbar") == null) { if (Objects.equals(plugin.nbt.getNBT(invItem, "CommandPanelsHotbar"), "")) {
return false; return false;
} }
}catch(IllegalArgumentException | NullPointerException nu){ }catch(IllegalArgumentException | NullPointerException nu){
@ -119,7 +120,7 @@ public class HotbarItemLoader {
stationaryItems.put(p.getUniqueId(),new HotbarPlayerManager()); stationaryItems.put(p.getUniqueId(),new HotbarPlayerManager());
for(int i = 0; i <= 35; i++){ for(int i = 0; i <= 35; i++){
try { 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 //do not remove items that are not stationary
if(!plugin.nbt.getNBT(p.getInventory().getItem(i), "CommandPanelsHotbar").endsWith("-1")) { if(!plugin.nbt.getNBT(p.getInventory().getItem(i), "CommandPanelsHotbar").endsWith("-1")) {
p.getInventory().setItem(i, new ItemStack(Material.AIR)); p.getInventory().setItem(i, new ItemStack(Material.AIR));