From 4bf786e954d56de48ae4f6ac3a566ea1a8d51957 Mon Sep 17 00:00:00 2001 From: Roberto Santalla Date: Sat, 6 Jul 2019 20:13:17 +0200 Subject: [PATCH] Moved unrelated items to Util class --- .../roobre/chestorganizer/ChestOrganizer.java | 47 ++++--------------- .../java/es/roobre/chestorganizer/Util.java | 42 +++++++++++++++++ 2 files changed, 50 insertions(+), 39 deletions(-) create mode 100644 src/main/java/es/roobre/chestorganizer/Util.java diff --git a/src/main/java/es/roobre/chestorganizer/ChestOrganizer.java b/src/main/java/es/roobre/chestorganizer/ChestOrganizer.java index e5339c3..8815d47 100644 --- a/src/main/java/es/roobre/chestorganizer/ChestOrganizer.java +++ b/src/main/java/es/roobre/chestorganizer/ChestOrganizer.java @@ -1,16 +1,8 @@ package es.roobre.chestorganizer; -import java.util.Comparator; -import java.util.List; -import java.util.Set; -import java.util.logging.Logger; -import java.util.stream.Collectors; -import java.util.stream.Stream; - import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.BlockState; -import org.bukkit.block.Chest; import org.bukkit.block.Container; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -23,6 +15,11 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; +import java.util.Set; +import java.util.logging.Logger; +import java.util.stream.Collectors; +import java.util.stream.Stream; + public final class ChestOrganizer extends JavaPlugin implements Listener { private Logger log = getLogger(); @@ -123,7 +120,7 @@ private void organize(Container container, ItemStack items) { this, () -> { ItemStack notAdded = targetChest.getInventory().addItem(items.clone()).get(0); - int notRemoved = removeItems(container.getInventory(), items.getType(), items.getAmount() - (notAdded == null ? 0 : notAdded.getAmount())); + int notRemoved = Util.removeItems(container.getInventory(), items.getType(), items.getAmount() - (notAdded == null ? 0 : notAdded.getAmount())); log.info("Moved " + items.getAmount() + " " + items.getType() + " from " + container.getBlock().getLocation() + " to " + targetChest.getBlock().getLocation()); @@ -165,34 +162,6 @@ private Container findSuitable(Location chestLocation, Material material) { return bestCandidate; } - /** - * Removes the specified number of items matching a given type from an inventory. - * Stacks of items will be removed until the specified amount has been reached, or until there are no more items to remove. - * Smaller stacks of items are removed first, in an attempt to keep the inventory as tidy as possible. - * - * @param src Inventory to remove items from - * @param type Material to target - * @param amount Maximum amount of items to remove - * @return The number of items that could not be removed, if amount was higher than the total number of items in the inventory - */ - private static int removeItems(Inventory src, Material type, int amount) { - List stackList = src.all(type).values().stream() - .sorted(Comparator.comparingInt(ItemStack::getAmount)) - .collect(Collectors.toList()); - - for (ItemStack stack : stackList) { - int toRemove = Math.min(amount, stack.getAmount()); - stack.setAmount(stack.getAmount() - toRemove); - amount -= toRemove; - - if (amount == 0) { - break; // We are done removing - } - } - - return amount; - } - /** * Checks if the given location contains an unlocked chest with at least one instance of the given item * @@ -200,7 +169,7 @@ private static int removeItems(Inventory src, Material type, int amount) { * @param mat Item to look for * @return A suitable chest (as InventoryHolder), or null if it wasn't suitable */ - private static Container isSuitableReceiver(BlockState block, Material mat) { + private Container isSuitableReceiver(BlockState block, Material mat) { // TODO: add array of accepted types of containers instead (just like we check it for the source) if (block instanceof Container) { Container container = (Container) block; @@ -220,7 +189,7 @@ private static Container isSuitableReceiver(BlockState block, Material mat) { * @param holder The holder to check * @return Whether the chest is an organizer or not */ - private static boolean isOrganizer(InventoryHolder holder) { + private boolean isOrganizer(InventoryHolder holder) { // check if the holder is a container if (holder instanceof Container) { Container container = (Container) holder; diff --git a/src/main/java/es/roobre/chestorganizer/Util.java b/src/main/java/es/roobre/chestorganizer/Util.java new file mode 100644 index 0000000..964f3da --- /dev/null +++ b/src/main/java/es/roobre/chestorganizer/Util.java @@ -0,0 +1,42 @@ +package es.roobre.chestorganizer; + +import org.bukkit.Material; +import org.bukkit.block.BlockState; +import org.bukkit.block.Container; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; +import org.bukkit.inventory.ItemStack; + +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +public class Util { + /** + * Removes the specified number of items matching a given type from an inventory. + * Stacks of items will be removed until the specified amount has been reached, or until there are no more items to remove. + * Smaller stacks of items are removed first, in an attempt to keep the inventory as tidy as possible. + * + * @param src Inventory to remove items from + * @param type Material to target + * @param amount Maximum amount of items to remove + * @return The number of items that could not be removed, if amount was higher than the total number of items in the inventory + */ + protected static int removeItems(Inventory src, Material type, int amount) { + List stackList = src.all(type).values().stream() + .sorted(Comparator.comparingInt(ItemStack::getAmount)) + .collect(Collectors.toList()); + + for (ItemStack stack : stackList) { + int toRemove = Math.min(amount, stack.getAmount()); + stack.setAmount(stack.getAmount() - toRemove); + amount -= toRemove; + + if (amount == 0) { + break; // We are done removing + } + } + + return amount; + } +}