made progress in saving building items, for #1575

This commit is contained in:
SpaceToad 2014-04-13 19:22:41 +02:00
parent a65f6f5bb6
commit e15ecdbac9
8 changed files with 171 additions and 3 deletions

View file

@ -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();
}

View file

@ -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 <S extends Schematic> {
private static final HashMap <String, SchematicFactory> factories =
new HashMap <String, SchematicFactory> ();
private static final HashMap <Class <? extends Schematic>, SchematicFactory> schematicToFactory =
new HashMap <Class <? extends Schematic>, 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 <? extends Schematic> clas, SchematicFactory factory) {
schematicToFactory.put(clas, factory);
factories.put(factory.getClass().getCanonicalName(), factory);
}
public static SchematicFactory getFactory (Class <? extends Schematic> clas) {
Class superClass = clas.getSuperclass();
if (schematicToFactory.containsKey(clas)) {
return schematicToFactory.get(clas);
} else if (superClass != null) {
return getFactory(superClass);
} else {
return null;
}
}
}

View file

@ -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 <SchematicBlock> {
@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);
}
}

View file

@ -82,7 +82,6 @@ public class SchematicRegistry {
}
public static SchematicEntity newSchematicEntity (Class <? extends Entity> entityClass) {
if (!schematicEntities.containsKey(entityClass)) {
return null;
}
@ -106,4 +105,5 @@ public class SchematicRegistry {
return null;
}
}

View file

@ -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 <ItemStack> stacksToBuild = new LinkedList<ItemStack>();
@ -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);
}
}

View file

@ -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);
}
}
}

View file

@ -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);

View file

@ -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<Buildi
nbt.setInteger("z", z);
NBTTagCompound schematicNBT = new NBTTagCompound();
schematic.writeToNBT(schematicNBT, registry);
SchematicFactory.getFactory(schematic.getClass())
.saveSchematicToWorldNBT(schematicNBT, schematic, registry);
nbt.setTag("schematic", schematicNBT);
}
@Override
public void readFromNBT (NBTTagCompound nbt, MappingRegistry registry) {
mode = Mode.values() [nbt.getByte("mode")];
x = nbt.getInteger("x");
y = nbt.getInteger("y");
z = nbt.getInteger("z");
schematic = (SchematicBlockBase) SchematicFactory
.createSchematicFromWorldNBT(nbt, registry);
}
}