Skip to content

Commit

Permalink
Fixed client sync issue
Browse files Browse the repository at this point in the history
+Incremented version.
*Turns out I was an idiot and set the initial buffer size too small
  • Loading branch information
CleverNucleus committed Feb 5, 2023
1 parent c16acb5 commit 4075be8
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 78 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@

public class DataAttributesClient implements ClientModInitializer {
private static CompletableFuture<PacketByteBuf> loginQueryReceived(MinecraftClient client, ClientLoginNetworkHandler handler, PacketByteBuf buf, Consumer<GenericFutureListener<? extends Future<? super Void>>> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,9 @@ public final class AttributeManager implements SimpleResourceReloadListener<Attr
private Map<Identifier, EntityAttributeData> entityAttributeData = ImmutableMap.of();
private Map<Identifier, EntityTypeData> entityTypeData = ImmutableMap.of();
public Map<EntityType<? extends LivingEntity>, DefaultAttributeContainer> containers = ImmutableMap.of();
private byte[] currentData;
private byte[] entityAttributeBytes, entityTypeBytes;

protected static class Wrapper {
public final Map<Identifier, EntityAttributeData> entityAttributeData;
public final Map<Identifier, EntityTypeData> entityTypeData;

public Wrapper(Map<Identifier, EntityAttributeData> entityAttributeData, Map<Identifier, EntityTypeData> entityTypeData) {
this.entityAttributeData = entityAttributeData;
this.entityTypeData = entityTypeData;
}
}
protected record Wrapper(Map<Identifier, EntityAttributeData> entityAttributeData, Map<Identifier, EntityTypeData> entityTypeData) {}

public AttributeManager() {}

Expand Down Expand Up @@ -257,58 +249,44 @@ private static void loadEntityTypes(ResourceManager manager, Map<Identifier, Ent
}
}

private void generateCurrentData() {
private <T extends NbtIO> byte[] generateCurrentData(final Map<Identifier, T> 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};
}

Deflater deflater = new Deflater();
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) {
Expand All @@ -326,37 +304,50 @@ public void readFromData(byte[] data) {
nbt = new NbtCompound();
}

if(nbt.contains("Attributes")) {
ImmutableMap.Builder<Identifier, EntityAttributeData> 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<Identifier, EntityAttributeData> 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<Identifier, EntityTypeData> 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<Identifier, EntityTypeData> 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<? extends LivingEntity> entityType) {
return this.containers.getOrDefault(entityType, DefaultAttributeRegistry.get(entityType));
}

@SuppressWarnings("unchecked")
public void apply() {
MutableRegistryImpl.unregister(Registry.ATTRIBUTE);

Expand Down Expand Up @@ -388,7 +379,7 @@ public void apply() {
for(Identifier identifier : this.entityTypeData.keySet()) {
if(!entityTypes.contains(identifier)) continue;

@SuppressWarnings("unchecked")

EntityType<? extends LivingEntity> entityType = (EntityType<? extends LivingEntity>)Registry.ENTITY_TYPE.get(identifier);
DefaultAttributeContainer.Builder builder = DefaultAttributeContainer.builder();
EntityTypeData entityTypeData = this.entityTypeData.get(identifier);
Expand Down Expand Up @@ -422,12 +413,13 @@ public CompletableFuture<Void> apply(AttributeManager.Wrapper data, ResourceMana
ImmutableMap.Builder<Identifier, EntityAttributeData> entityAttributeData = ImmutableMap.builder();
data.entityAttributeData.forEach(entityAttributeData::put);
this.entityAttributeData = entityAttributeData.build();
this.entityAttributeBytes = this.generateCurrentData(this.entityAttributeData);

ImmutableMap.Builder<Identifier, EntityTypeData> entityTypeData = ImmutableMap.builder();
data.entityTypeData.forEach(entityTypeData::put);
this.entityTypeData = entityTypeData.build();
this.entityTypeBytes = this.generateCurrentData(this.entityTypeData);

this.generateCurrentData();
this.apply();
}, executor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Identifier, AttributeFunctionJson> functions;
private final Map<String, String> properties;
Expand Down Expand Up @@ -56,6 +56,7 @@ public void putProperties(Map<String, String> properties) {
this.properties.putAll(properties);
}

@Override
public void readFromNbt(NbtCompound tag) {
if(tag.contains("Attribute")) {
this.attribute = new AttributeOverrideJson();
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Identifier, Double> data;

public EntityTypeData() {
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ private static void data_tryReloadDataPacks(Collection<String> 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));
}
Expand Down

0 comments on commit 4075be8

Please sign in to comment.