From 167326292df0fef5e088480b34778a0ba64e5847 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Thu, 20 Aug 2020 23:25:43 +0200 Subject: [PATCH] Maybe fixes #1951 (+ performance improvements) --- CHANGELOG.md | 2 + .../electric/machines/ElectricSmeltery.java | 19 ++++-- .../abstractItems/AContainer.java | 58 ++++++++++++------- 3 files changed, 52 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad9af9284..46d21eec3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ * Small performance improvements for Slimefun guides * Small performance improvements for Cargo networks * Small performance improvements for Miner Androids +* Small performance improvements for all machines, especially Electric Smelteries #### Fixes * Fixed Programmable Androids rotating in the wrong direction @@ -70,6 +71,7 @@ * Fixed ChestTerminal timings showing up as cargo nodes * Fixed timings reports never arriving sometimes * Fixed #2138 +* Fixed #1951 (again) ## Release Candidate 15 (01 Aug 2020) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/ElectricSmeltery.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/ElectricSmeltery.java index df30e46ca..f1a5c7e04 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/ElectricSmeltery.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/ElectricSmeltery.java @@ -14,6 +14,7 @@ import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; +import io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks.Smeltery; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils; import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu.AdvancedMenuClickHandler; @@ -28,6 +29,12 @@ import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset; import me.mrCookieSlime.Slimefun.api.inventory.DirtyChestMenu; import me.mrCookieSlime.Slimefun.api.item_transport.ItemTransportFlow; +/** + * The {@link ElectricSmeltery} is an electric version of the standard {@link Smeltery}. + * + * @author TheBusyBiscuit + * + */ public abstract class ElectricSmeltery extends AContainer { private static final int[] border = { 4, 5, 6, 7, 8, 13, 31, 40, 41, 42, 43, 44 }; @@ -65,8 +72,8 @@ public abstract class ElectricSmeltery extends AContainer { for (int slot : getInputSlots()) { ItemStack stack = menu.getItemInSlot(slot); - if (stack != null && SlimefunUtils.isItemSimilar(stack, item, true)) { - if (stack.getAmount() < stack.getMaxStackSize()) { + if (stack != null && SlimefunUtils.isItemSimilar(stack, item, true, false)) { + if (stack.getAmount() >= stack.getMaxStackSize()) { fullSlots++; } @@ -117,18 +124,18 @@ public abstract class ElectricSmeltery extends AContainer { @Override protected void constructMenu(BlockMenuPreset preset) { for (int i : border) { - preset.addItem(i, new CustomItem(new ItemStack(Material.GRAY_STAINED_GLASS_PANE), " "), ChestMenuUtils.getEmptyClickHandler()); + preset.addItem(i, new CustomItem(Material.GRAY_STAINED_GLASS_PANE, " "), ChestMenuUtils.getEmptyClickHandler()); } for (int i : inputBorder) { - preset.addItem(i, new CustomItem(new ItemStack(Material.CYAN_STAINED_GLASS_PANE), " "), ChestMenuUtils.getEmptyClickHandler()); + preset.addItem(i, new CustomItem(Material.CYAN_STAINED_GLASS_PANE, " "), ChestMenuUtils.getEmptyClickHandler()); } for (int i : outputBorder) { - preset.addItem(i, new CustomItem(new ItemStack(Material.ORANGE_STAINED_GLASS_PANE), " "), ChestMenuUtils.getEmptyClickHandler()); + preset.addItem(i, new CustomItem(Material.ORANGE_STAINED_GLASS_PANE, " "), ChestMenuUtils.getEmptyClickHandler()); } - preset.addItem(22, new CustomItem(new ItemStack(Material.BLACK_STAINED_GLASS_PANE), " "), ChestMenuUtils.getEmptyClickHandler()); + preset.addItem(22, new CustomItem(Material.BLACK_STAINED_GLASS_PANE, " "), ChestMenuUtils.getEmptyClickHandler()); for (int i : getOutputSlots()) { preset.addMenuClickHandler(i, new AdvancedMenuClickHandler() { diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AContainer.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AContainer.java index 7d135c987..6912af6b5 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AContainer.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AContainer.java @@ -18,6 +18,7 @@ import io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent; import io.github.thebusybiscuit.slimefun4.core.networks.energy.EnergyNetComponentType; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils; +import io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper; import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config; import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu.AdvancedMenuClickHandler; import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ClickAction; @@ -257,39 +258,54 @@ public abstract class AContainer extends SlimefunItem implements InventoryBlock, } } else { - MachineRecipe r = null; - Map found = new HashMap<>(); + MachineRecipe next = nextRecipe(inv); - for (MachineRecipe recipe : recipes) { - for (ItemStack input : recipe.getInput()) { - for (int slot : getInputSlots()) { - if (SlimefunUtils.isItemSimilar(inv.getItemInSlot(slot), input, true)) { - found.put(slot, input.getAmount()); - break; - } + if (next != null) { + processing.put(b, next); + progress.put(b, next.getTicks()); + } + } + } + + private MachineRecipe nextRecipe(BlockMenu inv) { + Map inventory = new HashMap<>(); + + for (int slot : getInputSlots()) { + ItemStack item = inv.getItemInSlot(slot); + + if (item != null) { + inventory.put(slot, new ItemStackWrapper(item)); + } + } + + Map found = new HashMap<>(); + + for (MachineRecipe recipe : recipes) { + for (ItemStack input : recipe.getInput()) { + for (int slot : getInputSlots()) { + if (SlimefunUtils.isItemSimilar(inventory.get(slot), input, true)) { + found.put(slot, input.getAmount()); + break; } } - if (found.size() == recipe.getInput().length) { - r = recipe; - break; - } - else { - found.clear(); - } } - if (r != null) { - if (!InvUtils.fitAll(inv.toInventory(), r.getOutput(), getOutputSlots())) { - return; + if (found.size() == recipe.getInput().length) { + if (!InvUtils.fitAll(inv.toInventory(), recipe.getOutput(), getOutputSlots())) { + return null; } for (Map.Entry entry : found.entrySet()) { inv.consumeItem(entry.getKey(), entry.getValue()); } - processing.put(b, r); - progress.put(b, r.getTicks()); + return recipe; + } + else { + found.clear(); } } + + return null; } }