diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/machines/electric/AutomatedCraftingChamber.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/machines/electric/AutomatedCraftingChamber.java index c968a31f3..3b6fb6078 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/machines/electric/AutomatedCraftingChamber.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/machines/electric/AutomatedCraftingChamber.java @@ -68,6 +68,12 @@ public abstract class AutomatedCraftingChamber extends SlimefunItem implements I return false; }); } + + menu.replaceExistingItem(7, new CustomItem(new ItemStack(Material.CRAFTING_TABLE), "&7Craft Last", "", "&e> Click to craft the last shaped recipe", "&cOnly works with the last one")); + menu.addMenuClickHandler(7, (p, slot, item, action) -> { + tick(b, true); + return false; + }); } @Override @@ -183,7 +189,7 @@ public abstract class AutomatedCraftingChamber extends SlimefunItem implements I @Override public void tick(Block b, SlimefunItem sf, Config data) { - AutomatedCraftingChamber.this.tick(b); + AutomatedCraftingChamber.this.tick(b, false); } @Override @@ -192,40 +198,59 @@ public abstract class AutomatedCraftingChamber extends SlimefunItem implements I } }); } - - protected void tick(Block b) { - if (BlockStorage.getLocationInfo(b.getLocation(), "enabled").equals("false")) return; - if (ChargableBlock.getCharge(b) < getEnergyConsumption()) return; - - BlockMenu menu = BlockStorage.getInventory(b); - + + protected void tick(Block block, boolean craftLast) { + if (!craftLast && BlockStorage.getLocationInfo(block.getLocation(), "enabled").equals("false")) return; + if (ChargableBlock.getCharge(block) < getEnergyConsumption()) return; + + String input = getSerializedInput(block, craftLast); + testInputAgainstRecipes(block, input); + } + + private String getSerializedInput(Block block, boolean craftLast) { + BlockMenu menu = BlockStorage.getInventory(block); StringBuilder builder = new StringBuilder(); int i = 0; + boolean lastIteration = false; for (int j = 0; j < 9; j++) { if (i > 0) { builder.append(" "); } - + ItemStack item = menu.getItemInSlot(getInputSlots()[j]); - if (item != null && item.getAmount() == 1) return; + if (item != null && item.getAmount() == 1) { + if (craftLast) + lastIteration = true; + else + return ""; + } + builder.append(CustomItemSerializer.serialize(item, ItemFlag.MATERIAL, ItemFlag.ITEMMETA_DISPLAY_NAME, ItemFlag.ITEMMETA_LORE)); - + i++; } - - String input = builder.toString(); - + + // we're only executing the last possible shaped recipe + // we don't want to allow this to be pressed instead of the default timer-based + // execution to prevent abuse and auto clickers + if (craftLast && !lastIteration) return ""; + + return builder.toString(); + } + + private void testInputAgainstRecipes(Block block, String input) { + BlockMenu menu = BlockStorage.getInventory(block); + if (SlimefunPlugin.getUtilities().automatedCraftingChamberRecipes.containsKey(input)) { ItemStack output = SlimefunPlugin.getUtilities().automatedCraftingChamberRecipes.get(input).clone(); - + if (menu.fits(output, getOutputSlots())) { menu.pushItem(output, getOutputSlots()); - ChargableBlock.addCharge(b, -getEnergyConsumption()); + ChargableBlock.addCharge(block, -getEnergyConsumption()); for (int j = 0; j < 9; j++) { if (menu.getItemInSlot(getInputSlots()[j]) != null) menu.replaceExistingItem(getInputSlots()[j], InvUtils.decreaseItem(menu.getItemInSlot(getInputSlots()[j]), 1)); } } } } - }