hohserg.elegant.networking.api.NbtSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elegant-networking-1.16 Show documentation
Show all versions of elegant-networking-1.16 Show documentation
This is a runtime library of ElegantNetworking for MinecraftForge 1.16
The newest version!
package hohserg.elegant.networking.api;
import hohserg.elegant.networking.impl.ISerializerBase;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.minecraft.nbt.ByteArrayNBT;
import net.minecraft.nbt.CompoundNBT;
public class NbtSerializer {
private final ISerializerBase serializer;
public NbtSerializer(ISerializerBase serializer) {
this.serializer = serializer;
}
public CompoundNBT serialize(A value) {
CompoundNBT r = new CompoundNBT();
r.put("content", serializeToByteArray(value));
return r;
}
public A unserialize(CompoundNBT nbt) {
if (nbt.contains("content", 7))
return unserializeFromByteArray((ByteArrayNBT) nbt.get("content"));
else
throw new IllegalArgumentException("invalid nbt data " + nbt);
}
public ByteArrayNBT serializeToByteArray(A value) {
ByteBuf buffer = Unpooled.buffer();
serializer.serialize(value, buffer);
byte[] bytes = new byte[buffer.readableBytes()];
buffer.readBytes(bytes);
return new ByteArrayNBT(bytes);
}
public A unserializeFromByteArray(ByteArrayNBT nbt) {
ByteBuf buffer = Unpooled.buffer(nbt.getByteArray().length);
buffer.writeBytes(nbt.getByteArray());
return serializer.unserialize(buffer);
}
}