47
47
import dev .galacticraft .machinelib .impl .compat .vanilla .RecipeOutputStorageSlot ;
48
48
import dev .galacticraft .machinelib .impl .compat .vanilla .StorageSlot ;
49
49
import dev .galacticraft .machinelib .impl .menu .MachineMenuDataImpl ;
50
- import io . netty . buffer . ByteBufAllocator ;
50
+ import dev . galacticraft . machinelib . impl . util . Utils ;
51
51
import net .fabricmc .fabric .api .screenhandler .v1 .ExtendedScreenHandlerType ;
52
52
import net .minecraft .core .BlockPos ;
53
53
import net .minecraft .network .RegistryFriendlyByteBuf ;
54
- import net .minecraft .network .codec .StreamCodec ;
55
54
import net .minecraft .server .level .ServerPlayer ;
56
55
import net .minecraft .world .entity .player .Inventory ;
57
56
import net .minecraft .world .entity .player .Player ;
79
78
* @param <Machine> The type of machine block entity this menu is linked to.
80
79
*/
81
80
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
-
99
81
/**
100
82
* The machine type associated with this menu.
101
83
*/
@@ -165,7 +147,7 @@ public void encode(RegistryFriendlyByteBuf src, RegistryFriendlyByteBuf dst) {
165
147
/**
166
148
* The redstone mode of the machine associated with this menu.
167
149
*/
168
- public @ NotNull RedstoneMode redstoneMode ;
150
+ public @ NotNull RedstoneMode redstoneMode = RedstoneMode . IGNORE ;
169
151
170
152
/**
171
153
* Array of {@link MachineItemStorage item storage}-backed slots.
@@ -199,7 +181,7 @@ public MachineMenu(int syncId, @NotNull ServerPlayer player, @NotNull Machine ma
199
181
assert !Objects .requireNonNull (machine .getLevel ()).isClientSide ;
200
182
this .type = machine .getMachineType ();
201
183
this .machine = machine ;
202
- this .data = new MachineMenuDataImpl (player );
184
+ this .data = new MachineMenuDataImpl (player , syncId );
203
185
this .server = true ;
204
186
205
187
this .player = player ;
@@ -248,7 +230,6 @@ public MachineMenu(int syncId, @NotNull ServerPlayer player, @NotNull Machine ma
248
230
249
231
this .addPlayerInventorySlots (player .getInventory (), 0 , 0 ); // never displayed on the server.
250
232
this .registerData (this .data );
251
- this .data .synchronizeFull ();
252
233
}
253
234
254
235
/**
@@ -265,28 +246,21 @@ protected MachineMenu(int syncId, @NotNull Inventory inventory, @NotNull Registr
265
246
266
247
this .type = type ;
267
248
this .server = false ;
268
- this .data = new MachineMenuDataClient ();
249
+ this .data = new MachineMenuDataClient (syncId );
269
250
this .player = inventory .player ;
270
251
this .playerInventory = inventory ;
271
252
this .playerUUID = inventory .player .getUUID ();
272
253
273
254
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 );
275
256
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 ();
290
264
291
265
this .machineSlots = new StorageSlot [this .itemStorage .size ()];
292
266
@@ -316,6 +290,7 @@ protected MachineMenu(int syncId, @NotNull Inventory inventory, @NotNull Registr
316
290
317
291
this .addPlayerInventorySlots (inventory , invX , invY );
318
292
this .registerData (this .data );
293
+ this .data .handleInitial (buf );
319
294
}
320
295
321
296
/**
@@ -329,7 +304,7 @@ protected MachineMenu(int syncId, @NotNull Inventory inventory, @NotNull Registr
329
304
*/
330
305
@ Contract (value = "_, _ -> new" , pure = true )
331
306
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 );
333
308
}
334
309
335
310
/**
@@ -342,7 +317,7 @@ protected MachineMenu(int syncId, @NotNull Inventory inventory, @NotNull Registr
342
317
*/
343
318
@ Contract (value = "_ -> new" , pure = true )
344
319
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 );
346
321
}
347
322
348
323
/**
@@ -356,7 +331,7 @@ protected MachineMenu(int syncId, @NotNull Inventory inventory, @NotNull Registr
356
331
*/
357
332
@ Contract (value = "_, _, _ -> new" , pure = true )
358
333
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 );
360
335
}
361
336
362
337
/**
@@ -389,13 +364,13 @@ protected MachineMenu(int syncId, @NotNull Inventory inventory, @NotNull Registr
389
364
*/
390
365
@ MustBeInvokedByOverriders
391
366
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 );
397
372
data .registerEnum (RedstoneMode .values (), () -> this .redstoneMode , mode -> this .redstoneMode = mode );
398
- data .register (this .state , new MachineState () );
373
+ data .register (this .state );
399
374
}
400
375
401
376
/**
@@ -634,14 +609,9 @@ private short calculateIoBitmask() {
634
609
return bits ;
635
610
}
636
611
637
- /**
638
- * Receives and deserializes sync packets from the server (called on the client).
639
- *
640
- * @param buf The packet buffer.
641
- */
642
612
@ ApiStatus .Internal
643
- public void receiveState ( @ NotNull RegistryFriendlyByteBuf buf ) {
644
- this . data . handle ( buf ) ;
613
+ public MachineMenuData getData ( ) {
614
+ return data ;
645
615
}
646
616
647
617
/**
0 commit comments