Skip to content

Commit 2b305cd

Browse files
committed
feat: improve gametest api
revert io config rename make IngredientSupplier purpose less ambiguous
1 parent 364c108 commit 2b305cd

File tree

22 files changed

+213
-73
lines changed

22 files changed

+213
-73
lines changed

src/main/java/dev/galacticraft/machinelib/api/block/MachineBlock.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public void appendHoverText(ItemStack stack, TooltipContext context, List<Compon
312312
BlockEntity blockEntity = reader.getBlockEntity(pos);
313313
if (blockEntity instanceof MachineBlockEntity machine) {
314314
CompoundTag config = new CompoundTag();
315-
config.put(Constant.Nbt.CONFIGURATION, machine.getIoConfig().createTag());
315+
config.put(Constant.Nbt.CONFIGURATION, machine.getIOConfig().createTag());
316316
config.put(Constant.Nbt.SECURITY, machine.getSecurity().createTag());
317317
config.put(Constant.Nbt.REDSTONE_MODE, machine.getRedstoneMode().createTag());
318318
BlockItem.setBlockEntityData(stack, blockEntity.getType(), config);

src/main/java/dev/galacticraft/machinelib/api/block/entity/MachineBlockEntity.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public abstract class MachineBlockEntity extends BlockEntity implements Extended
114114
*/
115115
private final MachineType<? extends MachineBlockEntity, ? extends MachineMenu<? extends MachineBlockEntity>> type;
116116

117-
private final IoConfig configuration;
117+
private final IOConfig configuration;
118118
private final SecuritySettings security;
119119
private @NotNull RedstoneMode redstone = RedstoneMode.IGNORE;
120120

@@ -214,11 +214,11 @@ protected MachineBlockEntity(@NotNull MachineType<? extends MachineBlockEntity,
214214
this.type = type;
215215
this.name = name;
216216

217-
IoFace[] faces = new IoFace[6];
217+
IOFace[] faces = new IOFace[6];
218218
for (int i = 0; i < faces.length; i++) {
219-
faces[i] = new InternalIoFace(i);
219+
faces[i] = new InternalIOFace(i);
220220
}
221-
this.configuration = new IoConfig(faces);
221+
this.configuration = new IOConfig(faces);
222222
this.security = new InternalSecuritySettings();
223223

224224
this.state = new InternalMachineState();
@@ -347,7 +347,7 @@ public void setRedstoneMode(@NotNull RedstoneMode redstone) {
347347
* {@return the IO configuration of this machine}
348348
*/
349349
@Contract(pure = true)
350-
public final @NotNull IoConfig getIoConfig() {
350+
public final @NotNull IOConfig getIOConfig() {
351351
return this.configuration;
352352
}
353353

@@ -499,7 +499,7 @@ public boolean isActive() {
499499
@ApiStatus.Internal
500500
private @Nullable EnergyStorage getExposedEnergyStorage(@Nullable BlockFace face) {
501501
if (face == null) return this.energyStorage;
502-
return this.getIoConfig().get(face).getExposedEnergyStorage(this.energyStorage);
502+
return this.getIOConfig().get(face).getExposedEnergyStorage(this.energyStorage);
503503
}
504504

505505
/**
@@ -533,7 +533,7 @@ public boolean isActive() {
533533
@ApiStatus.Internal
534534
private @Nullable ExposedStorage<Item, ItemVariant> getExposedItemStorage(@Nullable BlockFace face) {
535535
if (face == null) return this.createExposedItemStorage(ResourceFlow.BOTH);
536-
return this.getIoConfig().get(face).getExposedItemStorage(this::createExposedItemStorage);
536+
return this.getIOConfig().get(face).getExposedItemStorage(this::createExposedItemStorage);
537537
}
538538

539539
/**
@@ -576,7 +576,7 @@ protected ExposedStorage<Item, ItemVariant> createExposedItemStorage(ResourceFlo
576576
@ApiStatus.Internal
577577
private @Nullable ExposedStorage<Fluid, FluidVariant> getExposedFluidStorage(@Nullable BlockFace face) {
578578
if (face == null) return this.createExposedFluidStorage(ResourceFlow.BOTH);
579-
return this.getIoConfig().get(face).getExposedFluidStorage(this::createExposedFluidStorage);
579+
return this.getIOConfig().get(face).getExposedFluidStorage(this::createExposedFluidStorage);
580580
}
581581

582582
/**
@@ -884,13 +884,13 @@ public void setAccessLevel(@NotNull AccessLevel accessLevel) {
884884
/**
885885
* Subclass that tracks modifications to save and re-render the block entity.
886886
*/
887-
private class InternalIoFace extends IoFace {
887+
private class InternalIOFace extends IOFace {
888888
private final BlockFace face;
889889
private @Nullable ExposedStorage<Item, ItemVariant> cachedItemStorage = null;
890890
private @Nullable ExposedStorage<Fluid, FluidVariant> cachedFluidStorage = null;
891891
private @Nullable EnergyStorage cachedEnergyStorage = null;
892892

893-
public InternalIoFace(int i) {
893+
public InternalIOFace(int i) {
894894
super(ResourceType.NONE, ResourceFlow.BOTH);
895895
this.face = BlockFace.values()[i];
896896
}

src/main/java/dev/galacticraft/machinelib/api/compat/transfer/ExposedStorage.java

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
/**
3939
* Represents a resource storage exposed to adjacent blocks or items.
40+
* Has additional restrictions on the flow of resources compared to {@link ResourceStorage}s.
4041
*
4142
* @param <Resource> the type of resource stored in the storage
4243
* @param <Variant> the type of variant associated with the resource

src/main/java/dev/galacticraft/machinelib/api/gametest/MachineGameTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ public TestFunction createDrainFluidIntoItemTest(int slot, Fluid fluid, int tank
160160
});
161161
}
162162

163+
/**
164+
* Creates a machine at the {@link #MACHINE_POS} position.
165+
* @param helper the game test helper
166+
* @return the newly created machine
167+
*/
163168
protected Machine createMachine(GameTestHelper helper) {
164169
helper.setBlock(MACHINE_POS, this.type.getBlock());
165170
return (Machine) helper.getBlockEntity(MACHINE_POS);

src/main/java/dev/galacticraft/machinelib/api/gametest/MachineTestContext.java

+18
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import java.util.ArrayList;
3131
import java.util.List;
3232

33+
/**
34+
* Wrapper around {@link GameTestHelper} with additional methods for testing machines.
35+
*/
3336
public class MachineTestContext {
3437
private final BlockPos pos;
3538
private final GameTestHelper helper;
@@ -40,6 +43,9 @@ public MachineTestContext(BlockPos pos, GameTestHelper helper) {
4043
this.helper = helper;
4144
}
4245

46+
/**
47+
* {@return the game test helper}
48+
*/
4349
public GameTestHelper helper() {
4450
return this.helper;
4551
}
@@ -54,10 +60,22 @@ public void assertTrue(boolean b, String message) {
5460
}
5561
}
5662

63+
/**
64+
* Fails the test with the given message.
65+
* @param message the message to fail with
66+
* @param objects the objects to format the message with
67+
* @return a new {@link GameTestAssertPosException} with the given message
68+
*/
5769
public GameTestAssertPosException fail(String message, Object... objects) {
5870
return new GameTestAssertPosException(new MessageFormat(message).format(objects), this.helper.absolutePos(this.pos), this.pos, this.helper.getTick());
5971
}
6072

73+
/**
74+
* Fails the test with the given message.
75+
*
76+
* @param message the message to fail with
77+
* @return a new {@link GameTestAssertPosException} with the given message
78+
*/
6179
public GameTestAssertPosException fail(String message) {
6280
return new GameTestAssertPosException(message, this.helper.absolutePos(this.pos), this.pos, this.helper.getTick());
6381
}

src/main/java/dev/galacticraft/machinelib/api/gametest/RecipeGameTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected void fillOutputSlots(@NotNull MachineItemStorage storage) {
8484

8585
protected void fulfillRunConditions(Machine machine) {
8686
for (IngredientSupplier<C, R, Machine> condition : this.conditions) {
87-
condition.fulfillRunRequirements(machine);
87+
condition.supplyIngredient(machine);
8888
}
8989
}
9090

@@ -129,7 +129,7 @@ protected void tryCraftPartial(GameTestHelper helper, int ignored) {
129129

130130
for (int i = 0; i < this.conditions.size(); i++) {
131131
if (i == ignored) continue;
132-
this.conditions.get(i).fulfillRunRequirements(machine);
132+
this.conditions.get(i).supplyIngredient(machine);
133133
}
134134

135135
helper.runAfterDelay(this.recipeRuntime, () -> {

src/main/java/dev/galacticraft/machinelib/api/gametest/SimpleGameTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ public abstract class SimpleGameTest implements FabricGameTest {
6161
tests.add(this.createTest(basicTest.batch(), basicTest.group(), method.getName(), basicTest.structure(), basicTest.workTime(), basicTest.setupTime(), helper -> {
6262
Runnable runnable = GameTestUtils.invokeUnorderedArguments(this, method, helper);
6363
if (runnable == null) {
64-
if (basicTest.workTime() == 1) helper.succeed();
64+
if (basicTest.setupTime() == 1) helper.succeed();
6565
} else {
66-
helper.runAfterDelay(1, () -> {
66+
helper.runAfterDelay(basicTest.setupTime(), () -> {
6767
runnable.run();
68-
helper.succeed();
68+
if (basicTest.workTime() == 1) helper.succeed();
6969
});
7070
}
7171
}));

src/main/java/dev/galacticraft/machinelib/api/gametest/annotation/BasicTest.java

+39
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,59 @@
2323
package dev.galacticraft.machinelib.api.gametest.annotation;
2424

2525
import dev.galacticraft.machinelib.api.gametest.SimpleGameTest;
26+
import net.minecraft.gametest.framework.GameTest;
2627

2728
import java.lang.annotation.ElementType;
2829
import java.lang.annotation.Retention;
2930
import java.lang.annotation.RetentionPolicy;
3031
import java.lang.annotation.Target;
3132

33+
/**
34+
* Creates simple one-step game tests when used in conjunction with {@link SimpleGameTest} (and descendants).
35+
*
36+
* <p>{@code @BasicTest} annotated methods must not be {@code private} or {@code static}.
37+
*
38+
* <p>{@code @BasicTest} annotated methods can either take no arguments or a single {@link net.minecraft.gametest.framework.GameTestHelper} argument.
39+
*
40+
* <p>{@code @BasicTest} annotated methods can return void or a {@link Runnable}.
41+
*
42+
* <p>If a {@link Runnable} is returned, it will be run after the setup time has passed.
43+
* And the test will succeed if the runnable executes without failure and work time is 1.
44+
* Otherwise, if setup time is 1, the test will succeed immediately (assuming no failures raised).
45+
*
46+
* @see GameTest
47+
*/
3248
@Target(ElementType.METHOD)
3349
@Retention(RetentionPolicy.RUNTIME)
3450
public @interface BasicTest {
51+
/**
52+
* {@return the batch name of the test}
53+
* @see GameTest#batch()
54+
*/
3555
String batch();
56+
57+
/**
58+
* {@return the group name of the test}
59+
* Used for naming purposes only.
60+
*/
3661
String group() default "";
62+
63+
/**
64+
* {@return the structure to use for the test}
65+
* Defaults to an empty (all air) 3x3 structure.
66+
* @see GameTest#template()
67+
*/
3768
String structure() default SimpleGameTest.STRUCTURE_3x3;
3869

70+
/**
71+
* {@return the time to wait before executing the work runnable, or before succeeding if no work runnable is provided}
72+
* If the value is greater than one and no runnable is provided, the test must be succeeded manually.
73+
*/
3974
int setupTime() default 1;
4075

76+
/**
77+
* {@return the time to wait before succeeding after the work runnable}
78+
* If the value is greater than one, the test must be succeeded manually.
79+
*/
4180
int workTime() default 1;
4281
}

src/main/java/dev/galacticraft/machinelib/api/gametest/annotation/MachineTest.java

+35-3
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,60 @@
2222

2323
package dev.galacticraft.machinelib.api.gametest.annotation;
2424

25+
import dev.galacticraft.machinelib.api.gametest.MachineTestContext;
2526
import dev.galacticraft.machinelib.api.gametest.SimpleGameTest;
27+
import net.minecraft.gametest.framework.GameTest;
2628

2729
import java.lang.annotation.ElementType;
2830
import java.lang.annotation.Retention;
2931
import java.lang.annotation.RetentionPolicy;
3032
import java.lang.annotation.Target;
3133

3234
/**
33-
* Represents a test that has to wait a specified number of ticks for results.
34-
* Annotated methods should have initial setup in their body,
35-
* and return a {@link Runnable} that verifies state at the end.
35+
* <p>{@code @MachineTest} annotated methods should only be found in {@link dev.galacticraft.machinelib.api.gametest.MachineGameTest}s and descendants.
36+
*
37+
* <p>{@code @MachineTest} annotated methods must not be {@code private} or {@code static}.
38+
*
39+
* <p>{@code @MachineTest} annotated methods can take
40+
* a {@link net.minecraft.gametest.framework.GameTestHelper},
41+
* a {@link MachineTestContext},
42+
* OR a {@link dev.galacticraft.machinelib.api.block.entity.MachineBlockEntity} (and descendant classes).
43+
*
44+
* <p>{@code @MachineTest} annotated methods should return a {@link Runnable} that will be executed after {@code workTime} ticks have passed.
3645
*/
3746
@Target(ElementType.METHOD)
3847
@Retention(RetentionPolicy.RUNTIME)
3948
public @interface MachineTest {
49+
/**
50+
* {@return the batch name of the test}
51+
* @see GameTest#batch()
52+
*/
4053
String batch() default "";
54+
55+
/**
56+
* {@return the group name of the test}
57+
* Used for test naming only.
58+
*/
4159
String group() default "";
60+
61+
/**
62+
* {@return the structure file to use for the test}
63+
* @see GameTest#template()
64+
*/
4265
String structure() default SimpleGameTest.STRUCTURE_3x3;
4366

67+
/**
68+
* {@return the setup time of the test}
69+
*/
4470
int setupTime() default 1;
4571

72+
/**
73+
* {@return the work time of the test}
74+
*/
4675
int workTime() default 1;
4776

77+
/**
78+
* {@return whether to capture {@link net.minecraft.gametest.framework.GameTestAssertException}s and unwrap them.
79+
*/
4880
boolean captureAssertions() default true;
4981
}

src/main/java/dev/galacticraft/machinelib/api/gametest/annotation/TestSuite.java

+7
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,15 @@
2727
import java.lang.annotation.RetentionPolicy;
2828
import java.lang.annotation.Target;
2929

30+
/**
31+
* Applies default values to all test methods in the class.
32+
*/
3033
@Target(ElementType.TYPE)
3134
@Retention(RetentionPolicy.RUNTIME)
3235
public @interface TestSuite {
36+
/**
37+
* {@return the name of the test suite}
38+
* Sets the default group name for all test methods in the class.
39+
*/
3340
String value(); // name
3441
}

src/main/java/dev/galacticraft/machinelib/api/gametest/recipe/IngredientSupplier.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,18 @@
2626
import net.minecraft.world.Container;
2727
import net.minecraft.world.item.crafting.Recipe;
2828

29+
/**
30+
* A functional interface for fulfilling the run requirements of a machine.
31+
* @param <C> the type of container
32+
* @param <R> the type of recipe
33+
* @param <Machine> the type of machine
34+
*/
2935
@FunctionalInterface
3036
public interface IngredientSupplier<C extends Container, R extends Recipe<C>, Machine extends RecipeMachineBlockEntity<C, R>> {
31-
void fulfillRunRequirements(Machine machine);
37+
/**
38+
* Fulfills a single run requirement of a machine (energy, item, fluid, etc.).
39+
* It is best to split requirements to test as many edge cases as possible.
40+
* @param machine the machine to supply an ingredient to
41+
*/
42+
void supplyIngredient(Machine machine);
3243
}

src/main/java/dev/galacticraft/machinelib/api/machine/MachineRenderData.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,15 @@
2222

2323
package dev.galacticraft.machinelib.api.machine;
2424

25-
import dev.galacticraft.machinelib.api.machine.configuration.IoConfig;
25+
import dev.galacticraft.machinelib.api.machine.configuration.IOConfig;
2626

27+
/**
28+
* The data required to render a machine on the client.
29+
* By default, no data is synced to the client, so this should be manually synchronized if more data is needed.
30+
*/
2731
public interface MachineRenderData {
28-
IoConfig getIoConfig();
32+
/**
33+
* {@return the I/O configuration of the machine}
34+
*/
35+
IOConfig getIOConfig();
2936
}

0 commit comments

Comments
 (0)