mirror of
https://github.com/rockyhawk64/CommandPanels.git
synced 2025-11-18 07:14:17 +01:00
Merge pull request #352 from TinyTank800/latest
NBT Rework, Player input click tags, duplicate: commands
This commit is contained in:
commit
5ba814c824
2
pom.xml
2
pom.xml
@ -134,7 +134,7 @@
|
||||
<dependency>
|
||||
<groupId>net.kyori</groupId>
|
||||
<artifactId>adventure-text-minimessage</artifactId>
|
||||
<version>4.17.0</version>
|
||||
<version>4.19.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
@ -1,13 +1,17 @@
|
||||
package me.rockyhawk.commandpanels;
|
||||
|
||||
import me.rockyhawk.commandpanels.api.Panel;
|
||||
import me.rockyhawk.commandpanels.commandtags.PaywallEvent;
|
||||
import me.rockyhawk.commandpanels.commandtags.PaywallOutput;
|
||||
import me.rockyhawk.commandpanels.interactives.input.PlayerInput;
|
||||
import me.rockyhawk.commandpanels.openpanelsmanager.PanelPosition;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@ -95,6 +99,26 @@ public class Utils implements Listener {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If we didn't find the slot directly, check if it's a duplicate
|
||||
if(foundSlot == null){
|
||||
// Loop through all items to check their duplicate configurations
|
||||
for(String item : Objects.requireNonNull(panel.getConfig().getConfigurationSection("item")).getKeys(false)){
|
||||
String section = plugin.has.hasSection(panel, position, panel.getConfig().getConfigurationSection("item." + item), p);
|
||||
|
||||
// Check if this item has a duplicate configuration
|
||||
if(panel.getConfig().contains("item." + item + section + ".duplicate")) {
|
||||
String duplicateValue = panel.getConfig().getString("item." + item + section + ".duplicate");
|
||||
|
||||
// Check if the clicked slot is in the duplicate configuration
|
||||
if(isSlotInDuplicate(clickedSlot, duplicateValue)) {
|
||||
foundSlot = item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(foundSlot == null){
|
||||
e.setCancelled(true);
|
||||
return;
|
||||
@ -115,16 +139,37 @@ public class Utils implements Listener {
|
||||
e.setCancelled(true);
|
||||
p.updateInventory();
|
||||
|
||||
//if an item has an area for input instead of commands
|
||||
//if an item has an area for input instead of commands - Now with click type support
|
||||
if(panel.getConfig().contains("item." + foundSlot + section + ".player-input")) {
|
||||
List<String> playerInputs = panel.getConfig().getStringList("item." + foundSlot + section + ".player-input");
|
||||
List<String> filteredPlayerInputs = new ArrayList<>();
|
||||
|
||||
//Check for cancel player input events.
|
||||
List<String> cancelCommands = null;
|
||||
if(panel.getConfig().contains("item." + foundSlot + section + ".player-canceled-input")){
|
||||
cancelCommands = panel.getConfig().getStringList("item." + foundSlot + section + ".player-canceled-input");
|
||||
}
|
||||
plugin.inputUtils.playerInput.put(p,new PlayerInput(panel,panel.getConfig().getStringList("item." + foundSlot + section + ".player-input"),cancelCommands,e.getClick()));
|
||||
plugin.inputUtils.sendMessage(panel,position,p);
|
||||
|
||||
// Check for valid click types in player inputs
|
||||
ClickType click = e.getClick();
|
||||
boolean validClickFound = false;
|
||||
|
||||
for(String playerInput : playerInputs) {
|
||||
String validInput = plugin.commandRunner.hasCorrectClick(playerInput, click);
|
||||
if(!validInput.isEmpty()) {
|
||||
filteredPlayerInputs.add(validInput);
|
||||
validClickFound = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Only process player input if we have valid inputs for this click type
|
||||
if(validClickFound) {
|
||||
plugin.inputUtils.playerInput.put(p, new PlayerInput(panel, filteredPlayerInputs, cancelCommands, click));
|
||||
plugin.inputUtils.sendMessage(panel, position, p);
|
||||
}
|
||||
}
|
||||
|
||||
// Run commands section.
|
||||
if(panel.getConfig().contains("item." + foundSlot + section + ".commands")) {
|
||||
List<String> commands = panel.getConfig().getStringList("item." + foundSlot + section + ".commands");
|
||||
if (!commands.isEmpty()) {
|
||||
@ -136,9 +181,9 @@ public class Utils implements Listener {
|
||||
}
|
||||
}
|
||||
if (panel.getConfig().contains("item." + foundSlot + section + ".multi-paywall")) {
|
||||
plugin.commandRunner.runMultiPaywall(panel,position,p,
|
||||
plugin.commandRunner.runMultiPaywall(panel, position, p,
|
||||
panel.getConfig().getStringList("item." + foundSlot + section + ".multi-paywall"),
|
||||
commands,e.getClick());
|
||||
commands, e.getClick());
|
||||
} else {
|
||||
plugin.commandRunner.runCommands(panel, position, p, commands, e.getClick());
|
||||
}
|
||||
@ -153,4 +198,36 @@ public class Utils implements Listener {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//Helper method to see if the slot is a duplicate so it can copy commands.
|
||||
private boolean isSlotInDuplicate(int slot, String duplicateConfig) {
|
||||
if(duplicateConfig == null) return false;
|
||||
|
||||
String[] dupeItems = duplicateConfig.split(",");
|
||||
|
||||
for(String dupeItem : dupeItems) {
|
||||
dupeItem = dupeItem.trim(); // Remove any whitespace
|
||||
|
||||
if(dupeItem.contains("-")) {
|
||||
// This is a range
|
||||
String[] range = dupeItem.split("-");
|
||||
int min = Integer.parseInt(range[0]);
|
||||
int max = Integer.parseInt(range[1]);
|
||||
|
||||
if(slot >= min && slot <= max) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
// This is a single slot
|
||||
try {
|
||||
int dupeSlot = Integer.parseInt(dupeItem);
|
||||
if(dupeSlot == slot) {
|
||||
return true;
|
||||
}
|
||||
} catch(NumberFormatException ignored) {}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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", panelName + ":" + slot);
|
||||
}catch(Exception e) {
|
||||
//return air if null
|
||||
return new ItemStack(Material.AIR);
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package me.rockyhawk.commandpanels.classresources;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBTItem;
|
||||
import dev.lone.itemsadder.api.CustomStack;
|
||||
import me.arcaniax.hdb.api.HeadDatabaseAPI;
|
||||
import me.rockyhawk.commandpanels.CommandPanels;
|
||||
@ -33,6 +34,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class ItemCreation {
|
||||
CommandPanels plugin;
|
||||
|
||||
public ItemCreation(CommandPanels pl) {
|
||||
plugin = pl;
|
||||
}
|
||||
@ -141,6 +143,36 @@ public class ItemCreation {
|
||||
}
|
||||
}
|
||||
|
||||
//Nexo support, needs itemID (eg, nexo= rubySword)
|
||||
if (matraw.split("\\s")[0].equalsIgnoreCase("nexo=")) {
|
||||
String itemID = matraw.split("\\s")[1];
|
||||
try {
|
||||
if (plugin.getServer().getPluginManager().isPluginEnabled("Nexo")) {
|
||||
// Get the NexoItems class dynamically
|
||||
Class<?> nexoItemsClass = Class.forName("com.nexomc.nexo.api.NexoItems");
|
||||
|
||||
// Get the itemFromId method (which takes a String)
|
||||
Method itemFromIdMethod = nexoItemsClass.getMethod("itemFromId", String.class);
|
||||
|
||||
// Invoke itemFromId with the item ID (assuming it's static)
|
||||
Object nexoItem = itemFromIdMethod.invoke(null, itemID);
|
||||
|
||||
if (nexoItem != null) {
|
||||
// Get the build() method and invoke it
|
||||
Method buildMethod = nexoItem.getClass().getMethod("build");
|
||||
Object itemStack = buildMethod.invoke(nexoItem);
|
||||
|
||||
if (itemStack instanceof ItemStack) {
|
||||
s = ((ItemStack) itemStack).clone();
|
||||
normalCreation = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(); // Print stack trace for debugging
|
||||
}
|
||||
}
|
||||
|
||||
//creates custom MMOItems items
|
||||
if(matraw.split("\\s")[0].equalsIgnoreCase("mmo=") && plugin.getServer().getPluginManager().isPluginEnabled("MMOItems")){
|
||||
String itemType = matraw.split("\\s")[1];
|
||||
@ -245,7 +277,7 @@ public class ItemCreation {
|
||||
plugin.nbt.applyNBTRecursively(s, itemSection.getConfigurationSection("nbt"), p, panel, position);
|
||||
}
|
||||
if(addNBT){
|
||||
plugin.nbt.setNBT(s, "CommandPanelsItem","boolean_" + "true");
|
||||
plugin.nbt.setNBT(s, "CommandPanelsItem","true");
|
||||
}
|
||||
|
||||
if (itemSection.contains("enchanted")) {
|
||||
@ -289,6 +321,25 @@ public class ItemCreation {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (itemSection.contains("tooltip-style")) {
|
||||
//Item Model 1.21.4+
|
||||
ItemMeta itemMeta = s.getItemMeta();
|
||||
assert itemMeta != null;
|
||||
|
||||
try {
|
||||
// Check if the setHideTooltip method exists
|
||||
Method setTooltipMethod = ItemMeta.class.getMethod("setTooltipStyle", NamespacedKey.class);
|
||||
|
||||
// Invoke it dynamically
|
||||
setTooltipMethod.invoke(itemMeta, NamespacedKey.fromString(plugin.tex.placeholders(panel, position, p, itemSection.getString("tooltip-style"))));
|
||||
|
||||
s.setItemMeta(itemMeta);
|
||||
} catch (NoSuchMethodException e) {
|
||||
// The method does not exist in older Spigot versions
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (itemSection.contains("customdata")) {
|
||||
ItemMeta customMeta = s.getItemMeta();
|
||||
assert customMeta != null;
|
||||
@ -544,7 +595,9 @@ public class ItemCreation {
|
||||
file.set("panels." + panelName + ".item." + i + ".name", Objects.requireNonNull(cont.getItemMeta()).getDisplayName());
|
||||
file.set("panels." + panelName + ".item." + i + ".lore", Objects.requireNonNull(cont.getItemMeta()).getLore());
|
||||
if(plugin.legacy.MAJOR_VERSION.greaterThanOrEqualTo(MinecraftVersions.v1_14)){
|
||||
file.set("panels." + panelName + ".item." + i + ".customdata", Objects.requireNonNull(cont.getItemMeta()).getCustomModelData());
|
||||
if(cont.getItemMeta().hasCustomModelData()){
|
||||
file.set("panels." + panelName + ".item." + i + ".customdata", Objects.requireNonNull(cont.getItemMeta()).getCustomModelData());
|
||||
}
|
||||
}
|
||||
if(plugin.legacy.MAJOR_VERSION.greaterThanOrEqualTo(MinecraftVersions.v1_22) ||
|
||||
(plugin.legacy.MAJOR_VERSION.greaterThanOrEqualTo(MinecraftVersions.v1_21) && plugin.legacy.MINOR_VERSION >= 4)){
|
||||
@ -562,8 +615,35 @@ public class ItemCreation {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Try getting item NBT
|
||||
try {
|
||||
NBTItem nbtItem = new NBTItem(cont); // Convert item to NBTItem
|
||||
|
||||
// Base path where NBT data is stored in the YAML file
|
||||
String basePath = "panels." + panelName + ".item." + i + ".nbt";
|
||||
|
||||
// Iterate over all NBT keys
|
||||
for (String key : nbtItem.getKeys()) {
|
||||
Object value = plugin.nbt.getNBTValue(nbtItem.getItem(), key); // Retrieve dynamically
|
||||
|
||||
if (value == null) continue; // Skip null values
|
||||
|
||||
if (value instanceof Map) {
|
||||
// Save nested NBT compounds properly
|
||||
ConfigurationSection subSection = file.createSection(basePath + "." + key);
|
||||
plugin.nbt.saveMapToYAML((Map<String, Object>) value, subSection);
|
||||
} else {
|
||||
// Directly set primitive values
|
||||
file.set(basePath + "." + key, value);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("Error while saving NBT data: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}catch(Exception n){
|
||||
//skip over an item that spits an error
|
||||
//skip over an item that spits an error - Usually air
|
||||
}
|
||||
}
|
||||
return file;
|
||||
@ -636,7 +716,6 @@ public class ItemCreation {
|
||||
} catch (Exception e) {
|
||||
plugin.debug(e,null);
|
||||
}
|
||||
|
||||
}
|
||||
}catch(Exception ignore){}
|
||||
//check for nbt
|
||||
|
||||
@ -1,12 +1,15 @@
|
||||
package me.rockyhawk.commandpanels.classresources.placeholders;
|
||||
|
||||
import com.earth2me.essentials.Essentials;
|
||||
import de.tr7zw.changeme.nbtapi.NBTItem;
|
||||
import me.rockyhawk.commandpanels.CommandPanels;
|
||||
import me.rockyhawk.commandpanels.api.Panel;
|
||||
import me.rockyhawk.commandpanels.ioclasses.legacy.MinecraftVersions;
|
||||
import me.rockyhawk.commandpanels.openpanelsmanager.PanelPosition;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.Damageable;
|
||||
import org.bukkit.inventory.meta.PotionMeta;
|
||||
@ -125,7 +128,7 @@ public class Placeholders {
|
||||
}
|
||||
}
|
||||
|
||||
//placeholder to check for server availability %cp-server-IP:PORT%
|
||||
//placeholder to check for server availability %cp-server-IP:PORT% returns true, false
|
||||
if(identifier.startsWith("server-")) {
|
||||
String ip_port = identifier.replace("server-", "");
|
||||
Socket s = new Socket();
|
||||
@ -138,18 +141,30 @@ public class Placeholders {
|
||||
}
|
||||
}
|
||||
|
||||
//placeholder to check if an item has NBT %cp-nbt-slot:type:key%
|
||||
//placeholder to check if the player has material and amount of in their inventory %cp-checkinv-MATERIAL:<AMOUNT>% returns true, false
|
||||
if(identifier.startsWith("checkinv-")) {
|
||||
String[] materialList = identifier.replace("checkinv-", "").split(":");
|
||||
int amount = 0;
|
||||
if(materialList[0] == null){ return "false"; }
|
||||
if(materialList[1] == null){ amount = 1; }
|
||||
|
||||
String material = materialList[0];
|
||||
amount = Integer.parseInt(materialList[1]);
|
||||
|
||||
Inventory inv = p.getInventory();
|
||||
if(inv.contains(Material.valueOf(material), amount)){
|
||||
return "true";
|
||||
} else {
|
||||
return "false";
|
||||
}
|
||||
}
|
||||
|
||||
//placeholder to check if an item has NBT %cp-nbt-slot:key% returns value, empty, ""
|
||||
if (identifier.startsWith("nbt-")) {
|
||||
try {
|
||||
String slot_key = identifier.replace("nbt-", "");
|
||||
Object value;
|
||||
value = plugin.nbt.getNBT(
|
||||
p.getOpenInventory().getTopInventory().getItem(
|
||||
(int) Double.parseDouble(slot_key.split(":")[0])
|
||||
),
|
||||
slot_key.split(":")[2],
|
||||
slot_key.split(":")[1]
|
||||
);
|
||||
value = plugin.nbt.getNBTValue(p.getOpenInventory().getTopInventory().getItem((int) Double.parseDouble(slot_key.split(":")[0])), slot_key.split(":")[1]);
|
||||
// Convert any object type to a string, handle null explicitly if desired
|
||||
return value == null ? "empty" : String.valueOf(value);
|
||||
} catch (Exception ex) {
|
||||
|
||||
@ -2,6 +2,7 @@ package me.rockyhawk.commandpanels.ioclasses.nbt;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBTCompound;
|
||||
import de.tr7zw.changeme.nbtapi.NBTItem;
|
||||
import de.tr7zw.changeme.nbtapi.NBTType;
|
||||
import me.rockyhawk.commandpanels.CommandPanels;
|
||||
import me.rockyhawk.commandpanels.api.Panel;
|
||||
import me.rockyhawk.commandpanels.openpanelsmanager.PanelPosition;
|
||||
@ -10,170 +11,169 @@ import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class NBTManager {
|
||||
CommandPanels plugin;
|
||||
|
||||
public NBTManager(CommandPanels pl) {
|
||||
this.plugin = pl;
|
||||
}
|
||||
|
||||
public boolean hasSameNBT(ItemStack one, ItemStack two){
|
||||
NBTItem nbtitem1 = new NBTItem(one);
|
||||
NBTItem nbtitem2 = new NBTItem(two);
|
||||
|
||||
return nbtitem1.equals(nbtitem2);
|
||||
public boolean hasSameNBT(ItemStack one, ItemStack two) {
|
||||
return new NBTItem(one).equals(new NBTItem(two));
|
||||
}
|
||||
|
||||
public Object getNBT(ItemStack item, String key, String type) {
|
||||
type = type.toLowerCase();
|
||||
public boolean hasNBT(ItemStack item, String key) {
|
||||
return new NBTItem(item).hasTag(key);
|
||||
}
|
||||
|
||||
public ItemStack setNBT(ItemStack item, String key, Object value) {
|
||||
if (item == null || item.getType() == Material.AIR) return item;
|
||||
|
||||
NBTItem nbtItem = new NBTItem(item);
|
||||
switch(type.toLowerCase()) {
|
||||
case "byte":
|
||||
return nbtItem.getByte(key);
|
||||
case "boolean":
|
||||
return nbtItem.getBoolean(key);
|
||||
case "short":
|
||||
return nbtItem.getShort(key);
|
||||
case "integer":
|
||||
return nbtItem.getInteger(key);
|
||||
case "long":
|
||||
return nbtItem.getLong(key);
|
||||
case "float":
|
||||
return nbtItem.getFloat(key);
|
||||
case "double":
|
||||
return nbtItem.getDouble(key);
|
||||
case "string":
|
||||
return nbtItem.getString(key);
|
||||
default:
|
||||
throw new IllegalArgumentException("Unsupported NBT type: " + type);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasNBT(ItemStack item, String key){
|
||||
NBTItem nbti = new NBTItem(item);
|
||||
return nbti.hasTag(key);
|
||||
}
|
||||
|
||||
//nbt will be assigned with the value "string_value_1
|
||||
//which uses the type "String" and value "value_1"
|
||||
// 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);
|
||||
|
||||
setNBTValue(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) {
|
||||
if (item == null || item.getType() == Material.AIR) return;
|
||||
|
||||
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
|
||||
Object value = section.get(key);
|
||||
|
||||
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);
|
||||
setNBTValue(nbtItem, key, value);
|
||||
}
|
||||
}
|
||||
|
||||
item.setItemMeta(nbtItem.getItem().getItemMeta());
|
||||
}
|
||||
|
||||
// Convert ConfigurationSection to NBTCompound recursively
|
||||
public void saveMapToYAML(Map<String, Object> map, ConfigurationSection section) {
|
||||
if (map == null || section == null) return;
|
||||
|
||||
map.forEach((key, value) -> {
|
||||
if (value instanceof Map) {
|
||||
saveMapToYAML((Map<String, Object>) value, section.createSection(key));
|
||||
} else if (value instanceof Byte) {
|
||||
// Convert only Byte values that are actually Boolean representations
|
||||
byte byteValue = (Byte) value;
|
||||
if (byteValue == 1 || byteValue == 0) {
|
||||
section.set(key, byteValue == 1);
|
||||
} else {
|
||||
section.set(key, byteValue);
|
||||
}
|
||||
} else {
|
||||
section.set(key, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void convertSectionToNBT(NBTCompound compound, ConfigurationSection section, Player player, Panel panel, PanelPosition position) {
|
||||
for (String key : section.getKeys(false)) {
|
||||
Object value = section.get(key);
|
||||
|
||||
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);
|
||||
// Properly handles nested compounds instead of treating them as strings
|
||||
convertSectionToNBT(compound.addCompound(key), 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);
|
||||
setNBTValue(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);
|
||||
private void setNBTValue(NBTCompound compound, String key, Object value) {
|
||||
if (value instanceof Boolean) {
|
||||
compound.setByte(key, (Boolean) value ? (byte) 1 : (byte) 0); // Store as NBT Byte since boolean is shown as 1b or 0b
|
||||
} else if (value instanceof Integer) {
|
||||
compound.setInteger(key, (Integer) value);
|
||||
} else if (value instanceof Double) {
|
||||
compound.setDouble(key, (Double) value);
|
||||
} else if (value instanceof Long) {
|
||||
compound.setLong(key, (Long) value);
|
||||
} else if (value instanceof Short) {
|
||||
compound.setShort(key, (Short) value);
|
||||
} else if (value instanceof Float) {
|
||||
compound.setFloat(key, (Float) value);
|
||||
} else if (value instanceof Byte) {
|
||||
compound.setByte(key, (Byte) value);
|
||||
} else if (value instanceof Map) {
|
||||
saveMapToNBTCompound((Map<String, Object>) value, compound.addCompound(key));
|
||||
} else {
|
||||
compound.setString(key, value.toString());
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
public Object getNBTValue(ItemStack item, String key) {
|
||||
if (item == null || item.getType() == Material.AIR) return null;
|
||||
|
||||
String type = value.substring(0, underscoreIndex);
|
||||
String val = value.substring(underscoreIndex + 1);
|
||||
NBTItem nbtItem = new NBTItem(item);
|
||||
if (!nbtItem.hasTag(key)) return null;
|
||||
|
||||
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;
|
||||
return extractNBTValue(nbtItem, key, nbtItem.getType(key));
|
||||
}
|
||||
|
||||
private Object extractNBTValue(NBTCompound compound, String key, NBTType type) {
|
||||
switch (type) {
|
||||
case NBTTagInt:
|
||||
return compound.getInteger(key);
|
||||
case NBTTagDouble:
|
||||
return compound.getDouble(key);
|
||||
case NBTTagByte:
|
||||
byte byteValue = compound.getByte(key);
|
||||
if (byteValue == 1 || byteValue == 0) {
|
||||
return byteValue == 1; // Convert to Boolean since its displayed as 1b and 0b
|
||||
}
|
||||
return byteValue;
|
||||
case NBTTagFloat:
|
||||
return compound.getFloat(key);
|
||||
case NBTTagLong:
|
||||
return compound.getLong(key);
|
||||
case NBTTagShort:
|
||||
return compound.getShort(key);
|
||||
case NBTTagByteArray:
|
||||
return compound.getByteArray(key);
|
||||
case NBTTagIntArray:
|
||||
return compound.getIntArray(key);
|
||||
case NBTTagList:
|
||||
return compound.getStringList(key); // Assuming it's a String list
|
||||
case NBTTagCompound:
|
||||
return convertCompoundToMap(compound.getCompound(key)); // Recursively convert sub-compounds
|
||||
case NBTTagString:
|
||||
String str = compound.getString(key);
|
||||
if (str.equalsIgnoreCase("true")) return true;
|
||||
if (str.equalsIgnoreCase("false")) return false;
|
||||
if (str.matches("-?\\d+")) return Integer.parseInt(str);
|
||||
if (str.matches("-?\\d+\\.\\d+")) return Double.parseDouble(str);
|
||||
return str;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unsupported NBT type: " + type);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Map<String, Object> convertCompoundToMap(NBTCompound compound) {
|
||||
Map<String, Object> compoundMap = new LinkedHashMap<>();
|
||||
compound.getKeys().forEach(key -> compoundMap.put(key, extractNBTValue(compound, key, compound.getType(key))));
|
||||
return compoundMap;
|
||||
}
|
||||
|
||||
private void saveMapToNBTCompound(Map<String, Object> map, NBTCompound compound) {
|
||||
map.forEach((key, value) -> {
|
||||
if (value instanceof Map) {
|
||||
saveMapToNBTCompound((Map<String, Object>) value, compound.addCompound(key));
|
||||
} else {
|
||||
setNBTValue(compound, key, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package me.rockyhawk.commandpanels.openwithitem;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBTItem;
|
||||
import me.rockyhawk.commandpanels.CommandPanels;
|
||||
import me.rockyhawk.commandpanels.api.Panel;
|
||||
import me.rockyhawk.commandpanels.openpanelsmanager.PanelPosition;
|
||||
@ -36,7 +37,7 @@ public class HotbarItemLoader {
|
||||
if(stationaryItems.get(p.getUniqueId()).list.containsKey(String.valueOf(slot))){
|
||||
if(openPanel) {
|
||||
try {
|
||||
if (!String.valueOf(plugin.nbt.getNBT(p.getInventory().getItem(slot), "CommandPanelsHotbar", "string")).split(":")[1].equals(String.valueOf(slot))) {
|
||||
if (!String.valueOf(plugin.nbt.getNBTValue(p.getInventory().getItem(slot), "CommandPanelsHotbar")).split(":")[1].equals(String.valueOf(slot))) {
|
||||
return false;
|
||||
}
|
||||
}catch(Exception ex){
|
||||
@ -64,7 +65,7 @@ public class HotbarItemLoader {
|
||||
//return true if found
|
||||
public boolean itemCheckExecute(ItemStack invItem, Player p, boolean openPanel, boolean stationaryOnly){
|
||||
try {
|
||||
if (Objects.equals(String.valueOf(plugin.nbt.getNBT(invItem, "CommandPanelsHotbar", "string")), "")) {
|
||||
if (Objects.equals(String.valueOf(plugin.nbt.getNBTValue(invItem, "CommandPanelsHotbar")), "")) {
|
||||
return false;
|
||||
}
|
||||
}catch(IllegalArgumentException | NullPointerException nu){
|
||||
@ -73,13 +74,13 @@ public class HotbarItemLoader {
|
||||
for(Panel panel : plugin.panelList) {
|
||||
if(stationaryOnly){
|
||||
try {
|
||||
if (String.valueOf(plugin.nbt.getNBT(invItem, "CommandPanelsHotbar", "string")).split(":")[1].equals("-1")) {
|
||||
if (String.valueOf(plugin.nbt.getNBTValue(invItem, "CommandPanelsHotbar")).split(":")[1].equals("-1")) {
|
||||
continue;
|
||||
}
|
||||
}catch(NullPointerException | IllegalArgumentException ignore){}
|
||||
}
|
||||
if(panel.hasHotbarItem()){
|
||||
if(String.valueOf(plugin.nbt.getNBT(invItem, "CommandPanelsHotbar", "string")).split(":")[0].equals(panel.getName())){
|
||||
if(String.valueOf(plugin.nbt.getNBTValue(invItem, "CommandPanelsHotbar")).split(":")[0].equals(panel.getName())){
|
||||
if(openPanel) {
|
||||
//only open panel automatically if there are no commands and if world is not disabled
|
||||
if(!plugin.panelPerms.isPanelWorldEnabled(p,panel.getConfig())){
|
||||
@ -120,9 +121,9 @@ public class HotbarItemLoader {
|
||||
stationaryItems.put(p.getUniqueId(),new HotbarPlayerManager());
|
||||
for(int i = 0; i <= 35; i++){
|
||||
try {
|
||||
if (!Objects.equals(String.valueOf(plugin.nbt.getNBT(p.getInventory().getItem(i), "CommandPanelsHotbar", "string")), "")) {
|
||||
if (!Objects.equals(String.valueOf(plugin.nbt.getNBTValue(p.getInventory().getItem(i), "CommandPanelsHotbar")), "")) {
|
||||
//do not remove items that are not stationary
|
||||
if(!String.valueOf(plugin.nbt.getNBT(p.getInventory().getItem(i), "CommandPanelsHotbar","string")).endsWith("-1")) {
|
||||
if(!String.valueOf(plugin.nbt.getNBTValue(p.getInventory().getItem(i), "CommandPanelsHotbar")).endsWith("-1")) {
|
||||
p.getInventory().setItem(i, new ItemStack(Material.AIR));
|
||||
}
|
||||
}
|
||||
|
||||
@ -110,9 +110,9 @@ public class UtilsOpenWithItem implements Listener {
|
||||
try {
|
||||
for (ItemStack s : new ArrayList<>(e.getDrops())) {
|
||||
try {
|
||||
if (!String.valueOf(plugin.nbt.getNBT(s, "CommandPanelsHotbar", "string")).isEmpty()) {
|
||||
if (!String.valueOf(plugin.nbt.getNBTValue(s, "CommandPanelsHotbar")).isEmpty()) {
|
||||
//do not remove items that are not stationary
|
||||
if (!String.valueOf(plugin.nbt.getNBT(s, "CommandPanelsHotbar", "string")).endsWith("-1")) {
|
||||
if (!String.valueOf(plugin.nbt.getNBTValue(s, "CommandPanelsHotbar")).endsWith("-1")) {
|
||||
e.getDrops().remove(s);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user