Introduced a layer specific for tile schematic, further optimizing for #1484

This commit is contained in:
SpaceToad 2014-03-09 11:07:17 +01:00
parent add526d8f9
commit b775c045fc
16 changed files with 134 additions and 74 deletions

View file

@ -12,6 +12,7 @@ import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.init.Blocks;
public class BlueprintManager {
@ -38,7 +39,11 @@ public class BlueprintManager {
}
if (!schematicClasses.containsKey(block)) {
registerSchematicClass(block, Schematic.class);
if (block instanceof BlockContainer) {
registerSchematicClass(block, SchematicTile.class);
} else {
registerSchematicClass(block, Schematic.class);
}
}
try {

View file

@ -12,13 +12,10 @@ import java.util.ArrayList;
import java.util.LinkedList;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import buildcraft.core.utils.Utils;
/**
@ -56,14 +53,6 @@ public class Schematic {
*/
public ArrayList<ItemStack> storedRequirements = new ArrayList<ItemStack>();
/**
* This tree contains additional data to be stored in the blueprint. By
* default, it will be initialized from Schematic.readFromWord with
* the standard readNBT function of the corresponding tile (if any) and will
* be loaded from BptBlock.buildBlock using the standard writeNBT function.
*/
public NBTTagCompound cpt = new NBTTagCompound();
@SuppressWarnings("unchecked")
@Override
public Schematic clone() {
@ -71,7 +60,6 @@ public class Schematic {
obj.block = block;
obj.meta = meta;
obj.cpt = (NBTTagCompound) cpt.copy();
obj.storedRequirements = (ArrayList<ItemStack>) storedRequirements.clone();
return obj;
@ -174,27 +162,6 @@ public class Schematic {
// Meta needs to be specified twice, depending on the block behavior
context.world().setBlock(x, y, z, block, meta, 3);
context.world().setBlockMetadataWithNotify(x, y, z, meta, 3);
if (block instanceof BlockContainer) {
TileEntity tile = context.world().getTileEntity(x, y, z);
cpt.setInteger("x", x);
cpt.setInteger("y", y);
cpt.setInteger("z", z);
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);
}
}
}
}
/**
@ -215,14 +182,6 @@ public class Schematic {
* save / load the block.
*/
public void readFromWorld(IBuilderContext context, int x, int y, int z) {
if (block instanceof BlockContainer) {
TileEntity tile = context.world().getTileEntity(x, y, z);
if (tile != null) {
tile.writeToNBT(cpt);
}
}
if (block != null) {
ArrayList<ItemStack> req = block.getDrops(context.world(), x,
y, z, context.world().getBlockMetadata(x, y, z), 0);
@ -245,7 +204,6 @@ public class Schematic {
public void writeToNBT(NBTTagCompound nbt, MappingRegistry registry) {
nbt.setInteger("blockId", registry.getIdForBlock(block));
nbt.setInteger("blockMeta", meta);
nbt.setTag("blockCpt", cpt);
NBTTagList rq = new NBTTagList();
@ -263,7 +221,6 @@ public class Schematic {
public void readFromNBT(NBTTagCompound nbt, MappingRegistry registry) {
block = registry.getBlockForId(nbt.getInteger("blockId"));
meta = nbt.getInteger("blockMeta");
cpt = nbt.getCompoundTag("blockCpt");
NBTTagList rq = nbt.getTagList("rq", Utils.NBTTag_Types.NBTTagList.ordinal());

View file

@ -0,0 +1,98 @@
/**
* 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.block.BlockContainer;
import net.minecraft.inventory.IInventory;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
public class SchematicTile extends Schematic {
/**
* This tree contains additional data to be stored in the blueprint. By
* default, it will be initialized from Schematic.readFromWord with
* the standard readNBT function of the corresponding tile (if any) and will
* be loaded from BptBlock.buildBlock using the standard writeNBT function.
*/
public NBTTagCompound cpt = new NBTTagCompound();
@SuppressWarnings("unchecked")
@Override
public Schematic clone() {
SchematicTile s = (SchematicTile) super.clone();
s.cpt = (NBTTagCompound) cpt.copy();
return s;
}
/**
* Places the block in the world, at the location specified in the slot.
*/
@Override
public void writeToWorld(IBuilderContext context, int x, int y, int z) {
super.writeToWorld(context, x, y, z);
if (block instanceof BlockContainer) {
TileEntity tile = context.world().getTileEntity(x, y, z);
cpt.setInteger("x", x);
cpt.setInteger("y", y);
cpt.setInteger("z", z);
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);
}
}
}
}
/**
* 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
* that "id" and "meta" will be set automatically, corresponding to the
* block id and meta.
*
* By default, if the block is a BlockContainer, tile information will be to
* save / load the block.
*/
@Override
public void readFromWorld(IBuilderContext context, int x, int y, int z) {
super.readFromWorld(context, x, y, z);
if (block instanceof BlockContainer) {
TileEntity tile = context.world().getTileEntity(x, y, z);
if (tile != null) {
tile.writeToNBT(cpt);
}
}
}
@Override
public void writeToNBT(NBTTagCompound nbt, MappingRegistry registry) {
super.writeToNBT(nbt, registry);
nbt.setTag("blockCpt", cpt);
}
@Override
public void readFromNBT(NBTTagCompound nbt, MappingRegistry registry) {
cpt = nbt.getCompoundTag("blockCpt");
}
}

View file

@ -17,7 +17,7 @@ import net.minecraft.nbt.NBTTagList;
public class SchematicUtils {
public static void requestInventoryContents(Schematic slot, IBuilderContext context, LinkedList<ItemStack> requirements) {
public static void requestInventoryContents(SchematicTile slot, IBuilderContext context, LinkedList<ItemStack> requirements) {
ItemStack[] stacks = getItemStacks(slot, context);
for (ItemStack stack : stacks) {
@ -27,7 +27,7 @@ public class SchematicUtils {
}
}
public static void initializeInventoryContents(Schematic slot, IBuilderContext context, IInventory inventory) {
public static void initializeInventoryContents(SchematicTile slot, IBuilderContext context, IInventory inventory) {
ItemStack[] stacks = new ItemStack[inventory.getSizeInventory()];
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
@ -37,7 +37,7 @@ public class SchematicUtils {
setItemStacks(slot, context, stacks);
}
public static void buildInventoryContents(Schematic slot, IBuilderContext context, IInventory inventory) {
public static void buildInventoryContents(SchematicTile slot, IBuilderContext context, IInventory inventory) {
ItemStack[] stacks = getItemStacks(slot, context);
for (int i = 0; i < stacks.length; ++i) {
@ -45,7 +45,7 @@ public class SchematicUtils {
}
}
public static ItemStack[] getItemStacks(Schematic slot, IBuilderContext context) {
public static ItemStack[] getItemStacks(SchematicTile slot, IBuilderContext context) {
NBTTagList list = (NBTTagList) slot.cpt.getTag("inv");
if (list == null) {
@ -65,7 +65,7 @@ public class SchematicUtils {
return stacks;
}
public static void setItemStacks(Schematic slot, IBuilderContext context, ItemStack[] stacks) {
public static void setItemStacks(SchematicTile slot, IBuilderContext context, ItemStack[] stacks) {
NBTTagList nbttaglist = new NBTTagList();
for (ItemStack stack : stacks) {

View file

@ -10,9 +10,9 @@ package buildcraft.api.schematics;
import net.minecraft.inventory.IInventory;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.SchematicTile;
public class SchematicInventory extends Schematic {
public class SchematicInventory extends SchematicTile {
@Override
public void writeToWorld(IBuilderContext context, int x, int y, int z) {

View file

@ -12,9 +12,9 @@ import java.util.LinkedList;
import net.minecraft.item.ItemStack;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.SchematicTile;
public class SchematicRotateMeta extends Schematic {
public class SchematicRotateMeta extends SchematicTile {
int[] rot;
boolean rotateForward;

View file

@ -13,10 +13,10 @@ import java.util.LinkedList;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.SchematicTile;
public class SchematicSign extends Schematic {
public class SchematicSign extends SchematicTile {
boolean isWall;

View file

@ -10,9 +10,9 @@ package buildcraft.energy;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.SchematicTile;
public class SchematicEngine extends Schematic {
public class SchematicEngine extends SchematicTile {
@Override
public void rotateLeft(IBuilderContext context) {

View file

@ -13,10 +13,10 @@ import java.util.LinkedList;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.SchematicTile;
import buildcraft.api.blueprints.SchematicUtils;
public class SchematicAutoWorkbench extends Schematic {
public class SchematicAutoWorkbench extends SchematicTile {
@Override
public void addRequirements(IBuilderContext context, LinkedList<ItemStack> requirements) {

View file

@ -10,9 +10,9 @@ package buildcraft.factory;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.SchematicTile;
public class SchematicRefinery extends Schematic {
public class SchematicRefinery extends SchematicTile {
@Override
public void rotateLeft(IBuilderContext context) {

View file

@ -9,9 +9,9 @@
package buildcraft.factory;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.SchematicTile;
public class SchematicTank extends Schematic {
public class SchematicTank extends SchematicTile {
@Override
public void readFromWorld(IBuilderContext context, int x, int y, int z) {

View file

@ -10,8 +10,8 @@ package buildcraft.transport.blueprints;
import net.minecraft.item.Item;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.SchematicTile;
import buildcraft.core.inventory.SimpleInventory;
public class BptItemPipeFilters extends BptPipeExtension {
@ -22,7 +22,7 @@ public class BptItemPipeFilters extends BptPipeExtension {
@Override
public void rotateLeft(Schematic slot, IBuilderContext context) {
public void rotateLeft(SchematicTile slot, IBuilderContext context) {
SimpleInventory inv = new SimpleInventory(54, "Filters", 1);
SimpleInventory newInv = new SimpleInventory(54, "Filters", 1);
inv.readFromNBT(slot.cpt);

View file

@ -3,8 +3,8 @@ package buildcraft.transport.blueprints;
import java.util.HashMap;
import net.minecraft.item.Item;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.SchematicTile;
public class BptPipeExtension {
@ -14,11 +14,11 @@ public class BptPipeExtension {
bptPipeExtensionRegistry.put(i, this);
}
public void postProcessing(Schematic slot, IBuilderContext context) {
public void postProcessing(SchematicTile slot, IBuilderContext context) {
}
public void rotateLeft(Schematic slot, IBuilderContext context) {
public void rotateLeft(SchematicTile slot, IBuilderContext context) {
}

View file

@ -10,8 +10,8 @@ package buildcraft.transport.blueprints;
import net.minecraft.item.Item;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.SchematicTile;
public class BptPipeIron extends BptPipeExtension {
@ -20,7 +20,7 @@ public class BptPipeIron extends BptPipeExtension {
}
@Override
public void rotateLeft(Schematic slot, IBuilderContext context) {
public void rotateLeft(SchematicTile slot, IBuilderContext context) {
int orientation = slot.meta & 7;
int others = slot.meta - orientation;

View file

@ -10,8 +10,8 @@ package buildcraft.transport.blueprints;
import net.minecraft.item.Item;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.SchematicTile;
public class BptPipeWooden extends BptPipeExtension {
@ -20,7 +20,7 @@ public class BptPipeWooden extends BptPipeExtension {
}
@Override
public void rotateLeft(Schematic slot, IBuilderContext context) {
public void rotateLeft(SchematicTile slot, IBuilderContext context) {
int orientation = slot.meta & 7;
int others = slot.meta - orientation;

View file

@ -14,12 +14,12 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.SchematicTile;
import buildcraft.transport.BlockGenericPipe;
import buildcraft.transport.Pipe;
import buildcraft.transport.TileGenericPipe.SideProperties;
public class SchematicPipe extends Schematic {
public class SchematicPipe extends SchematicTile {
@Override
public void addRequirements(IBuilderContext context, LinkedList<ItemStack> requirements) {