Skip to content

Commit 364c108

Browse files
committed
chore: improve test coverage
add (de)serialization tests improve documentation of `set` on slots fix machine slot calculation in menus
1 parent ef807aa commit 364c108

24 files changed

+1260
-1093
lines changed

build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ tasks.withType<Jar> {
242242

243243
tasks.test {
244244
useJUnitPlatform()
245+
enableAssertions = true
245246
workingDir("run")
246247

247248
Files.createDirectories(workingDir.toPath())

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import net.minecraft.core.component.DataComponentPatch;
4242
import net.minecraft.core.component.DataComponents;
4343
import net.minecraft.core.registries.BuiltInRegistries;
44-
import net.minecraft.nbt.ByteTag;
4544
import net.minecraft.nbt.CompoundTag;
4645
import net.minecraft.nbt.NbtOps;
4746
import net.minecraft.nbt.Tag;
@@ -252,7 +251,7 @@ public void appendHoverText(ItemStack stack, TooltipContext context, List<Compon
252251
}
253252

254253
if (nbt.contains(Constant.Nbt.REDSTONE_MODE, Tag.TAG_BYTE)) {
255-
tooltip.add(Component.translatable(Constant.TranslationKey.REDSTONE_MODE, RedstoneMode.readTag((ByteTag) Objects.requireNonNull(nbt.get(Constant.Nbt.REDSTONE_MODE))).getName()).setStyle(Constant.Text.DARK_RED_STYLE));
254+
tooltip.add(Component.translatable(Constant.TranslationKey.REDSTONE_MODE, RedstoneMode.readTag(Objects.requireNonNull(nbt.get(Constant.Nbt.REDSTONE_MODE))).getName()).setStyle(Constant.Text.DARK_RED_STYLE));
256255
}
257256
}
258257
}
@@ -277,7 +276,7 @@ public void appendHoverText(ItemStack stack, TooltipContext context, List<Compon
277276
}
278277

