Skip to content

Commit f8977e1

Browse files
committed
fix tests
1 parent 4d178e6 commit f8977e1

File tree

4 files changed

+63
-118
lines changed

4 files changed

+63
-118
lines changed

src/main/java/io/github/mooy1/infinitylib/machines/CraftingBlockRecipe.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public final class CraftingBlockRecipe {
2626
boolean check(ItemStackSnapshot[] input) {
2727
for (int i = 0; i < recipe.length; i++) {
2828
boolean similar = StackUtils.isSimilar(input[i], recipe[i]);
29-
if (!similar && (recipe[i] == null || recipe[i].getAmount() > input[i].getAmount())) {
29+
if (!similar || (recipe[i] != null && recipe[i].getAmount() > input[i].getAmount())) {
3030
return false;
3131
}
3232
}

src/main/java/io/github/mooy1/infinitylib/machines/MachineBlock.java

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Map;
77

88
import javax.annotation.Nonnull;
9+
import javax.annotation.Nullable;
910
import javax.annotation.ParametersAreNonnullByDefault;
1011

1112
import lombok.Setter;
@@ -91,9 +92,40 @@ protected boolean process(Block b, BlockMenu menu) {
9192
return true;
9293
}
9394

95+
int[] slots = layout.inputSlots();
96+
ItemStack[] input = new ItemStack[slots.length];
97+
for (int i = 0; i < slots.length; i++) {
98+
input[i] = menu.getItemInSlot(slots[i]);
99+
}
100+
101+
MachineBlockRecipe recipe = getOutput(input);
102+
if (recipe != null) {
103+
ItemStack rem = menu.pushItem(recipe.output.clone(), layout.outputSlots());
104+
if (rem == null || rem.getAmount() < recipe.output.getAmount()) {
105+
recipe.consume();
106+
if (menu.hasViewer()) {
107+
menu.replaceExistingItem(getStatusSlot(), PROCESSING_ITEM);
108+
}
109+
return true;
110+
}
111+
else {
112+
if (menu.hasViewer()) {
113+
menu.replaceExistingItem(getStatusSlot(), NO_ROOM_ITEM);
114+
}
115+
return false;
116+
}
117+
}
118+
119+
if (menu.hasViewer()) {
120+
menu.replaceExistingItem(getStatusSlot(), IDLE_ITEM);
121+
}
122+
return false;
123+
}
124+
125+
@Nullable
126+
MachineBlockRecipe getOutput(ItemStack[] items) {
94127
Map<String, MachineInput> map = new HashMap<>(2, 1F);
95-
for (int slot : getInputSlots()) {
96-
ItemStack item = menu.getItemInSlot(slot);
128+
for (ItemStack item : items) {
97129
if (item != null) {
98130
String string = StackUtils.getId(item);
99131
if (string == null) {
@@ -105,27 +137,10 @@ protected boolean process(Block b, BlockMenu menu) {
105137

106138
for (MachineBlockRecipe recipe : recipes) {
107139
if (recipe.check(map)) {
108-
ItemStack rem = menu.pushItem(recipe.output.clone(), layout.outputSlots());
109-
if (rem == null || rem.getAmount() < recipe.output.getAmount()) {
110-
recipe.consume(map);
111-
if (menu.hasViewer()) {
112-
menu.replaceExistingItem(getStatusSlot(), PROCESSING_ITEM);
113-
}
114-
return true;
115-
}
116-
else {
117-
if (menu.hasViewer()) {
118-
menu.replaceExistingItem(getStatusSlot(), NO_ROOM_ITEM);
119-
}
120-
return false;
121-
}
140+
return recipe;
122141
}
123142
}
124-
125-
if (menu.hasViewer()) {
126-
menu.replaceExistingItem(getStatusSlot(), IDLE_ITEM);
127-
}
128-
return false;
143+
return null;
129144
}
130145

131146
@Override

src/main/java/io/github/mooy1/infinitylib/machines/MachineBlockRecipe.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ final class MachineBlockRecipe {
1414
private final String[] strings;
1515
private final int[] amounts;
1616
final ItemStack output;
17+
private Map<String, MachineInput> lastMatch;
1718

1819
MachineBlockRecipe(ItemStack output, ItemStack[] input) {
1920
this.output = output;
@@ -40,13 +41,14 @@ boolean check(Map<String, MachineInput> map) {
4041
return false;
4142
}
4243
}
44+
lastMatch = map;
4345
return true;
4446
}
4547

46-
void consume(Map<String, MachineInput> map) {
48+
void consume() {
4749
for (int i = 0; i < strings.length; i++) {
4850
int consume = amounts[i];
49-
for (ItemStack item : map.get(strings[i]).items) {
51+
for (ItemStack item : lastMatch.get(strings[i]).items) {
5052
int amt = item.getAmount();
5153
if (amt >= consume) {
5254
ItemUtils.consumeItem(item, consume, true);
Lines changed: 22 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.github.mooy1.infinitylib.machines;
22

33
import org.bukkit.Material;
4-
import org.bukkit.block.Block;
54
import org.bukkit.inventory.ItemStack;
65
import org.junit.jupiter.api.AfterAll;
76
import org.junit.jupiter.api.BeforeAll;
@@ -11,47 +10,38 @@
1110
import org.junit.jupiter.api.TestMethodOrder;
1211

1312
import be.seeseemelk.mockbukkit.MockBukkit;
14-
import be.seeseemelk.mockbukkit.ServerMock;
1513
import io.github.mooy1.infinitylib.core.MockAddon;
1614
import io.github.mooy1.infinitylib.groups.SubGroup;
1715
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
1816
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
1917
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
2018
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems;
2119
import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack;
22-
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
23-
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset;
2420

2521
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2622
import static org.junit.jupiter.api.Assertions.assertEquals;
27-
import static org.junit.jupiter.api.Assertions.assertFalse;
2823
import static org.junit.jupiter.api.Assertions.assertNotNull;
29-
import static org.junit.jupiter.api.Assertions.assertNotSame;
24+
import static org.junit.jupiter.api.Assertions.assertNull;
3025
import static org.junit.jupiter.api.Assertions.assertSame;
3126
import static org.junit.jupiter.api.Assertions.assertThrows;
32-
import static org.junit.jupiter.api.Assertions.assertTrue;
3327

3428
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
3529
class TestMachineBlock {
3630

37-
private static ServerMock server;
3831
private static MockAddon addon;
3932
private static MachineBlock machine;
40-
private static Block block;
4133
private static ItemStack input1;
4234
private static ItemStack input2;
4335
private static ItemStack output;
44-
private static BlockMenu menu;
4536

4637
@BeforeAll
4738
public static void load() {
48-
server = MockBukkit.mock();
39+
MockBukkit.mock();
4940
addon = MockBukkit.load(MockAddon.class);
5041
Slimefun.getCfg().setValue("URID.enable-tickers", true);
5142
machine = new MachineBlock(new SubGroup("key", new ItemStack(Material.DIAMOND)),
5243
new SlimefunItemStack("ID", Material.STONE, "name"),
5344
RecipeType.ANCIENT_ALTAR, new ItemStack[0]);
54-
block = server.addSimpleWorld("").getBlockAt(0, 0, 0);
5545
output = new CustomItemStack(SlimefunItems.SALT, 2);
5646
input1 = SlimefunItems.COPPER_DUST;
5747
input2 = new ItemStack(Material.NETHERITE_BLOCK, 2);
@@ -80,102 +70,40 @@ void testRegister() {
8070

8171
@Test
8272
@Order(1)
83-
void testBlockMenuPreset() {
84-
BlockMenuPreset preset = BlockMenuPreset.getPreset(machine.getId());
85-
assertNotNull(preset);
86-
menu = new BlockMenu(preset, block.getLocation());
87-
}
88-
89-
@Test
90-
@Order(2)
9173
void testAddRecipes() {
9274
machine.addRecipe(output, input1, input2);
9375
assertThrows(IllegalArgumentException.class, () -> machine.addRecipe(output));
9476
}
9577

96-
@Test
97-
@Order(3)
98-
void testTicksPerOutput() {
99-
assertFalse(machine.process(block, menu));
100-
server.getScheduler().performOneTick();
101-
assertTrue(machine.process(block, menu));
102-
server.getScheduler().performOneTick();
103-
assertFalse(machine.process(block, menu));
104-
}
105-
10678
@Test
10779
void testProcess() {
108-
assertFalse(machine.process(block, menu));
109-
110-
menu.replaceExistingItem(19, input1.clone());
111-
menu.replaceExistingItem(20, input2.clone());
112-
menu.replaceExistingItem(24, null);
113-
menu.replaceExistingItem(24, null);
114-
115-
assertTrue(machine.process(block, menu));
116-
assertNotSame(output, menu.getItemInSlot(24));
117-
assertEquals(output, menu.getItemInSlot(24));
118-
assertEquals(0, menu.getItemInSlot(19).getAmount());
119-
assertEquals(0, menu.getItemInSlot(20).getAmount());
120-
}
80+
ItemStack[] input = new ItemStack[2];
81+
assertNull(machine.getOutput(input));
12182

122-
@Test
123-
void testShapelessProcessTwice() {
124-
menu.replaceExistingItem(19, new CustomItemStack(input2, 4));
125-
menu.replaceExistingItem(20, new CustomItemStack(input1, 2));
126-
menu.replaceExistingItem(24, null);
127-
menu.replaceExistingItem(25, null);
128-
129-
assertTrue(machine.process(block, menu));
130-
assertEquals(2, menu.getItemInSlot(24).getAmount());
131-
assertEquals(2, menu.getItemInSlot(19).getAmount());
132-
assertEquals(1, menu.getItemInSlot(20).getAmount());
133-
134-
assertTrue(machine.process(block, menu));
135-
assertEquals(4, menu.getItemInSlot(24).getAmount());
136-
assertEquals(0, menu.getItemInSlot(19).getAmount());
137-
assertEquals(0, menu.getItemInSlot(20).getAmount());
138-
}
83+
input[0] = input1.clone();
84+
input[1] = input2.clone();
85+
MachineBlockRecipe out = machine.getOutput(input);
13986

140-
@Test
141-
void testSplitOutput() {
142-
menu.replaceExistingItem(19, new CustomItemStack(input2, 2));
143-
menu.replaceExistingItem(20, new CustomItemStack(input1, 1));
144-
menu.replaceExistingItem(24, new CustomItemStack(output, 63));
145-
menu.replaceExistingItem(25, null);
146-
147-
assertTrue(machine.process(block, menu));
148-
assertEquals(64, menu.getItemInSlot(24).getAmount());
149-
assertEquals(output, menu.getItemInSlot(25));
150-
}
87+
assertNotNull(out);
88+
assertSame(output, out.output);
15189

152-
@Test
153-
void testPartialOutput() {
154-
menu.replaceExistingItem(19, new CustomItemStack(input2, 2));
155-
menu.replaceExistingItem(20, new CustomItemStack(input1, 1));
156-
menu.replaceExistingItem(24, new CustomItemStack(output, 64));
157-
menu.replaceExistingItem(25, new CustomItemStack(output, 63));
158-
159-
assertTrue(machine.process(block, menu));
160-
assertEquals(64, menu.getItemInSlot(24).getAmount());
161-
assertEquals(64, menu.getItemInSlot(25).getAmount());
162-
assertEquals(0, menu.getItemInSlot(19).getAmount());
163-
assertEquals(0, menu.getItemInSlot(20).getAmount());
164-
}
90+
out.consume();
16591

166-
@Test
167-
void testNoRoom() {
168-
menu.replaceExistingItem(19, new CustomItemStack(input2, 2));
169-
menu.replaceExistingItem(20, new CustomItemStack(input1, 1));
170-
menu.replaceExistingItem(24, new CustomItemStack(output, 64));
171-
menu.replaceExistingItem(25, new CustomItemStack(output, 64));
92+
assertEquals(0, input[0].getAmount());
93+
assertEquals(0, input[1].getAmount());
94+
assertNull(machine.getOutput(input));
95+
96+
input[0] = new CustomItemStack(input2, 4);
97+
input[1] = new CustomItemStack(input1, 2);
98+
99+
out = machine.getOutput(input);
172100

173-
assertFalse(machine.process(block, menu));
101+
assertNotNull(out);
174102

175-
menu.replaceExistingItem(24, new CustomItemStack(input1, 1));
176-
menu.replaceExistingItem(25, new CustomItemStack(input2, 1));
103+
out.consume();
177104

178-
assertFalse(machine.process(block, menu));
105+
assertEquals(2, input[0].getAmount());
106+
assertEquals(1, input[1].getAmount());
179107
}
180108

181109
}

0 commit comments

Comments
 (0)