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; + } }