43
43
import dev .galacticraft .machinelib .client .api .screen .MachineScreen ;
44
44
import dev .galacticraft .machinelib .impl .Constant ;
45
45
import dev .galacticraft .machinelib .impl .MachineLib ;
46
+ import dev .galacticraft .machinelib .impl .network .s2c .BaseMachineUpdatePayload ;
47
+ import dev .galacticraft .machinelib .impl .network .s2c .SideConfigurationUpdatePayload ;
46
48
import io .netty .buffer .ByteBuf ;
47
49
import io .netty .buffer .Unpooled ;
48
50
import net .fabricmc .fabric .api .blockview .v2 .RenderDataBlockEntity ;
51
+ import net .fabricmc .fabric .api .networking .v1 .ServerPlayNetworking ;
49
52
import net .fabricmc .fabric .api .screenhandler .v1 .ExtendedScreenHandlerFactory ;
50
53
import net .fabricmc .fabric .api .transfer .v1 .fluid .FluidStorage ;
51
54
import net .fabricmc .fabric .api .transfer .v1 .fluid .FluidVariant ;
55
58
import net .minecraft .core .BlockPos ;
56
59
import net .minecraft .core .Direction ;
57
60
import net .minecraft .core .HolderLookup ;
58
- import net .minecraft .nbt .*;
61
+ import net .minecraft .nbt .ByteTag ;
62
+ import net .minecraft .nbt .CompoundTag ;
63
+ import net .minecraft .nbt .LongTag ;
64
+ import net .minecraft .nbt .Tag ;
59
65
import net .minecraft .network .RegistryFriendlyByteBuf ;
60
66
import net .minecraft .network .chat .Component ;
61
67
import net .minecraft .network .protocol .Packet ;
68
+ import net .minecraft .network .protocol .common .ClientboundCustomPayloadPacket ;
69
+ import net .minecraft .network .protocol .common .custom .CustomPacketPayload ;
62
70
import net .minecraft .network .protocol .game .ClientGamePacketListener ;
63
- import net .minecraft .network .protocol .game .ClientboundBlockEntityDataPacket ;
64
71
import net .minecraft .resources .ResourceLocation ;
65
72
import net .minecraft .server .level .ServerLevel ;
66
73
import net .minecraft .server .level .ServerPlayer ;
67
74
import net .minecraft .util .profiling .ProfilerFiller ;
68
75
import net .minecraft .world .entity .player .Player ;
69
76
import net .minecraft .world .item .Item ;
70
77
import net .minecraft .world .item .crafting .RecipeHolder ;
78
+ import net .minecraft .world .level .ChunkPos ;
71
79
import net .minecraft .world .level .block .Block ;
72
80
import net .minecraft .world .level .block .Blocks ;
73
81
import net .minecraft .world .level .block .entity .BlockEntity ;
74
82
import net .minecraft .world .level .block .entity .BlockEntityType ;
75
83
import net .minecraft .world .level .block .state .BlockState ;
76
84
import net .minecraft .world .level .block .state .properties .BlockStateProperties ;
77
85
import net .minecraft .world .level .material .Fluid ;
78
- import org .jetbrains .annotations .*;
86
+ import org .jetbrains .annotations .ApiStatus ;
87
+ import org .jetbrains .annotations .Contract ;
88
+ import org .jetbrains .annotations .NotNull ;
89
+ import org .jetbrains .annotations .Nullable ;
79
90
import team .reborn .energy .api .EnergyStorage ;
80
91
import team .reborn .energy .api .EnergyStorageUtil ;
81
92
82
- import javax .crypto .Mac ;
83
93
import java .util .Collections ;
84
94
import java .util .Objects ;
85
95
import java .util .Optional ;
@@ -206,7 +216,7 @@ protected MachineBlockEntity(@NotNull MachineType<? extends MachineBlockEntity,
206
216
207
217
MachineIOFace [] faces = new MachineIOFace [6 ];
208
218
for (int i = 0 ; i < faces .length ; i ++) {
209
- faces [i ] = new InternalMachineIoFace ();
219
+ faces [i ] = new InternalMachineIoFace (i );
210
220
}
211
221
this .configuration = new MachineIOConfig (faces );
212
222
this .security = new InternalSecuritySettings ();
@@ -334,7 +344,6 @@ public void setRedstoneMode(@NotNull RedstoneMode redstone) {
334
344
* Used to determine who can interact with this machine.
335
345
*
336
346
* @return the security settings of this machine.
337
- * @see MachineConfiguration
338
347
*/
339
348
@ Contract (pure = true )
340
349
public final @ NotNull SecuritySettings getSecurity () {
@@ -782,6 +791,8 @@ public RegistryFriendlyByteBuf getScreenOpeningData(ServerPlayer player) {
782
791
783
792
buf .writeBlockPos (this .getBlockPos ());
784
793
this .configuration .writePacket (buf );
794
+ this .security .writePacket (buf );
795
+ this .redstone .writePacket (buf );
785
796
this .state .writePacket (buf );
786
797
this .energyStorage .writePacket (buf );
787
798
this .itemStorage .writePacket (buf );
@@ -802,7 +813,7 @@ public RegistryFriendlyByteBuf getScreenOpeningData(ServerPlayer player) {
802
813
803
814
@ Override
804
815
public @ NotNull MachineRenderData getRenderData () {
805
- return this .getIOConfig () ;
816
+ return this .configuration ;
806
817
}
807
818
808
819
@ Override
@@ -815,7 +826,26 @@ public CompoundTag getUpdateTag(HolderLookup.Provider registryLookup) {
815
826
@ Nullable
816
827
@ Override
817
828
public Packet <ClientGamePacketListener > getUpdatePacket () {
818
- return ClientboundBlockEntityDataPacket .create (this );
829
+ // safe cast because the ClientCommonPacketListener is a superclass of ClientGamePacketListener
830
+ // noinspection unchecked, rawtypes
831
+ return (Packet ) new ClientboundCustomPayloadPacket (new BaseMachineUpdatePayload (this .worldPosition , this .configuration ));
832
+ }
833
+
834
+ @ Override
835
+ public void setChanged () {
836
+ super .setChanged ();
837
+ }
838
+
839
+ protected void broadcastUpdate (CustomPacketPayload payload ) {
840
+ if (this .level != null && !this .level .isClientSide ) {
841
+ for (ServerPlayer player : ((ServerLevel ) MachineBlockEntity .this .level ).getChunkSource ().chunkMap .getPlayers (new ChunkPos (MachineBlockEntity .this .worldPosition ), false )) {
842
+ ServerPlayNetworking .getSender (player ).sendPacket (payload );
843
+ }
844
+ }
845
+ }
846
+
847
+ public void markForRerender () {
848
+ this .level .sendBlockUpdated (this .worldPosition , this .getBlockState (), this .getBlockState (), 0 );
819
849
}
820
850
821
851
public void awardUsedRecipes (@ NotNull ServerPlayer player , @ NotNull Set <ResourceLocation > recipes ) {
@@ -870,24 +900,33 @@ public void setAccessLevel(@NotNull AccessLevel accessLevel) {
870
900
}
871
901
872
902
private class InternalMachineIoFace extends MachineIOFace {
903
+ private final BlockFace face ;
873
904
private @ Nullable ExposedStorage <Item , ItemVariant > cachedItemStorage = null ;
874
905
private @ Nullable ExposedStorage <Fluid , FluidVariant > cachedFluidStorage = null ;
875
906
private @ Nullable EnergyStorage cachedEnergyStorage = null ;
876
907
877
- public InternalMachineIoFace () {
908
+ public InternalMachineIoFace (int i ) {
878
909
super (ResourceType .NONE , ResourceFlow .BOTH );
910
+ this .face = BlockFace .values ()[i ];
879
911
}
880
912
881
913
@ Override
882
914
public void setOption (@ NotNull ResourceType type , @ NotNull ResourceFlow flow ) {
883
- this .type = type ;
884
- this .flow = flow ;
915
+ if (this .type != type || this .flow != flow ) {
916
+ this .type = type ;
917
+ this .flow = flow ;
885
918
886
- MachineBlockEntity .this .setChanged ();
919
+ MachineBlockEntity .this .setChanged ();
920
+ if (MachineBlockEntity .this .level .isClientSide ) {
921
+ MachineBlockEntity .this .markForRerender ();
922
+ } else {
923
+ MachineBlockEntity .this .broadcastUpdate (new SideConfigurationUpdatePayload (MachineBlockEntity .this .worldPosition , this .face , type , flow ));
924
+ }
887
925
888
- this .cachedItemStorage = null ;
889
- this .cachedFluidStorage = null ;
890
- this .cachedEnergyStorage = null ;
926
+ this .cachedItemStorage = null ;
927
+ this .cachedFluidStorage = null ;
928
+ this .cachedEnergyStorage = null ;
929
+ }
891
930
}
892
931
893
932
@ Override
0 commit comments