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.7.10 Show documentation
Show all versions of elegant-networking-1.7.10 Show documentation
This is a runtime library of ElegantNetworking for MinecraftForge 1.7.10
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.NBTTagByteArray;
import net.minecraft.nbt.NBTTagCompound;
public class NbtSerializer {
private final ISerializerBase serializer;
public NbtSerializer(ISerializerBase serializer) {
this.serializer = serializer;
}
public NBTTagCompound serialize(A value) {
NBTTagCompound r = new NBTTagCompound();
r.setTag("content", serializeToByteArray(value));
return r;
}
public A unserialize(NBTTagCompound nbt) {
if (nbt.hasKey("content", 7))
return unserializeFromByteArray((NBTTagByteArray) nbt.getTag("content"));
else
throw new IllegalArgumentException("invalid nbt data " + nbt);
}
public NBTTagByteArray serializeToByteArray(A value) {
ByteBuf buffer = Unpooled.buffer();
serializer.serialize(value, buffer);
byte[] bytes = new byte[buffer.readableBytes()];
buffer.readBytes(bytes);
return new NBTTagByteArray(bytes);
}
public A unserializeFromByteArray(NBTTagByteArray nbt) {
ByteBuf buffer = Unpooled.buffer(nbt.func_150292_c().length);
buffer.writeBytes(nbt.func_150292_c());
return serializer.unserialize(buffer);
}
}