diff --git a/common/buildcraft/BuildCraftBuilders.java b/common/buildcraft/BuildCraftBuilders.java index 7158bce2..dd4a0c04 100644 --- a/common/buildcraft/BuildCraftBuilders.java +++ b/common/buildcraft/BuildCraftBuilders.java @@ -24,6 +24,9 @@ import net.minecraftforge.client.event.TextureStitchEvent; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.config.Configuration; import net.minecraftforge.common.config.Property; +import buildcraft.api.blueprints.SchematicBlock; +import buildcraft.api.blueprints.SchematicFactory; +import buildcraft.api.blueprints.SchematicFactoryBlock; import buildcraft.api.blueprints.SchematicRegistry; import buildcraft.api.filler.FillerManager; import buildcraft.api.filler.IFillerPattern; @@ -281,6 +284,10 @@ public class BuildCraftBuilders extends BuildCraftMod { SchematicRegistry.registerSchematicBlock(markerBlock, SchematicWallSide.class); SchematicRegistry.registerSchematicBlock(pathMarkerBlock, SchematicWallSide.class); + // Factories required to save entities in world + + SchematicFactory.registerSchematicFactory(SchematicBlock.class, new SchematicFactoryBlock()); + if (BuildCraftCore.loadDefaultRecipes) { loadRecipes(); } diff --git a/common/buildcraft/api/blueprints/SchematicFactory.java b/common/buildcraft/api/blueprints/SchematicFactory.java new file mode 100755 index 00000000..d834b5ee --- /dev/null +++ b/common/buildcraft/api/blueprints/SchematicFactory.java @@ -0,0 +1,56 @@ +/** + * 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.api.blueprints; + +import java.util.HashMap; + +import net.minecraft.nbt.NBTTagCompound; + +public abstract class SchematicFactory { + + private static final HashMap factories = + new HashMap (); + + private static final HashMap , SchematicFactory> schematicToFactory = + new HashMap , SchematicFactory> (); + + protected abstract S loadSchematicFromWorldNBT (NBTTagCompound nbt, MappingRegistry registry); + + public void saveSchematicToWorldNBT (NBTTagCompound nbt, S object, MappingRegistry registry) { + nbt.setString("factoryID", getClass().getCanonicalName()); + } + + public static Schematic createSchematicFromWorldNBT (NBTTagCompound nbt, MappingRegistry registry) { + String factoryName = nbt.getString("factoryID"); + + if (factories.containsKey(factoryName)) { + return factories.get(factoryName).loadSchematicFromWorldNBT(nbt, registry); + } else { + return null; + } + } + + public static void registerSchematicFactory (Class clas, SchematicFactory factory) { + schematicToFactory.put(clas, factory); + factories.put(factory.getClass().getCanonicalName(), factory); + } + + public static SchematicFactory getFactory (Class clas) { + Class superClass = clas.getSuperclass(); + + if (schematicToFactory.containsKey(clas)) { + return schematicToFactory.get(clas); + } else if (superClass != null) { + return getFactory(superClass); + } else { + return null; + } + } + +} diff --git a/common/buildcraft/api/blueprints/SchematicFactoryBlock.java b/common/buildcraft/api/blueprints/SchematicFactoryBlock.java new file mode 100755 index 00000000..eab4173e --- /dev/null +++ b/common/buildcraft/api/blueprints/SchematicFactoryBlock.java @@ -0,0 +1,37 @@ +/** + * 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.api.blueprints; + +import net.minecraft.nbt.NBTTagCompound; + +public class SchematicFactoryBlock extends SchematicFactory { + + @Override + protected SchematicBlock loadSchematicFromWorldNBT (NBTTagCompound nbt, MappingRegistry registry) { + int blockId = nbt.getInteger("blockId"); + SchematicBlock s = SchematicRegistry.newSchematicBlock(registry.getBlockForId(blockId)); + + if (s != null) { + s.readFromNBT(nbt, registry); + } else { + return null; + } + + return s; + } + + @Override + public void saveSchematicToWorldNBT (NBTTagCompound nbt, SchematicBlock object, MappingRegistry registry) { + super.saveSchematicToWorldNBT(nbt, object, registry); + + nbt.setInteger("blockId", registry.getIdForBlock(object.block)); + object.writeToNBT(nbt, registry); + } + +} diff --git a/common/buildcraft/api/blueprints/SchematicRegistry.java b/common/buildcraft/api/blueprints/SchematicRegistry.java index 1411db1c..4252b4dd 100644 --- a/common/buildcraft/api/blueprints/SchematicRegistry.java +++ b/common/buildcraft/api/blueprints/SchematicRegistry.java @@ -82,7 +82,6 @@ public class SchematicRegistry { } public static SchematicEntity newSchematicEntity (Class entityClass) { - if (!schematicEntities.containsKey(entityClass)) { return null; } @@ -106,4 +105,5 @@ public class SchematicRegistry { return null; } + } diff --git a/common/buildcraft/builders/BuildingItem.java b/common/buildcraft/builders/BuildingItem.java index f272595e..338cbef8 100755 --- a/common/buildcraft/builders/BuildingItem.java +++ b/common/buildcraft/builders/BuildingItem.java @@ -20,14 +20,20 @@ import buildcraft.api.blueprints.IBuilderContext; import buildcraft.api.blueprints.MappingRegistry; import buildcraft.api.core.Position; import buildcraft.core.blueprints.BuildingSlot; +import buildcraft.core.blueprints.BuildingSlotBlock; +import buildcraft.core.blueprints.BuildingSlotEntity; import buildcraft.core.blueprints.IBuilder; import buildcraft.core.network.NetworkData; +import buildcraft.core.utils.Utils; public class BuildingItem implements IBuilder { @NetworkData public Position origin, destination; + @NetworkData + double lifetime = 0; + @NetworkData public LinkedList stacksToBuild = new LinkedList(); @@ -43,7 +49,6 @@ public class BuildingItem implements IBuilder { public boolean isDone = false; long previousUpdate; - double lifetime = 0; double lifetimeDisplay = 0; double maxLifetime = 0; private boolean initialized = false; @@ -226,12 +231,15 @@ public class BuildingItem implements IBuilder { nbt.setDouble("lifeTime", lifetime); NBTTagList items = new NBTTagList(); + for (ItemStack s : stacksToBuild) { NBTTagCompound cpt = new NBTTagCompound(); s.writeToNBT(cpt); items.appendTag(cpt); } + nbt.setTag("items", items); + MappingRegistry registry = new MappingRegistry(); NBTTagCompound slotNBT = new NBTTagCompound(); @@ -241,10 +249,38 @@ public class BuildingItem implements IBuilder { registry.write(registryNBT); nbt.setTag("registry", registryNBT); + + if (slotToBuild instanceof BuildingSlotBlock) { + nbt.setByte ("slotKind", (byte) 0); + } else { + nbt.setByte ("slotKind", (byte) 1); + } + nbt.setTag("slotToBuild", slotNBT); } public void readFromNBT (NBTTagCompound nbt) { + origin = new Position(nbt.getCompoundTag("origin")); + destination = new Position (nbt.getCompoundTag("destination")); + lifetime = nbt.getDouble("lifetime"); + NBTTagList items = nbt.getTagList("items", + Utils.NBTTag_Types.NBTTagCompound.ordinal()); + + for (int i = 0; i < items.tagCount(); ++i) { + stacksToBuild.add(ItemStack.loadItemStackFromNBT(items + .getCompoundTagAt(i))); + } + + MappingRegistry registry = new MappingRegistry(); + registry.read(nbt.getCompoundTag("registry")); + + if (nbt.getByte("slotKind") == 0) { + slotToBuild = new BuildingSlotBlock(); + } else { + slotToBuild = new BuildingSlotEntity(); + } + + slotToBuild.readFromNBT(nbt.getCompoundTag("slotToBuild"), registry); } } diff --git a/common/buildcraft/builders/TileAbstractBuilder.java b/common/buildcraft/builders/TileAbstractBuilder.java index f0cd4bc6..f3c0a61e 100755 --- a/common/buildcraft/builders/TileAbstractBuilder.java +++ b/common/buildcraft/builders/TileAbstractBuilder.java @@ -13,6 +13,7 @@ import java.util.LinkedList; import net.minecraft.inventory.IInventory; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; import buildcraft.api.core.SafeTimeTracker; import buildcraft.api.mj.MjBattery; import buildcraft.core.IBoxProvider; @@ -22,6 +23,7 @@ import buildcraft.core.network.NetworkData; import buildcraft.core.network.RPC; import buildcraft.core.network.RPCHandler; import buildcraft.core.network.RPCSide; +import buildcraft.core.utils.Utils; public abstract class TileAbstractBuilder extends TileBuildCraft implements IInventory, IBoxProvider { @@ -90,6 +92,16 @@ public abstract class TileAbstractBuilder extends TileBuildCraft implements IInv super.readFromNBT(nbttagcompound); mjStored = nbttagcompound.getDouble("mjStored"); + + NBTTagList buildingList = new NBTTagList(); + + for (BuildingItem item : buildersInAction) { + NBTTagCompound sub = new NBTTagCompound(); + item.writeToNBT(sub); + buildingList.appendTag(sub); + } + + nbttagcompound.setTag("buildersInAction", buildingList); } @Override @@ -97,5 +109,15 @@ public abstract class TileAbstractBuilder extends TileBuildCraft implements IInv super.writeToNBT(nbttagcompound); nbttagcompound.setDouble("mjStored", mjStored); + + NBTTagList buildingList = nbttagcompound + .getTagList("buildersInAction", + Utils.NBTTag_Types.NBTTagCompound.ordinal()); + + for (int i = 0; i < buildingList.tagCount(); ++i) { + BuildingItem item = new BuildingItem(); + item.readFromNBT(buildingList.getCompoundTagAt(i)); + addBuildingItem(item); + } } } diff --git a/common/buildcraft/builders/TileBuilder.java b/common/buildcraft/builders/TileBuilder.java index 9755b995..c9f02a50 100644 --- a/common/buildcraft/builders/TileBuilder.java +++ b/common/buildcraft/builders/TileBuilder.java @@ -477,7 +477,9 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine { NBTTagCompound builderCpt = new NBTTagCompound(); bluePrintBuilder.saveBuildStateToNBT(builderCpt); nbttagcompound.setTag("builderState", builderCpt); + } + if (currentPathIterator != null) { NBTTagCompound iteratorNBT = new NBTTagCompound(); currentPathIterator.to.writeTo(iteratorNBT); nbttagcompound.setTag ("iterator", iteratorNBT); diff --git a/common/buildcraft/core/blueprints/BuildingSlotBlock.java b/common/buildcraft/core/blueprints/BuildingSlotBlock.java index 4558ebad..89180ebb 100755 --- a/common/buildcraft/core/blueprints/BuildingSlotBlock.java +++ b/common/buildcraft/core/blueprints/BuildingSlotBlock.java @@ -16,6 +16,7 @@ import net.minecraft.tileentity.TileEntity; import buildcraft.api.blueprints.IBuilderContext; import buildcraft.api.blueprints.MappingRegistry; import buildcraft.api.blueprints.SchematicBlockBase; +import buildcraft.api.blueprints.SchematicFactory; import buildcraft.api.blueprints.SchematicMask; import buildcraft.api.core.Position; @@ -135,12 +136,19 @@ public class BuildingSlotBlock extends BuildingSlot implements Comparable