completed builder status save, for #1575

This commit is contained in:
SpaceToad 2014-04-18 08:50:48 +02:00
parent 22f0ecdd36
commit d1dbabdabd
9 changed files with 171 additions and 23 deletions

View file

@ -25,8 +25,12 @@ 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.SchematicEntity;
import buildcraft.api.blueprints.SchematicFactory;
import buildcraft.api.blueprints.SchematicFactoryBlock;
import buildcraft.api.blueprints.SchematicFactoryEntity;
import buildcraft.api.blueprints.SchematicFactoryMask;
import buildcraft.api.blueprints.SchematicMask;
import buildcraft.api.blueprints.SchematicRegistry;
import buildcraft.api.filler.FillerManager;
import buildcraft.api.filler.IFillerPattern;
@ -287,6 +291,8 @@ public class BuildCraftBuilders extends BuildCraftMod {
// Factories required to save entities in world
SchematicFactory.registerSchematicFactory(SchematicBlock.class, new SchematicFactoryBlock());
SchematicFactory.registerSchematicFactory(SchematicMask.class, new SchematicFactoryMask());
SchematicFactory.registerSchematicFactory(SchematicEntity.class, new SchematicFactoryEntity());
if (BuildCraftCore.loadDefaultRecipes) {
loadRecipes();

View file

@ -39,7 +39,7 @@ import buildcraft.core.utils.Utils;
* such block id is found, BuildCraft will assume that the block is not
* installed and will not load the blueprint.
*/
public class Schematic {
public abstract class Schematic {
/**
* Return true if the block on the world correspond to the block stored in

View file

@ -32,7 +32,7 @@ package buildcraft.api.blueprints;
* such block id is found, BuildCraft will assume that the block is not
* installed and will not load the blueprint.
*/
public class SchematicBlockBase extends Schematic {
public abstract class SchematicBlockBase extends Schematic {
public void writeCompleted(IBuilderContext context, int x, int y, int z, double completed) {

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 SchematicFactoryEntity extends SchematicFactory <SchematicEntity> {
@Override
protected SchematicEntity loadSchematicFromWorldNBT (NBTTagCompound nbt, MappingRegistry registry) {
int entityId = nbt.getInteger("entityId");
SchematicEntity s = SchematicRegistry.newSchematicEntity(registry.getEntityForId(entityId));
if (s != null) {
s.readFromNBT(nbt, registry);
} else {
return null;
}
return s;
}
@Override
public void saveSchematicToWorldNBT (NBTTagCompound nbt, SchematicEntity object, MappingRegistry registry) {
super.saveSchematicToWorldNBT(nbt, object, registry);
nbt.setInteger("entityId", registry.getIdForEntity(object.entity));
object.writeToNBT(nbt, registry);
}
}

View file

@ -0,0 +1,30 @@
/**
* 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 SchematicFactoryMask extends SchematicFactory <SchematicMask> {
@Override
protected SchematicMask loadSchematicFromWorldNBT (NBTTagCompound nbt, MappingRegistry registry) {
SchematicMask s = new SchematicMask();
s.readFromNBT(nbt, registry);
return s;
}
@Override
public void saveSchematicToWorldNBT (NBTTagCompound nbt, SchematicMask object, MappingRegistry registry) {
super.saveSchematicToWorldNBT(nbt, object, registry);
object.writeToNBT(nbt, registry);
}
}

View file

@ -12,6 +12,7 @@ import java.util.LinkedList;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.BlockUtil;
@ -19,6 +20,10 @@ public class SchematicMask extends SchematicBlockBase {
public boolean isConcrete = true;
public SchematicMask () {
}
public SchematicMask (boolean isConcrete) {
this.isConcrete = isConcrete;
}
@ -67,4 +72,13 @@ public class SchematicMask extends SchematicBlockBase {
}
}
@Override
public void writeToNBT(NBTTagCompound nbt, MappingRegistry registry) {
nbt.setBoolean("isConcrete", isConcrete);
}
@Override
public void readFromNBT(NBTTagCompound nbt, MappingRegistry registry) {
isConcrete = nbt.getBoolean("isConcrete");
}
}

View file

@ -15,10 +15,12 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Map.Entry;
import java.util.TreeSet;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraft.world.WorldSettings.GameType;
import buildcraft.api.blueprints.Schematic;
@ -37,6 +39,8 @@ public class BptBuilderBlueprint extends BptBuilderBase {
private LinkedList<BuildingSlotEntity> entityList = new LinkedList<BuildingSlotEntity>();
private LinkedList<BuildingSlot> postProcessing = new LinkedList<BuildingSlot>();
protected TreeSet <Integer> builtEntities = new TreeSet <Integer> ();
private BuildingSlotIterator iterator;
public LinkedList <ItemStack> neededItems = new LinkedList <ItemStack> ();
@ -83,22 +87,25 @@ public class BptBuilderBlueprint extends BptBuilderBase {
int yCoord = j + y - blueprint.anchorY;
int zCoord = k + z - blueprint.anchorZ;
SchematicBlock slot = (SchematicBlock) bluePrint.contents[i][j][k];
if (slot == null) {
continue;
}
BuildingSlotBlock b = new BuildingSlotBlock();
b.schematic = slot;
b.x = xCoord;
b.y = yCoord;
b.z = zCoord;
b.mode = Mode.Build;
if (!builtLocations.contains(new BlockIndex(xCoord, yCoord,
zCoord))) {
SchematicBlock slot = (SchematicBlock) bluePrint.contents[i][j][k];
if (slot == null) {
continue;
}
BuildingSlotBlock b = new BuildingSlotBlock();
b.schematic = slot;
b.x = xCoord;
b.y = yCoord;
b.z = zCoord;
b.mode = Mode.Build;
zCoord))) {
tmpBuildList.add(b);
} else {
postProcessing.add(b);
}
}
}
@ -110,12 +117,21 @@ public class BptBuilderBlueprint extends BptBuilderBase {
iterator = new BuildingSlotIterator(buildList);
int seqId = 0;
for (SchematicEntity e : bluePrint.entities) {
// TODO: take into account items already built... How to identify
// them? id in list?
BuildingSlotEntity b = new BuildingSlotEntity();
b.schematic = e;
entityList.add(b);
b.sequenceNumber = seqId;
if (!builtEntities.contains(seqId)) {
entityList.add(b);
} else {
postProcessing.add(b);
}
seqId++;
}
recomputeNeededItems();
@ -226,6 +242,7 @@ public class BptBuilderBlueprint extends BptBuilderBase {
it.remove();
postProcessing.add(slot);
builtEntities.add(slot.sequenceNumber);
return slot;
}
}
@ -526,4 +543,31 @@ public class BptBuilderBlueprint extends BptBuilderBase {
}
}
@Override
public void saveBuildStateToNBT (NBTTagCompound nbt, TileAbstractBuilder builder) {
super.saveBuildStateToNBT(nbt, builder);
int [] entitiesBuiltArr = new int [builtEntities.size()];
int id = 0;
for (Integer i : builtEntities) {
entitiesBuiltArr [id] = i;
id++;
}
nbt.setIntArray("builtEntities", entitiesBuiltArr);
}
@Override
public void loadBuildStateToNBT (NBTTagCompound nbt, TileAbstractBuilder builder) {
super.loadBuildStateToNBT(nbt, builder);
int [] entitiesBuiltArr = nbt.getIntArray("builtEntities");
for (int i = 0; i < entitiesBuiltArr.length; ++i) {
builtEntities.add(i);
}
}
}

View file

@ -15,6 +15,7 @@ import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.blueprints.SchematicBlockBase;
import buildcraft.builders.TileAbstractBuilder;
import buildcraft.core.BlockIndex;
import buildcraft.core.blueprints.BuildingSlotBlock.Mode;
import buildcraft.core.inventory.InventoryIterator;
import buildcraft.core.inventory.InventoryIterator.IInvSlot;
@ -37,7 +38,7 @@ public class BptBuilderTemplate extends BptBuilderBase {
SchematicBlockBase slot = bluePrint.contents[i][j][k];
if (slot == null) {
if (slot == null && !clearedLocations.contains(new BlockIndex(xCoord, yCoord, zCoord))) {
BuildingSlotBlock b = new BuildingSlotBlock();
b.schematic = null;
@ -61,7 +62,7 @@ public class BptBuilderTemplate extends BptBuilderBase {
SchematicBlockBase slot = bluePrint.contents[i][j][k];
if (slot != null) {
if (slot != null && !builtLocations.contains(new BlockIndex(xCoord, yCoord, zCoord))) {
BuildingSlotBlock b = new BuildingSlotBlock();
b.schematic = slot;
@ -133,10 +134,12 @@ public class BptBuilderTemplate extends BptBuilderBase {
if (BlockUtil.isSoftBlock(world, slot.x, slot.y, slot.z)
|| BlockUtil.isUnbreakableBlock(world, slot.x, slot.y, slot.z)) {
iterator.remove();
clearedLocations.add(new BlockIndex(slot.x, slot.y, slot.z));
} else {
if (setupForDestroy(builder, context, slot)) {
result = slot;
iterator.remove();
clearedLocations.add(new BlockIndex(slot.x, slot.y, slot.z));
break;
}
@ -144,6 +147,7 @@ public class BptBuilderTemplate extends BptBuilderBase {
} else if (slot.mode == Mode.Build) {
if (!BlockUtil.isSoftBlock(world, slot.x, slot.y, slot.z)) {
iterator.remove();
builtLocations.add(new BlockIndex(slot.x, slot.y, slot.z));
} else {
if (builder.energyAvailable() > TileAbstractBuilder.BUILD_ENERGY && firstSlotToConsume != null) {
builder.consumeEnergy(TileAbstractBuilder.BUILD_ENERGY);
@ -152,6 +156,7 @@ public class BptBuilderTemplate extends BptBuilderBase {
.decreaseStackInSlot());
result = slot;
iterator.remove();
builtLocations.add(new BlockIndex(slot.x, slot.y, slot.z));
break;
}

View file

@ -16,12 +16,20 @@ import net.minecraft.nbt.NBTTagList;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.MappingRegistry;
import buildcraft.api.blueprints.SchematicEntity;
import buildcraft.api.blueprints.SchematicFactory;
import buildcraft.api.core.Position;
public class BuildingSlotEntity extends BuildingSlot {
public SchematicEntity schematic;
/**
* This value is set by builders to identify in which order entities are
* being built. It can be used later for unique identification within a
* blueprint.
*/
public int sequenceNumber;
@Override
public void writeToWorld(IBuilderContext context) {
schematic.writeToWorld(context);
@ -59,11 +67,15 @@ public class BuildingSlotEntity extends BuildingSlot {
@Override
public void writeToNBT (NBTTagCompound nbt, MappingRegistry registry) {
NBTTagCompound schematicNBT = new NBTTagCompound();
SchematicFactory.getFactory(schematic.getClass())
.saveSchematicToWorldNBT(schematicNBT, schematic, registry);
nbt.setTag("schematic", schematicNBT);
}
@Override
public void readFromNBT (NBTTagCompound nbt, MappingRegistry registry) {
schematic = (SchematicEntity) SchematicFactory
.createSchematicFromWorldNBT(nbt.getCompoundTag("schematic"), registry);
}
}