|
22 | 22 |
|
23 | 23 | package dev.galacticraft.machinelib.api.gametest; |
24 | 24 |
|
25 | | -import com.google.common.base.CaseFormat; |
26 | 25 | import dev.galacticraft.machinelib.api.block.entity.MachineBlockEntity; |
27 | | -import dev.galacticraft.machinelib.api.gametest.annotation.InstantTest; |
28 | | -import dev.galacticraft.machinelib.api.gametest.annotation.ProcessingTest; |
29 | | -import dev.galacticraft.machinelib.api.gametest.annotation.container.DefaultedMetadata; |
| 26 | +import dev.galacticraft.machinelib.api.gametest.annotation.BasicTest; |
| 27 | +import dev.galacticraft.machinelib.api.gametest.annotation.MachineTest; |
30 | 28 | import dev.galacticraft.machinelib.api.machine.MachineType; |
31 | | -import net.fabricmc.fabric.api.gametest.v1.FabricGameTest; |
32 | 29 | import net.minecraft.core.BlockPos; |
33 | | -import net.minecraft.core.registries.BuiltInRegistries; |
34 | | -import net.minecraft.gametest.framework.GameTest; |
35 | 30 | import net.minecraft.gametest.framework.GameTestAssertException; |
| 31 | +import net.minecraft.gametest.framework.GameTestGenerator; |
36 | 32 | import net.minecraft.gametest.framework.GameTestHelper; |
37 | 33 | import net.minecraft.gametest.framework.TestFunction; |
38 | 34 | import net.minecraft.world.item.Item; |
39 | | -import net.minecraft.world.level.block.Rotation; |
40 | 35 | import org.jetbrains.annotations.MustBeInvokedByOverriders; |
41 | 36 | import org.jetbrains.annotations.NotNull; |
42 | 37 |
|
43 | | -import java.lang.reflect.InvocationTargetException; |
44 | 38 | import java.lang.reflect.Method; |
45 | | -import java.util.ArrayList; |
46 | 39 | import java.util.List; |
47 | | -import java.util.function.Function; |
48 | 40 |
|
49 | 41 | /** |
50 | 42 | * A class for testing machines. |
51 | 43 | * |
52 | 44 | * @param <Machine> the type of machine block entity |
53 | | - * @see InstantTest |
54 | | - * @see ProcessingTest |
| 45 | + * @see BasicTest |
| 46 | + * @see MachineTest |
55 | 47 | */ |
56 | | -public abstract class MachineGameTest<Machine extends MachineBlockEntity> implements FabricGameTest { |
57 | | - private static final Function<String, String> NAME_CONVERSION = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.LOWER_UNDERSCORE); |
| 48 | +public abstract class MachineGameTest<Machine extends MachineBlockEntity> extends SimpleGameTest { |
| 49 | + public static final BlockPos MACHINE_POS = new BlockPos(1, 2, 1); |
58 | 50 |
|
59 | 51 | private final MachineType<Machine, ?> type; |
60 | 52 |
|
61 | 53 | protected MachineGameTest(MachineType<Machine, ?> type) { |
62 | 54 | this.type = type; |
63 | 55 | } |
64 | 56 |
|
65 | | - @Override |
66 | | - public void invokeTestMethod(GameTestHelper context, Method method) { |
67 | | - if (method.getAnnotation(GameTest.class) != null) FabricGameTest.super.invokeTestMethod(context, method); |
68 | | - } |
69 | | - |
| 57 | + @GameTestGenerator |
70 | 58 | @MustBeInvokedByOverriders |
71 | | - public @NotNull List<TestFunction> generateTests() { |
72 | | - List<TestFunction> tests = new ArrayList<>(); |
| 59 | + public @NotNull List<TestFunction> registerTests() { |
| 60 | + List<TestFunction> tests = super.registerTests(); |
73 | 61 |
|
74 | | - tests.add(new TestFunction(this.getBaseId(), this.getBaseId() + "/create_machine", EMPTY_STRUCTURE, Rotation.NONE, 1, 1, true, 1, 1, helper -> { |
| 62 | + tests.add(this.createTest("place", STRUCTURE_3x3, 1, 1, helper -> { |
75 | 63 | if (this.createMachine(helper) == null) { |
76 | 64 | throw new GameTestAssertException("No machine associated with block!"); |
77 | 65 | } |
78 | 66 | helper.succeed(); |
79 | 67 | })); |
80 | 68 |
|
81 | | - Class<? extends MachineGameTest<Machine>> clazz = (Class<? extends MachineGameTest<Machine>>) this.getClass(); |
82 | | - DefaultedMetadata meta = clazz.getAnnotation(DefaultedMetadata.class); |
83 | | - String structure = meta != null ? meta.structure() : EMPTY_STRUCTURE; |
84 | | - for (Method method : clazz.getMethods()) { |
85 | | - ProcessingTest processingTest = method.getAnnotation(ProcessingTest.class); |
86 | | - if (processingTest != null) { |
87 | | - String subId = ""; |
88 | | - if (!processingTest.group().isBlank()) { |
89 | | - subId = "/" + processingTest.group(); |
90 | | - } else { |
91 | | - if (meta != null && !meta.group().isBlank()) { |
92 | | - subId = "/" + meta.group(); |
93 | | - } |
94 | | - } |
95 | | - tests.add(new TestFunction(this.getBaseId() + subId, this.getBaseId() + '/' + NAME_CONVERSION.apply(method.getName()), structure, Rotation.NONE, processingTest.workTime() + 1, 1, true, 1, 1, helper -> { |
| 69 | + for (Method method : this.getClass().getMethods()) { |
| 70 | + MachineTest machineTest = method.getAnnotation(MachineTest.class); |
| 71 | + if (machineTest != null) { |
| 72 | + tests.add(this.createTest(machineTest.batch(), machineTest.group(), method.getName(), machineTest.structure(), machineTest.workTime(), machineTest.setupTime(), helper -> { |
96 | 73 | Machine machine = this.createMachine(helper); |
97 | | - if (processingTest.requiresEnergy()) machine.energyStorage().setEnergy(machine.energyStorage().getCapacity()); |
98 | 74 |
|
99 | | - try { |
100 | | - Runnable runnable = (Runnable) method.invoke(this, machine); |
101 | | - helper.runAfterDelay(processingTest.workTime() + 1, () -> { |
| 75 | + Runnable runnable = this.invokeTestMethod(method, machine, helper); |
| 76 | + if (runnable != null) { |
| 77 | + helper.runAfterDelay(machineTest.workTime(), () -> { |
102 | 78 | runnable.run(); |
103 | 79 | helper.succeed(); |
104 | 80 | }); |
105 | | - } catch (IllegalAccessException e) { |
106 | | - throw new RuntimeException("Failed to invoke test method!", e); |
107 | | - } catch (InvocationTargetException e) { |
108 | | - handleException(e); |
109 | 81 | } |
110 | 82 | })); |
111 | | - } else { |
112 | | - InstantTest instantTest = method.getAnnotation(InstantTest.class); |
113 | | - if (instantTest != null) { |
114 | | - String subId = ""; |
115 | | - if (!instantTest.group().isBlank()) { |
116 | | - subId = "/" + instantTest.group(); |
117 | | - } else { |
118 | | - if (meta != null && !meta.group().isBlank()) { |
119 | | - subId = "/" + meta.group(); |
120 | | - } |
121 | | - } |
122 | | - tests.add(new TestFunction(this.getBaseId() + subId, this.getBaseId() + '/' + NAME_CONVERSION.apply(method.getName()), EMPTY_STRUCTURE, Rotation.NONE, 1, 1, true, 1, 1, helper -> { |
123 | | - Machine machine = this.createMachine(helper); |
124 | | - if (instantTest.requiresEnergy()) |
125 | | - machine.energyStorage().setEnergy(machine.energyStorage().getCapacity()); |
126 | | - try { |
127 | | - Runnable runnable = (Runnable) method.invoke(this, machine); |
128 | | - if (runnable == null) { |
129 | | - helper.succeed(); |
130 | | - return; |
131 | | - } |
132 | | - |
133 | | - helper.runAfterDelay(1, () -> { |
134 | | - runnable.run(); |
135 | | - helper.succeed(); |
136 | | - }); |
137 | | - } catch (IllegalAccessException e) { |
138 | | - throw new RuntimeException("Failed to invoke test method!", e); |
139 | | - } catch (InvocationTargetException e) { |
140 | | - handleException(e); |
141 | | - } |
142 | | - })); |
143 | | - } |
144 | 83 | } |
145 | 84 | } |
146 | 85 |
|
147 | 86 | return tests; |
148 | 87 | } |
149 | 88 |
|
150 | | - public @NotNull TestFunction createChargeFromEnergyItemTest(int slot, Item infiniteBattery) { |
151 | | - return new TestFunction(this.getBaseId(), this.getBaseId() + "/charge_from_item", EMPTY_STRUCTURE, Rotation.NONE, 2, 1, true, 1, 1, helper -> { |
| 89 | + public TestFunction createChargeFromEnergyItemTest(int slot, Item infiniteBattery) { |
| 90 | + return this.createTest("chargeFromItem", STRUCTURE_3x3, 2, 1, helper -> { |
152 | 91 | Machine machine = this.createMachine(helper); |
153 | 92 | machine.itemStorage().getSlot(slot).set(infiniteBattery, 1); |
154 | 93 | helper.runAfterDelay(1, () -> { |
155 | 94 | if (machine.energyStorage().isEmpty()) { |
156 | | - helper.fail("Machine did not charge from the stack!", BlockPos.ZERO); |
| 95 | + helper.fail("Machine did not charge from the stack!", machine.getBlockPos()); |
| 96 | + } else { |
| 97 | + helper.succeed(); |
157 | 98 | } |
158 | | - helper.succeed(); |
159 | 99 | }); |
160 | 100 | }); |
161 | 101 | } |
162 | 102 |
|
163 | | - public @NotNull TestFunction createDrainToEnergyItemTest(int slot, Item battery) { |
164 | | - return new TestFunction(this.getBaseId(), this.getBaseId() + "/drain_to_item", EMPTY_STRUCTURE, Rotation.NONE, 2, 1, true, 1, 1, helper -> { |
| 103 | + public TestFunction createDrainToEnergyItemTest(int slot, Item battery) { |
| 104 | + return this.createTest("drainToItem", STRUCTURE_3x3, 2, 1, helper -> { |
165 | 105 | Machine machine = this.createMachine(helper); |
166 | | - machine.energyStorage().setEnergy(machine.energyStorage().getCapacity()); |
167 | 106 |
|
| 107 | + machine.energyStorage().setEnergy(machine.energyStorage().getCapacity()); |
168 | 108 | machine.itemStorage().getSlot(slot).set(battery, 1); |
169 | 109 |
|
170 | 110 | helper.runAfterDelay(1, () -> { |
171 | 111 | if (machine.energyStorage().isFull()) { |
172 | 112 | helper.fail("Machine did not drain energy to the stack!", BlockPos.ZERO); |
| 113 | + } else { |
| 114 | + helper.succeed(); |
173 | 115 | } |
174 | | - helper.succeed(); |
175 | 116 | }); |
176 | 117 | }); |
177 | 118 | } |
178 | 119 |
|
179 | | - public static void handleException(@NotNull InvocationTargetException e) { |
180 | | - if (e.getCause() instanceof GameTestAssertException ex) { |
181 | | - throw ex; |
182 | | - } else if (e.getCause() instanceof RuntimeException ex){ |
183 | | - throw ex; |
184 | | - } else if (e.getCause() != null) { |
185 | | - throw new RuntimeException(e.getCause()); |
186 | | - } else { |
187 | | - throw new RuntimeException(e); |
188 | | - } |
189 | | - } |
190 | | - |
191 | | - public @NotNull String getBaseId() { |
192 | | - return BuiltInRegistries.BLOCK.getKey(this.type.getBlock()).toString().replace(":", "."); |
193 | | - } |
194 | | - |
195 | 120 | protected Machine createMachine(GameTestHelper helper) { |
196 | | - helper.setBlock(BlockPos.ZERO, this.type.getBlock()); |
197 | | - return (Machine) helper.getBlockEntity(BlockPos.ZERO); |
| 121 | + helper.setBlock(MACHINE_POS, this.type.getBlock()); |
| 122 | + return (Machine) helper.getBlockEntity(MACHINE_POS); |
198 | 123 | } |
199 | 124 | } |
0 commit comments