This commit is contained in:
rockyhawk64 2024-05-24 13:15:12 +10:00
parent 724d5039c0
commit 44e4c5831e
9 changed files with 68 additions and 46 deletions

View File

@ -1,4 +1,4 @@
version: 3.21.2.3 version: 3.21.2.4
main: me.rockyhawk.commandpanels.CommandPanels main: me.rockyhawk.commandpanels.CommandPanels
name: CommandPanels name: CommandPanels
author: RockyHawk author: RockyHawk

View File

@ -119,7 +119,7 @@ public class ItemCreation {
//creates item from custom-items section of panel //creates item from custom-items section of panel
if(matraw.split("\\s")[0].equalsIgnoreCase("cpi=")){ if(matraw.split("\\s")[0].equalsIgnoreCase("cpi=")){
s = makeCustomItemFromConfig(panel,position,panel.getConfig().getConfigurationSection("custom-item." + matraw.split("\\s")[1]), p, true, true, true); s = makeCustomItemFromConfig(panel,position,panel.getConfig().getConfigurationSection("custom-item." + matraw.split("\\s")[1]), p, true, true, false);
normalCreation = false; normalCreation = false;
} }
@ -185,18 +185,18 @@ public class ItemCreation {
if(itemSection.getStringList("itemType").contains("noAttributes")){ if(itemSection.getStringList("itemType").contains("noAttributes")){
hideAttributes = true; hideAttributes = true;
} }
if(itemSection.getStringList("itemType").contains("noNBT")){
addNBT = false;
}
if(itemSection.getStringList("itemType").contains("placeable")){ if(itemSection.getStringList("itemType").contains("placeable")){
addNBT = false; addNBT = false;
} }
} }
if(addNBT && itemSection.contains("nbt")){ if(itemSection.contains("nbt")){
plugin.nbt.applyNBTRecursively("", itemSection, s, p, panel, position); plugin.nbt.applyNBTRecursively("", itemSection, s, p, panel, position);
} }
if(addNBT){
plugin.nbt.setNBT(s, "CommandPanelsItem", "true");
}
if (itemSection.contains("enchanted")) { if (itemSection.contains("enchanted")) {
try { try {
ItemMeta EnchantMeta; ItemMeta EnchantMeta;
@ -325,14 +325,6 @@ public class ItemCreation {
} }
} }
} }
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));
}
}
// 1.20 Trim Feature for Player Armor // 1.20 Trim Feature for Player Armor
if(plugin.legacy.MAJOR_VERSION.greaterThanOrEqualTo(MinecraftVersions.v1_20) && itemSection.contains("trim")){ if(plugin.legacy.MAJOR_VERSION.greaterThanOrEqualTo(MinecraftVersions.v1_20) && itemSection.contains("trim")){
// trim: <Material> <Pattern> // trim: <Material> <Pattern>

View File

@ -3,6 +3,7 @@ package me.rockyhawk.commandpanels.commandtags;
import me.rockyhawk.commandpanels.CommandPanels; import me.rockyhawk.commandpanels.CommandPanels;
import me.rockyhawk.commandpanels.api.Panel; import me.rockyhawk.commandpanels.api.Panel;
import me.rockyhawk.commandpanels.commandtags.paywalls.*; import me.rockyhawk.commandpanels.commandtags.paywalls.*;
import me.rockyhawk.commandpanels.commandtags.paywalls.itempaywall.ItemPaywall;
import me.rockyhawk.commandpanels.commandtags.tags.other.DataTags; import me.rockyhawk.commandpanels.commandtags.tags.other.DataTags;
import me.rockyhawk.commandpanels.commandtags.tags.other.PlaceholderTags; import me.rockyhawk.commandpanels.commandtags.tags.other.PlaceholderTags;
import me.rockyhawk.commandpanels.commandtags.tags.other.SpecialTags; import me.rockyhawk.commandpanels.commandtags.tags.other.SpecialTags;

View File

@ -0,0 +1,21 @@
package me.rockyhawk.commandpanels.commandtags.paywalls.itempaywall;
import org.bukkit.inventory.ItemStack;
public class InventoryOperationResult {
private final boolean success;
private final ItemStack[] inventory;
public InventoryOperationResult(boolean success, ItemStack[] inventory) {
this.success = success;
this.inventory = inventory;
}
public boolean isSuccess() {
return success;
}
public ItemStack[] getInventory() {
return inventory;
}
}

View File

@ -1,4 +1,4 @@
package me.rockyhawk.commandpanels.commandtags.paywalls; package me.rockyhawk.commandpanels.commandtags.paywalls.itempaywall;
import me.rockyhawk.commandpanels.CommandPanels; import me.rockyhawk.commandpanels.CommandPanels;
import me.rockyhawk.commandpanels.commandtags.PaywallEvent; import me.rockyhawk.commandpanels.commandtags.PaywallEvent;
@ -74,37 +74,34 @@ public class ItemPaywall implements Listener {
} }
public boolean removeItem(Player p, ItemStack itemToRemove, boolean ignoreNBT) { public boolean removeItem(Player p, ItemStack itemToRemove, boolean ignoreNBT) {
boolean result; InventoryOperationResult result;
if (plugin.inventorySaver.hasNormalInventory(p)) { if (plugin.inventorySaver.hasNormalInventory(p)) {
result = removeItemFromInventory(p.getInventory().getContents(), itemToRemove, ignoreNBT); result = removeItemFromInventory(p.getInventory().getContents(), itemToRemove, ignoreNBT);
p.getInventory().setContents(result.getInventory());
} else { } else {
// Load the saved inventory from config, manipulate it, and save it back
ItemStack[] savedInventory = plugin.inventorySaver.getNormalInventory(p); ItemStack[] savedInventory = plugin.inventorySaver.getNormalInventory(p);
result = removeItemFromInventory(savedInventory, itemToRemove, ignoreNBT); result = removeItemFromInventory(savedInventory, itemToRemove, ignoreNBT);
plugin.inventorySaver.inventoryConfig.set(p.getUniqueId().toString(), plugin.itemSerializer.itemStackArrayToBase64(savedInventory)); plugin.inventorySaver.inventoryConfig.set(p.getUniqueId().toString(), plugin.itemSerializer.itemStackArrayToBase64(result.getInventory()));
} }
return result; // Return true if the items were successfully removed, otherwise false return result.isSuccess(); // Return the success status of the inventory operation
} }
private boolean removeItemFromInventory(ItemStack[] inventory, ItemStack itemToRemove, boolean ignoreNBT) { private InventoryOperationResult removeItemFromInventory(ItemStack[] inventory, ItemStack itemToRemove, boolean ignoreNBT) {
int amountToRemove = itemToRemove.getAmount(); int amountToRemove = itemToRemove.getAmount();
int count = 0; // To count how many of the required items are present int count = 0;
// First pass: count the items to ensure there are enough
for (ItemStack item : inventory) { for (ItemStack item : inventory) {
if (item != null && plugin.itemCreate.isIdentical(item, itemToRemove, !ignoreNBT)) { if (item != null && plugin.itemCreate.isIdentical(item, itemToRemove, !ignoreNBT)) {
count += item.getAmount(); count += item.getAmount();
} }
} }
// If not enough items, return false and do not modify the inventory
if (count < amountToRemove) { if (count < amountToRemove) {
return false; return new InventoryOperationResult(false, inventory); // Not enough items, return with original inventory unchanged
} }
// Second pass: remove the items if there are enough
for (int i = 0; i < inventory.length; i++) { for (int i = 0; i < inventory.length; i++) {
ItemStack currentItem = inventory[i]; ItemStack currentItem = inventory[i];
if (currentItem != null && plugin.itemCreate.isIdentical(currentItem, itemToRemove, !ignoreNBT)) { if (currentItem != null && plugin.itemCreate.isIdentical(currentItem, itemToRemove, !ignoreNBT)) {
@ -112,18 +109,16 @@ public class ItemPaywall implements Listener {
currentItem.setAmount(currentItem.getAmount() - removeAmount); currentItem.setAmount(currentItem.getAmount() - removeAmount);
amountToRemove -= removeAmount; amountToRemove -= removeAmount;
// Remove the item stack if it becomes empty
if (currentItem.getAmount() == 0) { if (currentItem.getAmount() == 0) {
inventory[i] = null; inventory[i] = null;
} }
// If removed all needed, break out of the loop
if (amountToRemove == 0) { if (amountToRemove == 0) {
break; break;
} }
} }
} }
return true; // Return true as items were successfully removed return new InventoryOperationResult(true, inventory); // Return true and the modified inventory
} }
} }

