diff --git a/common/buildcraft/BuildCraftBuilders.java b/common/buildcraft/BuildCraftBuilders.java index 2254f6b8..b9c1d059 100644 --- a/common/buildcraft/BuildCraftBuilders.java +++ b/common/buildcraft/BuildCraftBuilders.java @@ -46,8 +46,8 @@ import buildcraft.builders.FillerRegistry; import buildcraft.builders.FillerRemover; import buildcraft.builders.GuiHandler; import buildcraft.builders.IBuilderHook; -import buildcraft.builders.ItemBptBluePrint; -import buildcraft.builders.ItemBptTemplate; +import buildcraft.builders.ItemBlueprintStandard; +import buildcraft.builders.ItemBlueprintTemplate; import buildcraft.builders.TileArchitect; import buildcraft.builders.TileBlueprintLibrary; import buildcraft.builders.TileBuilder; @@ -100,8 +100,8 @@ public class BuildCraftBuilders { public static BlockBuilder builderBlock; public static BlockArchitect architectBlock; public static BlockBlueprintLibrary libraryBlock; - public static ItemBptTemplate templateItem; - public static ItemBptBluePrint blueprintItem; + public static ItemBlueprintTemplate templateItem; + public static ItemBlueprintStandard blueprintItem; public static boolean fillerDestroy; public static int fillerLifespanTough; public static int fillerLifespanNormal; @@ -239,12 +239,12 @@ public class BuildCraftBuilders { fillerLifespanNormalProp.comment = "Lifespan in ticks of items dropped by the filler from non-tough blocks (those that can be broken by hand)"; fillerLifespanNormal = fillerLifespanNormalProp.getInt(DefaultProps.FILLER_LIFESPAN_NORMAL); - templateItem = new ItemBptTemplate(templateItemId.getInt()); + templateItem = new ItemBlueprintTemplate(templateItemId.getInt()); templateItem.setUnlocalizedName("templateItem"); LanguageRegistry.addName(templateItem, "Template"); CoreProxy.proxy.registerItem(templateItem); - blueprintItem = new ItemBptBluePrint(blueprintItemId.getInt()); + blueprintItem = new ItemBlueprintStandard(blueprintItemId.getInt()); blueprintItem.setUnlocalizedName("blueprintItem"); LanguageRegistry.addName(blueprintItem, "Blueprint"); CoreProxy.proxy.registerItem(blueprintItem); @@ -300,8 +300,8 @@ public class BuildCraftBuilders { CoreProxy.proxy.addCraftingRecipe(new ItemStack(templateItem, 1), new Object[]{"ppp", "pip", "ppp", 'i', new ItemStack(Item.dyePowder, 1, 0), 'p', Item.paper}); -// CoreProxy.proxy.addCraftingRecipe(new ItemStack(blueprintItem, 1), new Object[]{"ppp", "pip", "ppp", 'i', -// new ItemStack(Item.dyePowder, 1, 4), 'p', Item.paper}); + CoreProxy.proxy.addCraftingRecipe(new ItemStack(blueprintItem, 1), new Object[]{"ppp", "pip", "ppp", 'i', + new ItemStack(Item.dyePowder, 1, 4), 'p', Item.paper}); CoreProxy.proxy.addCraftingRecipe(new ItemStack(markerBlock, 1), new Object[]{"l ", "r ", 'l', new ItemStack(Item.dyePowder, 1, 4), 'r', Block.torchRedstoneActive}); diff --git a/common/buildcraft/builders/BlockArchitect.java b/common/buildcraft/builders/BlockArchitect.java index c88b2bca..1217eb68 100644 --- a/common/buildcraft/builders/BlockArchitect.java +++ b/common/buildcraft/builders/BlockArchitect.java @@ -12,6 +12,7 @@ package buildcraft.builders; import buildcraft.BuildCraftBuilders; import buildcraft.api.core.Position; import buildcraft.api.tools.IToolWrench; +import buildcraft.core.CreativeTabBuildCraft; import buildcraft.core.GuiIds; import buildcraft.core.proxy.CoreProxy; import buildcraft.core.utils.Utils; @@ -42,7 +43,7 @@ public class BlockArchitect extends BlockContainer { public BlockArchitect(int i) { super(i, Material.iron); setHardness(5F); - //setCreativeTab(CreativeTabBuildCraft.MACHINES.get()); + setCreativeTab(CreativeTabBuildCraft.MACHINES.get()); } @Override diff --git a/common/buildcraft/builders/ItemBlueprint.java b/common/buildcraft/builders/ItemBlueprint.java new file mode 100644 index 00000000..1035d3d1 --- /dev/null +++ b/common/buildcraft/builders/ItemBlueprint.java @@ -0,0 +1,48 @@ +/** + * Copyright (c) SpaceToad, 2011-2012 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.builders; + +import buildcraft.builders.blueprints.Blueprint; +import buildcraft.builders.blueprints.BlueprintDatabase; +import buildcraft.core.CreativeTabBuildCraft; +import buildcraft.core.ItemBuildCraft; +import buildcraft.core.utils.NBTUtils; +import buildcraft.core.utils.StringUtils; +import java.util.List; +import java.util.UUID; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; + +public abstract class ItemBlueprint extends ItemBuildCraft { + + public ItemBlueprint(int i) { + super(i); + setMaxStackSize(1); + setCreativeTab(CreativeTabBuildCraft.MACHINES.get()); + } + + @Override + public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean advanced) { + Blueprint blueprint = getBlueprint(stack); + if (blueprint != null) { + list.add(String.format(StringUtils.localize("item.blueprint.name"), blueprint.getName())); + list.add(String.format(StringUtils.localize("item.blueprint.creator"), blueprint.getCreator())); + } else + list.add(StringUtils.localize("item.blueprint.blank")); + } + + public static Blueprint getBlueprint(ItemStack stack) { + if (stack != null && stack.getItem() instanceof ItemBlueprint) { + NBTTagCompound nbt = NBTUtils.getItemData(stack); + UUID uuid = NBTUtils.readUUID(nbt, "blueprint"); + return BlueprintDatabase.getBlueprint(uuid); + } + return null; + } +} diff --git a/common/buildcraft/builders/ItemBlueprintStandard.java b/common/buildcraft/builders/ItemBlueprintStandard.java new file mode 100644 index 00000000..66f31c37 --- /dev/null +++ b/common/buildcraft/builders/ItemBlueprintStandard.java @@ -0,0 +1,38 @@ +/** + * Copyright (c) SpaceToad, 2011-2012 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.builders; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.client.renderer.texture.IconRegister; +import net.minecraft.util.Icon; + +public class ItemBlueprintStandard extends ItemBlueprint { + + private Icon cleanBlueprint; + private Icon usedBlueprint; + + public ItemBlueprintStandard(int i) { + super(i); + } + + @Override + public Icon getIconFromDamage(int damage) { + if (damage == 0) + return cleanBlueprint; + else + return usedBlueprint; + } + + @Override + @SideOnly(Side.CLIENT) + public void registerIcons(IconRegister par1IconRegister) { + cleanBlueprint = par1IconRegister.registerIcon("buildcraft:blueprint_clean"); + usedBlueprint = par1IconRegister.registerIcon("buildcraft:blueprint_used"); + } +} diff --git a/common/buildcraft/builders/ItemBptTemplate.java b/common/buildcraft/builders/ItemBlueprintTemplate.java similarity index 77% rename from common/buildcraft/builders/ItemBptTemplate.java rename to common/buildcraft/builders/ItemBlueprintTemplate.java index 8f0557f6..4e1ee7ef 100644 --- a/common/buildcraft/builders/ItemBptTemplate.java +++ b/common/buildcraft/builders/ItemBlueprintTemplate.java @@ -5,9 +5,11 @@ import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.client.renderer.texture.IconRegister; import net.minecraft.util.Icon; -public class ItemBptTemplate extends ItemBptBase { +public class ItemBlueprintTemplate extends ItemBlueprint { + private Icon usedTemplate; - public ItemBptTemplate(int i) { + + public ItemBlueprintTemplate(int i) { super(i); } @@ -21,8 +23,7 @@ public class ItemBptTemplate extends ItemBptBase { @Override @SideOnly(Side.CLIENT) - public void registerIcons(IconRegister par1IconRegister) - { + public void registerIcons(IconRegister par1IconRegister) { itemIcon = par1IconRegister.registerIcon("buildcraft:template_clean"); usedTemplate = par1IconRegister.registerIcon("buildcraft:template_used"); } diff --git a/common/buildcraft/builders/ItemBptBase.java b/common/buildcraft/builders/ItemBptBase.java deleted file mode 100644 index 040137ad..00000000 --- a/common/buildcraft/builders/ItemBptBase.java +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Copyright (c) SpaceToad, 2011-2012 - * 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.builders; - -import buildcraft.BuildCraftBuilders; -import buildcraft.core.CreativeTabBuildCraft; -import buildcraft.core.ItemBuildCraft; -import buildcraft.core.blueprints.BptBase; -import buildcraft.core.proxy.CoreProxy; -import java.util.List; -import net.minecraft.entity.Entity; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; -import net.minecraft.util.Icon; -import net.minecraft.world.World; - -public abstract class ItemBptBase extends ItemBuildCraft { - - public ItemBptBase(int i) { - super(i); - - maxStackSize = 1; - setCreativeTab(CreativeTabBuildCraft.MACHINES.get()); - } - - @SuppressWarnings({ "all" }) - // @Override (client only) - public abstract Icon getIconFromDamage(int i); - - @SuppressWarnings({ "all" }) - // @Override (client only) - public void addInformation(ItemStack itemstack, EntityPlayer player, List list, boolean advanced) { - if (itemstack.hasTagCompound() && itemstack.getTagCompound().hasKey("BptName")) { - list.add(itemstack.getTagCompound().getString("BptName")); - } - } - - @Override - public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) { - if (CoreProxy.proxy.isSimulating(world)) { - BptBase bpt = BuildCraftBuilders.getBptRootIndex().getBluePrint(itemStack.getItemDamage()); - if (bpt != null) - return BuildCraftBuilders.getBptItemStack(itemStack.itemID, itemStack.getItemDamage(), bpt.getName()); - } - return itemStack; - } - - @Override - public void onUpdate(ItemStack itemstack, World world, Entity entity, int i, boolean flag) { - } -} diff --git a/common/buildcraft/builders/ItemBptBluePrint.java b/common/buildcraft/builders/ItemBptBluePrint.java deleted file mode 100644 index aa50c903..00000000 --- a/common/buildcraft/builders/ItemBptBluePrint.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright (c) SpaceToad, 2011-2012 - * 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.builders; - -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; -import net.minecraft.client.renderer.texture.IconRegister; -import net.minecraft.util.Icon; - -public class ItemBptBluePrint extends ItemBptBase { - - private Icon cleanBlueprint; - private Icon usedBlueprint; - - public ItemBptBluePrint(int i) { - super(i); - setCreativeTab(null); - } - - @Override - public Icon getIconFromDamage(int i) { - if (i == 0) - return cleanBlueprint; - else - return usedBlueprint; - } - - @Override - @SideOnly(Side.CLIENT) - public void registerIcons(IconRegister par1IconRegister) - { - cleanBlueprint = par1IconRegister.registerIcon("buildcraft:blueprint_clean"); - usedBlueprint = par1IconRegister.registerIcon("buildcraft:blueprint_used"); - } -} diff --git a/common/buildcraft/builders/TileArchitect.java b/common/buildcraft/builders/TileArchitect.java index c219cf27..e8b61229 100644 --- a/common/buildcraft/builders/TileArchitect.java +++ b/common/buildcraft/builders/TileArchitect.java @@ -7,16 +7,13 @@ */ package buildcraft.builders; - import buildcraft.BuildCraftBuilders; import buildcraft.api.core.IAreaProvider; import buildcraft.api.core.LaserKind; import buildcraft.builders.blueprints.Blueprint; +import buildcraft.builders.blueprints.BlueprintDatabase; import buildcraft.core.Box; import buildcraft.core.TileBuildCraft; -import buildcraft.core.blueprints.BptBase; -import buildcraft.core.blueprints.BptBlueprint; -import buildcraft.core.blueprints.BptContext; import buildcraft.core.network.PacketUpdate; import buildcraft.core.network.TileNetworkData; import buildcraft.core.proxy.CoreProxy; @@ -30,7 +27,6 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraftforge.common.ForgeDirection; - public class TileArchitect extends TileBuildCraft implements IInventory { public @TileNetworkData @@ -40,9 +36,6 @@ public class TileArchitect extends TileBuildCraft implements IInventory { public int computingTime = 0; public @TileNetworkData String name = ""; - // Use that field to avoid creating several times the same template if - // they're the same! - private int lastBptId = 0; @Override public void updateEntity() { @@ -52,7 +45,7 @@ public class TileArchitect extends TileBuildCraft implements IInventory { if (computingTime < 200) { computingTime++; } else { - createBpt(); + createBlueprint(); } } } @@ -78,100 +71,85 @@ public class TileArchitect extends TileBuildCraft implements IInventory { sendNetworkUpdate(); } - public void createBpt() { + public void createBlueprint() { if (!box.isInitialized() || items[1] != null) return; - BptBase result; - BptContext context = null; + Blueprint blueprint; - if (items[0].getItem() instanceof ItemBptTemplate) { - result = createBptTemplate(); - context = new BptContext(worldObj, null, box); + if (items[0].getItem() instanceof ItemBlueprintTemplate) { + blueprint = createMaskBlueprint(box); } else { - result = createBptBlueprint(); - context = new BptContext(worldObj, (BptBlueprint) result, box); + blueprint = createStandardBlueprint(box); } if (!name.equals("")) { - result.setName(name); + blueprint.setName(name); } - result.anchorX = xCoord - box.xMin; - result.anchorY = yCoord - box.yMin; - result.anchorZ = zCoord - box.zMin; + blueprint.anchorX = xCoord - box.xMin; + blueprint.anchorY = yCoord - box.yMin; + blueprint.anchorZ = zCoord - box.zMin; - ForgeDirection o = ForgeDirection.values()[worldObj.getBlockMetadata(xCoord, yCoord, zCoord)].getOpposite(); + ForgeDirection o = ForgeDirection.getOrientation(worldObj.getBlockMetadata(xCoord, yCoord, zCoord)).getOpposite(); - if (o == ForgeDirection.EAST) { - // Do nothing - } else if (o == ForgeDirection.SOUTH) { - result.rotateLeft(context); - result.rotateLeft(context); - result.rotateLeft(context); - } else if (o == ForgeDirection.WEST) { - result.rotateLeft(context); - result.rotateLeft(context); - } else if (o == ForgeDirection.NORTH) { - result.rotateLeft(context); - } +// if (o == ForgeDirection.NORTH) { +// // Do nothing +// } else if (o == ForgeDirection.SOUTH) { +// blueprint.rotateLeft(); +// blueprint.rotateLeft(); +// blueprint.rotateLeft(); +// } else if (o == ForgeDirection.WEST) { +// blueprint.rotateLeft(); +// blueprint.rotateLeft(); +// } else if (o == ForgeDirection.NORTH) { +// blueprint.rotateLeft(); +// } - ItemStack stack; - if (result.equals(BuildCraftBuilders.getBptRootIndex().getBluePrint(lastBptId))) { - result = BuildCraftBuilders.getBptRootIndex().getBluePrint(lastBptId); - stack = BuildCraftBuilders.getBptItemStack(items[0].itemID, lastBptId, result.getName()); - } else { - int bptId = BuildCraftBuilders.getBptRootIndex().storeBluePrint(result); - stack = BuildCraftBuilders.getBptItemStack(items[0].itemID, bptId, result.getName()); - lastBptId = bptId; - } - - setInventorySlotContents(1, stack); + BlueprintDatabase.addBlueprint(blueprint); + + setInventorySlotContents(1, blueprint.getBlueprintItem()); setInventorySlotContents(0, null); } - public BptBase createBptTemplate() { - int mask1 = 1; - int mask0 = 0; - - if (worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord)) { - mask1 = 0; - mask0 = 1; - } - + private Blueprint createMaskBlueprint(Box box) { Blueprint blueprint = new Blueprint(box.sizeX(), box.sizeY(), box.sizeZ()); for (int x = box.xMin; x <= box.xMax; ++x) { for (int y = box.yMin; y <= box.yMax; ++y) { for (int z = box.zMin; z <= box.zMax; ++z) { - if (!worldObj.isAirBlock(x, y, z)) { - Block block = Block.blocksList[worldObj.getBlockId(x, y, z)]; - if (block != null) { - blueprint.setSchematic(x - box.xMin, y - box.yMin, z - box.zMin, worldObj, block); - } - } + if (worldObj.isAirBlock(x, y, z)) + continue; + Block block = Block.blocksList[worldObj.getBlockId(x, y, z)]; + if (block == null) + continue; + + blueprint.setSchematic(x - box.xMin, y - box.yMin, z - box.zMin, worldObj, block); } } } -// return blueprint; - return null; + return blueprint; } - private BptBase createBptBlueprint() { - BptBlueprint result = new BptBlueprint(box.sizeX(), box.sizeY(), box.sizeZ()); - - BptContext context = new BptContext(worldObj, result, box); + private Blueprint createStandardBlueprint(Box box) { + Blueprint blueprint = new Blueprint(box.sizeX(), box.sizeY(), box.sizeZ()); for (int x = box.xMin; x <= box.xMax; ++x) { for (int y = box.yMin; y <= box.yMax; ++y) { for (int z = box.zMin; z <= box.zMax; ++z) { - result.readFromWorld(context, this, x, y, z); + if (worldObj.isAirBlock(x, y, z)) + continue; + Block block = Block.blocksList[worldObj.getBlockId(x, y, z)]; + if (block == null) + continue; + + blueprint.setSchematic(x - box.xMin, y - box.yMin, z - box.zMin, worldObj, block); } } } - return result; + return blueprint; } public void handleClientInput(char c) { @@ -243,9 +221,8 @@ public class TileArchitect extends TileBuildCraft implements IInventory { } @Override - public boolean isItemValidForSlot(int i, ItemStack itemstack) { - // TODO Auto-generated method stub - return false; + public boolean isItemValidForSlot(int slot, ItemStack stack) { + return slot == 0 && stack != null && stack.getItem() == BuildCraftBuilders.blueprintItem; } @Override @@ -257,7 +234,6 @@ public class TileArchitect extends TileBuildCraft implements IInventory { public void readFromNBT(NBTTagCompound nbttagcompound) { super.readFromNBT(nbttagcompound); - lastBptId = nbttagcompound.getInteger("lastTemplateId"); computingTime = nbttagcompound.getInteger("computingTime"); isComputing = nbttagcompound.getBoolean("isComputing"); @@ -282,7 +258,6 @@ public class TileArchitect extends TileBuildCraft implements IInventory { public void writeToNBT(NBTTagCompound nbttagcompound) { super.writeToNBT(nbttagcompound); - nbttagcompound.setInteger("lastTemplateId", lastBptId); nbttagcompound.setInteger("computingTime", computingTime); nbttagcompound.setBoolean("isComputing", isComputing); @@ -323,7 +298,7 @@ public class TileArchitect extends TileBuildCraft implements IInventory { if (!box.isInitialized()) return; else if (!isComputing) { - if (items[0] != null && items[0].getItem() instanceof ItemBptBase && items[1] == null) { + if (items[0] != null && items[0].getItem() instanceof ItemBlueprint && items[1] == null) { isComputing = true; computingTime = 0; } else { @@ -331,7 +306,7 @@ public class TileArchitect extends TileBuildCraft implements IInventory { computingTime = 0; } } else { - if (items[0] == null || !(items[0].getItem() instanceof ItemBptBase)) { + if (items[0] == null || !(items[0].getItem() instanceof ItemBlueprint)) { isComputing = false; computingTime = 0; } diff --git a/common/buildcraft/builders/TileBlueprintLibrary.java b/common/buildcraft/builders/TileBlueprintLibrary.java index d5b87120..99990427 100644 --- a/common/buildcraft/builders/TileBlueprintLibrary.java +++ b/common/buildcraft/builders/TileBlueprintLibrary.java @@ -192,7 +192,7 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory { stack[i] = itemstack; if (i == 0) { - if (stack[0] != null && stack[0].getItem() instanceof ItemBptBase) { + if (stack[0] != null && stack[0].getItem() instanceof ItemBlueprint) { progressIn = 1; } else { progressIn = 0; @@ -200,7 +200,7 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory { } if (i == 2) { - if (stack[2] != null && stack[2].getItem() instanceof ItemBptBase) { + if (stack[2] != null && stack[2].getItem() instanceof ItemBlueprint) { progressOut = 1; } else { progressOut = 0; diff --git a/common/buildcraft/builders/TileBuilder.java b/common/buildcraft/builders/TileBuilder.java index 33810d4d..9b3adc3b 100644 --- a/common/buildcraft/builders/TileBuilder.java +++ b/common/buildcraft/builders/TileBuilder.java @@ -7,169 +7,46 @@ */ package buildcraft.builders; -import buildcraft.BuildCraftBuilders; import buildcraft.api.core.LaserKind; -import buildcraft.api.core.Position; import buildcraft.api.gates.IAction; import buildcraft.api.power.IPowerReceptor; import buildcraft.api.power.PowerHandler; import buildcraft.api.power.PowerHandler.PowerReceiver; import buildcraft.api.power.PowerHandler.Type; -import buildcraft.core.BlockIndex; +import buildcraft.builders.blueprints.Blueprint; +import buildcraft.builders.blueprints.BlueprintBuilder; import buildcraft.core.Box; -import buildcraft.core.EntityLaser; -import buildcraft.core.EntityPowerLaser; import buildcraft.core.EntityRobot; import buildcraft.core.IBuilderInventory; import buildcraft.core.IMachine; import buildcraft.core.TileBuildCraft; -import buildcraft.core.blueprints.BptBase; -import buildcraft.core.blueprints.BptBlueprint; -import buildcraft.core.blueprints.BptBuilderBase; -import buildcraft.core.blueprints.BptBuilderBlueprint; -import buildcraft.core.blueprints.BptBuilderTemplate; -import buildcraft.core.blueprints.BptContext; +import buildcraft.core.inventory.SimpleInventory; import buildcraft.core.network.PacketUpdate; import buildcraft.core.network.TileNetworkData; import buildcraft.core.proxy.CoreProxy; -import buildcraft.core.utils.Utils; import java.io.IOException; -import java.util.Collection; -import java.util.Iterator; -import java.util.LinkedList; +import java.util.ListIterator; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.AxisAlignedBB; import net.minecraftforge.common.ForgeDirection; public class TileBuilder extends TileBuildCraft implements IBuilderInventory, IPowerReceptor, IMachine { - private final ItemStack items[] = new ItemStack[28]; - private BptBuilderBase bluePrintBuilder; + private static final int SLOT_BLUEPRINT = 0; public @TileNetworkData Box box = new Box(); private PowerHandler powerHandler; - private LinkedList path; - private LinkedList pathLasers; private EntityRobot builderRobot; - - private class PathIterator { - - public Iterator currentIterator; - public double cx, cy, cz; - public float ix, iy, iz; - public BlockIndex to; - public double lastDistance; - AxisAlignedBB oldBoundingBox = null; - ForgeDirection o = null; - - public PathIterator(BlockIndex from, Iterator it) { - this.to = it.next(); - - currentIterator = it; - - double dx = to.x - from.x; - double dy = to.y - from.y; - double dz = to.z - from.z; - - double size = Math.sqrt(dx * dx + dy * dy + dz * dz); - - cx = dx / size / 10; - cy = dy / size / 10; - cz = dz / size / 10; - - ix = from.x; - iy = from.y; - iz = from.z; - - lastDistance = (ix - to.x) * (ix - to.x) + (iy - to.y) * (iy - to.y) + (iz - to.z) * (iz - to.z); - - if (Math.abs(dx) > Math.abs(dz)) { - if (dx > 0) { - o = ForgeDirection.EAST; - } else { - o = ForgeDirection.WEST; - } - } else { - if (dz > 0) { - o = ForgeDirection.SOUTH; - } else { - o = ForgeDirection.NORTH; - } - } - } - - /** - * Return false when reached the end of the iteration - */ - public BptBuilderBase next() { - while (true) { - BptBuilderBase bpt; - - int newX = Math.round(ix); - int newY = Math.round(iy); - int newZ = Math.round(iz); - - bpt = instanciateBluePrint(newX, newY, newZ, o); - - if (bpt == null) - return null; - - AxisAlignedBB boundingBox = bpt.getBoundingBox(); - - if (oldBoundingBox == null || !collision(oldBoundingBox, boundingBox)) { - - oldBoundingBox = boundingBox; - - if (bpt != null) - return bpt; - } - - ix += cx; - iy += cy; - iz += cz; - - double distance = (ix - to.x) * (ix - to.x) + (iy - to.y) * (iy - to.y) + (iz - to.z) * (iz - to.z); - - if (distance > lastDistance) - return null; - else { - lastDistance = distance; - } - } - } - - public PathIterator iterate() { - if (currentIterator.hasNext()) { - PathIterator next = new PathIterator(to, currentIterator); - next.oldBoundingBox = oldBoundingBox; - - return next; - } else - return null; - } - - public boolean collision(AxisAlignedBB left, AxisAlignedBB right) { - if (left.maxX < right.minX || left.minX > right.maxX) - return false; - if (left.maxY < right.minY || left.minY > right.maxY) - return false; - if (left.maxZ < right.minZ || left.minZ > right.maxZ) - return false; - return true; - } - } - private PathIterator currentPathIterator; - private boolean done = true; + private BlueprintBuilder blueprintBuilder; + private ListIterator blueprintIterator; + private boolean builderDone = true; + private SimpleInventory inv = new SimpleInventory(28, "Builder", 64); public TileBuilder() { super(); - powerHandler = new PowerHandler(this, Type.MACHINE); - powerHandler.configure(25, 25, 25, 25); + powerHandler.configure(25, 25, 25, 100); } @Override @@ -179,86 +56,7 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory, IP if (CoreProxy.proxy.isRenderWorld(worldObj)) return; - for (int x = xCoord - 1; x <= xCoord + 1; ++x) { - for (int y = yCoord - 1; y <= yCoord + 1; ++y) { - for (int z = zCoord - 1; z <= zCoord + 1; ++z) { - TileEntity tile = worldObj.getBlockTileEntity(x, y, z); - if (tile instanceof TilePathMarker) { - path = ((TilePathMarker) tile).getPath(); - - for (BlockIndex b : path) { - worldObj.setBlock(b.x, b.y, b.z, 0); - - BuildCraftBuilders.pathMarkerBlock.dropBlockAsItem(worldObj, b.x, b.y, b.z, BuildCraftBuilders.pathMarkerBlock.blockID, 0); - } - - break; - } - } - } - } - - if (path != null && pathLasers == null) { - path.getFirst().x = xCoord; - path.getFirst().y = yCoord; - path.getFirst().z = zCoord; - - createLasersForPath(); - } - - iterateBpt(); - } - - public void createLasersForPath() { - pathLasers = new LinkedList(); - BlockIndex previous = null; - - for (BlockIndex b : path) { - if (previous != null) { - - EntityPowerLaser laser = new EntityPowerLaser(worldObj, new Position(previous.x + 0.5, previous.y + 0.5, previous.z + 0.5), new Position( - b.x + 0.5, b.y + 0.5, b.z + 0.5)); - - laser.setTexture(0); - laser.show(); - worldObj.spawnEntityInWorld(laser); - pathLasers.add(laser); - } - - previous = b; - } - } - - public BptBuilderBase instanciateBluePrint(int x, int y, int z, ForgeDirection o) { - BptBase bpt = BuildCraftBuilders.getBptRootIndex().getBluePrint(items[0].getItemDamage()); - - if (bpt == null) - return null; - - bpt = bpt.clone(); - - BptContext context = new BptContext(worldObj, null, bpt.getBoxForPos(x, y, z)); - - if (o == ForgeDirection.EAST) { - // Do nothing - } else if (o == ForgeDirection.SOUTH) { - bpt.rotateLeft(context); - } else if (o == ForgeDirection.WEST) { - bpt.rotateLeft(context); - bpt.rotateLeft(context); - } else if (o == ForgeDirection.NORTH) { - bpt.rotateLeft(context); - bpt.rotateLeft(context); - bpt.rotateLeft(context); - } - - if (items[0].getItem() instanceof ItemBptTemplate) - return new BptBuilderTemplate(bpt, worldObj, x, y, z); - else if (items[0].getItem() instanceof ItemBptBluePrint) - return new BptBuilderBlueprint((BptBlueprint) bpt, worldObj, x, y, z); - else - return null; } @Override @@ -266,109 +64,61 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory, IP if (CoreProxy.proxy.isRenderWorld(worldObj)) return; - if (done) - return; +// if (builderDone) +// return; - if (builderRobot != null && !builderRobot.readyToBuild()) - return; + initializeBuilder(); + build(); + } - if (powerHandler.useEnergy(25, 25, true) < 25) - return; + public Blueprint getBlueprint() { + ItemStack blueprintStack = getStackInSlot(SLOT_BLUEPRINT); + return ItemBlueprint.getBlueprint(blueprintStack); + } - iterateBpt(); - - if (bluePrintBuilder != null && !bluePrintBuilder.done) { - if (!box.isInitialized()) { - box.initialize(bluePrintBuilder); - } - - if (builderRobot == null) { - builderRobot = new EntityRobot(worldObj, box); - worldObj.spawnEntityInWorld(builderRobot); - } + public BlueprintBuilder getBlueprintBuilder() { + return blueprintBuilder; + } + private void initializeBuilder() { + Blueprint blueprint = getBlueprint(); + if (blueprintBuilder == null && blueprint != null) { + box.initialize(xCoord + 1, yCoord, zCoord + 1, xCoord + 1 + blueprint.sizeX, yCoord + blueprint.sizeY, zCoord + 1 + blueprint.sizeZ); box.createLasers(worldObj, LaserKind.Stripes); - - -// builderRobot.scheduleContruction(bluePrintBuilder.getNextBlock(worldObj, this), bluePrintBuilder.getContext()); + blueprintBuilder = new BlueprintBuilder(blueprint, worldObj, xCoord + 1, yCoord, zCoord + 1, ForgeDirection.NORTH, null); + blueprintIterator = blueprintBuilder.getBuilders().listIterator(); + } else if (blueprint == null) { + box.deleteLasers(); + box.reset(); + blueprintBuilder = null; + blueprintIterator = null; } } - public void iterateBpt() { - if (items[0] == null || !(items[0].getItem() instanceof ItemBptBase)) { - - if (bluePrintBuilder != null) { - bluePrintBuilder = null; - } - - if (builderRobot != null) { - builderRobot.setDead(); - builderRobot = null; - } - - if (box.isInitialized()) { - box.deleteLasers(); - box.reset(); - } - - if (currentPathIterator != null) { - currentPathIterator = null; - } - + protected void build() { + if (blueprintBuilder == null) return; + + if (blueprintIterator == null) + return; + + if (!blueprintIterator.hasNext()) + return; + + float mj = 25; + if (powerHandler.useEnergy(mj, mj, true) != mj) + return; + + if (builderRobot == null) { + builderRobot = new EntityRobot(worldObj, box); + worldObj.spawnEntityInWorld(builderRobot); } - if (bluePrintBuilder == null || bluePrintBuilder.done) { - if (path != null && path.size() > 1) { - if (currentPathIterator == null) { - Iterator it = path.iterator(); - BlockIndex start = it.next(); - currentPathIterator = new PathIterator(start, it); - } - - if (bluePrintBuilder != null && builderRobot != null) { -// builderRobot.markEndOfBlueprint(bluePrintBuilder); - } - - bluePrintBuilder = currentPathIterator.next(); - - if (bluePrintBuilder != null) { - box.deleteLasers(); - box.reset(); - - /* - box.initialize(bluePrintBuilder); - box.createLasers(worldObj, LaserKind.Stripes); - */ - } - - if (builderRobot != null) { - builderRobot.setBox(box); - } - - if (bluePrintBuilder == null) { - currentPathIterator = currentPathIterator.iterate(); - } - - if (currentPathIterator == null) { - done = true; - } - } else { - if (bluePrintBuilder != null && bluePrintBuilder.done) { - if (builderRobot != null) { -// builderRobot.markEndOfBlueprint(bluePrintBuilder); - } - - done = true; - bluePrintBuilder = null; - } else { - bluePrintBuilder = instanciateBluePrint(xCoord, yCoord, zCoord, - ForgeDirection.values()[worldObj.getBlockMetadata(xCoord, yCoord, zCoord)].getOpposite()); - - if (bluePrintBuilder != null) { - box.initialize(bluePrintBuilder); - box.createLasers(worldObj, LaserKind.Stripes); - } + if (builderRobot.readyToBuild()) { + while (blueprintIterator.hasNext()) { + if (builderRobot.scheduleContruction(blueprintIterator.next())) { + powerHandler.useEnergy(0, 25, true); + break; } } } @@ -376,51 +126,27 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory, IP @Override public int getSizeInventory() { - return items.length; + return inv.getSizeInventory(); } @Override - public ItemStack getStackInSlot(int i) { - return items[i]; + public ItemStack getStackInSlot(int slot) { + return inv.getStackInSlot(slot); } @Override - public ItemStack decrStackSize(int i, int j) { - ItemStack result; - if (items[i] == null) { - result = null; - } else if (items[i].stackSize > j) { - result = items[i].splitStack(j); - } else { - ItemStack tmp = items[i]; - items[i] = null; - result = tmp; - } - - if (i == 0) { - iterateBpt(); - } - - return result; + public ItemStack decrStackSize(int slot, int amount) { + return inv.decrStackSize(slot, amount); } @Override - public void setInventorySlotContents(int i, ItemStack itemstack) { - items[i] = itemstack; - - if (i == 0) { - iterateBpt(); - done = false; - } + public void setInventorySlotContents(int slot, ItemStack stack) { + inv.setInventorySlotContents(slot, stack); } @Override public ItemStack getStackInSlotOnClosing(int slot) { - if (items[slot] == null) - return null; - ItemStack toReturn = items[slot]; - items[slot] = null; - return toReturn; + return inv.getStackInSlotOnClosing(slot); } @Override @@ -434,7 +160,10 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory, IP } @Override - public boolean isItemValidForSlot(int i, ItemStack itemstack) { + public boolean isItemValidForSlot(int slot, ItemStack stack) { + if (slot == SLOT_BLUEPRINT) { + return ItemBlueprint.getBlueprint(stack) != null; + } return true; } @@ -444,53 +173,31 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory, IP } @Override - public void readFromNBT(NBTTagCompound nbttagcompound) { - super.readFromNBT(nbttagcompound); + public void readFromNBT(NBTTagCompound nbt) { + super.readFromNBT(nbt); - Utils.readStacksFromNBT(nbttagcompound, "Items", items); + inv.readFromNBT(nbt); - if (nbttagcompound.hasKey("box")) { - box.initialize(nbttagcompound.getCompoundTag("box")); + if (nbt.hasKey("box")) { + box.initialize(nbt.getCompoundTag("box")); } - if (nbttagcompound.hasKey("path")) { - path = new LinkedList(); - NBTTagList list = nbttagcompound.getTagList("path"); - - for (int i = 0; i < list.tagCount(); ++i) { - path.add(new BlockIndex((NBTTagCompound) list.tagAt(i))); - } - } - - done = nbttagcompound.getBoolean("done"); - + builderDone = nbt.getBoolean("done"); } @Override - public void writeToNBT(NBTTagCompound nbttagcompound) { - super.writeToNBT(nbttagcompound); + public void writeToNBT(NBTTagCompound nbt) { + super.writeToNBT(nbt); - Utils.writeStacksToNBT(nbttagcompound, "Items", items); + inv.writeToNBT(nbt); if (box.isInitialized()) { NBTTagCompound boxStore = new NBTTagCompound(); box.writeToNBT(boxStore); - nbttagcompound.setTag("box", boxStore); + nbt.setTag("box", boxStore); } - if (path != null) { - NBTTagList list = new NBTTagList(); - - for (BlockIndex i : path) { - NBTTagCompound c = new NBTTagCompound(); - i.writeTo(c); - list.appendTag(c); - } - - nbttagcompound.setTag("path", list); - } - - nbttagcompound.setBoolean("done", done); + nbt.setBoolean("done", builderDone); } @Override @@ -509,8 +216,6 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory, IP builderRobot.setDead(); builderRobot = null; } - - cleanPathLasers(); } @Override @@ -548,19 +253,12 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory, IP public void closeChest() { } -// @Override -// public int powerRequest(ForgeDirection from) { -// if ((bluePrintBuilder != null || currentPathIterator != null) && !done) -// return powerProvider.getMaxEnergyReceived(); -// else -// return 0; -// } @Override public void updateEntity() { super.updateEntity(); - if ((bluePrintBuilder == null || bluePrintBuilder.done) && box.isInitialized() && (builderRobot == null || builderRobot.done())) { + if (blueprintBuilder == null && box.isInitialized() && (builderRobot == null || builderRobot.done())) { box.deleteLasers(); box.reset(); @@ -572,7 +270,7 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory, IP return; } - if (!box.isInitialized() && bluePrintBuilder == null && builderRobot != null) { + if (!box.isInitialized() && blueprintBuilder == null && builderRobot != null) { builderRobot.setDead(); builderRobot = null; } @@ -580,7 +278,7 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory, IP @Override public boolean isActive() { - return !done; + return !builderDone; } @Override @@ -593,27 +291,6 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory, IP return true; } - public void cleanPathLasers() { - if (pathLasers != null) { - for (EntityLaser laser : pathLasers) { - laser.setDead(); - } - - pathLasers = null; - } - } - - public boolean isBuildingBlueprint() { - return getStackInSlot(0) != null && getStackInSlot(0).getItem() instanceof ItemBptBluePrint; - } - - public Collection getNeededItems() { - if (bluePrintBuilder instanceof BptBuilderBlueprint) - return ((BptBuilderBlueprint) bluePrintBuilder).neededItems; - else - return null; - } - @Override public boolean isBuildingMaterial(int i) { return i != 0; diff --git a/common/buildcraft/builders/blueprints/Blueprint.java b/common/buildcraft/builders/blueprints/Blueprint.java index 8552100e..4f983818 100644 --- a/common/buildcraft/builders/blueprints/Blueprint.java +++ b/common/buildcraft/builders/blueprints/Blueprint.java @@ -7,10 +7,12 @@ */ package buildcraft.builders.blueprints; -import buildcraft.BuildCraftCore; +import buildcraft.BuildCraftBuilders; import buildcraft.api.builder.BlockHandler; import buildcraft.core.inventory.StackHelper; import buildcraft.core.utils.BCLog; +import buildcraft.core.utils.NBTUtils; +import buildcraft.factory.TileQuarry; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; @@ -239,4 +241,15 @@ public class Blueprint { public void setCreator(String creator) { this.creator = creator; } + + public void rotateLeft() { + anchorOrientation = anchorOrientation.getRotation(ForgeDirection.DOWN); + } + + public ItemStack getBlueprintItem() { + ItemStack blueprint = new ItemStack(BuildCraftBuilders.blueprintItem, 1, 1); + NBTTagCompound nbt = NBTUtils.getItemData(blueprint); + NBTUtils.writeUUID(nbt, "blueprint", uuid); + return blueprint; + } } diff --git a/common/buildcraft/builders/blueprints/BlueprintDatabase.java b/common/buildcraft/builders/blueprints/BlueprintDatabase.java index 6cf10cf2..5b70dd81 100644 --- a/common/buildcraft/builders/blueprints/BlueprintDatabase.java +++ b/common/buildcraft/builders/blueprints/BlueprintDatabase.java @@ -32,6 +32,8 @@ public class BlueprintDatabase { private static Map blueprints = new HashMap(); public static Blueprint getBlueprint(UUID uuid) { + if(uuid == null) + return null; Blueprint blueprint = blueprints.get(uuid); if (blueprint == null) { blueprint = loadBlueprint(uuid); diff --git a/common/buildcraft/builders/gui/ContainerBuilder.java b/common/buildcraft/builders/gui/ContainerBuilder.java index 3e9e1ac7..9f749f19 100644 --- a/common/buildcraft/builders/gui/ContainerBuilder.java +++ b/common/buildcraft/builders/gui/ContainerBuilder.java @@ -31,18 +31,16 @@ public class ContainerBuilder extends BuildCraftContainer { for (int j1 = 0; j1 < 9; j1++) { addSlotToContainer(new Slot(builder, 1 + j1 + k * 9, 8 + j1 * 18, 72 + k * 18)); } - } - for (int l = 0; l < 3; l++) { - for (int k1 = 0; k1 < 9; k1++) { - addSlotToContainer(new Slot(playerInventory, k1 + l * 9 + 9, 8 + k1 * 18, 140 + l * 18)); + for (int y = 0; y < 3; y++) { + for (int x = 0; x < 9; x++) { + addSlotToContainer(new Slot(playerInventory, x + y * 9 + 9, 8 + x * 18, 140 + y * 18)); } - } - for (int i1 = 0; i1 < 9; i1++) { - addSlotToContainer(new Slot(playerInventory, i1, 8 + i1 * 18, 198)); + for (int x = 0; x < 9; x++) { + addSlotToContainer(new Slot(playerInventory, x, 8 + x * 18, 198)); } } diff --git a/common/buildcraft/builders/gui/GuiBuilder.java b/common/buildcraft/builders/gui/GuiBuilder.java index a3bd533d..3657bb49 100644 --- a/common/buildcraft/builders/gui/GuiBuilder.java +++ b/common/buildcraft/builders/gui/GuiBuilder.java @@ -8,6 +8,7 @@ package buildcraft.builders.gui; import buildcraft.builders.TileBuilder; +import buildcraft.builders.blueprints.Blueprint; import buildcraft.core.DefaultProps; import buildcraft.core.gui.GuiAdvancedInterface; import buildcraft.core.utils.StringUtils; @@ -49,9 +50,9 @@ public class GuiBuilder extends GuiAdvancedInterface { fontRenderer.drawString(StringUtils.localize("gui.building.resources"), 8, 60, 0x404040); fontRenderer.drawString(StringUtils.localize("gui.inventory"), 8, ySize - 97, 0x404040); - if (builder.isBuildingBlueprint()) { +// if (builder.isBuildingBlueprint()) { fontRenderer.drawString(StringUtils.localize("gui.needed"), 185, 7, 0x404040); - } +// } drawForegroundSelection(par1, par2); } @@ -63,13 +64,13 @@ public class GuiBuilder extends GuiAdvancedInterface { int k = (height - ySize) / 2; int realXSize = 0; - if (builder.isBuildingBlueprint()) { +// if (builder.isBuildingBlueprint()) { mc.renderEngine.bindTexture(BLUEPRINT_TEXTURE); realXSize = 256; - } else { - mc.renderEngine.bindTexture(TEXTURE); - realXSize = 176; - } +// } else { +// mc.renderEngine.bindTexture(TEXTURE); +// realXSize = 176; +// } drawTexturedModalRect(j, k, 0, 0, realXSize, ySize); @@ -77,18 +78,21 @@ public class GuiBuilder extends GuiAdvancedInterface { ((ItemSlot) slots[s]).stack = null; } - Collection needs = builder.getNeededItems(); + Blueprint blueprint = builder.getBlueprint(); + if(blueprint != null){ + Collection needs = blueprint.getCost(); - if (needs != null) { - int s = 0; + if (needs != null) { + int s = 0; - for (ItemStack stack : needs) { - if (s >= slots.length) { - break; + for (ItemStack stack : needs) { + if (s >= slots.length) { + break; + } + + ((ItemSlot) slots[s]).stack = stack.copy(); + s++; } - - ((ItemSlot) slots[s]).stack = stack.copy(); - s++; } } diff --git a/common/buildcraft/core/EntityRobot.java b/common/buildcraft/core/EntityRobot.java index 10bb4679..8894acce 100644 --- a/common/buildcraft/core/EntityRobot.java +++ b/common/buildcraft/core/EntityRobot.java @@ -8,10 +8,8 @@ package buildcraft.core; -import buildcraft.BuildCraftCore; import buildcraft.api.core.Position; import buildcraft.builders.blueprints.BlueprintBuilder.SchematicBuilder; -import buildcraft.core.blueprints.BptSlot.Mode; import buildcraft.core.proxy.CoreProxy; import buildcraft.core.utils.BCLog; import buildcraft.core.utils.BlockUtil; diff --git a/common/buildcraft/core/inventory/StackHelper.java b/common/buildcraft/core/inventory/StackHelper.java index 95ee6a0b..eb918530 100644 --- a/common/buildcraft/core/inventory/StackHelper.java +++ b/common/buildcraft/core/inventory/StackHelper.java @@ -8,6 +8,7 @@ package buildcraft.core.inventory; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.oredict.OreDictionary; public class StackHelper { @@ -87,9 +88,9 @@ public class StackHelper { if (oreDictionary) { int idBase = OreDictionary.getOreID(base); if (idBase >= 0) { - for(ItemStack itemstack : OreDictionary.getOres(idBase)) { - if(comparison.itemID == itemstack.itemID && (itemstack.getItemDamage() == OreDictionary.WILDCARD_VALUE || comparison.getItemDamage() == itemstack.getItemDamage())) - return true; + for (ItemStack itemstack : OreDictionary.getOres(idBase)) { + if (comparison.itemID == itemstack.itemID && (itemstack.getItemDamage() == OreDictionary.WILDCARD_VALUE || comparison.getItemDamage() == itemstack.getItemDamage())) + return true; } } } diff --git a/common/buildcraft/core/utils/NBTUtils.java b/common/buildcraft/core/utils/NBTUtils.java new file mode 100644 index 00000000..3fa415a6 --- /dev/null +++ b/common/buildcraft/core/utils/NBTUtils.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) SpaceToad, 2011-2012 + * 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.core.utils; + +import java.util.UUID; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; + +/** + * + * @author CovertJaguar + */ +public class NBTUtils { + + public static NBTTagCompound getItemData(ItemStack stack) { + NBTTagCompound nbt = stack.getTagCompound(); + if (nbt == null) { + nbt = new NBTTagCompound("tag"); + stack.setTagCompound(nbt); + } + return nbt; + } + + public static void writeUUID(NBTTagCompound data, String tag, UUID uuid) { + if (uuid == null) + return; + NBTTagCompound nbtTag = new NBTTagCompound(); + nbtTag.setLong("most", uuid.getMostSignificantBits()); + nbtTag.setLong("least", uuid.getLeastSignificantBits()); + data.setTag(tag, nbtTag); + } + + public static UUID readUUID(NBTTagCompound data, String tag) { + if (data.hasKey(tag)) { + NBTTagCompound nbtTag = data.getCompoundTag(tag); + return new UUID(nbtTag.getLong("most"), nbtTag.getLong("least")); + } + return null; + } +}