From 23210a549bb75bbedb6b493ccdd70287ad89bc0e Mon Sep 17 00:00:00 2001 From: RelativoBR Date: Sat, 16 Nov 2024 20:37:08 -0300 Subject: [PATCH] Fix input and output handling in GenericMachine --- .../machine/FlexItemContainerMachine.java | 40 -- ...tainerMachine.java => GenericMachine.java} | 384 +++++++++--------- .../machine/SimpleItemContainerMachine.java | 10 +- .../SimpleItemWithLargeContainerMachine.java | 10 +- .../supreme/machine/ElectricCrafter.java | 4 +- .../relativobr/supreme/machine/Foundry.java | 4 +- .../supreme/machine/MagicAltar.java | 4 +- .../supreme/machine/MobCollector.java | 23 +- .../supreme/machine/VirtualAquarium.java | 23 +- .../supreme/machine/VirtualGarden.java | 22 +- .../multiblock/ElectricCoreFabricator.java | 4 +- .../multiblock/ElectricGearFabricator.java | 4 +- .../multiblock/ElectricMagicalFabricator.java | 4 +- 13 files changed, 255 insertions(+), 281 deletions(-) delete mode 100644 src/main/java/com/github/relativobr/supreme/generic/machine/FlexItemContainerMachine.java rename src/main/java/com/github/relativobr/supreme/generic/machine/{MediumContainerMachine.java => GenericMachine.java} (56%) diff --git a/src/main/java/com/github/relativobr/supreme/generic/machine/FlexItemContainerMachine.java b/src/main/java/com/github/relativobr/supreme/generic/machine/FlexItemContainerMachine.java deleted file mode 100644 index 2758c32..0000000 --- a/src/main/java/com/github/relativobr/supreme/generic/machine/FlexItemContainerMachine.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.github.relativobr.supreme.generic.machine; - -import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; -import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; -import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; -import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack; -import java.util.ArrayList; -import java.util.List; -import javax.annotation.Nonnull; -import javax.annotation.ParametersAreNonnullByDefault; -import org.bukkit.Material; -import org.bukkit.inventory.ItemStack; - -/** - * Machine that uses flex item in the input (1~9) and only 1 item in the output - */ -public class FlexItemContainerMachine extends MediumContainerMachine { - - @ParametersAreNonnullByDefault - protected FlexItemContainerMachine(ItemGroup category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { - super(category, item, recipeType, recipe); - } - - @Nonnull - @Override - public List getDisplayRecipes() { - List displayRecipes = new ArrayList(); - machineRecipes.forEach(recipe -> { - displayRecipes.add(new CustomItemStack(Material.GRAY_STAINED_GLASS_PANE, " ")); - displayRecipes.add(recipe.getFirstItemOutput()); - }); - return displayRecipes; - } - - @Override - public int getSizeProcessInput(){ - return -1; - } - -} \ No newline at end of file diff --git a/src/main/java/com/github/relativobr/supreme/generic/machine/MediumContainerMachine.java b/src/main/java/com/github/relativobr/supreme/generic/machine/GenericMachine.java similarity index 56% rename from src/main/java/com/github/relativobr/supreme/generic/machine/MediumContainerMachine.java rename to src/main/java/com/github/relativobr/supreme/generic/machine/GenericMachine.java index a209bea..dc483b3 100644 --- a/src/main/java/com/github/relativobr/supreme/generic/machine/MediumContainerMachine.java +++ b/src/main/java/com/github/relativobr/supreme/generic/machine/GenericMachine.java @@ -39,24 +39,16 @@ import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.inventory.ItemStack; -/** - * Machine that can use up to 9 items in the input and only 1 item in the output - */ -public class MediumContainerMachine extends AContainer implements NotHopperable, RecipeDisplayItem { +public class GenericMachine extends AContainer implements NotHopperable, RecipeDisplayItem { private final Map processing = new HashMap(); - private final Map progressItem = new HashMap(); private final Map progressTime = new HashMap(); public List machineRecipes = new ArrayList<>(); private Integer timeProcess; private String machineIdentifier = "MediumContainerMachine"; - public int getSizeProcessInput(){ - return 9; - } - @ParametersAreNonnullByDefault - public MediumContainerMachine(ItemGroup category, SlimefunItemStack item, RecipeType recipeType, + public GenericMachine(ItemGroup category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(category, item, recipeType, recipe); @@ -135,49 +127,32 @@ public void onBlockBreak(Block b) { } progressTime.remove(b); processing.remove(b); - progressItem.remove(b); } }; } - private void invalidInput(BlockMenu menu) { - menu.replaceExistingItem(getStatusSlot(), new CustomItemStack( - Material.RED_STAINED_GLASS_PANE, - "&cInput a valid material to start" - )); - } - - private void invalidOutput(BlockMenu menu) { - menu.replaceExistingItem(getStatusSlot(), new CustomItemStack( - Material.RED_STAINED_GLASS_PANE, - "&cOutput is full" - )); + protected void updateStatusReset(BlockMenu menu) { + menu.replaceExistingItem(getStatusSlot(), getDisplayOrInfo(null, " ")); } - private void noEnergyStart(BlockMenu menu) { - menu.replaceExistingItem(getStatusSlot(), new CustomItemStack( - Material.RED_STAINED_GLASS_PANE, "&cConnect energy to start" - )); + protected void updateStatusInvalidInput(BlockMenu menu) { + menu.replaceExistingItem(getStatusSlot(),getDisplayOrWarn(null,"&cInput a valid material to start")); } - private void noEnergyContinue(BlockMenu menu, ItemStack material) { - menu.replaceExistingItem(getStatusSlot(), new CustomItemStack( - material, "&cConnect energy to continue" - )); + protected void updateStatusOutputFull(BlockMenu menu) { + menu.replaceExistingItem(getStatusSlot(), getDisplayOrWarn(null,"&cOutput is full")); } - private void noMaterialContinue(BlockMenu menu, ItemStack material) { - menu.replaceExistingItem(getStatusSlot(), new CustomItemStack( - material, "&cDeposit more materials to continue" - )); + protected void updateStatusConnectEnergy(BlockMenu menu, ItemStack itemStack) { + menu.replaceExistingItem(getStatusSlot(), getDisplayOrWarn(itemStack, "&cConnect energy to continue")); } - public MediumContainerMachine setMachineRecipes(@Nonnull List machineRecipes) { + public GenericMachine setMachineRecipes(@Nonnull List machineRecipes) { this.machineRecipes = machineRecipes; return this; } - public MediumContainerMachine setTimeProcess(int timeProcess) { + public GenericMachine setTimeProcess(int timeProcess) { this.timeProcess = timeProcess; return this; } @@ -272,7 +247,7 @@ public String getMachineIdentifier() { return nonNull(this.machineIdentifier) ? this.machineIdentifier : "MachineIdentifier"; } - public MediumContainerMachine setMachineIdentifier(@Nonnull String machineIdentifier) { + public GenericMachine setMachineIdentifier(@Nonnull String machineIdentifier) { this.machineIdentifier = machineIdentifier; return this; } @@ -280,224 +255,227 @@ public MediumContainerMachine setMachineIdentifier(@Nonnull String machineIdenti @Override protected void tick(Block b) { BlockMenu inv = BlockStorage.getInventory(b); + if (inv == null) { + return; + } if (this.isProcessing(b)) { + doProcessing(b, inv); + } else { + nextProcessing(b, inv); + } + } - final MachineRecipe machineRecipe = processing.get(b); - - if (getCharge(b.getLocation()) < getEnergyConsumption()) { - noEnergyContinue(inv, (machineRecipe.getOutput()[0])); - return; - } - - final int processItem = getProgressItem(b); - final int ticks = getProcessing(b).getTicks(); - int ticksLeft = getProgressTime(b); - ItemStack[] result = machineRecipe.getOutput(); - - //checks if inventory is full - int fullSlots = 0; - List slots = new LinkedList<>(); - for (int slot : getOutputSlots()) { - ItemStack stack = inv.getItemInSlot(slot); - if (stack != null && SlimefunUtils.isItemSimilar(stack, result[0], false, false)) { - if (stack.getAmount() >= stack.getMaxStackSize()) { - fullSlots++; - } - slots.add(slot); - } - } - - if (!slots.isEmpty() && fullSlots == getOutputSlots().length) { - // All slots with that item are already full - invalidOutput(inv); - return; - } + @Nonnull + @Override + public List getDisplayRecipes() { + List displayRecipes = new ArrayList(); + machineRecipes.forEach(recipe -> { + displayRecipes.add(new CustomItemStack(Material.GRAY_STAINED_GLASS_PANE, " ")); + displayRecipes.add(recipe.getFirstItemOutput()); + }); + return displayRecipes; + } - int sizeProcessInput = getSizeProcessInput(); - if(sizeProcessInput == -1){ - sizeProcessInput = machineRecipe.getInput().length; + @Override + protected MachineRecipe findNextRecipe(BlockMenu inv) { + for (AbstractItemRecipe recipe : machineRecipes) { + if (matchingRecipe(recipe.getInputNotNull(), inv)) { + return new MachineRecipe(getTimeProcess(), recipe.getInputNotNull(), recipe.getOutputNotNull()); } + } + return null; + } - if ((ticksLeft == ticks) && (ticksLeft > 0) && this.takeCharge(b.getLocation())) { - - startProcessTicks(b, inv, ticks, ticksLeft, result[0]); - - } else if ((ticksLeft <= 0) && (processItem >= sizeProcessInput) && this.takeCharge(b.getLocation())) { - - endProcessTicks(b, inv, result); + protected int getProgressTime(Block b) { + return progressTime.get(b) != null ? progressTime.get(b) : getTimeProcess(); + } - } else if ((ticksLeft <= 0) && (processItem < sizeProcessInput) && this.takeCharge(b.getLocation())) { + protected MachineRecipe getProcessing(Block b) { + return processing.get(b); + } - checkMaterialProgress(b, inv, machineRecipe, processItem, sizeProcessInput, ticks, ticksLeft, result); + protected boolean isProcessing(Block b) { + return getProcessing(b) != null; + } + protected boolean notHasSpaceOutput(BlockMenu inv, ItemStack[] result) { + int requiredSpace = result.length; + int availableSpace = 0; + for (int slot : getOutputSlots()) { + ItemStack itemInSlot = inv.getItemInSlot(slot); + if (itemInSlot == null) { + availableSpace++; } else { - - processTicks(b, inv, machineRecipe, processItem, sizeProcessInput, ticks, ticksLeft, result); - + for (ItemStack stack : result) { + if (SlimefunUtils.isItemSimilar(itemInSlot, stack, false, false) + && itemInSlot.getAmount() < itemInSlot.getMaxStackSize()) { + availableSpace++; + break; + } + } + } + if (availableSpace >= requiredSpace) { + return false; } + } + return true; + } + private void nextProcessing(Block b, BlockMenu inv) { + MachineRecipe next = this.findNextRecipe(inv); + if (next != null) { + processing.put(b, next); + progressTime.put(b, next.getTicks()); } else { - - if (getCharge(b.getLocation()) < getEnergyConsumption()) { - noEnergyStart(inv); - return; + if (getInputSlots().length <= 5) { + updateStatusReset(inv); + } else { + updateStatusInvalidInput(inv); } + } + } - MachineRecipe next = this.findNextRecipe(inv); - - if (next != null) { - - processing.put(b, next); - progressTime.put(b, next.getTicks()); - progressItem.put(b, 0); + private void doProcessing(Block b, BlockMenu inv) { - } else { + var result = processing.get(b).getOutput(); - invalidInput(inv); - } + if (result == null || result.length == 0) { + progressTime.remove(b); + processing.remove(b); + updateStatusReset(inv); + return; } + if (getCharge(b.getLocation()) < getEnergyConsumption()) { + updateStatusConnectEnergy(inv, null); + return; + } + removeCharge(b.getLocation(), getEnergyConsumption()); + + final int ticks = getProcessing(b).getTicks(); + int ticksRemaining = getProgressTime(b); + if (ticks == ticksRemaining) { + startProcessTicks(b, inv, ticksRemaining); + } else if (ticksRemaining == 0) { + endProcessTicks(b, inv, result); + } else { + doProcessTicks(b, inv, ticks, ticksRemaining, result[0]); + } } - private void processTicks(Block b, BlockMenu inv, MachineRecipe machineRecipe, int processItem, - int sizeProcessInput, int ticks, int ticksLeft, ItemStack[] result) { - final ItemStack[] recipeInput = machineRecipe.getInput(); - if (((ticksLeft < ticks) && (ticksLeft > 0)) && this.takeCharge(b.getLocation())) { - int time = ticksLeft - this.getSpeed(); - if (time < 0) { - time = 0; - } - progressTime.put(b, time); - ChestMenuUtils.updateProgressbar(inv, getStatusSlot(), ticksLeft, ticks, - result[0]); - - if (processItem < sizeProcessInput && recipeInput[processItem] != null) { - ItemStack itemStack = recipeInput[processItem]; - if (consumeItem(b, itemStack)) { - progressItem.put(b, processItem + 1); - } - } - + private void startProcessTicks(Block b, BlockMenu inv, int ticksRemaining) { + if (consumptionRecipe(inv, processing.get(b).getInput())) { + progressTime.put(b, Math.max(ticksRemaining - this.getSpeed(), 0)); + } else { + progressTime.remove(b); + processing.remove(b); + updateStatusInvalidInput(inv); } } private void endProcessTicks(Block b, BlockMenu inv, ItemStack[] result) { - //give result + if (notHasSpaceOutput(inv, result)) { + updateStatusOutputFull(inv); + return; + } for (ItemStack itemStack : result) { inv.pushItem(itemStack.clone(), getOutputSlots()); } progressTime.remove(b); processing.remove(b); - progressItem.remove(b); - invalidInput(inv); + updateStatusReset(inv); } - private void startProcessTicks(Block b, BlockMenu inv, int ticks, int ticksLeft, - ItemStack itemStack) { - ChestMenuUtils.updateProgressbar(inv, getStatusSlot(), ticksLeft, ticks, - itemStack); - int time = ticksLeft - this.getSpeed(); - if (time < 0) { - time = 0; + private void doProcessTicks(Block b, BlockMenu inv, int ticks, int ticksRemaining, ItemStack result) { + progressTime.put(b, Math.max(ticksRemaining - this.getSpeed(), 0)); + ChestMenuUtils.updateProgressbar(inv, getStatusSlot(), ticksRemaining, ticks, result); + } + + private boolean consumptionRecipe(BlockMenu inv, ItemStack[] input) { + + // Count the required amount of each item in the input + Map requiredItems = new HashMap<>(); + for (ItemStack item : input) { + requiredItems.merge(item, item.getAmount(), Integer::sum); } - progressTime.put(b, time); - progressItem.put(b, 0); - } - - private void checkMaterialProgress(Block b, BlockMenu inv, MachineRecipe machineRecipe, - int processItem, int sizeProcessInput, int ticks, int ticksLeft, ItemStack[] result) { - noMaterialContinue(inv, result[0]); - for (int i = processItem; i < sizeProcessInput; i++) { - ItemStack itemStack = machineRecipe.getInput()[i]; - if (consumeItem(b, itemStack)) { - if (ticksLeft > 0) { - int time = ticksLeft - this.getSpeed(); - if (time < 0) { - time = 0; + + // Try to consume the required items from the input slots + boolean consumeFailure = false; + List consumedItems = new ArrayList<>(); + + for (Map.Entry entry : requiredItems.entrySet()) { + ItemStack requiredItem = entry.getKey(); + int requiredAmount = entry.getValue(); + int foundAmount = 0; + + for (int slot : this.getInputSlots()) { + ItemStack itemInSlot = inv.getItemInSlot(slot); + if (itemInSlot != null && SlimefunUtils.isItemSimilar(itemInSlot, requiredItem, false, false)) { + int amountToConsume = Math.min(itemInSlot.getAmount(), requiredAmount - foundAmount); + inv.consumeItem(slot, amountToConsume); + foundAmount += amountToConsume; + consumedItems.add(new ItemStack(itemInSlot.getType(), amountToConsume)); + if (foundAmount >= requiredAmount) { + break; } - progressTime.put(b, time); } - progressItem.put(b, i + 1); - ChestMenuUtils.updateProgressbar(inv, getStatusSlot(), ticksLeft, ticks, - result[0]); + } + if (foundAmount < requiredAmount) { + consumeFailure = true; + break; } } - } - private boolean consumeItem(Block b, ItemStack itemToConsume) { - final BlockMenu inv = BlockStorage.getInventory(b); - int[] inputSlots = this.getInputSlots(); - for (int slot : inputSlots) { - ItemStack item = inv.getItemInSlot(slot); - if (item != null && SlimefunUtils.isItemSimilar(item, itemToConsume, false, true)) { - inv.consumeItem(slot, itemToConsume.getAmount()); - return true; + // Revert consumption if not all required items are found + if (consumeFailure) { + for (ItemStack consumedItem : consumedItems) { + inv.pushItem(consumedItem, getInputSlots()); } + return false; } - return false; + return true; } - @Override - protected MachineRecipe findNextRecipe(BlockMenu inv) { - - int[] inputSlots = this.getInputSlots(); - - for (AbstractItemRecipe recipe : machineRecipes) { - - ItemStack[] recipeInput = recipe.getInput(); - int sizeProcessInput = getSizeProcessInput(); + private boolean matchingRecipe(ItemStack[] recipe, BlockMenu inv) { - if(sizeProcessInput == -1){ - recipeInput = recipe.getInputNotNull(); - sizeProcessInput = recipeInput.length; - } + // Count the required amount of each item in the recipe + Map requiredItems = new HashMap<>(); + for (ItemStack recipeItem : recipe) { + requiredItems.merge(recipeItem, recipeItem.getAmount(), Integer::sum); + } - int foundSize = 0; - for (ItemStack itemStack : recipeInput) { - for (int inputSlot : inputSlots) { - if (SlimefunUtils.isItemSimilar(inv.getItemInSlot(inputSlot), itemStack, false, true)) { - foundSize++; - break; + // Check if all required items are present in the input slots + Map foundItems = new HashMap<>(); + for (int slot : this.getInputSlots()) { + ItemStack itemInSlot = inv.getItemInSlot(slot); + if (itemInSlot != null) { + for (Map.Entry entry : requiredItems.entrySet()) { + ItemStack requiredItem = entry.getKey(); + if (SlimefunUtils.isItemSimilar(itemInSlot, requiredItem, false, false)) { + foundItems.merge(requiredItem, itemInSlot.getAmount(), Integer::sum); } } } + } - if (foundSize == sizeProcessInput) { - return new MachineRecipe(getTimeProcess(), recipeInput, recipe.getOutput()); + // Verify if the found amount of each item is sufficient + for (Map.Entry entry : requiredItems.entrySet()) { + ItemStack requiredItem = entry.getKey(); + int requiredAmount = entry.getValue(); + int foundAmount = foundItems.getOrDefault(requiredItem, 0); + if (foundAmount < requiredAmount) { + return false; } - } - - return null; - - } - - @Nonnull - @Override - public List getDisplayRecipes() { - List displayRecipes = new ArrayList(); - machineRecipes.forEach(recipe -> { - displayRecipes.add(new CustomItemStack(Material.GRAY_STAINED_GLASS_PANE, " ")); - displayRecipes.add(recipe.getFirstItemOutput()); - }); - return displayRecipes; + return true; } - public int getProgressTime(Block b) { - return progressTime.get(b) != null ? progressTime.get(b) : getTimeProcess(); + private ItemStack getDisplayOrInfo(ItemStack itemStack, String name) { + return new CustomItemStack(itemStack != null ? itemStack : new ItemStack(Material.BLACK_STAINED_GLASS_PANE), name); } - public int getProgressItem(Block b) { - return progressItem.get(b) != null ? progressItem.get(b) : 0; + private ItemStack getDisplayOrWarn(ItemStack itemStack, String name) { + return new CustomItemStack(itemStack != null ? itemStack : new ItemStack(Material.RED_STAINED_GLASS_PANE), name); } - - public MachineRecipe getProcessing(Block b) { - return processing.get(b); - } - - public boolean isProcessing(Block b) { - return getProcessing(b) != null; - } - } \ No newline at end of file diff --git a/src/main/java/com/github/relativobr/supreme/generic/machine/SimpleItemContainerMachine.java b/src/main/java/com/github/relativobr/supreme/generic/machine/SimpleItemContainerMachine.java index f581044..65b4a13 100644 --- a/src/main/java/com/github/relativobr/supreme/generic/machine/SimpleItemContainerMachine.java +++ b/src/main/java/com/github/relativobr/supreme/generic/machine/SimpleItemContainerMachine.java @@ -10,10 +10,7 @@ import javax.annotation.ParametersAreNonnullByDefault; import org.bukkit.inventory.ItemStack; -/** - * Machine that uses only 1 item in the input and only 1 item in the output - */ -public class SimpleItemContainerMachine extends MediumContainerMachine { +public class SimpleItemContainerMachine extends GenericMachine { @ParametersAreNonnullByDefault protected SimpleItemContainerMachine(ItemGroup category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { @@ -31,11 +28,6 @@ public List getDisplayRecipes() { return displayRecipes; } - @Override - public int getSizeProcessInput(){ - return 1; - } - @Override public int[] getInputSlots() { return InventoryRecipe.SIMPLE_INPUT; diff --git a/src/main/java/com/github/relativobr/supreme/generic/machine/SimpleItemWithLargeContainerMachine.java b/src/main/java/com/github/relativobr/supreme/generic/machine/SimpleItemWithLargeContainerMachine.java index 474e47e..37e6fc0 100644 --- a/src/main/java/com/github/relativobr/supreme/generic/machine/SimpleItemWithLargeContainerMachine.java +++ b/src/main/java/com/github/relativobr/supreme/generic/machine/SimpleItemWithLargeContainerMachine.java @@ -10,10 +10,7 @@ import javax.annotation.ParametersAreNonnullByDefault; import org.bukkit.inventory.ItemStack; -/** - * Machine that uses only 1 item in the input and only 1 item in the output - */ -public class SimpleItemWithLargeContainerMachine extends MediumContainerMachine { +public class SimpleItemWithLargeContainerMachine extends GenericMachine { @ParametersAreNonnullByDefault protected SimpleItemWithLargeContainerMachine(ItemGroup category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { @@ -31,11 +28,6 @@ public List getDisplayRecipes() { return displayRecipes; } - @Override - public int getSizeProcessInput(){ - return 1; - } - @Override public int[] getInputSlots() { return InventoryRecipe.INVERT_LARGE_INPUT; diff --git a/src/main/java/com/github/relativobr/supreme/machine/ElectricCrafter.java b/src/main/java/com/github/relativobr/supreme/machine/ElectricCrafter.java index dd5b940..0e7b7ac 100644 --- a/src/main/java/com/github/relativobr/supreme/machine/ElectricCrafter.java +++ b/src/main/java/com/github/relativobr/supreme/machine/ElectricCrafter.java @@ -1,6 +1,6 @@ package com.github.relativobr.supreme.machine; -import com.github.relativobr.supreme.generic.machine.FlexItemContainerMachine; +import com.github.relativobr.supreme.generic.machine.GenericMachine; import com.github.relativobr.supreme.generic.recipe.AbstractItemRecipe; import com.github.relativobr.supreme.resource.SupremeComponents; import com.github.relativobr.supreme.resource.magical.SupremeCetrus; @@ -18,7 +18,7 @@ import org.bukkit.Material; import org.bukkit.inventory.ItemStack; -public class ElectricCrafter extends FlexItemContainerMachine { +public class ElectricCrafter extends GenericMachine { public static final SlimefunItemStack ELECTRIC_CRAFTER_MACHINE = new SupremeItemStack("SUPREME_ELECTRIC_CRAFTER_I", Material.CRAFTING_TABLE, "&bElectric Crafter", "", "&fCraft Items", "", diff --git a/src/main/java/com/github/relativobr/supreme/machine/Foundry.java b/src/main/java/com/github/relativobr/supreme/machine/Foundry.java index 5ce2fa8..a0e9734 100644 --- a/src/main/java/com/github/relativobr/supreme/machine/Foundry.java +++ b/src/main/java/com/github/relativobr/supreme/machine/Foundry.java @@ -13,7 +13,7 @@ import static com.github.relativobr.supreme.resource.core.SupremeCoreDeath.RESOURCE_CORE_STRING; import static com.github.relativobr.supreme.resource.core.SupremeCoreLife.RESOURCE_CORE_APPLE; -import com.github.relativobr.supreme.generic.machine.MediumContainerMachine; +import com.github.relativobr.supreme.generic.machine.GenericMachine; import com.github.relativobr.supreme.generic.recipe.AbstractItemRecipe; import com.github.relativobr.supreme.resource.SupremeComponents; import com.github.relativobr.supreme.resource.magical.SupremeAttribute; @@ -38,7 +38,7 @@ import org.springframework.scheduling.annotation.Async; @Async -public class Foundry extends MediumContainerMachine { +public class Foundry extends GenericMachine { public static final SlimefunItemStack FOUNDRY_MACHINE = new SupremeItemStack("SUPREME_FOUNDRY_I", Material.GRINDSTONE, "&bFoundry", "", "&fFoundry and Synthesizer Items", "", diff --git a/src/main/java/com/github/relativobr/supreme/machine/MagicAltar.java b/src/main/java/com/github/relativobr/supreme/machine/MagicAltar.java index 6a050a2..e4be968 100644 --- a/src/main/java/com/github/relativobr/supreme/machine/MagicAltar.java +++ b/src/main/java/com/github/relativobr/supreme/machine/MagicAltar.java @@ -1,6 +1,6 @@ package com.github.relativobr.supreme.machine; -import com.github.relativobr.supreme.generic.machine.MediumContainerMachine; +import com.github.relativobr.supreme.generic.machine.GenericMachine; import com.github.relativobr.supreme.generic.recipe.AbstractItemRecipe; import com.github.relativobr.supreme.resource.SupremeComponents; import com.github.relativobr.supreme.resource.magical.SupremeAttribute; @@ -21,7 +21,7 @@ import org.springframework.scheduling.annotation.Async; @Async -public class MagicAltar extends MediumContainerMachine { +public class MagicAltar extends GenericMachine { public static final SlimefunItemStack MAGIC_ALTAR_MACHINE = new SupremeItemStack("SUPREME_MAGIC_ALTAR_I", Material.SMOKER, "&bMagic Altar", "", "&fCraft Rune and Magical Items", "", diff --git a/src/main/java/com/github/relativobr/supreme/machine/MobCollector.java b/src/main/java/com/github/relativobr/supreme/machine/MobCollector.java index caff5a2..2cbf877 100644 --- a/src/main/java/com/github/relativobr/supreme/machine/MobCollector.java +++ b/src/main/java/com/github/relativobr/supreme/machine/MobCollector.java @@ -18,7 +18,6 @@ import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; import io.github.thebusybiscuit.slimefun4.libraries.commons.lang.Validate; import io.github.thebusybiscuit.slimefun4.libraries.dough.inventory.InvUtils; -import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import io.github.thebusybiscuit.slimefun4.utils.LoreBuilder; import java.util.HashMap; @@ -367,7 +366,23 @@ public final MobCollector setMobRange(int value) { @Override protected void tick(Block b) { BlockMenu inv = BlockStorage.getInventory(b); + if (inv == null) { + return; + } + if (isProcessing(b)) { + + var recipeOutput = processing.get(b).getOutput(); + if (notHasSpaceOutput(inv, recipeOutput)) { + updateStatusOutputFull(inv); + return; + } + + if (getCharge(b.getLocation()) < getEnergyConsumption()) { + updateStatusConnectEnergy(inv, recipeOutput[0]); + return; + } + if (takeCharge(b.getLocation())) { int timeleft = progress.get(b); if (timeleft > 0) { @@ -378,8 +393,7 @@ protected void tick(Block b) { } progress.put(b, time); } else { - inv.replaceExistingItem(getStatusSlot(), new CustomItemStack(Material.BLACK_STAINED_GLASS_PANE, " ")); - for (ItemStack output : processing.get(b).getOutput()) { + for (ItemStack output : recipeOutput) { if(output != null){ ItemStack clone = output.clone(); clone.setAmount(1); @@ -388,6 +402,7 @@ protected void tick(Block b) { } progress.remove(b); processing.remove(b); + updateStatusReset(inv); } } } else { @@ -395,6 +410,8 @@ protected void tick(Block b) { if (next != null) { processing.put(b, next); progress.put(b, next.getTicks()); + } else { + updateStatusReset(inv); } } } diff --git a/src/main/java/com/github/relativobr/supreme/machine/VirtualAquarium.java b/src/main/java/com/github/relativobr/supreme/machine/VirtualAquarium.java index 8d97766..a65f1ea 100644 --- a/src/main/java/com/github/relativobr/supreme/machine/VirtualAquarium.java +++ b/src/main/java/com/github/relativobr/supreme/machine/VirtualAquarium.java @@ -15,7 +15,6 @@ import io.github.thebusybiscuit.slimefun4.core.attributes.MachineType; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; import io.github.thebusybiscuit.slimefun4.libraries.dough.inventory.InvUtils; -import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import io.github.thebusybiscuit.slimefun4.utils.LoreBuilder; import java.util.HashMap; @@ -174,7 +173,23 @@ protected MachineRecipe findNextRecipe(@Nonnull BlockMenu inv) { @Override protected void tick(Block b) { BlockMenu inv = BlockStorage.getInventory(b); + if (inv == null) { + return; + } + if (isProcessing(b)) { + + var recipeOutput = processing.get(b).getOutput(); + if (notHasSpaceOutput(inv, recipeOutput)) { + updateStatusOutputFull(inv); + return; + } + + if (getCharge(b.getLocation()) < getEnergyConsumption()) { + updateStatusConnectEnergy(inv, recipeOutput[0]); + return; + } + if (takeCharge(b.getLocation())) { int timeleft = progress.get(b); if (timeleft > 0) { @@ -185,8 +200,7 @@ protected void tick(Block b) { } progress.put(b, time); } else { - inv.replaceExistingItem(this.getStatusSlot(), new CustomItemStack(Material.BLACK_STAINED_GLASS_PANE, " ")); - ItemStack material = UtilMachine.getMaterial(processing.get(b).getOutput(), UtilMachine.getRandomInt()); + ItemStack material = UtilMachine.getMaterial(recipeOutput, UtilMachine.getRandomInt()); if (material != null) { final ItemStack itemStack = material.clone(); itemStack.setAmount(1); @@ -194,6 +208,7 @@ protected void tick(Block b) { } progress.remove(b); processing.remove(b); + updateStatusReset(inv); } } } else { @@ -201,6 +216,8 @@ protected void tick(Block b) { if (next != null) { processing.put(b, next); progress.put(b, next.getTicks()); + } else { + updateStatusReset(inv); } } } diff --git a/src/main/java/com/github/relativobr/supreme/machine/VirtualGarden.java b/src/main/java/com/github/relativobr/supreme/machine/VirtualGarden.java index a81222a..00f5d76 100644 --- a/src/main/java/com/github/relativobr/supreme/machine/VirtualGarden.java +++ b/src/main/java/com/github/relativobr/supreme/machine/VirtualGarden.java @@ -149,7 +149,23 @@ protected MachineRecipe findNextRecipe(@Nonnull BlockMenu inv) { @Override protected void tick(Block b) { BlockMenu inv = BlockStorage.getInventory(b); + if (inv == null) { + return; + } + if (isProcessing(b)) { + + var recipeOutput = processing.get(b).getOutput(); + if (notHasSpaceOutput(inv, recipeOutput)) { + updateStatusOutputFull(inv); + return; + } + + if (getCharge(b.getLocation()) < getEnergyConsumption()) { + updateStatusConnectEnergy(inv, recipeOutput[0]); + return; + } + if (takeCharge(b.getLocation())) { int timeleft = progress.get(b); if (timeleft > 0) { @@ -160,8 +176,7 @@ protected void tick(Block b) { } progress.put(b, time); } else { - inv.replaceExistingItem(getStatusSlot(), new CustomItemStack(Material.BLACK_STAINED_GLASS_PANE, " ")); - for (ItemStack output : processing.get(b).getOutput()) { + for (ItemStack output : recipeOutput) { if(output != null){ ItemStack clone = output.clone(); clone.setAmount(1); @@ -170,6 +185,7 @@ protected void tick(Block b) { } progress.remove(b); processing.remove(b); + updateStatusReset(inv); } } } else { @@ -177,6 +193,8 @@ protected void tick(Block b) { if (next != null) { processing.put(b, next); progress.put(b, next.getTicks()); + } else { + updateStatusReset(inv); } } } diff --git a/src/main/java/com/github/relativobr/supreme/machine/multiblock/ElectricCoreFabricator.java b/src/main/java/com/github/relativobr/supreme/machine/multiblock/ElectricCoreFabricator.java index 52db377..002e458 100644 --- a/src/main/java/com/github/relativobr/supreme/machine/multiblock/ElectricCoreFabricator.java +++ b/src/main/java/com/github/relativobr/supreme/machine/multiblock/ElectricCoreFabricator.java @@ -1,6 +1,6 @@ package com.github.relativobr.supreme.machine.multiblock; -import com.github.relativobr.supreme.generic.machine.MediumContainerMachine; +import com.github.relativobr.supreme.generic.machine.GenericMachine; import com.github.relativobr.supreme.generic.recipe.AbstractItemRecipe; import com.github.relativobr.supreme.generic.recipe.CustomCoreRecipe; import com.github.relativobr.supreme.resource.SupremeComponents; @@ -26,7 +26,7 @@ import org.bukkit.Material; import org.bukkit.inventory.ItemStack; -public class ElectricCoreFabricator extends MediumContainerMachine { +public class ElectricCoreFabricator extends GenericMachine { public static final SlimefunItemStack ELECTRIC_CORE_MACHINE = new SupremeItemStack("SUPREME_ELECTRIC_CORE_I", Material.SHROOMLIGHT, "&bElectric Core Machine", "", "&fCraft resource of core", "", diff --git a/src/main/java/com/github/relativobr/supreme/machine/multiblock/ElectricGearFabricator.java b/src/main/java/com/github/relativobr/supreme/machine/multiblock/ElectricGearFabricator.java index e4647db..24a7822 100644 --- a/src/main/java/com/github/relativobr/supreme/machine/multiblock/ElectricGearFabricator.java +++ b/src/main/java/com/github/relativobr/supreme/machine/multiblock/ElectricGearFabricator.java @@ -1,6 +1,6 @@ package com.github.relativobr.supreme.machine.multiblock; -import com.github.relativobr.supreme.generic.machine.FlexItemContainerMachine; +import com.github.relativobr.supreme.generic.machine.GenericMachine; import com.github.relativobr.supreme.generic.recipe.AbstractItemRecipe; import com.github.relativobr.supreme.gear.ArmorThornium; import com.github.relativobr.supreme.gear.WeaponsThornium; @@ -23,7 +23,7 @@ import org.bukkit.Material; import org.bukkit.inventory.ItemStack; -public class ElectricGearFabricator extends FlexItemContainerMachine { +public class ElectricGearFabricator extends GenericMachine { public static final SlimefunItemStack ELECTRIC_GEAR_MACHINE = new SupremeItemStack("SUPREME_ELECTRIC_GEAR_I", Material.SMITHING_TABLE, "&bElectric Gear Machine", "", "&fCraft tier of gear", "", diff --git a/src/main/java/com/github/relativobr/supreme/machine/multiblock/ElectricMagicalFabricator.java b/src/main/java/com/github/relativobr/supreme/machine/multiblock/ElectricMagicalFabricator.java index 9cadf31..b38c463 100644 --- a/src/main/java/com/github/relativobr/supreme/machine/multiblock/ElectricMagicalFabricator.java +++ b/src/main/java/com/github/relativobr/supreme/machine/multiblock/ElectricMagicalFabricator.java @@ -1,6 +1,6 @@ package com.github.relativobr.supreme.machine.multiblock; -import com.github.relativobr.supreme.generic.machine.MediumContainerMachine; +import com.github.relativobr.supreme.generic.machine.GenericMachine; import com.github.relativobr.supreme.generic.recipe.AbstractItemRecipe; import com.github.relativobr.supreme.resource.SupremeComponents; import com.github.relativobr.supreme.resource.core.SupremeCoreAlloy; @@ -22,7 +22,7 @@ import org.bukkit.Material; import org.bukkit.inventory.ItemStack; -public class ElectricMagicalFabricator extends MediumContainerMachine { +public class ElectricMagicalFabricator extends GenericMachine { public static final SlimefunItemStack ELECTRIC_MAGICAL_MACHINE = new SupremeItemStack("SUPREME_ELECTRIC_MAGICAL_I", Material.CRYING_OBSIDIAN, "&bElectric Magical Machine", "", "&fCraft resource of magical", "",