From b775c045fcd7b7a8f5ac0cf7793fc81817967aee Mon Sep 17 00:00:00 2001 From: SpaceToad Date: Sun, 9 Mar 2014 11:07:17 +0100 Subject: [PATCH] Introduced a layer specific for tile schematic, further optimizing for #1484 --- .../api/blueprints/BlueprintManager.java | 7 +- .../buildcraft/api/blueprints/Schematic.java | 43 -------- .../api/blueprints/SchematicTile.java | 98 +++++++++++++++++++ .../api/blueprints/SchematicUtils.java | 10 +- .../api/schematics/SchematicInventory.java | 4 +- .../api/schematics/SchematicRotateMeta.java | 4 +- .../api/schematics/SchematicSign.java | 4 +- common/buildcraft/energy/SchematicEngine.java | 4 +- .../factory/SchematicAutoWorkbench.java | 4 +- .../buildcraft/factory/SchematicRefinery.java | 4 +- common/buildcraft/factory/SchematicTank.java | 4 +- .../blueprints/BptItemPipeFilters.java | 4 +- .../blueprints/BptPipeExtension.java | 6 +- .../transport/blueprints/BptPipeIron.java | 4 +- .../transport/blueprints/BptPipeWooden.java | 4 +- .../transport/blueprints/SchematicPipe.java | 4 +- 16 files changed, 134 insertions(+), 74 deletions(-) create mode 100755 common/buildcraft/api/blueprints/SchematicTile.java diff --git a/common/buildcraft/api/blueprints/BlueprintManager.java b/common/buildcraft/api/blueprints/BlueprintManager.java index be15244f..11fa2cb3 100644 --- a/common/buildcraft/api/blueprints/BlueprintManager.java +++ b/common/buildcraft/api/blueprints/BlueprintManager.java @@ -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 { diff --git a/common/buildcraft/api/blueprints/Schematic.java b/common/buildcraft/api/blueprints/Schematic.java index 6f84bcdb..baf6e906 100755 --- a/common/buildcraft/api/blueprints/Schematic.java +++ b/common/buildcraft/api/blueprints/Schematic.java @@ -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 storedRequirements = new ArrayList(); - /** - * 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) 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 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()); diff --git a/common/buildcraft/api/blueprints/SchematicTile.java b/common/buildcraft/api/blueprints/SchematicTile.java new file mode 100755 index 00000000..118222ab --- /dev/null +++ b/common/buildcraft/api/blueprints/SchematicTile.java @@ -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"); + } +} diff --git a/common/buildcraft/api/blueprints/SchematicUtils.java b/common/buildcraft/api/blueprints/SchematicUtils.java index a76d8b0a..061865ba 100644 --- a/common/buildcraft/api/blueprints/SchematicUtils.java +++ b/common/buildcraft/api/blueprints/SchematicUtils.java @@ -17,7 +17,7 @@ import net.minecraft.nbt.NBTTagList; public class SchematicUtils { - public static void requestInventoryContents(Schematic slot, IBuilderContext context, LinkedList requirements) { + public static void requestInventoryContents(SchematicTile slot, IBuilderContext context, LinkedList 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) { diff --git a/common/buildcraft/api/schematics/SchematicInventory.java b/common/buildcraft/api/schematics/SchematicInventory.java index 8d0b893a..5109b0ba 100644 --- a/common/buildcraft/api/schematics/SchematicInventory.java +++ b/common/buildcraft/api/schematics/SchematicInventory.java @@ -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) { diff --git a/common/buildcraft/api/schematics/SchematicRotateMeta.java b/common/buildcraft/api/schematics/SchematicRotateMeta.java index 82fc55c7..29aa5242 100644 --- a/common/buildcraft/api/schematics/SchematicRotateMeta.java +++ b/common/buildcraft/api/schematics/SchematicRotateMeta.java @@ -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; diff --git a/common/buildcraft/api/schematics/SchematicSign.java b/common/buildcraft/api/schematics/SchematicSign.java index d70c7fdb..620a7624 100644 --- a/common/buildcraft/api/schematics/SchematicSign.java +++ b/common/buildcraft/api/schematics/SchematicSign.java @@ -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; diff --git a/common/buildcraft/energy/SchematicEngine.java b/common/buildcraft/energy/SchematicEngine.java index 23ff44c5..6fdb4038 100644 --- a/common/buildcraft/energy/SchematicEngine.java +++ b/common/buildcraft/energy/SchematicEngine.java @@ -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) { diff --git a/common/buildcraft/factory/SchematicAutoWorkbench.java b/common/buildcraft/factory/SchematicAutoWorkbench.java index 7996fcbd..594cf452 100644 --- a/common/buildcraft/factory/SchematicAutoWorkbench.java +++ b/common/buildcraft/factory/SchematicAutoWorkbench.java @@ -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 requirements) { diff --git a/common/buildcraft/factory/SchematicRefinery.java b/common/buildcraft/factory/SchematicRefinery.java index 2035d138..de03a888 100644 --- a/common/buildcraft/factory/SchematicRefinery.java +++ b/common/buildcraft/factory/SchematicRefinery.java @@ -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) { diff --git a/common/buildcraft/factory/SchematicTank.java b/common/buildcraft/factory/SchematicTank.java index 5fa125bf..8312cd8a 100644 --- a/common/buildcraft/factory/SchematicTank.java +++ b/common/buildcraft/factory/SchematicTank.java @@ -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) { diff --git a/common/buildcraft/transport/blueprints/BptItemPipeFilters.java b/common/buildcraft/transport/blueprints/BptItemPipeFilters.java index efa97e2f..5e1b0d32 100644 --- a/common/buildcraft/transport/blueprints/BptItemPipeFilters.java +++ b/common/buildcraft/transport/blueprints/BptItemPipeFilters.java @@ -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); diff --git a/common/buildcraft/transport/blueprints/BptPipeExtension.java b/common/buildcraft/transport/blueprints/BptPipeExtension.java index ddb96d1b..869f9918 100755 --- a/common/buildcraft/transport/blueprints/BptPipeExtension.java +++ b/common/buildcraft/transport/blueprints/BptPipeExtension.java @@ -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) { } diff --git a/common/buildcraft/transport/blueprints/BptPipeIron.java b/common/buildcraft/transport/blueprints/BptPipeIron.java index ca708727..58c9681a 100644 --- a/common/buildcraft/transport/blueprints/BptPipeIron.java +++ b/common/buildcraft/transport/blueprints/BptPipeIron.java @@ -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; diff --git a/common/buildcraft/transport/blueprints/BptPipeWooden.java b/common/buildcraft/transport/blueprints/BptPipeWooden.java index e4232a81..1e4fa0f6 100644 --- a/common/buildcraft/transport/blueprints/BptPipeWooden.java +++ b/common/buildcraft/transport/blueprints/BptPipeWooden.java @@ -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; diff --git a/common/buildcraft/transport/blueprints/SchematicPipe.java b/common/buildcraft/transport/blueprints/SchematicPipe.java index 57d89adb..c170826e 100644 --- a/common/buildcraft/transport/blueprints/SchematicPipe.java +++ b/common/buildcraft/transport/blueprints/SchematicPipe.java @@ -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 requirements) {