View File

@ -16,47 +16,60 @@ public class DataTags implements Listener {
if(e.name.equalsIgnoreCase("set-data=")){ if(e.name.equalsIgnoreCase("set-data=")){
e.commandTagUsed(); e.commandTagUsed();
if(e.args.length == 3){ if(e.args.length == 3){
plugin.panelData.setUserData(plugin.panelData.getOffline(e.args[2]),e.args[0],plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[1]),true); plugin.panelData.setUserData(plugin.panelData.getOffline(plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[2])),
plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[0]),
plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[1]),true);
return; return;
} }
//this will overwrite data. set-data= [data point] [data value] [optional player] //this will overwrite data. set-data= [data point] [data value] [optional player]
plugin.panelData.setUserData(e.p.getUniqueId(),e.args[0],plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[1]),true); plugin.panelData.setUserData(e.p.getUniqueId(),
plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[0]),
plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[1]),true);
return; return;
} }
if(e.name.equalsIgnoreCase("add-data=")){ if(e.name.equalsIgnoreCase("add-data=")){
e.commandTagUsed(); e.commandTagUsed();
if(e.args.length == 3){ if(e.args.length == 3){
plugin.panelData.setUserData(plugin.panelData.getOffline(e.args[2]),e.args[0],plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[1]),false); plugin.panelData.setUserData(plugin.panelData.getOffline(plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[2])),
plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[0]),
plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[1]),false);
return; return;
} }
//this will not overwrite existing data. add-data= [data point] [data value] [optional player] //this will not overwrite existing data. add-data= [data point] [data value] [optional player]
plugin.panelData.setUserData(e.p.getUniqueId(),e.args[0],plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[1]),false); plugin.panelData.setUserData(e.p.getUniqueId(),
plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[0]),
plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[1]),false);
return; return;
} }
if(e.name.equalsIgnoreCase("math-data=")){ if(e.name.equalsIgnoreCase("math-data=")){
e.commandTagUsed(); e.commandTagUsed();
if(e.args.length == 3){ if(e.args.length == 3){
plugin.panelData.doDataMath(plugin.panelData.getOffline(e.args[2]),e.args[0],plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[1])); plugin.panelData.doDataMath(plugin.panelData.getOffline(plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[2])),
plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[0]),
plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[1]));
return; return;
} }
//only works if data is number, goes math-data= [data point] [operator:number] [optional player] eg, math-data= -1 OR /3 //only works if data is number, goes math-data= [data point] [operator:number] [optional player] eg, math-data= -1 OR /3
plugin.panelData.doDataMath(e.p.getUniqueId(),e.args[0],plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[1])); plugin.panelData.doDataMath(e.p.getUniqueId(),
plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[0]),
plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[1]));
return; return;
} }
if(e.name.equalsIgnoreCase("clear-data=")){ if(e.name.equalsIgnoreCase("clear-data=")){
e.commandTagUsed(); e.commandTagUsed();
//will clear all data for player clear-data= [playerName] //will clear all data for player clear-data= [playerName]
plugin.panelData.clearData(e.p.getUniqueId()); plugin.panelData.clearData(plugin.panelData.getOffline(plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[0])));
return; return;
} }
if(e.name.equalsIgnoreCase("del-data=")){ if(e.name.equalsIgnoreCase("del-data=")){
e.commandTagUsed(); e.commandTagUsed();
if(e.args.length == 3){ if(e.args.length == 2){
plugin.panelData.delUserData(plugin.panelData.getOffline(e.args[1]),e.args[0]); plugin.panelData.delUserData(plugin.panelData.getOffline(plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[1])),
plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[0]));
return; return;
} }
//this will remove data. del-data= [data point] [optional player] //this will remove data. del-data= [data point] [optional player]
plugin.panelData.delUserData(e.p.getUniqueId(),e.args[0]); plugin.panelData.delUserData(e.p.getUniqueId(),plugin.tex.placeholdersNoColour(e.panel,e.pos,e.p,e.args[0]));
} }
} }
} }

