diff --git a/src/main/java/io/github/mooy1/infinitylib/machines/CraftingBlockRecipe.java b/src/main/java/io/github/mooy1/infinitylib/machines/CraftingBlockRecipe.java index d1aaf4e..d6c67bd 100644 --- a/src/main/java/io/github/mooy1/infinitylib/machines/CraftingBlockRecipe.java +++ b/src/main/java/io/github/mooy1/infinitylib/machines/CraftingBlockRecipe.java @@ -26,7 +26,7 @@ public final class CraftingBlockRecipe { boolean check(ItemStackSnapshot[] input) { for (int i = 0; i < recipe.length; i++) { boolean similar = StackUtils.isSimilar(input[i], recipe[i]); - if (!similar && (recipe[i] == null || recipe[i].getAmount() > input[i].getAmount())) { + if (!similar || (recipe[i] != null && recipe[i].getAmount() > input[i].getAmount())) { return false; } } diff --git a/src/main/java/io/github/mooy1/infinitylib/machines/MachineBlock.java b/src/main/java/io/github/mooy1/infinitylib/machines/MachineBlock.java index ff9b237..4a53374 100644 --- a/src/main/java/io/github/mooy1/infinitylib/machines/MachineBlock.java +++ b/src/main/java/io/github/mooy1/infinitylib/machines/MachineBlock.java @@ -6,6 +6,7 @@ import java.util.Map; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; import lombok.Setter; @@ -91,9 +92,40 @@ protected boolean process(Block b, BlockMenu menu) { return true; } + int[] slots = layout.inputSlots(); + ItemStack[] input = new ItemStack[slots.length]; + for (int i = 0; i < slots.length; i++) { + input[i] = menu.getItemInSlot(slots[i]); + } + + MachineBlockRecipe recipe = getOutput(input); + if (recipe != null) { + ItemStack rem = menu.pushItem(recipe.output.clone(), layout.outputSlots()); + if (rem == null || rem.getAmount() < recipe.output.getAmount()) { + recipe.consume(); + if (menu.hasViewer()) { + menu.replaceExistingItem(getStatusSlot(), PROCESSING_ITEM); + } + return true; + } + else { + if (menu.hasViewer()) { + menu.replaceExistingItem(getStatusSlot(), NO_ROOM_ITEM); + } + return false; + } + } + + if (menu.hasViewer()) { + menu.replaceExistingItem(getStatusSlot(), IDLE_ITEM); + } + return false; + } + + @Nullable + MachineBlockRecipe getOutput(ItemStack[] items) { Map map = new HashMap<>(2, 1F); - for (int slot : getInputSlots()) { - ItemStack item = menu.getItemInSlot(slot); + for (ItemStack item : items) { if (item != null) { String string = StackUtils.getId(item); if (string == null) { @@ -105,27 +137,10 @@ protected boolean process(Block b, BlockMenu menu) { for (MachineBlockRecipe recipe : recipes) { if (recipe.check(map)) { - ItemStack rem = menu.pushItem(recipe.output.clone(), layout.outputSlots()); - if (rem == null || rem.getAmount() < recipe.output.getAmount()) { - recipe.consume(map); - if (menu.hasViewer()) { - menu.replaceExistingItem(getStatusSlot(), PROCESSING_ITEM); - } - return true; - } - else { - if (menu.hasViewer()) { - menu.replaceExistingItem(getStatusSlot(), NO_ROOM_ITEM); - } - return false; - } + return recipe; } } - - if (menu.hasViewer()) { - menu.replaceExistingItem(getStatusSlot(), IDLE_ITEM); - } - return false; + return null; } @Override diff --git a/src/main/java/io/github/mooy1/infinitylib/machines/MachineBlockRecipe.java b/src/main/java/io/github/mooy1/infinitylib/machines/MachineBlockRecipe.java index 51ab4f4..148e45a 100644 --- a/src/main/java/io/github/mooy1/infinitylib/machines/MachineBlockRecipe.java +++ b/src/main/java/io/github/mooy1/infinitylib/machines/MachineBlockRecipe.java @@ -14,6 +14,7 @@ final class MachineBlockRecipe { private final String[] strings; private final int[] amounts; final ItemStack output; + private Map lastMatch; MachineBlockRecipe(ItemStack output, ItemStack[] input) { this.output = output; @@ -40,13 +41,14 @@ boolean check(Map map) { return false; } } + lastMatch = map; return true; } - void consume(Map map) { + void consume() { for (int i = 0; i < strings.length; i++) { int consume = amounts[i]; - for (ItemStack item : map.get(strings[i]).items) { + for (ItemStack item : lastMatch.get(strings[i]).items) { int amt = item.getAmount(); if (amt >= consume) { ItemUtils.consumeItem(item, consume, true); diff --git a/src/test/java/io/github/mooy1/infinitylib/machines/TestMachineBlock.java b/src/test/java/io/github/mooy1/infinitylib/machines/TestMachineBlock.java index ae871a4..4c16edc 100644 --- a/src/test/java/io/github/mooy1/infinitylib/machines/TestMachineBlock.java +++ b/src/test/java/io/github/mooy1/infinitylib/machines/TestMachineBlock.java @@ -1,7 +1,6 @@ package io.github.mooy1.infinitylib.machines; import org.bukkit.Material; -import org.bukkit.block.Block; import org.bukkit.inventory.ItemStack; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; @@ -11,7 +10,6 @@ import org.junit.jupiter.api.TestMethodOrder; import be.seeseemelk.mockbukkit.MockBukkit; -import be.seeseemelk.mockbukkit.ServerMock; import io.github.mooy1.infinitylib.core.MockAddon; import io.github.mooy1.infinitylib.groups.SubGroup; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; @@ -19,39 +17,31 @@ import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack; -import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; -import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; @TestMethodOrder(MethodOrderer.OrderAnnotation.class) class TestMachineBlock { - private static ServerMock server; private static MockAddon addon; private static MachineBlock machine; - private static Block block; private static ItemStack input1; private static ItemStack input2; private static ItemStack output; - private static BlockMenu menu; @BeforeAll public static void load() { - server = MockBukkit.mock(); + MockBukkit.mock(); addon = MockBukkit.load(MockAddon.class); Slimefun.getCfg().setValue("URID.enable-tickers", true); machine = new MachineBlock(new SubGroup("key", new ItemStack(Material.DIAMOND)), new SlimefunItemStack("ID", Material.STONE, "name"), RecipeType.ANCIENT_ALTAR, new ItemStack[0]); - block = server.addSimpleWorld("").getBlockAt(0, 0, 0); output = new CustomItemStack(SlimefunItems.SALT, 2); input1 = SlimefunItems.COPPER_DUST; input2 = new ItemStack(Material.NETHERITE_BLOCK, 2); @@ -80,102 +70,40 @@ void testRegister() { @Test @Order(1) - void testBlockMenuPreset() { - BlockMenuPreset preset = BlockMenuPreset.getPreset(machine.getId()); - assertNotNull(preset); - menu = new BlockMenu(preset, block.getLocation()); - } - - @Test - @Order(2) void testAddRecipes() { machine.addRecipe(output, input1, input2); assertThrows(IllegalArgumentException.class, () -> machine.addRecipe(output)); } - @Test - @Order(3) - void testTicksPerOutput() { - assertFalse(machine.process(block, menu)); - server.getScheduler().performOneTick(); - assertTrue(machine.process(block, menu)); - server.getScheduler().performOneTick(); - assertFalse(machine.process(block, menu)); - } - @Test void testProcess() { - assertFalse(machine.process(block, menu)); - - menu.replaceExistingItem(19, input1.clone()); - menu.replaceExistingItem(20, input2.clone()); - menu.replaceExistingItem(24, null); - menu.replaceExistingItem(24, null); - - assertTrue(machine.process(block, menu)); - assertNotSame(output, menu.getItemInSlot(24)); - assertEquals(output, menu.getItemInSlot(24)); - assertEquals(0, menu.getItemInSlot(19).getAmount()); - assertEquals(0, menu.getItemInSlot(20).getAmount()); - } + ItemStack[] input = new ItemStack[2]; + assertNull(machine.getOutput(input)); - @Test - void testShapelessProcessTwice() { - menu.replaceExistingItem(19, new CustomItemStack(input2, 4)); - menu.replaceExistingItem(20, new CustomItemStack(input1, 2)); - menu.replaceExistingItem(24, null); - menu.replaceExistingItem(25, null); - - assertTrue(machine.process(block, menu)); - assertEquals(2, menu.getItemInSlot(24).getAmount()); - assertEquals(2, menu.getItemInSlot(19).getAmount()); - assertEquals(1, menu.getItemInSlot(20).getAmount()); - - assertTrue(machine.process(block, menu)); - assertEquals(4, menu.getItemInSlot(24).getAmount()); - assertEquals(0, menu.getItemInSlot(19).getAmount()); - assertEquals(0, menu.getItemInSlot(20).getAmount()); - } + input[0] = input1.clone(); + input[1] = input2.clone(); + MachineBlockRecipe out = machine.getOutput(input); - @Test - void testSplitOutput() { - menu.replaceExistingItem(19, new CustomItemStack(input2, 2)); - menu.replaceExistingItem(20, new CustomItemStack(input1, 1)); - menu.replaceExistingItem(24, new CustomItemStack(output, 63)); - menu.replaceExistingItem(25, null); - - assertTrue(machine.process(block, menu)); - assertEquals(64, menu.getItemInSlot(24).getAmount()); - assertEquals(output, menu.getItemInSlot(25)); - } + assertNotNull(out); + assertSame(output, out.output); - @Test - void testPartialOutput() { - menu.replaceExistingItem(19, new CustomItemStack(input2, 2)); - menu.replaceExistingItem(20, new CustomItemStack(input1, 1)); - menu.replaceExistingItem(24, new CustomItemStack(output, 64)); - menu.replaceExistingItem(25, new CustomItemStack(output, 63)); - - assertTrue(machine.process(block, menu)); - assertEquals(64, menu.getItemInSlot(24).getAmount()); - assertEquals(64, menu.getItemInSlot(25).getAmount()); - assertEquals(0, menu.getItemInSlot(19).getAmount()); - assertEquals(0, menu.getItemInSlot(20).getAmount()); - } + out.consume(); - @Test - void testNoRoom() { - menu.replaceExistingItem(19, new CustomItemStack(input2, 2)); - menu.replaceExistingItem(20, new CustomItemStack(input1, 1)); - menu.replaceExistingItem(24, new CustomItemStack(output, 64)); - menu.replaceExistingItem(25, new CustomItemStack(output, 64)); + assertEquals(0, input[0].getAmount()); + assertEquals(0, input[1].getAmount()); + assertNull(machine.getOutput(input)); + + input[0] = new CustomItemStack(input2, 4); + input[1] = new CustomItemStack(input1, 2); + + out = machine.getOutput(input); - assertFalse(machine.process(block, menu)); + assertNotNull(out); - menu.replaceExistingItem(24, new CustomItemStack(input1, 1)); - menu.replaceExistingItem(25, new CustomItemStack(input2, 1)); + out.consume(); - assertFalse(machine.process(block, menu)); + assertEquals(2, input[0].getAmount()); + assertEquals(1, input[1].getAmount()); } }