From be57a246482d937a099ba37c3dbe501c6d9e3b5a Mon Sep 17 00:00:00 2001 From: Mrec <66518248+RecoTG@users.noreply.github.com> Date: Sun, 17 Aug 2025 00:32:29 +0100 Subject: [PATCH] Add ItemUtil helper for meta checks --- .../com/yourorg/servershop/gui/SellMenu.java | 13 ++++++++++++- .../com/yourorg/servershop/shop/Catalog.java | 5 ++++- .../com/yourorg/servershop/shop/ItemEntry.java | 6 ++++-- .../yourorg/servershop/shop/ShopService.java | 6 ++++-- .../com/yourorg/servershop/util/ItemUtil.java | 18 ++++++++++++++++++ 5 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/yourorg/servershop/util/ItemUtil.java diff --git a/src/main/java/com/yourorg/servershop/gui/SellMenu.java b/src/main/java/com/yourorg/servershop/gui/SellMenu.java index 1cceda8..80e9459 100644 --- a/src/main/java/com/yourorg/servershop/gui/SellMenu.java +++ b/src/main/java/com/yourorg/servershop/gui/SellMenu.java @@ -1,6 +1,7 @@ package com.yourorg.servershop.gui; import com.yourorg.servershop.ServerShopPlugin; +import com.yourorg.servershop.util.ItemUtil; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -39,6 +40,7 @@ public void refresh(Player p) { for (ItemStack s : p.getInventory().getContents()) { if (s == null) continue; var m = s.getType(); var e = plugin.catalog().get(m).orElse(null); if (e == null || !e.canSell()) continue; + if (!e.allowMeta() && ItemUtil.hasCustomMeta(s)) continue; map.put(m, map.getOrDefault(m, 0) + s.getAmount()); } int i = 10; @@ -57,6 +59,7 @@ private void sellAll(Player p) { ItemStack s = p.getInventory().getItem(i); if (s == null) continue; var m = s.getType(); var e = plugin.catalog().get(m).orElse(null); if (e == null || !e.canSell()) continue; + if (!e.allowMeta() && ItemUtil.hasCustomMeta(s)) continue; int qty = s.getAmount(); double unit = plugin.shop().priceSell(m); double amount = unit * qty; @@ -70,5 +73,13 @@ private void sellAll(Player p) { refresh(p); } - private static int countOf(Player p, Material m){ int c=0; for (ItemStack s: p.getInventory().getContents()) if (s!=null && s.getType()==m) c+=s.getAmount(); return c; } + private int countOf(Player p, Material m){ + int c = 0; + var e = plugin.catalog().get(m).orElse(null); + boolean allow = e != null && e.allowMeta(); + for (ItemStack s: p.getInventory().getContents()) { + if (s != null && s.getType() == m && (allow || !ItemUtil.hasCustomMeta(s))) c += s.getAmount(); + } + return c; + } } diff --git a/src/main/java/com/yourorg/servershop/shop/Catalog.java b/src/main/java/com/yourorg/servershop/shop/Catalog.java index dd17426..7bc4981 100644 --- a/src/main/java/com/yourorg/servershop/shop/Catalog.java +++ b/src/main/java/com/yourorg/servershop/shop/Catalog.java @@ -26,14 +26,17 @@ public void reload() { if (cats == null) return; for (String cat : cats.getKeys(false)) { ConfigurationSection sec = cats.getConfigurationSection(cat); + boolean catAllow = sec.getBoolean("allow-custom", false); List mats = new ArrayList<>(); for (String key : sec.getKeys(false)) { + if ("allow-custom".equalsIgnoreCase(key)) continue; Material m = Material.matchMaterial(key); if (m == null) continue; var isec = sec.getConfigurationSection(key); double buy = isec.getDouble("buy", 0.0); double sell = isec.getDouble("sell", 0.0); - items.put(m, new ItemEntry(m, buy, sell)); + boolean allow = isec.getBoolean("allow-custom", catAllow); + items.put(m, new ItemEntry(m, buy, sell, allow)); mats.add(m); catByMat.put(m, cat); } diff --git a/src/main/java/com/yourorg/servershop/shop/ItemEntry.java b/src/main/java/com/yourorg/servershop/shop/ItemEntry.java index 5aa456e..ba34f2c 100644 --- a/src/main/java/com/yourorg/servershop/shop/ItemEntry.java +++ b/src/main/java/com/yourorg/servershop/shop/ItemEntry.java @@ -6,13 +6,15 @@ public final class ItemEntry { private final Material material; private final double buyPrice; private final double sellPrice; + private final boolean allowMeta; - public ItemEntry(Material material, double buyPrice, double sellPrice) { - this.material = material; this.buyPrice = buyPrice; this.sellPrice = sellPrice; + public ItemEntry(Material material, double buyPrice, double sellPrice, boolean allowMeta) { + this.material = material; this.buyPrice = buyPrice; this.sellPrice = sellPrice; this.allowMeta = allowMeta; } public Material material() { return material; } public double buyPrice() { return buyPrice; } public double sellPrice() { return sellPrice; } public boolean canBuy() { return buyPrice > 0; } public boolean canSell() { return sellPrice > 0; } + public boolean allowMeta() { return allowMeta; } } diff --git a/src/main/java/com/yourorg/servershop/shop/ShopService.java b/src/main/java/com/yourorg/servershop/shop/ShopService.java index 95325a3..2c4da5a 100644 --- a/src/main/java/com/yourorg/servershop/shop/ShopService.java +++ b/src/main/java/com/yourorg/servershop/shop/ShopService.java @@ -2,6 +2,7 @@ import com.yourorg.servershop.ServerShopPlugin; import com.yourorg.servershop.logging.Transaction; +import com.yourorg.servershop.util.ItemUtil; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -42,7 +43,7 @@ public Optional sell(Player p, Material mat, int qty) { if (opt.isEmpty() || !opt.get().canSell()) return Optional.of(msg("not-sellable").replace("%material%", mat.name())); String cat = plugin.catalog().categoryOf(mat); if (!plugin.categorySettings().isEnabled(cat)) return Optional.of("Category disabled: "+cat); - int removed = removeFromInventory(p, mat, qty); + int removed = removeFromInventory(p, mat, qty, opt.get().allowMeta()); if (removed <= 0) return Optional.of("You don't have that."); double unit = plugin.dynamic().sellPrice(mat, opt.get().sellPrice()); double total = unit * removed; @@ -63,11 +64,12 @@ public double priceSell(Material mat) { return plugin.dynamic().sellPrice(mat, e.sellPrice()); } - private int removeFromInventory(Player p, Material mat, int qty) { + private int removeFromInventory(Player p, Material mat, int qty, boolean allowMeta) { int remaining = qty; for (int i = 0; i < p.getInventory().getSize(); i++) { var stack = p.getInventory().getItem(i); if (stack == null || stack.getType() != mat) continue; + if (!allowMeta && ItemUtil.hasCustomMeta(stack)) continue; int take = Math.min(stack.getAmount(), remaining); stack.setAmount(stack.getAmount() - take); if (stack.getAmount() <= 0) p.getInventory().setItem(i, null); diff --git a/src/main/java/com/yourorg/servershop/util/ItemUtil.java b/src/main/java/com/yourorg/servershop/util/ItemUtil.java new file mode 100644 index 0000000..91f6fa2 --- /dev/null +++ b/src/main/java/com/yourorg/servershop/util/ItemUtil.java @@ -0,0 +1,18 @@ +package com.yourorg.servershop.util; + +import org.bukkit.Bukkit; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +public final class ItemUtil { + private ItemUtil() {} + + public static boolean hasCustomMeta(ItemStack stack) { + if (stack == null) return false; + ItemMeta meta = stack.getItemMeta(); + if (meta == null) return false; + var factory = Bukkit.getItemFactory(); + ItemMeta def = factory.getItemMeta(stack.getType()); + return !factory.equals(meta, def); + } +}