From 00eef79dc9aeb74afd0e3a62cbef823f6d92c74e Mon Sep 17 00:00:00 2001 From: fullwall Date: Tue, 17 Sep 2024 02:19:31 +0800 Subject: [PATCH] Extra sanity checks in shop trait --- .../main/java/net/citizensnpcs/trait/ShopTrait.java | 10 +++++++--- .../java/net/citizensnpcs/trait/shop/ItemAction.java | 9 +++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java b/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java index 7458bdba1..dccf15c03 100644 --- a/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java @@ -1000,11 +1000,15 @@ public class ShopTrait extends Trait { @EventHandler public void onInventoryClick(InventoryClickEvent evt) { - if (!evt.getView().equals(view)) - return; - if (evt.getSlotType() != SlotType.RESULT) + if (!evt.getView().equals(view) || evt.getSlotType() != SlotType.RESULT) return; evt.setCancelled(true); + if (selectedTrade == -1) + return; + if (!trades.containsKey(selectedTrade)) { + Messaging.severe("Invalid trade selection", selectedTrade, "from", evt.getWhoClicked()); + return; + } if (!evt.getAction().name().contains("PICKUP") || (evt.getCursor() != null && evt.getCursor().getType() != Material.AIR)) return; diff --git a/main/src/main/java/net/citizensnpcs/trait/shop/ItemAction.java b/main/src/main/java/net/citizensnpcs/trait/shop/ItemAction.java index b6f2019ca..a6d9131d7 100644 --- a/main/src/main/java/net/citizensnpcs/trait/shop/ItemAction.java +++ b/main/src/main/java/net/citizensnpcs/trait/shop/ItemAction.java @@ -192,6 +192,14 @@ public class ItemAction extends NPCShopAction { return true; } + private void sanityCheck() { + if (metaFilter.size() > 0) { + for (ItemStack item : items) { + metaMatches(item, item, metaFilter); + } + } + } + private String stringify(ItemStack item) { if (SUPPORT_TRANSLATABLE) { return BukkitComponentSerializer.legacy().serialize(Component.text(item.getAmount() + " ") @@ -311,6 +319,7 @@ public class ItemAction extends NPCShopAction { } } base.items = items; + base.sanityCheck(); callback.accept(items.isEmpty() ? null : base); } }