From 3820bba66c39a4bd577b661019bdf44fa0968c77 Mon Sep 17 00:00:00 2001 From: Johanmans10 Date: Tue, 30 Apr 2024 00:21:27 +0200 Subject: [PATCH 1/4] #579 Updated for the stock counter to be updated on any hopper input --- pom.xml | 4 +-- .../Acrobot/Breeze/Utils/InventoryUtil.java | 4 --- .../Listeners/Item/ItemMoveListener.java | 34 +++++++++++++++---- .../ChestShop/Signs/ChestShopSign.java | 29 +++++++++++----- .../com/Acrobot/ChestShop/Utils/ItemUtil.java | 8 +++++ 5 files changed, 57 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index 1b2926060..59d6bc508 100644 --- a/pom.xml +++ b/pom.xml @@ -374,8 +374,8 @@ maven-compiler-plugin 3.8.1 - 1.8 - 1.8 + 17 + 17 diff --git a/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java b/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java index 113e718bb..9ff57be41 100644 --- a/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java +++ b/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java @@ -41,10 +41,6 @@ public static int getAmount(ItemStack item, Inventory inventory) { return 0; } - if (inventory.getType() == null) { - return Integer.MAX_VALUE; - } - HashMap items = inventory.all(item.getType()); int itemAmount = 0; diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemMoveListener.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemMoveListener.java index 5e502613c..e1dc4188b 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemMoveListener.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemMoveListener.java @@ -1,11 +1,20 @@ package com.Acrobot.ChestShop.Listeners.Item; +import com.Acrobot.ChestShop.Listeners.Modules.StockCounterModule; import com.Acrobot.ChestShop.Signs.ChestShopSign; +import com.Acrobot.ChestShop.Utils.ItemUtil; +import com.Acrobot.ChestShop.Utils.uBlock; +import org.bukkit.Bukkit; +import org.bukkit.block.Block; import org.bukkit.block.BlockState; +import org.bukkit.block.Hopper; +import org.bukkit.block.Sign; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryMoveItemEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getHolder; @@ -16,14 +25,25 @@ public class ItemMoveListener implements Listener { @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) public static void onItemMove(InventoryMoveItemEvent event) { - if (event.getSource() == null || getHolder(event.getDestination(), false) instanceof BlockState) { - return; - } + InventoryHolder destinationHolder = getHolder(event.getDestination(), false); + InventoryHolder sourceHolder = getHolder(event.getSource(), false); - if (!ChestShopSign.isShopBlock(getHolder(event.getSource(), false))) { - return; - } + if (!(destinationHolder instanceof BlockState) && ChestShopSign.isShopBlock(sourceHolder)) { + event.setCancelled(true); + } else if (ChestShopSign.isShopBlock(destinationHolder) && sourceHolder instanceof Hopper) { + Block shopBlock = ChestShopSign.getShopBlock(destinationHolder); + Sign connectedSign = uBlock.getConnectedSign(shopBlock); + + Inventory tempInv = Bukkit.createInventory(null, destinationHolder.getInventory().getSize() + 9); + tempInv.setContents(ItemUtil.deepClone(destinationHolder.getInventory().getContents())); + tempInv.addItem(event.getItem().clone()); + + StockCounterModule.updateCounterOnQuantityLine(connectedSign, tempInv); - event.setCancelled(true); + tempInv.clear(); + tempInv.close(); + } } + + } diff --git a/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java b/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java index 31a24de14..1819cec2f 100644 --- a/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java +++ b/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java @@ -21,6 +21,7 @@ import org.bukkit.inventory.InventoryHolder; import java.util.Locale; +import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -109,25 +110,35 @@ public static boolean isShopChest(InventoryHolder holder) { return false; } - if (holder instanceof DoubleChest) { - return isShopChest(((DoubleChest) holder).getLocation().getBlock()); - } else if (holder instanceof Chest) { - return isShopChest(((Chest) holder).getBlock()); + if (holder instanceof DoubleChest dChest) { + return isShopChest(dChest.getLocation().getBlock()); + } else if (holder instanceof Chest chest) { + return isShopChest(chest.getBlock()); } else { return false; } } public static boolean isShopBlock(InventoryHolder holder) { - if (holder instanceof DoubleChest) { - return isShopBlock(((DoubleChest) holder).getLeftSide()) - || isShopBlock(((DoubleChest) holder).getRightSide()); - } else if (holder instanceof BlockState) { - return isShopBlock(((BlockState) holder).getBlock()); + if (holder instanceof DoubleChest dChest) { + return isShopBlock(dChest.getLeftSide()) + || isShopBlock(dChest.getRightSide()); + } else if (holder instanceof BlockState blockState) { + return isShopBlock(blockState.getBlock()); } return false; } + public static Block getShopBlock(InventoryHolder holder) { + if (holder instanceof DoubleChest dChest) { + return Optional.ofNullable(getShopBlock(dChest.getLeftSide())) + .orElse(getShopBlock(dChest.getRightSide())); + } else if (holder instanceof BlockState state) { + return state.getBlock(); + } + return null; + } + public static boolean canAccess(Player player, Sign sign) { return hasPermission(player, Permission.OTHER_NAME_ACCESS, sign); } diff --git a/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java b/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java index e13949003..31a1112b9 100644 --- a/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java +++ b/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java @@ -96,4 +96,12 @@ public static String getName(ItemStack itemStack, int maxWidth) { public static String getSignName(ItemStack itemStack) { return getName(itemStack, MAXIMUM_SIGN_WIDTH); } + + public static ItemStack[] deepClone(ItemStack[] toClone) { + ItemStack[] cloned = toClone.clone(); + for (int i = 0; i < toClone.length; i++) { + cloned[i] = toClone[i].clone(); + } + return cloned; + } } From a59c3c786d1e4fa8be961e3c960388abd8900410 Mon Sep 17 00:00:00 2001 From: Johanmans10 Date: Thu, 2 May 2024 22:57:52 +0200 Subject: [PATCH 2/4] #539 PR requested changes --- pom.xml | 4 +-- .../Acrobot/Breeze/Utils/InventoryUtil.java | 12 +++---- .../Listeners/Item/ItemMoveListener.java | 21 ++---------- .../Listeners/Modules/StockCounterModule.java | 16 +++++++++ .../ChestShop/Signs/ChestShopSign.java | 34 ++++++++----------- 5 files changed, 42 insertions(+), 45 deletions(-) diff --git a/pom.xml b/pom.xml index 59d6bc508..1b2926060 100644 --- a/pom.xml +++ b/pom.xml @@ -374,8 +374,8 @@ maven-compiler-plugin 3.8.1 - 17 - 17 + 1.8 + 1.8 diff --git a/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java b/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java index 9ff57be41..009e1fd78 100644 --- a/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java +++ b/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java @@ -1,15 +1,11 @@ package com.Acrobot.Breeze.Utils; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - import com.Acrobot.ChestShop.Configuration.Properties; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +import java.util.*; + /** * @author Acrobot */ @@ -41,6 +37,10 @@ public static int getAmount(ItemStack item, Inventory inventory) { return 0; } + if (inventory.getType() == null) { + return Integer.MAX_VALUE; + } + HashMap items = inventory.all(item.getType()); int itemAmount = 0; diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemMoveListener.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemMoveListener.java index e1dc4188b..7973e9d00 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemMoveListener.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemMoveListener.java @@ -1,19 +1,14 @@ package com.Acrobot.ChestShop.Listeners.Item; +import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.Listeners.Modules.StockCounterModule; import com.Acrobot.ChestShop.Signs.ChestShopSign; -import com.Acrobot.ChestShop.Utils.ItemUtil; -import com.Acrobot.ChestShop.Utils.uBlock; -import org.bukkit.Bukkit; -import org.bukkit.block.Block; import org.bukkit.block.BlockState; import org.bukkit.block.Hopper; -import org.bukkit.block.Sign; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryMoveItemEvent; -import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getHolder; @@ -30,18 +25,8 @@ public static void onItemMove(InventoryMoveItemEvent event) { if (!(destinationHolder instanceof BlockState) && ChestShopSign.isShopBlock(sourceHolder)) { event.setCancelled(true); - } else if (ChestShopSign.isShopBlock(destinationHolder) && sourceHolder instanceof Hopper) { - Block shopBlock = ChestShopSign.getShopBlock(destinationHolder); - Sign connectedSign = uBlock.getConnectedSign(shopBlock); - - Inventory tempInv = Bukkit.createInventory(null, destinationHolder.getInventory().getSize() + 9); - tempInv.setContents(ItemUtil.deepClone(destinationHolder.getInventory().getContents())); - tempInv.addItem(event.getItem().clone()); - - StockCounterModule.updateCounterOnQuantityLine(connectedSign, tempInv); - - tempInv.clear(); - tempInv.close(); + } else if (Properties.USE_STOCK_COUNTER && ChestShopSign.isShopBlock(destinationHolder) && sourceHolder instanceof Hopper) { + StockCounterModule.updateCounterOnItemMoveEvent(event.getItem(), destinationHolder); } } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/StockCounterModule.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/StockCounterModule.java index 62693c88c..608ba82d7 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/StockCounterModule.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/StockCounterModule.java @@ -8,8 +8,10 @@ import com.Acrobot.ChestShop.Events.PreShopCreationEvent; import com.Acrobot.ChestShop.Events.TransactionEvent; import com.Acrobot.ChestShop.Signs.ChestShopSign; +import com.Acrobot.ChestShop.Utils.ItemUtil; import com.Acrobot.ChestShop.Utils.uBlock; import org.bukkit.Bukkit; +import org.bukkit.block.Block; import org.bukkit.block.Container; import org.bukkit.block.Sign; import org.bukkit.event.EventHandler; @@ -143,6 +145,20 @@ public static void updateCounterOnQuantityLine(Sign sign, Inventory chestShopInv sign.update(true); } + public static void updateCounterOnItemMoveEvent(ItemStack toAdd, InventoryHolder destinationHolder) { + Block shopBlock = ChestShopSign.getShopBlock(destinationHolder); + Sign connectedSign = uBlock.getConnectedSign(shopBlock); + + Inventory tempInv = Bukkit.createInventory(null, destinationHolder.getInventory().getSize() + 9); + tempInv.setContents(ItemUtil.deepClone(destinationHolder.getInventory().getContents())); + tempInv.addItem(toAdd.clone()); + + updateCounterOnQuantityLine(connectedSign, tempInv); + + tempInv.clear(); + tempInv.close(); + } + public static void removeCounterFromQuantityLine(Sign sign) { int quantity; try { diff --git a/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java b/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java index 1819cec2f..dea3852ad 100644 --- a/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java +++ b/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java @@ -11,11 +11,7 @@ import com.Acrobot.ChestShop.UUIDs.NameManager; import com.Acrobot.ChestShop.Utils.uBlock; import org.bukkit.Bukkit; -import org.bukkit.block.Block; -import org.bukkit.block.BlockState; -import org.bukkit.block.Chest; -import org.bukkit.block.DoubleChest; -import org.bukkit.block.Sign; +import org.bukkit.block.*; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; @@ -110,31 +106,31 @@ public static boolean isShopChest(InventoryHolder holder) { return false; } - if (holder instanceof DoubleChest dChest) { - return isShopChest(dChest.getLocation().getBlock()); - } else if (holder instanceof Chest chest) { - return isShopChest(chest.getBlock()); + if (holder instanceof DoubleChest) { + return isShopChest(((DoubleChest) holder).getLocation().getBlock()); + } else if (holder instanceof Chest) { + return isShopChest(((Chest) holder).getBlock()); } else { return false; } } public static boolean isShopBlock(InventoryHolder holder) { - if (holder instanceof DoubleChest dChest) { - return isShopBlock(dChest.getLeftSide()) - || isShopBlock(dChest.getRightSide()); - } else if (holder instanceof BlockState blockState) { - return isShopBlock(blockState.getBlock()); + if (holder instanceof DoubleChest) { + return isShopBlock(((DoubleChest) holder).getLeftSide()) + || isShopBlock(((DoubleChest) holder).getRightSide()); + } else if (holder instanceof BlockState) { + return isShopBlock(((BlockState) holder).getBlock()); } return false; } public static Block getShopBlock(InventoryHolder holder) { - if (holder instanceof DoubleChest dChest) { - return Optional.ofNullable(getShopBlock(dChest.getLeftSide())) - .orElse(getShopBlock(dChest.getRightSide())); - } else if (holder instanceof BlockState state) { - return state.getBlock(); + if (holder instanceof DoubleChest) { + return Optional.ofNullable(getShopBlock(((DoubleChest) holder).getLeftSide())) + .orElse(getShopBlock(((DoubleChest) holder).getRightSide())); + } else if (holder instanceof BlockState) { + return ((BlockState) holder).getBlock(); } return null; } From 981912b3acf56a1ef41cdda81fade5065916a538 Mon Sep 17 00:00:00 2001 From: Johanmans10 Date: Sun, 5 May 2024 21:05:50 +0200 Subject: [PATCH 3/4] #539 PR requested changes (part 2) --- .../Listeners/Modules/StockCounterModule.java | 27 ++++++++++++------- .../com/Acrobot/ChestShop/Utils/ItemUtil.java | 8 ------ 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/StockCounterModule.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/StockCounterModule.java index 608ba82d7..1e3ca9d7a 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/StockCounterModule.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/StockCounterModule.java @@ -1,6 +1,7 @@ package com.Acrobot.ChestShop.Listeners.Modules; import com.Acrobot.Breeze.Utils.InventoryUtil; +import com.Acrobot.Breeze.Utils.MaterialUtil; import com.Acrobot.Breeze.Utils.QuantityUtil; import com.Acrobot.ChestShop.ChestShop; import com.Acrobot.ChestShop.Configuration.Properties; @@ -8,7 +9,6 @@ import com.Acrobot.ChestShop.Events.PreShopCreationEvent; import com.Acrobot.ChestShop.Events.TransactionEvent; import com.Acrobot.ChestShop.Signs.ChestShopSign; -import com.Acrobot.ChestShop.Utils.ItemUtil; import com.Acrobot.ChestShop.Utils.uBlock; import org.bukkit.Bukkit; import org.bukkit.block.Block; @@ -126,7 +126,13 @@ public static void onTransaction(final TransactionEvent event) { } } - public static void updateCounterOnQuantityLine(Sign sign, Inventory chestShopInventory) { + /** + * Update the stock counter on the sign's quantity line + * @param sign The sign to update + * @param chestShopInventory The inventory to search in + * @param extraItems The extra items to add in the search + */ + public static void updateCounterOnQuantityLine(Sign sign, Inventory chestShopInventory, ItemStack... extraItems) { ItemStack itemTradedByShop = determineItemTradedByShop(sign); if (itemTradedByShop == null) { return; @@ -141,6 +147,14 @@ public static void updateCounterOnQuantityLine(Sign sign, Inventory chestShopInv int numTradedItemsInChest = InventoryUtil.getAmount(itemTradedByShop, chestShopInventory); + for (ItemStack extraStack : extraItems) { + if (!MaterialUtil.equals(extraStack, itemTradedByShop)) { + continue; + } + + numTradedItemsInChest += extraStack.getAmount(); + } + sign.setLine(QUANTITY_LINE, String.format(PRICE_LINE_WITH_COUNT, quantity, numTradedItemsInChest)); sign.update(true); } @@ -149,14 +163,7 @@ public static void updateCounterOnItemMoveEvent(ItemStack toAdd, InventoryHolder Block shopBlock = ChestShopSign.getShopBlock(destinationHolder); Sign connectedSign = uBlock.getConnectedSign(shopBlock); - Inventory tempInv = Bukkit.createInventory(null, destinationHolder.getInventory().getSize() + 9); - tempInv.setContents(ItemUtil.deepClone(destinationHolder.getInventory().getContents())); - tempInv.addItem(toAdd.clone()); - - updateCounterOnQuantityLine(connectedSign, tempInv); - - tempInv.clear(); - tempInv.close(); + updateCounterOnQuantityLine(connectedSign, destinationHolder.getInventory(), toAdd); } public static void removeCounterFromQuantityLine(Sign sign) { diff --git a/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java b/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java index 31a1112b9..e13949003 100644 --- a/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java +++ b/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java @@ -96,12 +96,4 @@ public static String getName(ItemStack itemStack, int maxWidth) { public static String getSignName(ItemStack itemStack) { return getName(itemStack, MAXIMUM_SIGN_WIDTH); } - - public static ItemStack[] deepClone(ItemStack[] toClone) { - ItemStack[] cloned = toClone.clone(); - for (int i = 0; i < toClone.length; i++) { - cloned[i] = toClone[i].clone(); - } - return cloned; - } } From 208a6af6ec8628cc0be038781f16d9ce192dc364 Mon Sep 17 00:00:00 2001 From: Johanmans10 Date: Mon, 13 May 2024 17:12:37 +0200 Subject: [PATCH 4/4] #539 PR requested changes (part 3) --- src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java | 8 ++++++-- .../java/com/Acrobot/ChestShop/Signs/ChestShopSign.java | 6 +++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java b/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java index 009e1fd78..113e718bb 100644 --- a/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java +++ b/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java @@ -1,11 +1,15 @@ package com.Acrobot.Breeze.Utils; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + import com.Acrobot.ChestShop.Configuration.Properties; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; -import java.util.*; - /** * @author Acrobot */ diff --git a/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java b/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java index dea3852ad..eb8546c18 100644 --- a/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java +++ b/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java @@ -11,7 +11,11 @@ import com.Acrobot.ChestShop.UUIDs.NameManager; import com.Acrobot.ChestShop.Utils.uBlock; import org.bukkit.Bukkit; -import org.bukkit.block.*; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.block.Chest; +import org.bukkit.block.DoubleChest; +import org.bukkit.block.Sign; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder;