Add support for custom nbt-based serialize classes for @NetworkData

This commit is contained in:
Prototik 2014-05-28 14:16:40 +08:00
parent e6ff3d7825
commit d3e3f8bf21
3 changed files with 49 additions and 0 deletions

View file

@ -0,0 +1,17 @@
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.network;
import net.minecraft.nbt.NBTTagCompound;
public interface INBTSerializable {
NBTTagCompound serializeNBT();
void serializeNBT(NBTTagCompound nbt);
}

View file

@ -29,6 +29,7 @@ import net.minecraftforge.fluids.FluidStack;
import buildcraft.api.core.JavaTools;
import buildcraft.api.core.NetworkData;
import buildcraft.core.network.INBTSerializable;
import buildcraft.core.utils.Utils;
/**
@ -598,6 +599,8 @@ public class ClassMapping extends ClassSerializer {
mapping = classes.get(Block.class.getCanonicalName());
} else if (Item.class.isAssignableFrom(clas)) {
mapping = classes.get(Item.class.getCanonicalName());
} else if (INBTSerializable.class.isAssignableFrom(clas)) {
mapping = classes.get(INBTSerializable.class.getCanonicalName());
} else if (!classes.containsKey(clas.getCanonicalName())) {
mapping = new ClassMapping ();
registerSerializer(clas, mapping);
@ -620,5 +623,6 @@ public class ClassMapping extends ClassSerializer {
registerSerializer(ItemStack.class, new SerializerItemStack());
registerSerializer(FluidStack.class, new SerializerFluidStack());
registerSerializer(Integer.class, new SerializerInteger());
registerSerializer(INBTSerializable.class, new SerializerINBTSerializable());
}
}

View file

@ -0,0 +1,28 @@
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.network.serializers;
import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.core.network.INBTSerializable;
public class SerializerINBTSerializable extends SerializerNBT {
@Override
public void write (ByteBuf data, Object o, SerializationContext context) {
super.write(data, ((INBTSerializable) o).serializeNBT(), context);
}
@Override
public Object read (ByteBuf data, Object o, SerializationContext context) {
((INBTSerializable) o).serializeNBT((NBTTagCompound) super.read(data, o, context));
return o;
}
}