279278
@Override
280-
public BlockState playerWillDestroy(Level level, BlockPos pos, BlockState state, Player player) {
279+
public @NotNull BlockState playerWillDestroy(Level level, BlockPos pos, BlockState state, Player player) {
281280
super.playerWillDestroy(level, pos, state, player);
282281
BlockEntity entity = level.getBlockEntity(pos);
283282
if (entity instanceof MachineBlockEntity machine) {

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

-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public interface ExposedStorage<Resource, Variant extends TransferVariant<Resour
8080
return new ExposedStorageImpl<>(storage, slots);
8181
}
8282

83-
8483
@Override
8584
long getVersion();
8685
}

src/main/java/dev/galacticraft/machinelib/api/menu/MachineMenu.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public MachineMenu(int syncId, @NotNull ServerPlayer player, @NotNull Machine ma
217217

218218
this.levelAccess = ContainerLevelAccess.create(machine.getLevel(), machine.getBlockPos());
219219

220-
this.machineSlots = new StorageSlot[this.itemStorage.size()];
220+
StorageSlot[] slots = new StorageSlot[this.itemStorage.size()];
221221

222222
int index = 0;
223223
for (ItemResourceSlot slot : this.itemStorage) {
@@ -230,10 +230,13 @@ public MachineMenu(int syncId, @NotNull ServerPlayer player, @NotNull Machine ma
230230
slot1 = new StorageSlot(this.itemStorage, slot, display, index, this.playerUUID);
231231
}
232232
this.addSlot(slot1);
233-
this.machineSlots[index++] = slot1;
233+
slots[index++] = slot1;
234234
}
235235
}
236236

237+
this.machineSlots = new StorageSlot[index];
238+
System.arraycopy(slots, 0, this.machineSlots, 0, index);
239+
237240
index = 0;
238241
for (FluidResourceSlot slot : this.fluidStorage) {
239242
if (!slot.isHidden()) {

src/main/java/dev/galacticraft/machinelib/api/storage/slot/ItemResourceSlot.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
import org.jetbrains.annotations.NotNull;
3838
import org.jetbrains.annotations.Nullable;
3939

40+
/**
41+
* A resource slot that stores items.
42+
*/
4043
public interface ItemResourceSlot extends ResourceSlot<Item>, ContainerItemContext, FakeRecipeHolder {
4144
@Contract("_ -> new")
4245
static @NotNull Builder builder(InputType inputType) {
@@ -69,7 +72,9 @@ public interface ItemResourceSlot extends ResourceSlot<Item>, ContainerItemConte
6972
* @param resource the item type to consume
7073
* @return {@code true} if the item was consumed, {@code false} otherwise
7174
*/
72-
boolean consumeOne(@NotNull Item resource);
75+
default boolean consumeOne(@NotNull Item resource) {
76+
return this.consumeOne(resource, null);
77+
}
7378

7479
/**
7580
* Consumes one item of the specified type and components from the slot.
@@ -98,7 +103,9 @@ public interface ItemResourceSlot extends ResourceSlot<Item>, ContainerItemConte
98103
* @param amount the number of items to consume
99104
* @return the number of items that were actually consumed
100105
*/
101-
long consume(@NotNull Item resource, long amount);
106+
default long consume(@NotNull Item resource, long amount) {
107+
return this.consume(resource, null, amount);
108+
}
102109

103110
/**
104111
* Consumes the specified number of items of the specified type and components from the slot.
@@ -109,7 +116,7 @@ public interface ItemResourceSlot extends ResourceSlot<Item>, ContainerItemConte
109116
* @param amount the number of items to consume
110117
* @return the number of items that were actually consumed
111118
*/
112-
long consume(@NotNull Item resource, @NotNull DataComponentPatch components, long amount);
119+
long consume(@NotNull Item resource, @Nullable DataComponentPatch components, long amount);
113120

114121
/**
115122
* {@return the display properties of this slot, or {@code null} if hidden}

src/main/java/dev/galacticraft/machinelib/api/storage/slot/ResourceSlot.java

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.jetbrains.annotations.ApiStatus;
3535
import org.jetbrains.annotations.NotNull;
3636
import org.jetbrains.annotations.Nullable;
37+
import org.jetbrains.annotations.VisibleForTesting;
3738

3839
/**
3940
* A slot that can store multiple of a single instance of resource (e.g., sticks).
@@ -126,22 +127,26 @@ public interface ResourceSlot<Resource> extends StorageAccess<Resource>, Mutable
126127

127128
/**
128129
* Sets the resource stored in this slot to the given resource and amount.
130+
* For synchronization and testing purposes only.
129131
* Does not increment the modification counter.
130132
* Equivalent to {@link #set(Object, DataComponentPatch, long)} but assumes an empty component patch.
131133
*
132134
* @param resource The resource to set.
133135
* @param amount The amount of the resource to set.
134136
*/
137+
@VisibleForTesting
135138
void set(@Nullable Resource resource, long amount);
136139

137140
/**
138141
* Sets the resource stored in this slot to the given resource, components, and amount.
142+
* For synchronization and testing purposes only.
139143
* Does not increment the modification counter.
140144
*
141145
* @param resource The resource to set.
142146
* @param components The components of the resource to set.
143147
* @param amount The amount of the resource to set.
144148
*/
149+
@VisibleForTesting
145150
void set(@Nullable Resource resource, @NotNull DataComponentPatch components, long amount);
146151

147152
/**

src/main/java/dev/galacticraft/machinelib/impl/storage/MachineEnergyStorageImpl.java

+1-21
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ protected void readSnapshot(Long snapshot) {
6363
this.amount = snapshot;
6464
}
6565

66-
@Override
67-
public boolean supportsInsertion() {
68-
return true;
69-
}
70-
7166
@Override
7267
public boolean canExtract(long amount) {
7368
return this.amount >= amount;
@@ -144,11 +139,6 @@ public long insert(long amount, @NotNull TransactionContext transaction) {
144139
return 0;
145140
}
146141

147-
@Override
148-
public boolean supportsExtraction() {
149-
return this.maxOutput > 0;
150-
}
151-
152142
@Override
153143
public long extract(long amount, @NotNull TransactionContext transaction) {
154144
long extracted = this.tryExtract(amount);
@@ -211,7 +201,7 @@ public void setEnergy(long amount) {
211201
case BOTH -> {
212202
if (this.insert) {
213203
if (this.extract) {
214-
return this;
204+
return ExposedEnergyStorage.create(this, this.maxInput, this.maxOutput);
215205
} else {
216206
return ExposedEnergyStorage.create(this, this.maxInput, 0);
217207
}
@@ -274,16 +264,6 @@ private void markModified() {
274264
if (this.listener != null) this.listener.run();
275265
}
276266

277-
@Override
278-
public void readDeltaPacket(@NotNull ByteBuf buf) {
279-
this.amount = buf.readLong();
280-
}
281-
282-
@Override
283-
public void writeDeltaPacket(@NotNull ByteBuf buf, long[] previous) {
284-
buf.writeLong(this.amount);
285-
}
286-
287267
@Override
288268
public boolean hasChanged(long[] previous) {
289269
return previous[0] != this.amount;

src/main/java/dev/galacticraft/machinelib/impl/storage/slot/ItemResourceSlotImpl.java

-21
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,6 @@ public long getCapacityFor(@NotNull Item item, @NotNull DataComponentPatch compo
8989
return resource;
9090
}
9191

92-
@Override
93-
public boolean consumeOne(@NotNull Item resource) {
94-
DataComponentPatch tag = this.components;
95-
if (this.extractOne(resource)) {
96-
this.insertRemainder(resource, tag, 1);
97-
return true;
98-
} else {
99-
return false;
100-
}
101-
}
102-
10392
@Override
10493
public boolean consumeOne(@NotNull Item resource, @Nullable DataComponentPatch components) {
10594
DataComponentPatch actual = this.components;
@@ -124,16 +113,6 @@ public long consume(long amount) {
124113
return consumed;
125114
}
126115

127-
@Override
128-
public long consume(@NotNull Item resource, long amount) {
129-
DataComponentPatch components = this.components;
130-
long consumed = this.extract(resource, amount);
131-
if (consumed > 0) {
132-
this.insertRemainder(resource, components, (int) consumed);
133-
}
134-
return consumed;
135-
}
136-
137116
@Override
138117
public long consume(@NotNull Item resource, @Nullable DataComponentPatch components, long amount) {
139118
DataComponentPatch actual = this.components;

0 commit comments

Comments
 (0)