From c00fb1dc1b78f971dde3717b67950f26e381578b Mon Sep 17 00:00:00 2001 From: DNx Date: Fri, 10 Apr 2020 08:05:17 +0700 Subject: [PATCH] simplify cargo withdraw methods. trying to reduce unnecessary item clone as much as possible. --- .../core/networks/cargo/CargoUtils.java | 57 ++++++++++--------- .../api/inventory/DirtyChestMenu.java | 3 +- 2 files changed, 31 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 bde955675..b47032a63 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 @@ -29,31 +29,32 @@ final class CargoUtils { public static ItemStack withdraw(Block node, Block target, ItemStack template) { DirtyChestMenu menu = getChestMenu(target); - - if (menu != null) { - for (int slot : menu.getPreset().getSlotsAccessedByItemTransport(menu, ItemTransportFlow.WITHDRAW, null)) { - ItemStack is = menu.getItemInSlot(slot); - - if (SlimefunUtils.isItemSimilar(is, template, true) && matchesFilter(node, is, -1)) { - if (is.getAmount() > template.getAmount()) { - menu.replaceExistingItem(slot, new CustomItem(is, is.getAmount() - template.getAmount())); - return template; - } - else { - menu.replaceExistingItem(slot, null); - return is.clone(); - } + if (menu == null) { + if (BlockUtils.hasInventory(target)) { + BlockState state = target.getState(); + if (state instanceof InventoryHolder) { + return withdrawFromVanillaInventory(node, template, ((InventoryHolder) state).getInventory()); } } - } - else if (BlockUtils.hasInventory(target)) { - BlockState state = target.getState(); + return null; + } - if (state instanceof InventoryHolder) { - return withdrawFromVanillaInventory(node, template, ((InventoryHolder) state).getInventory()); + for (int slot : menu.getPreset().getSlotsAccessedByItemTransport(menu, ItemTransportFlow.WITHDRAW, null)) { + ItemStack is = menu.getItemInSlot(slot); + + if (SlimefunUtils.isItemSimilar(is, template, true) && matchesFilter(node, is, -1)) { + if (is.getAmount() > template.getAmount()) { + is.setAmount(is.getAmount() - template.getAmount()); + menu.replaceExistingItem(slot, is); + return template; + } + else { + menu.replaceExistingItem(slot, null); + return is; + } } } - + return null; } @@ -75,12 +76,14 @@ final class CargoUtils { if (SlimefunUtils.isItemSimilar(is, template, true) && matchesFilter(node, is, -1)) { if (is.getAmount() > template.getAmount()) { - inv.setItem(slot, new CustomItem(is, is.getAmount() - template.getAmount())); + is.setAmount(is.getAmount() - template.getAmount()); + inv.setItem(slot, is); return template; } else { - inv.setItem(slot, new CustomItem(is, is.getAmount() - template.getAmount())); - return is.clone(); + is.setAmount(is.getAmount() - template.getAmount()); + inv.setItem(slot, is); + return is; } } } @@ -97,10 +100,10 @@ final class CargoUtils { if (matchesFilter(node, is, index)) { menu.replaceExistingItem(slot, null); - return new ItemStackAndInteger(is.clone(), slot); + return new ItemStackAndInteger(is, slot); } } - } + } else if (BlockUtils.hasInventory(target)) { BlockState state = target.getState(); @@ -114,7 +117,7 @@ final class CargoUtils { if (inv instanceof FurnaceInventory) { minSlot = 2; maxSlot = 3; - } + } else if (inv instanceof BrewerInventory) { maxSlot = 3; } @@ -124,7 +127,7 @@ final class CargoUtils { if (matchesFilter(node, is, index)) { inv.setItem(slot, null); - return new ItemStackAndInteger(is.clone(), slot); + return new ItemStackAndInteger(is, slot); } } } diff --git a/src/main/java/me/mrCookieSlime/Slimefun/api/inventory/DirtyChestMenu.java b/src/main/java/me/mrCookieSlime/Slimefun/api/inventory/DirtyChestMenu.java index 261c93119..67db329a0 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/api/inventory/DirtyChestMenu.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/api/inventory/DirtyChestMenu.java @@ -112,9 +112,8 @@ public class DirtyChestMenu extends ChestMenu { } public void replaceExistingItem(int slot, ItemStack item, boolean event) { - ItemStack previous = getItemInSlot(slot); - if (event && this.event != null) { + ItemStack previous = getItemInSlot(slot); item = this.event.onEvent(slot, previous, item); }