diff --git a/src/main/java/dev/technici4n/moderndynamics/MdBlockEntity.java b/src/main/java/dev/technici4n/moderndynamics/MdBlockEntity.java index ec30e3e..cb56d6f 100644 --- a/src/main/java/dev/technici4n/moderndynamics/MdBlockEntity.java +++ b/src/main/java/dev/technici4n/moderndynamics/MdBlockEntity.java @@ -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; @@ -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() { @@ -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; @@ -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(); } diff --git a/src/main/java/dev/technici4n/moderndynamics/extender/MachineExtenderBlockEntity.java b/src/main/java/dev/technici4n/moderndynamics/extender/MachineExtenderBlockEntity.java index d98a81f..f07b90f 100644 --- a/src/main/java/dev/technici4n/moderndynamics/extender/MachineExtenderBlockEntity.java +++ b/src/main/java/dev/technici4n/moderndynamics/extender/MachineExtenderBlockEntity.java @@ -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; @@ -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) { } } diff --git a/src/main/java/dev/technici4n/moderndynamics/network/NodeHost.java b/src/main/java/dev/technici4n/moderndynamics/network/NodeHost.java index d8a7ec6..31e5f97 100644 --- a/src/main/java/dev/technici4n/moderndynamics/network/NodeHost.java +++ b/src/main/java/dev/technici4n/moderndynamics/network/NodeHost.java @@ -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; @@ -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() { diff --git a/src/main/java/dev/technici4n/moderndynamics/network/fluid/FluidHost.java b/src/main/java/dev/technici4n/moderndynamics/network/fluid/FluidHost.java index 46d57ab..82d6b52 100644 --- a/src/main/java/dev/technici4n/moderndynamics/network/fluid/FluidHost.java +++ b/src/main/java/dev/technici4n/moderndynamics/network/fluid/FluidHost.java @@ -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; @@ -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"); diff --git a/src/main/java/dev/technici4n/moderndynamics/network/item/ItemHost.java b/src/main/java/dev/technici4n/moderndynamics/network/item/ItemHost.java index 9ce549e..440ef59 100644 --- a/src/main/java/dev/technici4n/moderndynamics/network/item/ItemHost.java +++ b/src/main/java/dev/technici4n/moderndynamics/network/item/ItemHost.java @@ -34,6 +34,7 @@ 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; @@ -41,9 +42,10 @@ 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; @@ -51,6 +53,7 @@ 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 { @@ -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(); + } } } diff --git a/src/main/java/dev/technici4n/moderndynamics/network/item/TravelingItem.java b/src/main/java/dev/technici4n/moderndynamics/network/item/TravelingItem.java index 093b2ce..c74b1ae 100644 --- a/src/main/java/dev/technici4n/moderndynamics/network/item/TravelingItem.java +++ b/src/main/java/dev/technici4n/moderndynamics/network/item/TravelingItem.java @@ -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(); @@ -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()); + } } diff --git a/src/main/java/dev/technici4n/moderndynamics/pipe/PipeBlockEntity.java b/src/main/java/dev/technici4n/moderndynamics/pipe/PipeBlockEntity.java index 070be89..580ee13 100644 --- a/src/main/java/dev/technici4n/moderndynamics/pipe/PipeBlockEntity.java +++ b/src/main/java/dev/technici4n/moderndynamics/pipe/PipeBlockEntity.java @@ -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; @@ -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()); @@ -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");