View File

@ -37,7 +37,7 @@ public class ItemTags implements Listener {
e.commandTagUsed(); e.commandTagUsed();
//if player uses setitem= [custom item] [slot] [position] it will change the item slot to something, used for placeable items //if player uses setitem= [custom item] [slot] [position] it will change the item slot to something, used for placeable items
//make a section in the panel called "custom-item" then whatever the title of the item is, put that here //make a section in the panel called "custom-item" then whatever the title of the item is, put that here
ItemStack s = plugin.itemCreate.makeItemFromConfig(null, e.pos,e.panel.getConfig().getConfigurationSection("custom-item." + e.args[0]), e.p, true, true, true); ItemStack s = plugin.itemCreate.makeItemFromConfig(null, e.pos,e.panel.getConfig().getConfigurationSection("custom-item." + e.args[0]), e.p, true, true, false);
PanelPosition position = PanelPosition.valueOf(e.args[2]); PanelPosition position = PanelPosition.valueOf(e.args[2]);
if(position == PanelPosition.Top) { if(position == PanelPosition.Top) {
e.p.getOpenInventory().getTopInventory().setItem(Integer.parseInt(e.args[1]), s); e.p.getOpenInventory().getTopInventory().setItem(Integer.parseInt(e.args[1]), s);

View File

@ -25,10 +25,12 @@ public class NBTManager {
return nbtitem1.equals(nbtitem2); return nbtitem1.equals(nbtitem2);
} }
public ItemStack setNBT(ItemStack item, String key, String value){ public ItemStack setNBT(ItemStack item, String key, String value) {
if (item != null) {
NBT.modify(item, nbt -> { NBT.modify(item, nbt -> {
nbt.setString(key, value); nbt.setString(key, value);
}); });
}
return item; return item;
} }

View File

@ -63,7 +63,6 @@ public class OpenGUI {
} }
} }
//will only add NBT if not an editor GUI
ItemStack s = plugin.itemCreate.makeItemFromConfig(panel,position,Objects.requireNonNull(pconfig.getConfigurationSection("item." + item + section)), p, true, true, true); ItemStack s = plugin.itemCreate.makeItemFromConfig(panel,position,Objects.requireNonNull(pconfig.getConfigurationSection("item." + item + section)), p, true, true, true);
//This is for CUSTOM ITEMS //This is for CUSTOM ITEMS
@ -135,7 +134,6 @@ public class OpenGUI {
empty = plugin.itemCreate.makeItemFromConfig(panel,position,pconfig.getConfigurationSection("custom-item." + pconfig.getString("empty")),p,true,true,true); empty = plugin.itemCreate.makeItemFromConfig(panel,position,pconfig.getConfigurationSection("custom-item." + pconfig.getString("empty")),p,true,true,true);
}else{ }else{
empty = new ItemStack(Objects.requireNonNull(Material.matchMaterial(pconfig.getString("empty").toUpperCase())), 1,id); empty = new ItemStack(Objects.requireNonNull(Material.matchMaterial(pconfig.getString("empty").toUpperCase())), 1,id);
empty = plugin.nbt.setNBT(empty, "CommandPanelsItem", "true");
ItemMeta renamedMeta = empty.getItemMeta(); ItemMeta renamedMeta = empty.getItemMeta();
assert renamedMeta != null; assert renamedMeta != null;
renamedMeta.setDisplayName(" "); renamedMeta.setDisplayName(" ");