Skip to content

Commit 07af9e9

Browse files
committed
feat: utilize menu data to do the initial machine sync
remove slot/range item storage methods
1 parent a37561c commit 07af9e9

38 files changed

+297
-368
lines changed

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ plugins {
4747
id("fabric-loom") version("1.7-SNAPSHOT")
4848
id("org.cadixdev.licenser") version("0.6.1")
4949
id("org.ajoberstar.grgit") version("5.2.2")
50-
id("dev.galacticraft.mojarn") version("0.3.0+8")
50+
id("dev.galacticraft.mojarn") version("0.4.0+9")
5151
}
5252

5353
group = "dev.galacticraft"

settings.gradle.kts

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
pluginManagement {
22
repositories {
3+
mavenLocal {
4+
content {
5+
includeGroup("dev.galacticraft")
6+
includeGroup("dev.galacticraft.mojarn")
7+
}
8+
}
39
maven("https://maven.fabricmc.net/") {
410
name = "Fabric"
511
content {

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

+8-16
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,6 @@ public boolean areDropsDisabled() {
366366
return this.disableDrops;
367367
}
368368

369-
370369
/**
371370
* {@return whether the current machine is enabled}
372371
* @see RedstoneMode
@@ -475,7 +474,7 @@ public boolean isActive() {
475474
*/
476475
@ApiStatus.Internal
477476
private @Nullable EnergyStorage getExposedEnergyStorage(@Nullable BlockFace face) {
478-
if (face == null) return this.energyStorage.getExposedStorage(ResourceFlow.BOTH);
477+
if (face == null) return this.energyStorage.createExposedStorage(ResourceFlow.BOTH);
479478
return this.configuration.get(face).getExposedEnergyStorage(this.energyStorage);
480479
}
481480

@@ -529,14 +528,14 @@ protected void saveAdditional(CompoundTag tag, HolderLookup.Provider lookup) {
529528
@Override
530529
public void loadAdditional(CompoundTag tag, HolderLookup.Provider lookup) {
531530
super.loadAdditional(tag, lookup);
532-
if (tag.contains(Constant.Nbt.CONFIGURATION, Tag.TAG_COMPOUND))
533-
this.configuration.readTag(tag.getList(Constant.Nbt.CONFIGURATION, Tag.TAG_COMPOUND));
531+
if (tag.contains(Constant.Nbt.CONFIGURATION, Tag.TAG_LIST))
532+
this.configuration.readTag(tag.getList(Constant.Nbt.CONFIGURATION, Tag.TAG_BYTE));
534533
if (tag.contains(Constant.Nbt.SECURITY, Tag.TAG_COMPOUND))
535534
this.security.readTag(tag.getCompound(Constant.Nbt.SECURITY));
536535
if (tag.contains(Constant.Nbt.REDSTONE_MODE, Tag.TAG_BYTE))
537536
this.redstone = RedstoneMode.readTag(tag.get(Constant.Nbt.REDSTONE_MODE));
538-
if (tag.contains(Constant.Nbt.STATE, Tag.TAG_COMPOUND))
539-
this.state.readTag(tag.getCompound(Constant.Nbt.STATE));
537+
if (tag.contains(Constant.Nbt.STATE, Tag.TAG_BYTE))
538+
this.state.readTag((ByteTag) tag.get(Constant.Nbt.STATE));
540539
if (tag.contains(Constant.Nbt.ENERGY_STORAGE, Tag.TAG_LONG))
541540
this.energyStorage.readTag(Objects.requireNonNull(((LongTag) tag.get(Constant.Nbt.ENERGY_STORAGE))));
542541
if (tag.contains(Constant.Nbt.ITEM_STORAGE, Tag.TAG_LIST))
@@ -546,7 +545,7 @@ public void loadAdditional(CompoundTag tag, HolderLookup.Provider lookup) {
546545
this.disableDrops = tag.getBoolean(Constant.Nbt.DISABLE_DROPS);
547546

548547
if (this.level != null && this.level.isClientSide()) {
549-
this.level.sendBlockUpdated(worldPosition, Blocks.AIR.defaultBlockState(), this.getBlockState(), Block.UPDATE_IMMEDIATE);
548+
this.level.sendBlockUpdated(this.worldPosition, Blocks.AIR.defaultBlockState(), this.getBlockState(), Block.UPDATE_IMMEDIATE);
550549
}
551550
}
552551

@@ -694,14 +693,7 @@ public RegistryFriendlyByteBuf getScreenOpeningData(ServerPlayer player) {
694693
}
695694

696695
buf.writeBlockPos(this.getBlockPos());
697-
this.configuration.writePacket(buf);
698-
this.security.writePacket(buf);
699-
this.redstone.writePacket(buf);
700-
this.state.writePacket(buf);
701-
this.energyStorage.writePacket(buf);
702-
this.itemStorage.writePacket(buf);
703-
this.fluidStorage.writePacket(buf);
704-
696+
((MachineMenu<?>) player.containerMenu).getData().synchronizeInitial(buf);
705697
return buf;
706698
}
707699

@@ -871,7 +863,7 @@ public void setOption(@NotNull ResourceType type, @NotNull ResourceFlow flow) {
871863
public @Nullable EnergyStorage getExposedEnergyStorage(@NotNull MachineEnergyStorage storage) {
872864
if (this.type.willAcceptResource(ResourceType.ENERGY)) {
873865
if (this.cachedEnergyStorage == null) {
874-
this.cachedEnergyStorage = storage.getExposedStorage(this.flow);
866+
this.cachedEnergyStorage = storage.createExposedStorage(this.flow);
875867
}
876868
return this.cachedEnergyStorage;
877869
}

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

-15
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
import net.minecraft.core.BlockPos;
3131
import net.minecraft.core.HolderLookup;
3232
import net.minecraft.nbt.CompoundTag;
33-
import net.minecraft.network.RegistryFriendlyByteBuf;
3433
import net.minecraft.server.level.ServerLevel;
35-
import net.minecraft.server.level.ServerPlayer;
3634
import net.minecraft.util.profiling.ProfilerFiller;
3735
import net.minecraft.world.Container;
3836
import net.minecraft.world.item.crafting.Recipe;
@@ -251,19 +249,6 @@ protected void craft(@NotNull ProfilerFiller profiler, @NotNull RecipeHolder<R>
251249
return level.getRecipeManager().getRecipeFor(this.getRecipeType(), this.craftingInv(), level).orElse(null);
252250
}
253251

254-
@Override
255-
public RegistryFriendlyByteBuf getScreenOpeningData(ServerPlayer player) {
256-
RegistryFriendlyByteBuf buf = super.getScreenOpeningData(player);
257-
if (this.activeRecipe != null) {
258-
buf.writeInt(this.getProcessingTime(this.activeRecipe));
259-
buf.writeInt(this.progress);
260-
} else {
261-
buf.writeInt(0);
262-
}
263-
264-
return buf;
265-
}
266-
267252
/**
268253
* {@return the processing time of the given recipe}
269254
*

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

+14-11
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import dev.galacticraft.machinelib.api.misc.Serializable;
2828
import dev.galacticraft.machinelib.impl.Constant;
2929
import net.minecraft.ChatFormatting;
30-
import net.minecraft.nbt.CompoundTag;
30+
import net.minecraft.nbt.ByteTag;
3131
import net.minecraft.network.RegistryFriendlyByteBuf;
3232
import net.minecraft.network.chat.Component;
3333
import org.jetbrains.annotations.ApiStatus;
@@ -39,7 +39,7 @@
3939
/**
4040
* Stores the state of a machine.
4141
*/
42-
public class MachineState implements Serializable<CompoundTag>, DeltaPacketSerializable<RegistryFriendlyByteBuf, MachineState> {
42+
public class MachineState implements Serializable<ByteTag>, DeltaPacketSerializable<RegistryFriendlyByteBuf, MachineState> {
4343
/**
4444
* The current status of the machine.
4545
*/
@@ -59,10 +59,13 @@ public MachineState() {}
5959
}
6060

6161
@Override
62-
public @NotNull CompoundTag createTag() {
63-
CompoundTag tag = new CompoundTag();
64-
tag.putBoolean(Constant.Nbt.POWERED, this.powered);
65-
return tag;
62+
public @NotNull ByteTag createTag() {
63+
return ByteTag.valueOf(this.powered);
64+
}
65+
66+
@Override
67+
public void readTag(@NotNull ByteTag tag) {
68+
this.powered = tag.getAsByte() != 0;
6669
}
6770

6871
@Override
@@ -82,11 +85,6 @@ public void copyInto(MachineState other) {
8285
other.powered = this.powered;
8386
}
8487

85-
@Override
86-
public void readTag(@NotNull CompoundTag tag) {
87-
this.powered = tag.getBoolean(Constant.Nbt.POWERED);
88-
}
89-
9088
@Override
9189
public void readPacket(@NotNull RegistryFriendlyByteBuf buf) {
9290
this.status = MachineStatus.readPacket(buf);
@@ -136,4 +134,9 @@ public boolean isPowered() {
136134
public void setPowered(boolean powered) {
137135
this.powered = powered;
138136
}
137+
138+
@Override
139+
public MachineState createEquivalent() {
140+
return new MachineState();
141+
}
139142
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public interface MachineType<Machine extends MachineBlockEntity, Menu extends Ma
5151

5252
@Contract(value = "_, _, _, _, _, _ -> new", pure = true)
5353
static <Machine extends MachineBlockEntity, Menu extends MachineMenu<Machine>> @NotNull MachineType<Machine, Menu> create(@NotNull Block block, @NotNull BlockEntityType<Machine> blockEntityType, @NotNull MenuType<Menu> menuType, @NotNull Supplier<MachineEnergyStorage> energySupplier, @NotNull Supplier<MachineItemStorage> itemSupplier, @NotNull Supplier<MachineFluidStorage> fluidSupplier) {
54-
return new MachineTypeImpl<>(block, blockEntityType, menuType, energySupplier, itemSupplier, fluidSupplier);
54+
return new MachineTypeImpl<>(block, blockEntityType, menuType, energySupplier, itemSupplier, fluidSupplier);
5555
}
5656

5757
/**

src/main/java/dev/galacticraft/machinelib/api/machine/configuration/IOConfig.java

+5
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ public void writeDeltaPacket(@NotNull ByteBuf buf, @NotNull IOConfig previous) {
152152
}
153153
}
154154

155+
@Override
156+
public IOConfig createEquivalent() {
157+
return new IOConfig();
158+
}
159+
155160
@Override
156161
public void copyInto(@NotNull IOConfig other) {
157162
for (int i = 0; i < this.faces.length; i++) {

src/main/java/dev/galacticraft/machinelib/api/machine/configuration/SecuritySettings.java

+5
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ public void readDeltaPacket(@NotNull FriendlyByteBuf buf) {
190190
}
191191
}
192192

193+
@Override
194+
public SecuritySettings createEquivalent() {
195+
return new SecuritySettings();
196+
}
197+
193198
@Override
194199
public boolean hasChanged(SecuritySettings previous) {
195200
return !Objects.equals(previous.owner, this.owner) || previous.accessLevel != this.accessLevel;

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

+24-54
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@
4747
import dev.galacticraft.machinelib.impl.compat.vanilla.RecipeOutputStorageSlot;
4848
import dev.galacticraft.machinelib.impl.compat.vanilla.StorageSlot;
4949
import dev.galacticraft.machinelib.impl.menu.MachineMenuDataImpl;
50-
import io.netty.buffer.ByteBufAllocator;
50+
import dev.galacticraft.machinelib.impl.util.Utils;
5151
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerType;
5252
import net.minecraft.core.BlockPos;
5353
import net.minecraft.network.RegistryFriendlyByteBuf;
54-
import net.minecraft.network.codec.StreamCodec;
5554
import net.minecraft.server.level.ServerPlayer;
5655
import net.minecraft.world.entity.player.Inventory;
5756
import net.minecraft.world.entity.player.Player;
@@ -79,23 +78,6 @@
7978
* @param <Machine> The type of machine block entity this menu is linked to.
8079
*/
8180
public class MachineMenu<Machine extends MachineBlockEntity> extends AbstractContainerMenu {
82-
/**
83-
* A codec that copies the contents of a buffer.
84-
*/
85-
public static final StreamCodec<RegistryFriendlyByteBuf, RegistryFriendlyByteBuf> BUF_IDENTITY_CODEC = new StreamCodec<>() {
86-
@Override
87-
public void encode(RegistryFriendlyByteBuf src, RegistryFriendlyByteBuf dst) {
88-
src.writeBytes(dst);
89-
}
90-
91-
@Override
92-
public @NotNull RegistryFriendlyByteBuf decode(RegistryFriendlyByteBuf src) {
93-
RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(ByteBufAllocator.DEFAULT.buffer(src.capacity()), src.registryAccess());
94-
buf.writeBytes(src);
95-
return buf;
96-
}
97-
};
98-
9981
/**
10082
* The machine type associated with this menu.
10183
*/
@@ -165,7 +147,7 @@ public void encode(RegistryFriendlyByteBuf src, RegistryFriendlyByteBuf dst) {
165147
/**
166148
* The redstone mode of the machine associated with this menu.
167149
*/
168-
public @NotNull RedstoneMode redstoneMode;
150+
public @NotNull RedstoneMode redstoneMode = RedstoneMode.IGNORE;
169151

170152
/**
171153
* Array of {@link MachineItemStorage item storage}-backed slots.
@@ -199,7 +181,7 @@ public MachineMenu(int syncId, @NotNull ServerPlayer player, @NotNull Machine ma
199181
assert !Objects.requireNonNull(machine.getLevel()).isClientSide;
200182
this.type = machine.getMachineType();
201183
this.machine = machine;
202-
this.data = new MachineMenuDataImpl(player);
184+
this.data = new MachineMenuDataImpl(player, syncId);
203185
this.server = true;
204186

205187
this.player = player;
@@ -248,7 +230,6 @@ public MachineMenu(int syncId, @NotNull ServerPlayer player, @NotNull Machine ma
248230

249231
this.addPlayerInventorySlots(player.getInventory(), 0, 0); // never displayed on the server.
250232
this.registerData(this.data);
251-
this.data.synchronizeFull();
252233
}
253234

254235
/**
@@ -265,28 +246,21 @@ protected MachineMenu(int syncId, @NotNull Inventory inventory, @NotNull Registr
265246

266247
this.type = type;
267248
this.server = false;
268-
this.data = new MachineMenuDataClient();
249+
this.data = new MachineMenuDataClient(syncId);
269250
this.player = inventory.player;
270251
this.playerInventory = inventory;
271252
this.playerUUID = inventory.player.getUUID();
272253

273254
BlockPos blockPos = buf.readBlockPos();
274-
this.machine = (Machine) inventory.player.level().getBlockEntity(blockPos); //todo: actually stop using the BE on the client side
255+
this.machine = (Machine) inventory.player.level().getBlockEntity(blockPos);
275256
this.levelAccess = ContainerLevelAccess.create(inventory.player.level(), blockPos);
276-
this.configuration = new IOConfig();
277-
this.configuration.readPacket(buf);
278-
this.security = new SecuritySettings();
279-
this.security.readPacket(buf);
280-
this.redstoneMode = RedstoneMode.readPacket(buf);
281-
282-
this.state = new MachineState();
283-
this.state.readPacket(buf);
284-
this.energyStorage = type.createEnergyStorage();
285-
this.energyStorage.readPacket(buf);
286-
this.itemStorage = type.createItemStorage();
287-
this.itemStorage.readPacket(buf);
288-
this.fluidStorage = type.createFluidStorage();
289-
this.fluidStorage.readPacket(buf);
257+
this.configuration = this.machine.getIOConfig();
258+
this.security = this.machine.getSecurity();
259+
260+
this.state = this.machine.getState();
261+
this.energyStorage = this.machine.energyStorage();
262+
this.itemStorage = this.machine.itemStorage();
263+
this.fluidStorage = this.machine.fluidStorage();
290264

291265
this.machineSlots = new StorageSlot[this.itemStorage.size()];
292266

@@ -316,6 +290,7 @@ protected MachineMenu(int syncId, @NotNull Inventory inventory, @NotNull Registr
316290

317291
this.addPlayerInventorySlots(inventory, invX, invY);
318292
this.registerData(this.data);
293+
this.data.handleInitial(buf);
319294
}
320295

321296
/**
@@ -329,7 +304,7 @@ protected MachineMenu(int syncId, @NotNull Inventory inventory, @NotNull Registr
329304
*/
330305
@Contract(value = "_, _ -> new", pure = true)
331306
public static <Machine extends MachineBlockEntity, Menu extends MachineMenu<Machine>> @NotNull MenuType<Menu> createType(@NotNull MachineMenuFactory<Machine, Menu> factory, Supplier<MachineType<Machine, Menu>> typeSupplier) {
332-
return new ExtendedScreenHandlerType<>((syncId, inventory, buf) -> factory.create(syncId, inventory, buf, typeSupplier.get()), BUF_IDENTITY_CODEC);
307+
return new ExtendedScreenHandlerType<>((syncId, inventory, buf) -> factory.create(syncId, inventory, buf, typeSupplier.get()), Utils.BUF_IDENTITY_CODEC);
333308
}
334309

335310
/**
@@ -342,7 +317,7 @@ protected MachineMenu(int syncId, @NotNull Inventory inventory, @NotNull Registr
342317
*/
343318
@Contract(value = "_ -> new", pure = true)
344319
public static <Machine extends MachineBlockEntity, Menu extends MachineMenu<Machine>> @NotNull MenuType<Menu> createType(@NotNull BasicMachineMenuFactory<Machine, Menu> factory) {
345-
return new ExtendedScreenHandlerType<>(factory::create, BUF_IDENTITY_CODEC);
320+
return new ExtendedScreenHandlerType<>(factory::create, Utils.BUF_IDENTITY_CODEC);
346321
}
347322

348323
/**
@@ -356,7 +331,7 @@ protected MachineMenu(int syncId, @NotNull Inventory inventory, @NotNull Registr
356331
*/
357332
@Contract(value = "_, _, _ -> new", pure = true)
358333
public static <Machine extends MachineBlockEntity> @NotNull MenuType<MachineMenu<Machine>> createSimple(int invX, int invY, Supplier<MachineType<Machine, MachineMenu<Machine>>> typeSupplier) {
359-
return new ExtendedScreenHandlerType<>((syncId, inventory, buf) -> new MachineMenu<>(syncId, inventory, buf, invX, invY, typeSupplier.get()), BUF_IDENTITY_CODEC);
334+
return new ExtendedScreenHandlerType<>((syncId, inventory, buf) -> new MachineMenu<>(syncId, inventory, buf, invX, invY, typeSupplier.get()), Utils.BUF_IDENTITY_CODEC);
360335
}
361336

362337
/**
@@ -389,13 +364,13 @@ protected MachineMenu(int syncId, @NotNull Inventory inventory, @NotNull Registr
389364
*/
390365
@MustBeInvokedByOverriders
391366
public void registerData(MachineMenuData data) {
392-
data.register(this.itemStorage, new long[this.itemStorage.size()]); //todo: probably synced by vanilla - is this necessary?
393-
data.register(this.fluidStorage, new long[this.fluidStorage.size()]);
394-
data.register(this.energyStorage, new long[1]);
395-
data.register(this.configuration, new IOConfig());
396-
data.register(this.security, new SecuritySettings());
367+
data.register(this.itemStorage); //todo: probably synced by vanilla - is this necessary?
368+
data.register(this.fluidStorage);
369+
data.register(this.energyStorage);
370+
data.register(this.configuration);
371+
data.register(this.security);
397372
data.registerEnum(RedstoneMode.values(), () -> this.redstoneMode, mode -> this.redstoneMode = mode);
398-
data.register(this.state, new MachineState());
373+
data.register(this.state);
399374
}
400375

401376
/**
@@ -634,14 +609,9 @@ private short calculateIoBitmask() {
634609
return bits;
635610
}
636611

637-
/**
638-
* Receives and deserializes sync packets from the server (called on the client).
639-
*
640-
* @param buf The packet buffer.
641-
*/
642612
@ApiStatus.Internal
643-
public void receiveState(@NotNull RegistryFriendlyByteBuf buf) {
644-
this.data.handle(buf);
613+
public MachineMenuData getData() {
614+
return data;
645615
}
646616

647617
/**

0 commit comments

Comments
 (0)