fixed serialization of blueprint for the library

This commit is contained in:
SpaceToad 2014-03-03 20:49:35 +01:00
parent 46fe51803d
commit 955600e7ef
11 changed files with 250 additions and 27 deletions

View file

@ -146,8 +146,8 @@ public class BptBlock {
*/
public void buildBlock(BptSlotInfo slot, IBptContext context) {
// Meta needs to be specified twice, depending on the block behavior
context.world().setBlock(slot.x, slot.y, slot.z, slot.block, slot.meta, 0);
context.world().setBlockMetadataWithNotify(slot.x, slot.y, slot.z, slot.meta,3);
context.world().setBlock(slot.x, slot.y, slot.z, slot.block, slot.meta, 3);
//context.world().setBlockMetadataWithNotify(slot.x, slot.y, slot.z, slot.meta, 3);
if (slot.block instanceof BlockContainer) {
TileEntity tile = context.world().getTileEntity(slot.x, slot.y, slot.z);

View file

@ -15,15 +15,22 @@ import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import buildcraft.core.network.NetworkData;
import buildcraft.core.utils.Utils;
public class MappingRegistry {
private HashMap <Block, Integer> blockToId = new HashMap<Block, Integer>();
private ArrayList <Block> idToBlock = new ArrayList<Block>();
@NetworkData
public HashMap <Block, Integer> blockToId = new HashMap<Block, Integer>();
private HashMap <Item, Integer> itemToId = new HashMap<Item, Integer>();
private ArrayList <Item> idToItem = new ArrayList<Item>();
@NetworkData
public ArrayList <Block> idToBlock = new ArrayList<Block>();
@NetworkData
public HashMap <Item, Integer> itemToId = new HashMap<Item, Integer>();
@NetworkData
public ArrayList <Item> idToItem = new ArrayList<Item>();
private void registerItem (Item item) {
if (!itemToId.containsKey(item)) {

View file

@ -20,7 +20,6 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.BuildCraftBuilders;
import buildcraft.api.blueprints.IBptContext;
import buildcraft.api.gates.IAction;
import buildcraft.api.power.IPowerReceptor;
import buildcraft.api.power.PowerHandler;
@ -284,8 +283,6 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory,
result = null;
}
debugForceBlueprintCompletion(result, context);
return result;
}
@ -560,6 +557,10 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory,
builderRobot.setDead();
builderRobot = null;
}
if (!worldObj.isRemote) {
debugForceBlueprintCompletion();
}
}
@Override
@ -634,15 +635,13 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory,
return powerHandler.getPowerReceiver();
}
public void debugForceBlueprintCompletion (BptBuilderBase builder, IBptContext context) {
while (true) {
BptSlot slot = builder.getNextBlock(worldObj, this);
public void debugForceBlueprintCompletion () {
if (bluePrintBuilder != null) {
BptSlot slot = bluePrintBuilder.getNextBlock(worldObj, this);
if (slot == null) {
break;
if (slot != null) {
slot.buildBlock(bluePrintBuilder.context);
}
slot.buildBlock(context);
}
}

View file

@ -38,9 +38,8 @@ public abstract class BlueprintBase {
@NetworkData
public String version = "";
// This should not need to be synchronized over the network - the
// information deduced from it are on the contents nbt
protected MappingRegistry mapping = new MappingRegistry();
@NetworkData
public MappingRegistry mapping = new MappingRegistry();
public BlueprintBase() {
}
@ -238,10 +237,11 @@ public abstract class BlueprintBase {
for (int x = 0; x < sizeX; ++x) {
for (int y = 0; y < sizeY; ++y) {
for (int z = 0; z < sizeZ; ++z)
for (int z = 0; z < sizeZ; ++z) {
if (contents[x][y][z] != null) {
res.contents[x][y][z] = contents[x][y][z].clone();
}
}
}
}

View file

@ -19,7 +19,7 @@ public abstract class BptBuilderBase implements IAreaProvider {
public BlueprintBase blueprint;
int x, y, z;
public boolean done;
protected BptContext context;
public BptContext context;
public BptBuilderBase(BlueprintBase bluePrint, World world, int x, int y, int z) {
this.blueprint = bluePrint;

View file

@ -14,11 +14,13 @@ import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.TreeMap;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.core.network.NetworkData;
@ -62,7 +64,7 @@ import buildcraft.core.utils.Utils;
*/
public class ClassMapping extends ClassSerializer {
private static SerializationObject anonymousSerializer = new SerializationObject();
private static SerializerObject anonymousSerializer = new SerializerObject();
private LinkedList<Field> floatFields = new LinkedList<Field>();
private LinkedList<Field> doubleFields = new LinkedList<Field>();
@ -71,6 +73,7 @@ public class ClassMapping extends ClassSerializer {
private LinkedList<Field> booleanFields = new LinkedList<Field>();
private LinkedList<Field> enumFields = new LinkedList<Field>();
// TODO: Make a specific serializer for this one
private LinkedList<Field> arrayListFields = new LinkedList<Field>();
class FieldObject {
@ -616,7 +619,11 @@ public class ClassMapping extends ClassSerializer {
public static ClassSerializer get (Class clas) {
ClassSerializer mapping;
if (!classes.containsKey(clas.getCanonicalName())) {
if (Block.class.isAssignableFrom(clas)) {
mapping = classes.get(Block.class.getCanonicalName());
} else if (Item.class.isAssignableFrom(clas)) {
mapping = classes.get(Item.class.getCanonicalName());
} else if (!classes.containsKey(clas.getCanonicalName())) {
mapping = new ClassMapping ();
registerSerializer(clas, mapping);
((ClassMapping) mapping).analyzeClass(clas);
@ -629,8 +636,11 @@ public class ClassMapping extends ClassSerializer {
static {
registerSerializer(String.class, new SerializerString());
registerSerializer(HashMap.class, new SerializerHashMap());
registerSerializer(Block.class, new SerializerBlock());
registerSerializer(Item.class, new SerializerItem());
registerSerializer(NBTTagCompound.class, new SerializerNBT());
registerSerializer(ItemStack.class, new SerializerItemStack());
registerSerializer(Integer.class, new SerializerInteger());
}
}

View file

@ -0,0 +1,65 @@
/**
* 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 java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class SerializerHashMap extends ClassSerializer {
private static SerializerObject anonymousSerializer = new SerializerObject();
@Override
public void write(ByteBuf data, Object o, SerializationContext context)
throws IllegalArgumentException, IllegalAccessException {
HashMap map = (HashMap) o;
if (o == null) {
data.writeBoolean(false);
} else {
data.writeBoolean(true);
data.writeShort(map.size());
Set <Map.Entry> s = map.entrySet();
for (Map.Entry e : s) {
anonymousSerializer.write(data, e.getKey(), context);
anonymousSerializer.write(data, e.getValue(), context);
}
}
}
@Override
public Object read(ByteBuf data, Object o, SerializationContext context)
throws IllegalArgumentException, IllegalAccessException,
InstantiationException, ClassNotFoundException {
if (!data.readBoolean()) {
return null;
} else {
int size = data.readShort();
HashMap map = new HashMap ();
for (int i = 0; i < size; ++i) {
Object key = anonymousSerializer.read(data, null, context);
Object value = anonymousSerializer.read(data, null, context);
map.put(key, value);
}
return map;
}
}
}

View file

@ -0,0 +1,26 @@
/**
* 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;
public class SerializerInteger extends ClassSerializer {
@Override
public void write (ByteBuf data, Object o, SerializationContext context) {
Integer i = (Integer) o;
data.writeInt(i);
}
@Override
public Object read (ByteBuf data, Object o, SerializationContext context) {
return new Integer(data.readInt());
}
}

View file

@ -0,0 +1,36 @@
/**
* 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.item.Item;
public class SerializerItem extends ClassSerializer {
@Override
public void write (ByteBuf data, Object o, SerializationContext context) {
Item i = (Item) o;
if (i == null) {
data.writeBoolean(false);
} else {
data.writeBoolean(true);
data.writeInt(Item.getIdFromItem(i));
}
}
@Override
public Object read (ByteBuf data, Object o, SerializationContext context) {
if (!data.readBoolean()) {
return null;
} else {
return Item.getItemById(data.readInt());
}
}
}

View file

@ -0,0 +1,81 @@
/**
* 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 buildcraft.core.utils.Utils;
public class SerializerObject extends ClassSerializer {
@Override
public void write(ByteBuf data, Object o, SerializationContext context)
throws IllegalArgumentException, IllegalAccessException {
if (o == null) {
data.writeBoolean(false);
} else {
data.writeBoolean(true);
Class realClass = o.getClass();
ClassSerializer delegateMapping;
if (context.classToId.containsKey(realClass.getCanonicalName())) {
int index = context.classToId.get(realClass.getCanonicalName()) + 1;
data.writeByte(index);
delegateMapping = context.idToClass.get(index - 1);
} else {
int index = context.classToId.size() + 1;
delegateMapping = ClassMapping.get(realClass);
data.writeByte(index);
Utils.writeUTF(data, realClass.getCanonicalName());
context.classToId.put(realClass.getCanonicalName(),
context.classToId.size());
context.idToClass.add(delegateMapping);
}
if (delegateMapping instanceof ClassMapping) {
((ClassMapping) delegateMapping).writeClass(o, data, context);
} else {
delegateMapping.write(data, o, context);
}
}
}
@Override
public Object read(ByteBuf data, Object o, SerializationContext context)
throws IllegalArgumentException, IllegalAccessException,
InstantiationException, ClassNotFoundException {
if (!data.readBoolean()) {
return null;
} else {
int index = data.readByte();
ClassSerializer delegateMapping;
if (context.idToClass.size() < index) {
String className = Utils.readUTF(data);
Class cls = Class.forName(className);
delegateMapping = ClassMapping.get(cls);
context.idToClass.add(ClassMapping.get(cls));
} else {
delegateMapping = context.idToClass.get(index - 1);
}
if (delegateMapping instanceof ClassMapping) {
return ((ClassMapping) delegateMapping).readClass(o, data, context);
} else {
return delegateMapping.read(data, o, context);
}
}
}
}

View file

@ -41,8 +41,8 @@ public class BptBlockPipe extends BptBlock {
Pipe pipe = BlockGenericPipe.getPipe(context.world(), slot.x, slot.y, slot.z);
if (BlockGenericPipe.isValid(pipe)) {
return pipe.item == Item.itemRegistry.getObjectById(slot.cpt
.getInteger("pipeId"));
return pipe.item == context.getMappingRegistry().getItemForId(
slot.cpt.getInteger("pipeId"));
} else {
return false;
}
@ -73,8 +73,7 @@ public class BptBlockPipe extends BptBlock {
Item.getIdFromItem(context.getMappingRegistry().getItemForId(
slot.cpt.getInteger("pipeId"))));
context.world().setBlock(slot.x, slot.y, slot.z, slot.block);
context.world().setBlockMetadataWithNotify(slot.x, slot.y, slot.z, slot.meta, 0);
context.world().setBlock(slot.x, slot.y, slot.z, slot.block, slot.meta, 3);
TileEntity tile = context.world().getTileEntity(slot.x, slot.y, slot.z);
tile.readFromNBT(slot.cpt);