55import java .util .Collections ;
66import java .util .List ;
77import java .util .Map ;
8+ import java .util .Optional ;
89import java .util .Set ;
910import java .util .function .IntFunction ;
1011import mekanism .api .NBTConstants ;
11- import mekanism .api .math .MathUtils ;
1212import mekanism .api .text .EnumColor ;
13- import mekanism .common .content .network .transmitter .DiversionTransporter .DiversionControl ;
1413import mekanism .common .content .network .transmitter .LogisticalTransporterBase ;
1514import mekanism .common .content .transporter .TransporterPathfinder .Destination ;
1615import mekanism .common .content .transporter .TransporterPathfinder .IdlePathData ;
2625import net .minecraft .core .HolderLookup ;
2726import net .minecraft .nbt .CompoundTag ;
2827import net .minecraft .nbt .NbtUtils ;
29- import net .minecraft .network .FriendlyByteBuf ;
3028import net .minecraft .network .RegistryFriendlyByteBuf ;
3129import net .minecraft .network .codec .ByteBufCodecs ;
3230import net .minecraft .network .codec .StreamCodec ;
3331import net .minecraft .util .ByIdMap ;
3432import net .minecraft .world .item .ItemStack ;
3533import net .minecraft .world .level .Level ;
3634import net .minecraft .world .level .block .entity .BlockEntity ;
35+ import net .neoforged .neoforge .network .codec .NeoForgeStreamCodecs ;
3736import org .jetbrains .annotations .Contract ;
3837import org .jetbrains .annotations .NotNull ;
3938import org .jetbrains .annotations .Nullable ;
4039
4140public class TransporterStack {
4241
42+ //Make sure to call updateForPos before calling this method
43+ public static StreamCodec <RegistryFriendlyByteBuf , TransporterStack > STREAM_CODEC = NeoForgeStreamCodecs .composite (
44+ ByteBufCodecs .optional (EnumColor .STREAM_CODEC ), stack -> Optional .ofNullable (stack .color ),
45+ ByteBufCodecs .VAR_INT , stack -> stack .progress ,
46+ BlockPos .STREAM_CODEC , stack -> stack .originalLocation ,
47+ Path .STREAM_CODEC , TransporterStack ::getPathType ,
48+ ByteBufCodecs .optional (BlockPos .STREAM_CODEC ), stack -> Optional .ofNullable (stack .clientNext ),
49+ BlockPos .STREAM_CODEC , stack -> stack .clientPrev ,
50+ ItemStack .OPTIONAL_STREAM_CODEC , stack -> stack .itemStack ,
51+ (color , progress , originalLocation , pathType , clientNext , clientPrev , itemStack ) -> {
52+ TransporterStack stack = new TransporterStack ();
53+ stack .color = color .orElse (null );
54+ stack .progress = progress == 0 ? 5 : progress ;
55+ stack .originalLocation = originalLocation ;
56+ stack .pathType = pathType ;
57+ stack .clientNext = clientNext .orElse (null );
58+ stack .clientPrev = clientPrev ;
59+ stack .itemStack = itemStack ;
60+ return stack ;
61+ }
62+ );
63+
4364 public ItemStack itemStack = ItemStack .EMPTY ;
4465
4566 public int progress ;
@@ -51,6 +72,7 @@ public class TransporterStack {
5172 public Direction idleDir = null ;
5273 public BlockPos originalLocation ;
5374 public BlockPos homeLocation ;
75+ @ Nullable
5476 private BlockPos clientNext ;
5577 private BlockPos clientPrev ;
5678 @ Nullable
@@ -69,38 +91,6 @@ public static TransporterStack readFromUpdate(HolderLookup.Provider provider, Co
6991 return stack ;
7092 }
7193
72- public static TransporterStack readFromPacket (RegistryFriendlyByteBuf dataStream ) {
73- TransporterStack stack = new TransporterStack ();
74- stack .read (dataStream );
75- if (stack .progress == 0 ) {
76- stack .progress = 5 ;
77- }
78- return stack ;
79- }
80-
81- public void write (RegistryFriendlyByteBuf buf , BlockPos pos ) {
82- buf .writeVarInt (TransporterUtils .getColorIndex (color ));
83- buf .writeVarInt (progress );
84- buf .writeBlockPos (originalLocation );
85- buf .writeEnum (getPathType ());
86- buf .writeNullable (getNext (pos ), (b , p ) -> b .writeBlockPos (p ));
87- buf .writeBlockPos (getPrev (pos ));
88- ItemStack .OPTIONAL_STREAM_CODEC .encode (buf , itemStack );
89- }
90-
91- public void read (RegistryFriendlyByteBuf dataStream ) {
92- color = TransporterUtils .readColor (dataStream .readVarInt ());
93- progress = dataStream .readVarInt ();
94- originalLocation = dataStream .readBlockPos ();
95- pathType = dataStream .readEnum (Path .class );
96- //TODO - 1.20.4: SP: The clientNext and clientPrev have issues in single player as they won't get set
97- // though in all our use cases we are forcing a read/write to prevent mutation or leaking from one side to another
98- // so at least for now it doesn't fully matter
99- clientNext = dataStream .readNullable (buf -> buf .readBlockPos ());
100- clientPrev = dataStream .readBlockPos ();
101- itemStack = ItemStack .OPTIONAL_STREAM_CODEC .decode (dataStream );
102- }
103-
10494 public void writeToUpdateTag (HolderLookup .Provider provider , LogisticalTransporterBase transporter , CompoundTag updateTag ) {
10595 if (color != null ) {
10696 NBTUtils .writeEnum (updateTag , NBTConstants .COLOR , color );
@@ -248,6 +238,13 @@ public boolean isFinal(LogisticalTransporterBase transporter) {
248238 return pathToTarget .indexOf (transporter .getBlockPos ()) == (getPathType ().hasTarget () ? 1 : 0 );
249239 }
250240
241+ //TODO - 1.20.5: Re-evaluate this method
242+ public TransporterStack updateForPos (BlockPos pos ) {
243+ clientNext = getNext (pos );
244+ clientPrev = getPrev (pos );
245+ return this ;
246+ }
247+
251248 @ Nullable
252249 public BlockPos getNext (LogisticalTransporterBase transporter ) {
253250 return transporter .isRemote () ? clientNext : getNext (transporter .getBlockPos ());
0 commit comments