Skip to content

Commit

Permalink
Use binary format to sync traveling items. Fixes #131
Browse files Browse the repository at this point in the history
  • Loading branch information
Technici4n committed Jan 1, 2025
1 parent 93eb30b commit 108d7de
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.common.base.Preconditions;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistryAccess;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.server.level.ServerLevel;
Expand Down Expand Up @@ -53,9 +54,9 @@ public void sync() {

public abstract void fromTag(CompoundTag tag, HolderLookup.Provider registries);

public abstract void toClientTag(CompoundTag tag, HolderLookup.Provider registries);
public abstract void toClientTag(CompoundTag tag, RegistryAccess registries);

public abstract void fromClientTag(CompoundTag tag, HolderLookup.Provider registries);
public abstract void fromClientTag(CompoundTag tag, RegistryAccess registries);

@Override
public final ClientboundBlockEntityDataPacket getUpdatePacket() {
Expand All @@ -65,7 +66,7 @@ public final ClientboundBlockEntityDataPacket getUpdatePacket() {
@Override
public final CompoundTag getUpdateTag(HolderLookup.Provider registries) {
CompoundTag nbt = super.getUpdateTag(registries);
toClientTag(nbt, registries);
toClientTag(nbt, (RegistryAccess) registries);
nbt.putBoolean("#c", shouldClientRemesh); // mark client tag
shouldClientRemesh = false;
return nbt;
Expand All @@ -79,7 +80,7 @@ protected final void saveAdditional(CompoundTag nbt, HolderLookup.Provider regis
@Override
public final void loadAdditional(CompoundTag nbt, HolderLookup.Provider registries) {
if (nbt.contains("#c")) {
fromClientTag(nbt, registries);
fromClientTag(nbt, (RegistryAccess) registries);
if (nbt.getBoolean("#c")) {
remesh();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistryAccess;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.entity.BlockEntityType;
Expand Down Expand Up @@ -110,10 +111,10 @@ public void fromTag(CompoundTag tag, HolderLookup.Provider registries) {
}

@Override
public void toClientTag(CompoundTag tag, HolderLookup.Provider registries) {
public void toClientTag(CompoundTag tag, RegistryAccess registries) {
}

@Override
public void fromClientTag(CompoundTag tag, HolderLookup.Provider registries) {
public void fromClientTag(CompoundTag tag, RegistryAccess registries) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
Expand Down Expand Up @@ -242,11 +243,11 @@ public void readNbt(CompoundTag tag, HolderLookup.Provider registries) {
}

@MustBeInvokedByOverriders
public void writeClientNbt(CompoundTag tag, HolderLookup.Provider registries) {
public void writeClientNbt(CompoundTag tag, RegistryAccess registries) {
}

@MustBeInvokedByOverriders
public void readClientNbt(CompoundTag tag, HolderLookup.Provider registries) {
public void readClientNbt(CompoundTag tag, RegistryAccess registries) {
}

public void clientTick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.List;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistryAccess;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.capabilities.BlockCapability;
Expand Down Expand Up @@ -250,14 +251,14 @@ public void readNbt(CompoundTag tag, HolderLookup.Provider registries) {
}

@Override
public void writeClientNbt(CompoundTag tag, HolderLookup.Provider registries) {
public void writeClientNbt(CompoundTag tag, RegistryAccess registries) {
super.writeClientNbt(tag, registries);
tag.putInt("amount", amount);
tag.put("variant", variant.toNbt(registries));
}

@Override
public void readClientNbt(CompoundTag tag, HolderLookup.Provider registries) {
public void readClientNbt(CompoundTag tag, RegistryAccess registries) {
super.readClientNbt(tag, registries);
variant = FluidVariant.fromNbt(tag.getCompound("variant"), registries);
amount = tag.getInt("amount");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,26 @@
import dev.technici4n.moderndynamics.util.DropHelper;
import dev.technici4n.moderndynamics.util.ItemVariant;
import dev.technici4n.moderndynamics.util.SerializationHelper;
import io.netty.buffer.Unpooled;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.function.Predicate;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistryAccess;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.util.Mth;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.capabilities.BlockCapability;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.items.ItemHandlerHelper;
import net.neoforged.neoforge.items.wrapper.EmptyItemHandler;
import net.neoforged.neoforge.network.connection.ConnectionType;
import org.jetbrains.annotations.Nullable;

public class ItemHost extends NodeHost {
Expand Down Expand Up @@ -503,47 +506,43 @@ public void updateConnections() {
}

@Override
public void writeClientNbt(CompoundTag tag, HolderLookup.Provider registries) {
public void writeClientNbt(CompoundTag tag, RegistryAccess registries) {
super.writeClientNbt(tag, registries);

if (travelingItems.size() > 0) {
ListTag list = new ListTag();
for (var travelingItem : travelingItems) {
CompoundTag compound = new CompoundTag();
compound.putInt("id", travelingItem.id);
compound.put("v", travelingItem.variant.toNbt(registries));
compound.putInt("a", travelingItem.amount);
compound.putDouble("td", travelingItem.getPathLength() - 1);
compound.putDouble("d", travelingItem.traveledDistance);
int currentBlock = (int) Math.floor(travelingItem.traveledDistance);
compound.putByte("in", (byte) travelingItem.path.path[currentBlock].get3DDataValue());
compound.putByte("out", (byte) travelingItem.path.path[currentBlock + 1].get3DDataValue());
compound.putDouble("s", travelingItem.getSpeed());
list.add(compound);
if (!travelingItems.isEmpty()) {
var buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), registries, ConnectionType.NEOFORGE);
try {
buf.writeInt(travelingItems.size());
for (var travelingItem : travelingItems) {
travelingItem.writeClient(buf);
}
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);
tag.putByteArray("items", bytes);
} finally {
buf.release();
}
tag.put("travelingItems", list);
}
}

@Override
public void readClientNbt(CompoundTag tag, HolderLookup.Provider registries) {
public void readClientNbt(CompoundTag tag, RegistryAccess registries) {
super.readClientNbt(tag, registries);

clientTravelingItems.clear();
ListTag list = tag.getList("travelingItems", Tag.TAG_COMPOUND);
for (int i = 0; i < list.size(); ++i) {
CompoundTag compound = list.getCompound(i);
var newItem = new ClientTravelingItem(
compound.getInt("id"),
ItemVariant.fromNbt(compound.getCompound("v"), registries),
compound.getInt("a"),
compound.getDouble("td"),
compound.getDouble("d"),
Direction.from3DDataValue(compound.getByte("in")),
Direction.from3DDataValue(compound.getByte("out")),
compound.getDouble("s"));
clientTravelingItems.add(newItem);
ClientTravelingItemSmoothing.onReceiveItem(newItem);
byte[] bytes = tag.getByteArray("items");
if (bytes.length > 0) {
var buf = new RegistryFriendlyByteBuf(Unpooled.wrappedBuffer(bytes), registries, ConnectionType.NEOFORGE);
try {
int count = buf.readInt();
for (int i = 0; i < count; i++) {
var newItem = TravelingItem.readClient(buf);
clientTravelingItems.add(newItem);
ClientTravelingItemSmoothing.onReceiveItem(newItem);
}
} finally {
buf.release();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
package dev.technici4n.moderndynamics.network.item;

import dev.technici4n.moderndynamics.Constants;
import dev.technici4n.moderndynamics.network.item.sync.ClientTravelingItem;
import dev.technici4n.moderndynamics.util.ItemVariant;
import dev.technici4n.moderndynamics.util.SerializationHelper;
import java.util.concurrent.atomic.AtomicInteger;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.RegistryFriendlyByteBuf;

public class TravelingItem {
private static final AtomicInteger NEXT_ID = new AtomicInteger();
Expand Down Expand Up @@ -87,4 +90,28 @@ public static TravelingItem fromNbt(CompoundTag nbt, HolderLookup.Provider regis
nbt.getDouble("speedMultiplier"),
nbt.getDouble("d"));
}

void writeClient(RegistryFriendlyByteBuf buf) {
buf.writeInt(id);
ItemVariant.STREAM_CODEC.encode(buf, variant);
buf.writeInt(amount);
buf.writeDouble(getPathLength() - 1);
buf.writeDouble(traveledDistance);
int currentBlock = (int) Math.floor(traveledDistance);
buf.writeEnum(path.path[currentBlock]);
buf.writeEnum(path.path[currentBlock + 1]);
buf.writeDouble(getSpeed());
}

static ClientTravelingItem readClient(RegistryFriendlyByteBuf buf) {
return new ClientTravelingItem(
buf.readInt(),
ItemVariant.STREAM_CODEC.decode(buf),
buf.readInt(),
buf.readDouble(),
buf.readDouble(),
buf.readEnum(Direction.class),
buf.readEnum(Direction.class),
buf.readDouble());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.NonNullList;
import net.minecraft.core.RegistryAccess;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
Expand Down Expand Up @@ -141,7 +142,7 @@ public void sync() {
}

@Override
public void toClientTag(CompoundTag tag, HolderLookup.Provider registries) {
public void toClientTag(CompoundTag tag, RegistryAccess registries) {
tag.putByte("connectionBlacklist", (byte) connectionBlacklist);
tag.putByte("connections", (byte) getPipeConnections());
tag.putByte("inventoryConnections", (byte) getInventoryConnections());
Expand All @@ -161,7 +162,7 @@ public void toClientTag(CompoundTag tag, HolderLookup.Provider registries) {
}

@Override
public void fromClientTag(CompoundTag tag, HolderLookup.Provider registries) {
public void fromClientTag(CompoundTag tag, RegistryAccess registries) {
connectionBlacklist = tag.getByte("connectionBlacklist");
byte connections = tag.getByte("connections");
byte inventoryConnections = tag.getByte("inventoryConnections");
Expand Down

0 comments on commit 108d7de

Please sign in to comment.