From 4075be8595f44a11ac4dde546844c0891e5cdad3 Mon Sep 17 00:00:00 2001 From: CleverNucleus Date: Sun, 5 Feb 2023 13:21:57 +0000 Subject: [PATCH] Fixed client sync issue +Incremented version. *Turns out I was an idiot and set the initial buffer size too small --- gradle.properties | 2 +- .../dataattributes/DataAttributes.java | 4 +- .../dataattributes/DataAttributesClient.java | 17 +-- .../dataattributes/impl/AttributeManager.java | 118 ++++++++---------- .../impl/EntityAttributeData.java | 4 +- .../dataattributes/impl/EntityTypeData.java | 4 +- .../dataattributes/impl/NbtIO.java | 8 ++ .../mixin/ReloadCommandMixin.java | 4 +- 8 files changed, 83 insertions(+), 78 deletions(-) create mode 100644 src/main/java/com/github/clevernucleus/dataattributes/impl/NbtIO.java diff --git a/gradle.properties b/gradle.properties index 846fc1cb..f204f522 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ minecraft_version=1.19.2 yarn_mappings=1.19.2+build.28 loader_version=0.14.10 -mod_version = 1.4.1 +mod_version = 1.4.2 maven_group = com.github.clevernucleus archives_base_name = dataattributes diff --git a/src/main/java/com/github/clevernucleus/dataattributes/DataAttributes.java b/src/main/java/com/github/clevernucleus/dataattributes/DataAttributes.java index 1ff694c3..e04d828a 100644 --- a/src/main/java/com/github/clevernucleus/dataattributes/DataAttributes.java +++ b/src/main/java/com/github/clevernucleus/dataattributes/DataAttributes.java @@ -40,8 +40,8 @@ public class DataAttributes implements ModInitializer { private static void loginQueryStart(ServerLoginNetworkHandler handler, MinecraftServer server, PacketSender sender, ServerLoginNetworking.LoginSynchronizer synchronizer) { PacketByteBuf buf = PacketByteBufs.create(); - final byte[] bytes = DataAttributes.MANAGER.getCurrentData(); - buf.writeByteArray(bytes); + buf.writeByteArray(DataAttributes.MANAGER.getEntityAttributeData()); + buf.writeByteArray(DataAttributes.MANAGER.getEntityTypeData()); sender.sendPacket(HANDSHAKE, buf); } diff --git a/src/main/java/com/github/clevernucleus/dataattributes/DataAttributesClient.java b/src/main/java/com/github/clevernucleus/dataattributes/DataAttributesClient.java index 56be1c9a..f971c360 100644 --- a/src/main/java/com/github/clevernucleus/dataattributes/DataAttributesClient.java +++ b/src/main/java/com/github/clevernucleus/dataattributes/DataAttributesClient.java @@ -20,25 +20,26 @@ public class DataAttributesClient implements ClientModInitializer { private static CompletableFuture loginQueryReceived(MinecraftClient client, ClientLoginNetworkHandler handler, PacketByteBuf buf, Consumer>> listenerAdder) { -final byte[] bytes = buf.readByteArray(); + final byte[] entityAttributeData = buf.readByteArray(); + final byte[] entityTypeData = buf.readByteArray(); client.execute(() -> { - DataAttributes.MANAGER.readFromData(bytes); + DataAttributes.MANAGER.setEntityAttributeData(entityAttributeData); + DataAttributes.MANAGER.setEntityTypeData(entityTypeData); DataAttributes.MANAGER.apply(); }); - PacketByteBuf bufOut = PacketByteBufs.create(); - bufOut.writeByteArray(DataAttributes.semVer); - - return CompletableFuture.completedFuture(bufOut); + return CompletableFuture.completedFuture(PacketByteBufs.empty()); } private static void updateReceived(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) { - final byte[] bytes = buf.readByteArray(); + final byte[] entityAttributeData = buf.readByteArray(); + final byte[] entityTypeData = buf.readByteArray(); final int updateFlag = buf.readInt(); client.execute(() -> { - DataAttributes.MANAGER.readFromData(bytes); + DataAttributes.MANAGER.setEntityAttributeData(entityAttributeData); + DataAttributes.MANAGER.setEntityTypeData(entityTypeData); DataAttributes.MANAGER.apply(); ClientWorld world = client.world; diff --git a/src/main/java/com/github/clevernucleus/dataattributes/impl/AttributeManager.java b/src/main/java/com/github/clevernucleus/dataattributes/impl/AttributeManager.java index a1359180..5493414d 100644 --- a/src/main/java/com/github/clevernucleus/dataattributes/impl/AttributeManager.java +++ b/src/main/java/com/github/clevernucleus/dataattributes/impl/AttributeManager.java @@ -57,17 +57,9 @@ public final class AttributeManager implements SimpleResourceReloadListener entityAttributeData = ImmutableMap.of(); private Map entityTypeData = ImmutableMap.of(); public Map, DefaultAttributeContainer> containers = ImmutableMap.of(); - private byte[] currentData; + private byte[] entityAttributeBytes, entityTypeBytes; - protected static class Wrapper { - public final Map entityAttributeData; - public final Map entityTypeData; - - public Wrapper(Map entityAttributeData, Map entityTypeData) { - this.entityAttributeData = entityAttributeData; - this.entityTypeData = entityTypeData; - } - } + protected record Wrapper(Map entityAttributeData, Map entityTypeData) {} public AttributeManager() {} @@ -257,33 +249,22 @@ private static void loadEntityTypes(ResourceManager manager, Map byte[] generateCurrentData(final Map data) { StringNbtWriter writer = new StringNbtWriter(); - NbtCompound nbt = new NbtCompound(); - NbtCompound entityAttributeNbt = new NbtCompound(); - NbtCompound entityTypeNbt = new NbtCompound(); - - this.entityAttributeData.forEach((key, value) -> { - NbtCompound entry = new NbtCompound(); - value.writeToNbt(entry); - entityAttributeNbt.put(key.toString(), entry); - }); + NbtCompound tag = new NbtCompound(); - this.entityTypeData.forEach((key, value) -> { + data.forEach((key, value) -> { NbtCompound entry = new NbtCompound(); value.writeToNbt(entry); - entityTypeNbt.put(key.toString(), entry); + tag.put(key.toString(), entry); }); - nbt.put("Attributes", entityAttributeNbt); - nbt.put("EntityTypes", entityTypeNbt); - - String snbt = writer.apply(nbt); + String snbt = writer.apply(tag); byte[] bytes; try { bytes = snbt.getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { + } catch(UnsupportedEncodingException e) { bytes = new byte[] {(byte)0}; } @@ -291,24 +272,21 @@ private void generateCurrentData() { deflater.setInput(bytes); deflater.finish(); - byte[] compressed = new byte[8192]; + byte[] compressed = new byte[Short.MAX_VALUE]; int size = deflater.deflate(compressed); deflater.end(); - this.currentData = Arrays.copyOf(compressed, size); - } - - public byte[] getCurrentData() { - if(this.currentData == null) return new byte[] {(byte)0}; - return this.currentData; + + return Arrays.copyOf(compressed, size); } - public void readFromData(byte[] data) { + private NbtCompound readFromData(byte[] bytesIn) { Inflater inflater = new Inflater(); - inflater.setInput(data); + inflater.setInput(bytesIn); - byte[] cache = new byte[8192]; + byte[] cache = new byte[Short.MAX_VALUE]; int size; + try { size = inflater.inflate(cache); } catch (DataFormatException e) { @@ -326,37 +304,50 @@ public void readFromData(byte[] data) { nbt = new NbtCompound(); } - if(nbt.contains("Attributes")) { - ImmutableMap.Builder builder = ImmutableMap.builder(); - NbtCompound nbtCompound = nbt.getCompound("Attributes"); - nbtCompound.getKeys().forEach(key -> { - NbtCompound entry = nbtCompound.getCompound(key); - EntityAttributeData entityAttributeData = new EntityAttributeData(); - entityAttributeData.readFromNbt(entry); - builder.put(new Identifier(key), entityAttributeData); - }); - - this.entityAttributeData = builder.build(); - } + return nbt; + } + + public void setEntityAttributeData(byte[] bytesIn) { + NbtCompound tag = this.readFromData(bytesIn); + ImmutableMap.Builder builder = ImmutableMap.builder(); + tag.getKeys().forEach(key -> { + NbtCompound entry = tag.getCompound(key); + EntityAttributeData entityAttributeData = new EntityAttributeData(); + entityAttributeData.readFromNbt(entry); + builder.put(new Identifier(key), entityAttributeData); + }); - if(nbt.contains("EntityTypes")) { - ImmutableMap.Builder builder = ImmutableMap.builder(); - NbtCompound nbtCompound = nbt.getCompound("EntityTypes"); - nbtCompound.getKeys().forEach(key -> { - NbtCompound entry = nbtCompound.getCompound(key); - EntityTypeData entityTypeData = new EntityTypeData(); - entityTypeData.readFromNbt(entry); - builder.put(new Identifier(key), entityTypeData); - }); - - this.entityTypeData = builder.build(); - } + this.entityAttributeData = builder.build(); + } + + public void setEntityTypeData(byte[] bytesIn) { + NbtCompound tag = this.readFromData(bytesIn); + ImmutableMap.Builder builder = ImmutableMap.builder(); + tag.getKeys().forEach(key -> { + NbtCompound entry = tag.getCompound(key); + EntityTypeData entityTypeData = new EntityTypeData(); + entityTypeData.readFromNbt(entry); + builder.put(new Identifier(key), entityTypeData); + }); + + this.entityTypeData = builder.build(); + } + + public byte[] getEntityAttributeData() { + if(this.entityAttributeBytes == null) return new byte[] {(byte)0}; + return this.entityAttributeBytes; + } + + public byte[] getEntityTypeData() { + if(this.entityTypeBytes == null) return new byte[] {(byte)0}; + return this.entityTypeBytes; } public DefaultAttributeContainer getContainer(EntityType entityType) { return this.containers.getOrDefault(entityType, DefaultAttributeRegistry.get(entityType)); } + @SuppressWarnings("unchecked") public void apply() { MutableRegistryImpl.unregister(Registry.ATTRIBUTE); @@ -388,7 +379,7 @@ public void apply() { for(Identifier identifier : this.entityTypeData.keySet()) { if(!entityTypes.contains(identifier)) continue; - @SuppressWarnings("unchecked") + EntityType entityType = (EntityType)Registry.ENTITY_TYPE.get(identifier); DefaultAttributeContainer.Builder builder = DefaultAttributeContainer.builder(); EntityTypeData entityTypeData = this.entityTypeData.get(identifier); @@ -422,12 +413,13 @@ public CompletableFuture apply(AttributeManager.Wrapper data, ResourceMana ImmutableMap.Builder entityAttributeData = ImmutableMap.builder(); data.entityAttributeData.forEach(entityAttributeData::put); this.entityAttributeData = entityAttributeData.build(); + this.entityAttributeBytes = this.generateCurrentData(this.entityAttributeData); ImmutableMap.Builder entityTypeData = ImmutableMap.builder(); data.entityTypeData.forEach(entityTypeData::put); this.entityTypeData = entityTypeData.build(); + this.entityTypeBytes = this.generateCurrentData(this.entityTypeData); - this.generateCurrentData(); this.apply(); }, executor); } diff --git a/src/main/java/com/github/clevernucleus/dataattributes/impl/EntityAttributeData.java b/src/main/java/com/github/clevernucleus/dataattributes/impl/EntityAttributeData.java index 35865ddf..7d25dccb 100644 --- a/src/main/java/com/github/clevernucleus/dataattributes/impl/EntityAttributeData.java +++ b/src/main/java/com/github/clevernucleus/dataattributes/impl/EntityAttributeData.java @@ -13,7 +13,7 @@ import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; -public final class EntityAttributeData { +public final class EntityAttributeData implements NbtIO { private AttributeOverrideJson attribute; private final Map functions; private final Map properties; @@ -56,6 +56,7 @@ public void putProperties(Map properties) { this.properties.putAll(properties); } + @Override public void readFromNbt(NbtCompound tag) { if(tag.contains("Attribute")) { this.attribute = new AttributeOverrideJson(); @@ -69,6 +70,7 @@ public void readFromNbt(NbtCompound tag) { properties.getKeys().forEach(key -> this.properties.put(key, properties.getString(key))); } + @Override public void writeToNbt(NbtCompound tag) { NbtCompound attribute = new NbtCompound(); diff --git a/src/main/java/com/github/clevernucleus/dataattributes/impl/EntityTypeData.java b/src/main/java/com/github/clevernucleus/dataattributes/impl/EntityTypeData.java index 0729fe26..17d453eb 100644 --- a/src/main/java/com/github/clevernucleus/dataattributes/impl/EntityTypeData.java +++ b/src/main/java/com/github/clevernucleus/dataattributes/impl/EntityTypeData.java @@ -11,7 +11,7 @@ import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; -public final class EntityTypeData { +public final class EntityTypeData implements NbtIO { public final Map data; public EntityTypeData() { @@ -37,10 +37,12 @@ public void build(DefaultAttributeContainer.Builder builder, DefaultAttributeCon } } + @Override public void readFromNbt(NbtCompound tag) { tag.getKeys().forEach(key -> this.data.put(new Identifier(key), tag.getDouble(key))); } + @Override public void writeToNbt(NbtCompound tag) { this.data.forEach((key, value) -> tag.putDouble(key.toString(), value)); } diff --git a/src/main/java/com/github/clevernucleus/dataattributes/impl/NbtIO.java b/src/main/java/com/github/clevernucleus/dataattributes/impl/NbtIO.java new file mode 100644 index 00000000..fc3fe7c0 --- /dev/null +++ b/src/main/java/com/github/clevernucleus/dataattributes/impl/NbtIO.java @@ -0,0 +1,8 @@ +package com.github.clevernucleus.dataattributes.impl; + +import net.minecraft.nbt.NbtCompound; + +public interface NbtIO { + void readFromNbt(NbtCompound tag); + void writeToNbt(NbtCompound tag); +} diff --git a/src/main/java/com/github/clevernucleus/dataattributes/mixin/ReloadCommandMixin.java b/src/main/java/com/github/clevernucleus/dataattributes/mixin/ReloadCommandMixin.java index fdcbad9d..1c9a14e1 100644 --- a/src/main/java/com/github/clevernucleus/dataattributes/mixin/ReloadCommandMixin.java +++ b/src/main/java/com/github/clevernucleus/dataattributes/mixin/ReloadCommandMixin.java @@ -40,8 +40,8 @@ private static void data_tryReloadDataPacks(Collection dataPacks, Server mutableUpdateFlag.setUpdateFlag(updateFlag2); PacketByteBuf buf = PacketByteBufs.create(); - final byte[] bytes = DataAttributes.MANAGER.getCurrentData(); - buf.writeByteArray(bytes); + buf.writeByteArray(DataAttributes.MANAGER.getEntityAttributeData()); + buf.writeByteArray(DataAttributes.MANAGER.getEntityTypeData()); buf.writeInt(mutableUpdateFlag.getUpdateFlag()); PlayerLookup.all(server).forEach(player -> ServerPlayNetworking.send(player, DataAttributes.RELOAD, buf)); }