made progress for item requirements support in blueprints, for #1565

This commit is contained in:
SpaceToad 2014-04-05 19:03:53 +02:00
parent a9701ec4e4
commit b07d0b059d
14 changed files with 251 additions and 49 deletions

View file

@ -264,14 +264,14 @@ public class BuildCraftBuilders extends BuildCraftMod {
// Standard entities
SchematicRegistry.registerSchematicEntity(EntityMinecartEmpty.class, SchematicMinecart.class);
SchematicRegistry.registerSchematicEntity(EntityMinecartFurnace.class, SchematicMinecart.class);
SchematicRegistry.registerSchematicEntity(EntityMinecartTNT.class, SchematicMinecart.class);
SchematicRegistry.registerSchematicEntity(EntityMinecartChest.class, SchematicMinecart.class);
SchematicRegistry.registerSchematicEntity(EntityMinecartHopper.class, SchematicMinecart.class);
SchematicRegistry.registerSchematicEntity(EntityMinecartEmpty.class, SchematicMinecart.class, Items.minecart);
SchematicRegistry.registerSchematicEntity(EntityMinecartFurnace.class, SchematicMinecart.class, Items.furnace_minecart);
SchematicRegistry.registerSchematicEntity(EntityMinecartTNT.class, SchematicMinecart.class, Items.tnt_minecart);
SchematicRegistry.registerSchematicEntity(EntityMinecartChest.class, SchematicMinecart.class, Items.chest_minecart);
SchematicRegistry.registerSchematicEntity(EntityMinecartHopper.class, SchematicMinecart.class, Items.hopper_minecart);
SchematicRegistry.registerSchematicEntity(EntityPainting.class, SchematicHanging.class);
SchematicRegistry.registerSchematicEntity(EntityItemFrame.class, SchematicHanging.class);
SchematicRegistry.registerSchematicEntity(EntityPainting.class, SchematicHanging.class, Items.painting);
SchematicRegistry.registerSchematicEntity(EntityItemFrame.class, SchematicHanging.class, Items.item_frame);
// BuildCraft blocks

View file

@ -108,15 +108,6 @@ public class Schematic {
return result;
}
/**
* Return true if the block on the world correspond to the block stored in
* the blueprint at the location given by the slot. By default, this
* subprogram is permissive and doesn't take into account metadata.
*/
public boolean isValid(IBuilderContext context, int x, int y, int z) {
return true;
}
/**
* Perform a 90 degree rotation to the slot.
*/
@ -132,18 +123,6 @@ public class Schematic {
}
public void writeCompleted(IBuilderContext context, int x, int y, int z, double completed) {
}
/**
* Return true if the block should not be placed to the world. Requirements
* will not be asked on such a block, and building will not be called.
*/
public boolean ignoreBuilding() {
return false;
}
/**
* Initializes a slot from the blueprint according to an objet placed on {x,
* y, z} on the world. This typically means adding entries in slot.cpt. Note

View file

@ -18,7 +18,7 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import buildcraft.core.utils.Utils;
public class SchematicBlock extends Schematic implements Comparable<SchematicBlock> {
public class SchematicBlock extends SchematicBlockBase implements Comparable<SchematicBlock> {
public Block block = null;
public int meta = 0;

View file

@ -0,0 +1,58 @@
/**
* 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;
/**
* This class allow to specify specific behavior for blocks stored in
* blueprints:
*
* - what items needs to be used to create that block - how the block has to be
* built on the world - how to rotate the block - what extra data to store /
* load in the blueprint
*
* Default implementations of this can be seen in the package
* buildcraft.api.schematics. The class SchematicUtils provide some additional
* utilities.
*
* Blueprints perform "id translation" in case the block ids between a blueprint
* and the world installation are different. Mapping is done through the
* builder context.
*
* At blueprint load time, BuildCraft will check that each block id of the
* blueprint corresponds to the block id in the installation. If not, it will
* perform a search through the block list, and upon matching signature, it will
* translate all blocks ids of the blueprint to the installation ones. If no
* 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 {
/**
* Return true if the block on the world correspond to the block stored in
* the blueprint at the location given by the slot. By default, this
* subprogram is permissive and doesn't take into account metadata.
*/
public boolean isValid(IBuilderContext context, int x, int y, int z) {
return true;
}
public void writeCompleted(IBuilderContext context, int x, int y, int z, double completed) {
}
/**
* Return true if the block should not be placed to the world. Requirements
* will not be asked on such a block, and building will not be called.
*/
public boolean ignoreBuilding() {
return false;
}
}

View file

