Skip to content

Commit e052d61

Browse files
committed
Use Stream Codecs for transporter stacks
1 parent f9bb915 commit e052d61

File tree

4 files changed

+51
-63
lines changed

4 files changed

+51
-63
lines changed

src/main/java/mekanism/common/content/network/transmitter/LogisticalTransporterBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public void onUpdateServer() {
253253

254254
if (!deletes.isEmpty() || !needsSync.isEmpty()) {
255255
//Notify clients, so that we send the information before we start clearing our lists
256-
PacketUtils.sendToAllTracking(new PacketTransporterBatch(getLevel().registryAccess(), pos, deletes, needsSync), getTransmitterTile());
256+
PacketUtils.sendToAllTracking(PacketTransporterBatch.create(pos, deletes, needsSync), getTransmitterTile());
257257
// Now remove any entries from transit that have been deleted
258258
OfInt ofInt = deletes.iterator();
259259
while (ofInt.hasNext()) {
@@ -441,7 +441,7 @@ private TransitResponse updateTransit(boolean doEmit, TransporterStack stack, Tr
441441
if (doEmit) {
442442
int stackId = nextId++;
443443
addStack(stackId, stack);
444-
PacketUtils.sendToAllTracking(new PacketTransporterSync(getLevel().registryAccess(), getBlockPos(), stackId, stack), getTransmitterTile());
444+
PacketUtils.sendToAllTracking(PacketTransporterSync.create(getBlockPos(), stackId, stack), getTransmitterTile());
445445
getTransmitterTile().markForSave();
446446
}
447447
}

src/main/java/mekanism/common/content/transporter/TransporterStack.java

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
import java.util.Collections;
66
import java.util.List;
77
import java.util.Map;
8+
import java.util.Optional;
89
import java.util.Set;
910
import java.util.function.IntFunction;
1011
import mekanism.api.NBTConstants;
11-
import mekanism.api.math.MathUtils;
1212
import mekanism.api.text.EnumColor;
13-
import mekanism.common.content.network.transmitter.DiversionTransporter.DiversionControl;
1413
import mekanism.common.content.network.transmitter.LogisticalTransporterBase;
1514
import mekanism.common.content.transporter.TransporterPathfinder.Destination;
1615
import mekanism.common.content.transporter.TransporterPathfinder.IdlePathData;
@@ -26,20 +25,42 @@
2625
import net.minecraft.core.HolderLookup;
2726
import net.minecraft.nbt.CompoundTag;
2827
import net.minecraft.nbt.NbtUtils;
29-
import net.minecraft.network.FriendlyByteBuf;
3028
import net.minecraft.network.RegistryFriendlyByteBuf;
3129
import net.minecraft.network.codec.ByteBufCodecs;
3230
import net.minecraft.network.codec.StreamCodec;
3331
import net.minecraft.util.ByIdMap;
3432
import net.minecraft.world.item.ItemStack;
3533
import net.minecraft.world.level.Level;
3634
import net.minecraft.world.level.block.entity.BlockEntity;
35+
import net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs;
3736
import org.jetbrains.annotations.Contract;
3837
import org.jetbrains.annotations.NotNull;
3938
import org.jetbrains.annotations.Nullable;
4039

4140
public 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());

src/main/java/mekanism/common/network/to_client/transmitter/PacketTransporterBatch.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,28 @@
1111
import mekanism.common.network.PacketUtils;
1212
import mekanism.common.tile.transmitter.TileEntityLogisticalTransporterBase;
1313
import net.minecraft.core.BlockPos;
14-
import net.minecraft.core.RegistryAccess;
15-
import net.minecraft.network.FriendlyByteBuf;
14+
import net.minecraft.network.RegistryFriendlyByteBuf;
1615
import net.minecraft.network.codec.ByteBufCodecs;
1716
import net.minecraft.network.codec.StreamCodec;
1817
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
19-
import net.neoforged.neoforge.common.util.FriendlyByteBufUtil;
2018
import net.neoforged.neoforge.network.handling.IPayloadContext;
2119
import org.jetbrains.annotations.NotNull;
2220

23-
public record PacketTransporterBatch(BlockPos pos, IntSet deletes, byte[] rawUpdates) implements IMekanismPacket {
21+
public record PacketTransporterBatch(BlockPos pos, IntSet deletes, Int2ObjectMap<TransporterStack> updates) implements IMekanismPacket {
2422

2523
public static final CustomPacketPayload.Type<PacketTransporterBatch> TYPE = new CustomPacketPayload.Type<>(Mekanism.rl("transporter_batch"));
26-
public static final StreamCodec<FriendlyByteBuf, PacketTransporterBatch> STREAM_CODEC = StreamCodec.composite(
24+
public static final StreamCodec<RegistryFriendlyByteBuf, PacketTransporterBatch> STREAM_CODEC = StreamCodec.composite(
2725
BlockPos.STREAM_CODEC, PacketTransporterBatch::pos,
2826
ByteBufCodecs.VAR_INT.apply(ByteBufCodecs.collection(IntOpenHashSet::new)), PacketTransporterBatch::deletes,
29-
ByteBufCodecs.BYTE_ARRAY, PacketTransporterBatch::rawUpdates,
27+
ByteBufCodecs.map(Int2ObjectOpenHashMap::new, ByteBufCodecs.VAR_INT, TransporterStack.STREAM_CODEC), PacketTransporterBatch::updates,
3028
PacketTransporterBatch::new
3129
);
3230

33-
public PacketTransporterBatch(RegistryAccess registryAccess, BlockPos pos, IntSet deletes, Int2ObjectMap<TransporterStack> updates) {
34-
//TODO - 1.20.4: SP: Figure out if there is a better way for us to handle not leaking the instance than just forcing a write and read
35-
// Also validate that we can actually just directly use deletes without copying it or anything
36-
this(pos, deletes, FriendlyByteBufUtil.writeCustomData(buffer -> buffer.writeMap(updates, FriendlyByteBuf::writeVarInt, (buf, stack) -> stack.write(buffer, pos)), registryAccess));
31+
public static PacketTransporterBatch create(BlockPos pos, IntSet deletes, Int2ObjectMap<TransporterStack> updates) {
32+
for (TransporterStack stack : updates.values()) {
33+
stack.updateForPos(pos);
34+
}
35+
return new PacketTransporterBatch(pos, deletes, updates);
3736
}
3837

3938
@NotNull
@@ -46,8 +45,6 @@ public CustomPacketPayload.Type<PacketTransporterBatch> type() {
4645
public void handle(IPayloadContext context) {
4746
if (PacketUtils.blockEntity(context, pos) instanceof TileEntityLogisticalTransporterBase tile) {
4847
LogisticalTransporterBase transporter = tile.getTransmitter();
49-
Int2ObjectMap<TransporterStack> updates = PacketUtils.read(context.player().level().registryAccess(), rawUpdates, buffer ->
50-
buffer.readMap(Int2ObjectOpenHashMap::new, ByteBufCodecs.VAR_INT, buf -> TransporterStack.readFromPacket(buffer)));
5148
for (Int2ObjectMap.Entry<TransporterStack> entry : updates.int2ObjectEntrySet()) {
5249
transporter.addStack(entry.getIntKey(), entry.getValue());
5350
}
Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
11
package mekanism.common.network.to_client.transmitter;
22

3-
import io.netty.buffer.ByteBuf;
43
import mekanism.common.Mekanism;
5-
import mekanism.common.content.network.transmitter.LogisticalTransporterBase;
64
import mekanism.common.content.transporter.TransporterStack;
75
import mekanism.common.network.IMekanismPacket;
86
import mekanism.common.network.PacketUtils;
97
import mekanism.common.tile.transmitter.TileEntityLogisticalTransporterBase;
108
import net.minecraft.core.BlockPos;
11-
import net.minecraft.core.RegistryAccess;
9+
import net.minecraft.network.RegistryFriendlyByteBuf;
1210
import net.minecraft.network.codec.ByteBufCodecs;
1311
import net.minecraft.network.codec.StreamCodec;
1412
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
15-
import net.neoforged.neoforge.common.util.FriendlyByteBufUtil;
1613
import net.neoforged.neoforge.network.handling.IPayloadContext;
1714
import org.jetbrains.annotations.NotNull;
1815

19-
public record PacketTransporterSync(BlockPos pos, int stackId, byte[] rawStack) implements IMekanismPacket {
16+
public record PacketTransporterSync(BlockPos pos, int stackId, TransporterStack stack) implements IMekanismPacket {
2017

2118
public static final CustomPacketPayload.Type<PacketTransporterSync> TYPE = new CustomPacketPayload.Type<>(Mekanism.rl("transporter_sync"));
22-
public static final StreamCodec<ByteBuf, PacketTransporterSync> STREAM_CODEC = StreamCodec.composite(
19+
public static final StreamCodec<RegistryFriendlyByteBuf, PacketTransporterSync> STREAM_CODEC = StreamCodec.composite(
2320
BlockPos.STREAM_CODEC, PacketTransporterSync::pos,
24-
ByteBufCodecs.INT, PacketTransporterSync::stackId,
25-
ByteBufCodecs.BYTE_ARRAY, PacketTransporterSync::rawStack,
21+
ByteBufCodecs.VAR_INT, PacketTransporterSync::stackId,
22+
TransporterStack.STREAM_CODEC, PacketTransporterSync::stack,
2623
PacketTransporterSync::new
2724
);
2825

29-
public PacketTransporterSync(RegistryAccess registryAccess, BlockPos pos, int stackId, TransporterStack stack) {
30-
//TODO - 1.20.4: SP: Figure out if there is a better way for us to handle not leaking the instance than just forcing a write and read
31-
this(pos, stackId, FriendlyByteBufUtil.writeCustomData(buffer -> stack.write(buffer, pos), registryAccess));
26+
public static PacketTransporterSync create(BlockPos pos, int stackId, TransporterStack stack) {
27+
return new PacketTransporterSync(pos, stackId, stack.updateForPos(pos));
3228
}
3329

3430
@NotNull
@@ -40,9 +36,7 @@ public CustomPacketPayload.Type<PacketTransporterSync> type() {
4036
@Override
4137
public void handle(IPayloadContext context) {
4238
if (PacketUtils.blockEntity(context, pos) instanceof TileEntityLogisticalTransporterBase tile) {
43-
LogisticalTransporterBase transporter = tile.getTransmitter();
44-
TransporterStack stack = PacketUtils.read(context.player().level().registryAccess(), rawStack, TransporterStack::readFromPacket);
45-
transporter.addStack(stackId, stack);
39+
tile.getTransmitter().addStack(stackId, stack);
4640
}
4741
}
4842
}

0 commit comments

Comments
 (0)