From 9eb3ee2d53023de10da78932b942c2430c0eb73d Mon Sep 17 00:00:00 2001 From: DNx Date: Fri, 10 Apr 2020 03:12:30 +0700 Subject: [PATCH] Simplify and optimize the Cargo insertion --- .../core/networks/cargo/CargoUtils.java | 59 ++++++++++--------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoUtils.java index 1ad38324f..e97a9b04c 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoUtils.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoUtils.java @@ -137,40 +137,41 @@ final class CargoUtils { if (!matchesFilter(node, stack, index)) return stack; DirtyChestMenu menu = getChestMenu(target); - - if (menu != null) { - for (int slot : menu.getPreset().getSlotsAccessedByItemTransport(menu, ItemTransportFlow.INSERT, stack)) { - ItemStack is = menu.getItemInSlot(slot) == null ? null : menu.getItemInSlot(slot).clone(); - - if (is == null) { - menu.replaceExistingItem(slot, stack.clone()); - return null; - } - else if (SlimefunUtils.isItemSimilar(new CustomItem(is, 1), new CustomItem(stack, 1), true) && is.getAmount() < is.getType().getMaxStackSize()) { - int amount = is.getAmount() + stack.getAmount(); - - if (amount > is.getType().getMaxStackSize()) { - is.setAmount(is.getType().getMaxStackSize()); - stack.setAmount(amount - is.getType().getMaxStackSize()); - } - else { - is.setAmount(amount); - stack = null; - } - - menu.replaceExistingItem(slot, is); - return stack; + if (menu == null) { + if (BlockUtils.hasInventory(target)) { + BlockState state = target.getState(); + if (state instanceof InventoryHolder) { + return insertIntoVanillaInventory(stack, ((InventoryHolder) state).getInventory()); } } - } - else if (BlockUtils.hasInventory(target)) { - BlockState state = target.getState(); + return stack; + } - if (state instanceof InventoryHolder) { - return insertIntoVanillaInventory(stack, ((InventoryHolder) state).getInventory()); + for (int slot : menu.getPreset().getSlotsAccessedByItemTransport(menu, ItemTransportFlow.INSERT, stack)) { + ItemStack itemInSlot = menu.getItemInSlot(slot); + if (itemInSlot == null) { + menu.replaceExistingItem(slot, stack.clone()); + return null; + } + + itemInSlot = itemInSlot.clone(); + int inSlotMaxSize = itemInSlot.getType().getMaxStackSize(); + int inSlotAmount = itemInSlot.getAmount(); + if (SlimefunUtils.isItemSimilar(itemInSlot, stack, true, false) && inSlotAmount < inSlotMaxSize) { + int amount = inSlotAmount + stack.getAmount(); + + itemInSlot.setAmount(Math.max(amount, inSlotMaxSize)); + if (amount > inSlotMaxSize) { + stack.setAmount(amount - inSlotMaxSize); + } + else { + stack = null; + } + + menu.replaceExistingItem(slot, itemInSlot); + return stack; } } - return stack; }