Fixed issues with requirements.

Inventories are now properly stored in blueprints, for #1507
This commit is contained in:
SpaceToad 2014-03-16 13:11:12 +01:00
parent 047882ef59
commit 1ea19c2ae6
8 changed files with 59 additions and 161 deletions

View file

@ -38,14 +38,13 @@ import buildcraft.factory.BlockPump;
import buildcraft.factory.BlockQuarry;
import buildcraft.factory.BlockRefinery;
import buildcraft.factory.BlockTank;
import buildcraft.factory.SchematicAutoWorkbench;
import buildcraft.factory.SchematicFrame;
import buildcraft.factory.SchematicRefinery;
import buildcraft.factory.SchematicTank;
import buildcraft.factory.FactoryProxy;
import buildcraft.factory.FactoryProxyClient;
import buildcraft.factory.GuiHandler;
import buildcraft.factory.PumpDimensionList;
import buildcraft.factory.SchematicFrame;
import buildcraft.factory.SchematicRefinery;
import buildcraft.factory.SchematicTank;
import buildcraft.factory.TileAutoWorkbench;
import buildcraft.factory.TileFloodGate;
import buildcraft.factory.TileHopper;
@ -146,7 +145,6 @@ public class BuildCraftFactory extends BuildCraftMod {
FactoryProxy.proxy.initializeTileEntities();
SchematicRegistry.registerSchematicClass(autoWorkbenchBlock, SchematicAutoWorkbench.class);
SchematicRegistry.registerSchematicClass(frameBlock, SchematicFrame.class);
SchematicRegistry.registerSchematicClass(refineryBlock, SchematicRefinery.class);
SchematicRegistry.registerSchematicClass(tankBlock, SchematicTank.class);

View file

@ -14,8 +14,6 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import org.apache.commons.lang3.ArrayUtils;
/**
* This class allow to specify specific behavior for blocks stored in
* blueprints:
@ -41,13 +39,6 @@ import org.apache.commons.lang3.ArrayUtils;
*/
public class Schematic {
/**
* 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];
@SuppressWarnings("unchecked")
@Override
public Schematic clone() {
@ -58,8 +49,6 @@ public class Schematic {
return null;
}
obj.storedRequirements = ArrayUtils.clone(storedRequirements);
return obj;
}

View file

@ -192,8 +192,7 @@ public class SchematicBlock extends Schematic {
for (ItemStack stack : storedRequirements) {
NBTTagCompound sub = new NBTTagCompound();
stack.writeToNBT(stack.writeToNBT(sub));
sub.setInteger("id", Item.itemRegistry.getIDForObject(registry
.getItemForId(sub.getInteger("id"))));
sub.setInteger("id", registry.getIdForItem(stack.getItem()));
rq.appendTag(sub);
}
@ -205,17 +204,32 @@ public class SchematicBlock extends Schematic {
block = registry.getBlockForId(nbt.getInteger("blockId"));
meta = nbt.getInteger("blockMeta");
NBTTagList rq = nbt.getTagList("rq", Utils.NBTTag_Types.NBTTagList.ordinal());
storedRequirements = new ItemStack[rq.tagCount()];
NBTTagList rq = nbt.getTagList("rq", Utils.NBTTag_Types.NBTTagCompound.ordinal());
ArrayList<ItemStack> rqs = new ArrayList<ItemStack>();
for (int i = 0; i < rq.tagCount(); ++i) {
NBTTagCompound sub = rq.getCompoundTagAt(i);
try {
NBTTagCompound sub = rq.getCompoundTagAt(i);
// Maps the id in the blueprint to the id in the world
sub.setInteger("id", Item.itemRegistry.getIDForObject(registry
.getItemForId(sub.getInteger("id"))));
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"))));
storedRequirements [i] = ItemStack.loadItemStackFromNBT(sub);
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()]);
}
}

View file

@ -8,10 +8,14 @@
*/
package buildcraft.api.blueprints;
import java.util.ArrayList;
import net.minecraft.block.BlockContainer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import buildcraft.core.utils.Utils;
public class SchematicTile extends SchematicBlock {
@ -50,15 +54,6 @@ public class SchematicTile extends SchematicBlock {
if (tile != null) {
tile.readFromNBT(cpt);
}
// By default, clear the inventory to avoid possible dupe bugs
if (tile instanceof IInventory) {
IInventory inv = (IInventory) tile;
for (int i = 0; i < inv.getSizeInventory(); ++i) {
inv.setInventorySlotContents(i, null);
}
}
}
}
@ -81,6 +76,21 @@ public class SchematicTile extends SchematicBlock {
if (tile != null) {
tile.writeToNBT(cpt);
}
if (tile instanceof IInventory) {
IInventory inv = (IInventory) tile;
ArrayList <ItemStack> rqs = new ArrayList <ItemStack> ();
for (int i = 0; i < inv.getSizeInventory(); ++i) {
if (inv.getStackInSlot(i) != null) {
rqs.add(inv.getStackInSlot(i));
}
}
storedRequirements = Utils.concat(storedRequirements,
rqs.toArray(new ItemStack[rqs.size()]));
}
}
}

View file

@ -1,83 +0,0 @@
/**
* 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.LinkedList;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
public class SchematicUtils {
public static void requestInventoryContents(SchematicTile slot, IBuilderContext context, LinkedList<ItemStack> requirements) {
ItemStack[] stacks = getItemStacks(slot, context);
for (ItemStack stack : stacks) {
if (stack != null) {
requirements.add(stack);
}
}
}
public static void initializeInventoryContents(SchematicTile slot, IBuilderContext context, IInventory inventory) {
ItemStack[] stacks = new ItemStack[inventory.getSizeInventory()];
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
stacks[i] = inventory.getStackInSlot(i);
}
setItemStacks(slot, context, stacks);
}
public static void buildInventoryContents(SchematicTile slot, IBuilderContext context, IInventory inventory) {
ItemStack[] stacks = getItemStacks(slot, context);
for (int i = 0; i < stacks.length; ++i) {
inventory.setInventorySlotContents(i, stacks[i]);
}
}
public static ItemStack[] getItemStacks(SchematicTile slot, IBuilderContext context) {
NBTTagList list = (NBTTagList) slot.cpt.getTag("inv");
if (list == null) {
return new ItemStack[0];
}
ItemStack stacks[] = new ItemStack[list.tagCount()];
for (int i = 0; i < list.tagCount(); ++i) {
//ItemStack stack = ItemStack.loadItemStackFromNBT((NBTTagCompound) list.tagAt(i));
//if (stack != null && stack.itemID != 0 && stack.stackSize > 0) {
// stacks[i] = context.mapItemStack(stack);
//}
}
return stacks;
}
public static void setItemStacks(SchematicTile slot, IBuilderContext context, ItemStack[] stacks) {
NBTTagList nbttaglist = new NBTTagList();
for (ItemStack stack : stacks) {
NBTTagCompound cpt = new NBTTagCompound();
nbttaglist.appendTag(cpt);
if (stack != null && stack.stackSize != 0) {
stack.writeToNBT(cpt);
//context.storeId(stack.itemID);
}
}
slot.cpt.setTag("inv", nbttaglist);
}
}

View file

@ -261,6 +261,7 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory, IM
bpt = ItemBlueprint.loadBlueprint(items [0]);
} catch (Throwable t) {
setInventorySlotContents(0, null);
t.printStackTrace();
return null;
}

View file

@ -13,6 +13,7 @@ import io.netty.buffer.Unpooled;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -561,4 +562,16 @@ public class Utils {
nbt.setTag(name, nbttaglist);
}
public <T> T[] concatenate (T[] A, T[] B) {
int aLen = A.length;
int bLen = B.length;
@SuppressWarnings("unchecked")
T[] C = (T[]) Array.newInstance(A.getClass().getComponentType(), aLen+bLen);
System.arraycopy(A, 0, C, 0, aLen);
System.arraycopy(B, 0, C, aLen, bLen);
return C;
}
}

View file

@ -1,44 +0,0 @@
/**
* 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.factory;
import java.util.LinkedList;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.SchematicTile;
import buildcraft.api.blueprints.SchematicUtils;
public class SchematicAutoWorkbench extends SchematicTile {
@Override
public void addRequirements(IBuilderContext context, LinkedList<ItemStack> requirements) {
super.addRequirements(context, requirements);
SchematicUtils.requestInventoryContents(this, context, requirements);
}
@Override
public void readFromWorld(IBuilderContext context, int x, int y, int z) {
IInventory inventory = (IInventory) context.world().getTileEntity(x, y, z);
SchematicUtils.initializeInventoryContents(this, context, inventory);
}
@Override
public void writeToWorld(IBuilderContext context, int x, int y, int z) {
super.writeToWorld(context, x, y, z);
IInventory inventory = (IInventory) context.world().getTileEntity(x, y, z);
SchematicUtils.buildInventoryContents(this, context, inventory);
}
}