From da65805fc279bce44ce0c2fc3e6ea58270fb6f1b Mon Sep 17 00:00:00 2001 From: mcm Date: Tue, 16 Aug 2022 22:58:42 -0400 Subject: [PATCH] Rearrange chestlink to hopper logic so that other plugins with customized hoppers work --- .../chests/filters/HopperFilter.java | 4 ++ .../listeners/HopperFilterListener.java | 7 ++- .../listeners/LinkedChestHopperListener.java | 2 +- .../minecraft/chests/misc/Utils.java | 45 +++++++++---------- .../runnables/VirtualChestToHopper.java | 5 ++- 5 files changed, 33 insertions(+), 30 deletions(-) diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/filters/HopperFilter.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/filters/HopperFilter.java index 99dad90..026cfe4 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/filters/HopperFilter.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/filters/HopperFilter.java @@ -38,6 +38,10 @@ public class HopperFilter { return filters; } + public static boolean hasFilters(Block block) { + return getHopperFilters(block).size() > 0; + } + public static boolean isInFilter(Block block, ItemStack itemStack) { return isInFilter(getHopperFilters(block), itemStack); } diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/HopperFilterListener.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/HopperFilterListener.java index 246fcd9..fd434a6 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/HopperFilterListener.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/HopperFilterListener.java @@ -36,6 +36,8 @@ public class HopperFilterListener implements Listener { public void onHopperMoveEvent(InventoryMoveItemEvent event) { //TO HOPPER if(event.getDestination().getHolder() instanceof Hopper){ + if(!HopperFilter.hasFilters(event.getDestination().getLocation().getBlock())) return; + if(event.getDestination().getLocation() != null){ // If the event is cancelled by other plugin if(event.isCancelled()) return; @@ -49,7 +51,8 @@ public class HopperFilterListener implements Listener { // Item shouldn't be allowed if (event.isCancelled() && ServerType.getType() == ServerType.Type.PAPER) { - int index = event.getSource().first(event.getItem()); + int index = event.getSource().first(event.getItem().getType()); + int hopperAmount = SpigotConfig.getWorldSettings(event.getSource().getLocation()).getHopperAmount(); // Loop over the inventory until next item is found, if no item found return. @@ -76,7 +79,7 @@ public class HopperFilterListener implements Listener { @EventHandler(priority = EventPriority.HIGH) public void fromHopper(InventoryMoveItemEvent event){ //FROM HOPPER - if (event.getInitiator().getHolder() instanceof Hopper) { + if (event.getSource().getHolder() instanceof Hopper) { Location location = event.getDestination().getLocation(); ChestLinkStorageType storageType = Config.getChestLink(); if (storageType == null) return; diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/LinkedChestHopperListener.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/LinkedChestHopperListener.java index d00a7dc..1137de6 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/LinkedChestHopperListener.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/LinkedChestHopperListener.java @@ -19,7 +19,7 @@ public class LinkedChestHopperListener implements Listener { @EventHandler(priority = EventPriority.HIGH) public void fromHopper(InventoryMoveItemEvent event) { //FROM HOPPER - if (event.getInitiator().getHolder() instanceof Hopper) { + if (event.getSource().getHolder() instanceof Hopper) { Location location = event.getDestination().getLocation(); if (location == null) return; if (!Utils.isLocationChunkLoaded(location)) return; diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/misc/Utils.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/misc/Utils.java index f99e5a5..ca4d206 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/misc/Utils.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/misc/Utils.java @@ -21,6 +21,7 @@ import org.bukkit.block.BlockFace; import org.bukkit.block.Container; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryMoveItemEvent; import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -95,42 +96,36 @@ public class Utils { //player.closeInventory(); } - public static ItemStack removeStackFromInventory(Inventory inventory, int amount, List filters) { - ItemStack toRemove; - for (int i = 0; i < inventory.getContents().length; i++) { - ItemStack stack = inventory.getItem(i); - if ((stack != null) && (HopperFilter.isInFilter(filters, stack))) { - toRemove = stack.clone(); - toRemove.setAmount(Math.min(stack.getAmount(), amount)); - stack.setAmount(stack.getAmount() - toRemove.getAmount()); - return toRemove; + public static boolean hopperMove(Inventory from, int amount, Inventory to) { + // Search for an item stack to remove + for (int i = 0; i < from.getContents().length; i++) { + ItemStack stack = from.getItem(i); + if (stack != null) { + // if (hopperMove(from, stack, amount, to)) return true; + hopperMove(from, stack, amount, to); + return true; } } - return null; - } - - public static boolean hopperMove(Inventory from, int amount, Inventory to, List filters) { - ItemStack removed = removeStackFromInventory(from, amount, filters); - if (removed != null) { - HashMap leftOvers = to.addItem(removed); - for (ItemStack leftOver : leftOvers.values()) { - from.addItem(leftOver); - if (removed.equals(leftOver)) return false; - } - return true; - } return false; } - public static boolean hopperMove(Inventory from, int amount, Inventory to) { - return hopperMove(from, amount, to, null); + public static boolean hopperMove(Inventory from, ItemStack stack, int amount, Inventory to) { + return hopperMove(from, stack, amount, to, true); } - public static boolean hopperMove(Inventory from, ItemStack stack, int amount, Inventory to) { + public static boolean hopperMove(Inventory from, ItemStack stack, int amount, Inventory to, boolean triggerEvent) { if (stack != null) { ItemStack toRemove = stack.clone(); toRemove.setAmount(Math.min(stack.getAmount(), amount)); stack.setAmount(stack.getAmount() - toRemove.getAmount()); + if(triggerEvent) { + InventoryMoveItemEvent event = new InventoryMoveItemEvent(from, toRemove.clone(), to, false); + Bukkit.getPluginManager().callEvent(event); + if(event.isCancelled()) { + stack.setAmount(stack.getAmount() + toRemove.getAmount()); + return false; + } + } HashMap leftOvers = to.addItem(toRemove); for (ItemStack leftOver : leftOvers.values()) { diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/runnables/VirtualChestToHopper.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/runnables/VirtualChestToHopper.java index 943ec0e..b624d1d 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/runnables/VirtualChestToHopper.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/runnables/VirtualChestToHopper.java @@ -1,7 +1,6 @@ package com.jamesdpeters.minecraft.chests.runnables; import com.jamesdpeters.minecraft.chests.ChestsPlusPlus; -import com.jamesdpeters.minecraft.chests.filters.HopperFilter; import com.jamesdpeters.minecraft.chests.misc.Utils; import com.jamesdpeters.minecraft.chests.serialize.LocationInfo; import com.jamesdpeters.minecraft.chests.serialize.SpigotConfig; @@ -36,6 +35,8 @@ public class VirtualChestToHopper extends BukkitRunnable { if (location.getLocation() != null) { if (!Utils.isLocationChunkLoaded(location.getLocation()) || !location.getLocation().getChunk().isEntitiesLoaded()) continue; + if(storage.getInventory().isEmpty()) continue; + Location below = location.getLocation().clone().subtract(0, 1, 0); if (below.getBlock().getState() instanceof Hopper hopper) { if (below.getBlock().isBlockIndirectlyPowered() || below.getBlock().isBlockPowered()) { @@ -53,6 +54,6 @@ public class VirtualChestToHopper extends BukkitRunnable { public static boolean move(Location targetLocation, Inventory source, Inventory target) { int hopperAmount = SpigotConfig.getWorldSettings(targetLocation.getWorld()).getHopperAmount(); - return Utils.hopperMove(source, hopperAmount, target, HopperFilter.getHopperFilters(targetLocation.getBlock())); + return Utils.hopperMove(source, hopperAmount, target); } }