@ -8,20 +8,32 @@
*/
package buildcraft.api.blueprints;
import java.util.ArrayList;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagDouble;
import net.minecraft.nbt.NBTTagFloat;
import net.minecraft.nbt.NBTTagList;
import buildcraft.api.core.Position;
import buildcraft.core.utils.Utils;
public class SchematicEntity {
public class SchematicEntity extends Schematic {
public Class <? extends Entity> entity;
public NBTTagCompound cpt = new NBTTagCompound();
/**
* This field contains requirements for a given block when stored in the
* blueprint. Modders can either rely on this list or compute their own int
* Schematic.
*/
public ItemStack [] storedRequirements = new ItemStack [0];
public void writeToWorld(IBuilderContext context, CoordTransformation transform) {
NBTTagList nbttaglist = cpt.getTagList("Pos", 6);
Position pos = new Position(nbttaglist.func_150309_d(0),
@ -44,6 +56,7 @@ public class SchematicEntity {
cpt.setTag("Pos", this.newDoubleNBTList(new double[] {pos.x, pos.y, pos.z}));
}
@Override
public void rotateLeft(IBuilderContext context) {
NBTTagList nbttaglist = cpt.getTagList("Pos", 6);
Position pos = new Position(nbttaglist.func_150309_d(0),
@ -57,13 +70,54 @@ public class SchematicEntity {
cpt.setTag("Rotation", this.newFloatNBTList(new float[] {yaw, nbttaglist.func_150308_e (1)}));
}
@Override
public void writeToNBT(NBTTagCompound nbt, MappingRegistry registry) {
nbt.setInteger ("entityId", registry.getIdForEntity(entity));
nbt.setTag("entity", cpt);
NBTTagList rq = new NBTTagList();
for (ItemStack stack : storedRequirements) {
NBTTagCompound sub = new NBTTagCompound();
stack.writeToNBT(stack.writeToNBT(sub));
sub.setInteger("id", registry.getIdForItem(stack.getItem()));
rq.appendTag(sub);
}
nbt.setTag("rq", rq);
}
@Override
public void readFromNBT(NBTTagCompound nbt, MappingRegistry registry) {
cpt = nbt.getCompoundTag("entity");
NBTTagList rq = nbt.getTagList("rq", Utils.NBTTag_Types.NBTTagCompound.ordinal());
ArrayList<ItemStack> rqs = new ArrayList<ItemStack>();
for (int i = 0; i < rq.tagCount(); ++i) {
try {
NBTTagCompound sub = rq.getCompoundTagAt(i);
if (sub.getInteger("id") >= 0) {
// Maps the id in the blueprint to the id in the world
sub.setInteger("id", Item.itemRegistry
.getIDForObject(registry.getItemForId(sub
.getInteger("id"))));
rqs.add(ItemStack.loadItemStackFromNBT(sub));
} else {
// TODO: requirement can't be retreived, this blueprint is
// only useable in creative
}
} catch (Throwable t) {
t.printStackTrace();
// TODO: requirement can't be retreived, this blueprint is
// only useable in creative
}
}
storedRequirements = rqs.toArray(new ItemStack [rqs.size()]);
}
protected NBTTagList newDoubleNBTList(double... par1ArrayOfDouble) {

View file

@ -15,7 +15,7 @@ import net.minecraft.item.ItemStack;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.BlockUtil;
public class SchematicMask extends Schematic {
public class SchematicMask extends SchematicBlockBase {
public boolean isConcrete = true;

View file

@ -9,6 +9,10 @@
package buildcraft.builders.schematics;
import net.minecraft.entity.Entity;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.api.blueprints.CoordTransformation;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.SchematicEntity;
@ -16,6 +20,12 @@ import buildcraft.api.core.Position;
public class SchematicHanging extends SchematicEntity {
private Item baseItem;
public SchematicHanging (Item baseItem) {
this.baseItem = baseItem;
}
@Override
public void rotateLeft(IBuilderContext context) {
super.rotateLeft(context);
@ -39,6 +49,13 @@ public class SchematicHanging extends SchematicEntity {
cpt.setInteger("TileY", (int) pos.y);
cpt.setInteger("TileZ", (int) pos.z);
if (baseItem == Items.item_frame) {
NBTTagCompound tag = cpt.getCompoundTag("Item");
tag.setInteger("id", Item.itemRegistry.getIDForObject(context
.getMappingRegistry().getItemForId(tag.getInteger("id"))));
cpt.setTag("Item", tag);
}
super.writeToWorld(context, transform);
}
@ -52,5 +69,20 @@ public class SchematicHanging extends SchematicEntity {
cpt.setInteger("TileX", (int) pos.x);
cpt.setInteger("TileY", (int) pos.y);
cpt.setInteger("TileZ", (int) pos.z);
if (baseItem == Items.item_frame) {
NBTTagCompound tag = cpt.getCompoundTag("Item");
ItemStack stack = ItemStack.loadItemStackFromNBT(tag);
storedRequirements = new ItemStack [2];
storedRequirements [0] = new ItemStack(baseItem);
storedRequirements [1] = stack;
tag.setInteger("id", context.getMappingRegistry().getIdForItem(stack.getItem()));
cpt.setTag("Item", tag);
} else {
storedRequirements = new ItemStack [1];
storedRequirements [0] = new ItemStack(baseItem);
}
}
}

View file

@ -9,6 +9,8 @@
package buildcraft.builders.schematics;
import net.minecraft.entity.Entity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagList;
import buildcraft.api.blueprints.CoordTransformation;
import buildcraft.api.blueprints.IBuilderContext;
@ -17,6 +19,12 @@ import buildcraft.api.core.Position;
public class SchematicMinecart extends SchematicEntity {
private Item baseItem;
public SchematicMinecart (Item baseItem) {
this.baseItem = baseItem;
}
@Override
public void writeToWorld(IBuilderContext context, CoordTransformation transform) {
NBTTagList nbttaglist = cpt.getTagList("Pos", 6);
@ -39,5 +47,8 @@ public class SchematicMinecart extends SchematicEntity {
pos.x -= 0.5;
pos.z -= 0.5;
cpt.setTag("Pos", this.newDoubleNBTList(new double[] {pos.x, pos.y, pos.z}));
storedRequirements = new ItemStack [1];
storedRequirements [0] = new ItemStack(baseItem);
}
}

View file

@ -17,7 +17,7 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.MappingRegistry;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.SchematicBlockBase;
import buildcraft.builders.blueprints.BlueprintId;
import buildcraft.core.Box;
import buildcraft.core.Version;
@ -25,7 +25,7 @@ import buildcraft.core.utils.BCLog;
public abstract class BlueprintBase {
public Schematic contents[][][];
public SchematicBlockBase contents[][][];
public int anchorX, anchorY, anchorZ;
public int sizeX, sizeY, sizeZ;
public BlueprintId id = new BlueprintId();
@ -40,7 +40,7 @@ public abstract class BlueprintBase {
}
public BlueprintBase(int sizeX, int sizeY, int sizeZ) {
contents = new Schematic[sizeX][sizeY][sizeZ];
contents = new SchematicBlockBase[sizeX][sizeY][sizeZ];
this.sizeX = sizeX;
this.sizeY = sizeY;
@ -52,7 +52,7 @@ public abstract class BlueprintBase {
}
public void rotateLeft(BptContext context) {
Schematic newContents[][][] = new Schematic[sizeZ][sizeY][sizeX];
SchematicBlockBase newContents[][][] = new SchematicBlockBase[sizeZ][sizeY][sizeX];
for (int x = 0; x < sizeZ; ++x) {
for (int y = 0; y < sizeY; ++y) {
@ -150,7 +150,7 @@ public abstract class BlueprintBase {
rotate = true;
}
contents = new Schematic [sizeX][sizeY][sizeZ];
contents = new SchematicBlockBase [sizeX][sizeY][sizeZ];
try {
loadContents (nbt);

View file

@ -11,6 +11,7 @@ package buildcraft.core.blueprints;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Map.Entry;
@ -21,6 +22,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraft.world.WorldSettings.GameType;
import buildcraft.api.blueprints.CoordTransformation;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.SchematicBlock;
import buildcraft.api.blueprints.SchematicEntity;
import buildcraft.api.core.StackKey;
@ -138,7 +140,7 @@ public class BptBuilderBlueprint extends BptBuilderBase {
}
if (entityList.size() != 0) {
BuildingSlot slot = entityList.removeFirst();
BuildingSlot slot = internalGetNextEntity(world, inv, entityList);
checkDone ();
if (slot != null) {
@ -185,7 +187,7 @@ public class BptBuilderBlueprint extends BptBuilderBase {
} else {
if (BlockUtil.isSoftBlock(world, slot.x, slot.y, slot.z)
&& checkRequirements(builder,
(SchematicBlock) slot.schematic)) {
slot.schematic)) {
useRequirements(builder, slot);
iterator.remove();
@ -201,11 +203,26 @@ public class BptBuilderBlueprint extends BptBuilderBase {
return null;
}
public boolean checkRequirements(TileAbstractBuilder builder, SchematicBlock slot) {
if (slot.block == null) {
return true;
private BuildingSlot internalGetNextEntity(World world,
TileAbstractBuilder builder, LinkedList<BuildingSlotEntity> list) {
Iterator<BuildingSlotEntity> it = list.iterator();
while (iterator.hasNext()) {
BuildingSlotEntity slot = it.next();
if (checkRequirements(builder, slot.schematic)) {
useRequirements(builder, slot);
iterator.remove();
postProcessing.add(slot);
return slot;
}
}
return null;
}
public boolean checkRequirements(TileAbstractBuilder builder, Schematic slot) {
double energyRequired = 0;
LinkedList<ItemStack> tmpReq = new LinkedList<ItemStack>();
@ -268,7 +285,7 @@ public class BptBuilderBlueprint extends BptBuilderBase {
return true;
}
public void useRequirements(TileAbstractBuilder builder, BuildingSlotBlock slot) {
public void useRequirements(TileAbstractBuilder builder, BuildingSlot slot) {
LinkedList<ItemStack> tmpReq = new LinkedList<ItemStack>();
double energyRequired = 0;
@ -377,6 +394,35 @@ public class BptBuilderBlueprint extends BptBuilderBase {
BCLog.logger.throwing("BptBuilderBlueprint", "recomputeIfNeeded", t);
}
for (ItemStack stack : stacks) {
if (stack == null || stack.getItem() == null || stack.stackSize == 0) {
continue;
}
StackKey key = new StackKey(stack);
if (!computeStacks.containsKey(key)) {
computeStacks.put(key, stack.stackSize);
} else {
Integer num = computeStacks.get(key);
num += stack.stackSize;
computeStacks.put(key, num);
}
}
}
for (BuildingSlotEntity slot : entityList) {
LinkedList<ItemStack> stacks = new LinkedList<ItemStack>();
try {
stacks = slot.getRequirements(context);
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing("BptBuilderBlueprint", "recomputeIfNeeded", t);
}
for (ItemStack stack : stacks) {
if (stack == null || stack.getItem() == null || stack.stackSize == 0) {
continue;
@ -402,7 +448,6 @@ public class BptBuilderBlueprint extends BptBuilderBase {
neededItems.add(newStack);
}
LinkedList <ItemStack> sortedList = new LinkedList <ItemStack> ();
for (ItemStack toInsert : neededItems) {

View file

@ -13,7 +13,7 @@ import java.util.LinkedList;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.SchematicBlockBase;
import buildcraft.builders.TileAbstractBuilder;
import buildcraft.core.blueprints.BuildingSlotBlock.Mode;
import buildcraft.core.inventory.InventoryIterator;
@ -35,7 +35,7 @@ public class BptBuilderTemplate extends BptBuilderBase {
int yCoord = j + y - blueprint.anchorY;
int zCoord = k + z - blueprint.anchorZ;
Schematic slot = bluePrint.contents[i][j][k];
SchematicBlockBase slot = bluePrint.contents[i][j][k];
if (slot == null) {
BuildingSlotBlock b = new BuildingSlotBlock();
@ -59,7 +59,7 @@ public class BptBuilderTemplate extends BptBuilderBase {
int yCoord = j + y - blueprint.anchorY;
int zCoord = k + z - blueprint.anchorZ;
Schematic slot = bluePrint.contents[i][j][k];
SchematicBlockBase slot = bluePrint.contents[i][j][k];
if (slot != null) {
BuildingSlotBlock b = new BuildingSlotBlock();

View file

@ -12,6 +12,7 @@ import java.util.LinkedList;
import net.minecraft.item.ItemStack;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.core.Position;
public abstract class BuildingSlot {
@ -43,4 +44,6 @@ public abstract class BuildingSlot {
stackConsumed.add (stack);
}
public abstract Schematic getSchematic ();
}

View file

@ -13,14 +13,14 @@ import java.util.LinkedList;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.SchematicBlockBase;
import buildcraft.api.blueprints.SchematicMask;
import buildcraft.api.core.Position;
public class BuildingSlotBlock extends BuildingSlot implements Comparable<BuildingSlotBlock> {
public int x, y, z;
public Schematic schematic;
public SchematicBlockBase schematic;
public enum Mode {
ClearIfInvalid, Build
@ -28,7 +28,8 @@ public class BuildingSlotBlock extends BuildingSlot implements Comparable<Buildi
public Mode mode = Mode.Build;
public Schematic getSchematic () {
@Override
public SchematicBlockBase getSchematic () {
if (schematic == null) {
return new SchematicMask(false);
} else {

View file

@ -8,6 +8,9 @@
*/
package buildcraft.core.blueprints;
import java.util.LinkedList;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagList;
import buildcraft.api.blueprints.CoordTransformation;
import buildcraft.api.blueprints.IBuilderContext;
@ -33,4 +36,20 @@ public class BuildingSlotEntity extends BuildingSlot {
return pos;
}
@Override
public LinkedList<ItemStack> getRequirements (IBuilderContext context) {
LinkedList<ItemStack> results = new LinkedList<ItemStack>();
for (ItemStack s : schematic.storedRequirements) {
results.add(s);
}
return results;
}
@Override
public SchematicEntity getSchematic() {
return schematic;
}
}