diff --git a/src/main/java/forestry/arboriculture/gadgets/ForestryBlockLeaves.java b/src/main/java/forestry/arboriculture/gadgets/ForestryBlockLeaves.java index 5570c838e..28b8d389f 100644 --- a/src/main/java/forestry/arboriculture/gadgets/ForestryBlockLeaves.java +++ b/src/main/java/forestry/arboriculture/gadgets/ForestryBlockLeaves.java @@ -56,6 +56,7 @@ import forestry.arboriculture.items.ItemLeavesBlock; import forestry.arboriculture.render.LeafTexture; import forestry.core.proxy.Proxies; import forestry.core.utils.StackUtils; +import forestry.core.utils.Utils; import forestry.plugins.PluginArboriculture; import forestry.plugins.PluginLepidopterology; @@ -79,16 +80,6 @@ public class ForestryBlockLeaves extends BlockNewLeaf implements ITileEntityProv return null; } - private static NBTTagCompound getTagCompoundForLeaves(IBlockAccess world, int x, int y, int z) { - TileLeaves leaves = getLeafTile(world, x, y, z); - NBTTagCompound nbttagcompound = new NBTTagCompound(); - if (leaves == null) { - return nbttagcompound; - } - leaves.writeToNBT(nbttagcompound); - return nbttagcompound; - } - @SideOnly(Side.CLIENT) @Override @SuppressWarnings({"unchecked", "rawtypes"}) @@ -100,7 +91,7 @@ public class ForestryBlockLeaves extends BlockNewLeaf implements ITileEntityProv leaves.setTree(tree); NBTTagCompound leavesNBT = new NBTTagCompound(); - leaves.writeToNBT(leavesNBT); + leaves.writeToNBTDecorative(leavesNBT); ItemStack itemStack = new ItemStack(item, 1, 0); itemStack.setTagCompound(leavesNBT); @@ -180,7 +171,9 @@ public class ForestryBlockLeaves extends BlockNewLeaf implements ITileEntityProv @Override public ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z) { ItemStack itemStack = super.getPickBlock(target, world, x, y, z); - NBTTagCompound leavesNBT = getTagCompoundForLeaves(world, x, y, z); + TileLeaves leaves = getLeafTile(world, x, y, z); + NBTTagCompound leavesNBT = new NBTTagCompound(); + leaves.writeToNBTDecorative(leavesNBT); itemStack.setTagCompound(leavesNBT); return itemStack; } @@ -194,11 +187,13 @@ public class ForestryBlockLeaves extends BlockNewLeaf implements ITileEntityProv public ArrayList onSheared(ItemStack item, IBlockAccess world, int x, int y, int z, int fortune) { ArrayList ret = super.onSheared(item, world, x, y, z, fortune); - NBTTagCompound leavesNBT = getTagCompoundForLeaves(world, x, y, z); + TileLeaves leaves = getLeafTile(world, x, y, z); + NBTTagCompound shearedLeavesNBT = new NBTTagCompound(); + leaves.writeToNBTDecorative(shearedLeavesNBT); + for (ItemStack stack : ret) { if (stack.getItem() instanceof ItemLeavesBlock) { - NBTTagCompound leavesNBTCopy = (NBTTagCompound) leavesNBT.copy(); - stack.setTagCompound(leavesNBTCopy); + stack.setTagCompound(shearedLeavesNBT); } } diff --git a/src/main/java/forestry/arboriculture/gadgets/TileLeaves.java b/src/main/java/forestry/arboriculture/gadgets/TileLeaves.java index 5702768bd..ef524e4ea 100755 --- a/src/main/java/forestry/arboriculture/gadgets/TileLeaves.java +++ b/src/main/java/forestry/arboriculture/gadgets/TileLeaves.java @@ -19,10 +19,13 @@ import java.util.EnumSet; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTUtil; import net.minecraft.util.ChunkCoordinates; import net.minecraft.util.IIcon; import net.minecraft.world.World; +import com.mojang.authlib.GameProfile; + import net.minecraftforge.common.EnumPlantType; import cpw.mods.fml.relauncher.Side; @@ -47,6 +50,7 @@ import forestry.api.lepidopterology.IButterfly; import forestry.api.lepidopterology.IButterflyGenome; import forestry.api.lepidopterology.IButterflyNursery; import forestry.api.lepidopterology.IButterflyRoot; +import forestry.arboriculture.genetics.Tree; import forestry.arboriculture.genetics.TreeDefinition; import forestry.arboriculture.network.PacketRipeningUpdate; import forestry.core.genetics.alleles.Allele; @@ -83,43 +87,87 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru /* SAVING & LOADING */ @Override public void readFromNBT(NBTTagCompound nbttagcompound) { - super.readFromNBT(nbttagcompound); - - ripeningTime = nbttagcompound.getShort("RT"); - damage = nbttagcompound.getInteger("ENC"); - - if (nbttagcompound.hasKey("CATER")) { - maturationTime = nbttagcompound.getInteger("CATMAT"); - caterpillar = (IButterfly) AlleleManager.alleleRegistry.getSpeciesRoot("rootButterflies").getMember(nbttagcompound.getCompoundTag("CATER")); + // trees from itemStacks will have no coordinates + if (nbttagcompound.hasKey("x")) { + this.xCoord = nbttagcompound.getInteger("x"); + this.yCoord = nbttagcompound.getInteger("y"); + this.zCoord = nbttagcompound.getInteger("z"); } - if (nbttagcompound.hasKey("Decorative")) { //legacy - isDecorative = nbttagcompound.getBoolean("Decorative"); + if (nbttagcompound.hasKey("species")) { + isDecorative = true; + + String speciesUID = nbttagcompound.getString("species"); + IAllele[] treeTemplate = TreeManager.treeRoot.getTemplate(speciesUID); + ITree containedTree = TreeManager.treeRoot.templateAsIndividual(treeTemplate); + setTree(containedTree); } else { - isDecorative = nbttagcompound.getBoolean("DEC"); - } + if (nbttagcompound.hasKey("owner")) { + setOwner(NBTUtil.func_152459_a(nbttagcompound.getCompoundTag("owner"))); + } - ITree tree = getTree(); - if (tree != null) { - setTree(tree); + ripeningTime = nbttagcompound.getShort("RT"); + damage = nbttagcompound.getInteger("ENC"); + + if (nbttagcompound.hasKey("CATER")) { + maturationTime = nbttagcompound.getInteger("CATMAT"); + caterpillar = (IButterfly) AlleleManager.alleleRegistry.getSpeciesRoot("rootButterflies").getMember(nbttagcompound.getCompoundTag("CATER")); + } + + if (nbttagcompound.hasKey("Decorative")) { //legacy + isDecorative = nbttagcompound.getBoolean("Decorative"); + } else { //legacy + isDecorative = nbttagcompound.getBoolean("DEC"); + } + + if (nbttagcompound.hasKey("ContainedTree")) { + setTree(new Tree(nbttagcompound.getCompoundTag("ContainedTree"))); + } + } + } + + public void writeToNBTDecorative(NBTTagCompound nbtTagCompound) { + String speciesUID = getSpeciesUID(); + if (speciesUID != null) { + nbtTagCompound.setString("species", speciesUID); } } @Override - public void writeToNBT(NBTTagCompound nbttagcompound) { - super.writeToNBT(nbttagcompound); + public void writeToNBT(NBTTagCompound nbtTagCompound) { + nbtTagCompound.setString("id", "forestry.Leaves"); + nbtTagCompound.setInteger("x", this.xCoord); + nbtTagCompound.setInteger("y", this.yCoord); + nbtTagCompound.setInteger("z", this.zCoord); - nbttagcompound.setInteger("RT", getRipeningTime()); - nbttagcompound.setInteger("ENC", damage); + if (isDecorative) { + writeToNBTDecorative(nbtTagCompound); + } else { + ITree tree = getTree(); + if (tree != null) { + NBTTagCompound subcompound = new NBTTagCompound(); + tree.writeToNBT(subcompound); + nbtTagCompound.setTag("ContainedTree", subcompound); + } - if (caterpillar != null) { - nbttagcompound.setInteger("CATMAT", maturationTime); + GameProfile owner = getOwner(); + if (owner != null) { + NBTTagCompound nbt = new NBTTagCompound(); + NBTUtil.func_152460_a(nbt, owner); + nbtTagCompound.setTag("owner", nbt); + } - NBTTagCompound subcompound = new NBTTagCompound(); - caterpillar.writeToNBT(subcompound); - nbttagcompound.setTag("CATER", subcompound); + nbtTagCompound.setInteger("RT", getRipeningTime()); + nbtTagCompound.setInteger("ENC", damage); + + if (caterpillar != null) { + nbtTagCompound.setInteger("CATMAT", maturationTime); + + NBTTagCompound subcompound = new NBTTagCompound(); + caterpillar.writeToNBT(subcompound); + nbtTagCompound.setTag("CATER", subcompound); + } } - nbttagcompound.setBoolean("DEC", isDecorative); } @Override @@ -197,6 +245,7 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru } else if (caterpillar != null) { colourLeaves = Utils.multiplyRGBComponents(colourLeaves, 1.5f); } + markDirty(); } /* INFORMATION */ diff --git a/src/main/java/forestry/arboriculture/items/ItemLeavesBlock.java b/src/main/java/forestry/arboriculture/items/ItemLeavesBlock.java index dd02cc50f..ce032b740 100644 --- a/src/main/java/forestry/arboriculture/items/ItemLeavesBlock.java +++ b/src/main/java/forestry/arboriculture/items/ItemLeavesBlock.java @@ -1,3 +1,13 @@ +/******************************************************************************* + * Copyright (c) 2011-2014 SirSengir. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser Public License v3 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Various Contributors including, but not limited to: + * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges + ******************************************************************************/ package forestry.arboriculture.items; import net.minecraft.block.Block;