diff --git a/api/forestry b/api/forestry index 74ec891c2..3e5b83d1b 160000 --- a/api/forestry +++ b/api/forestry @@ -1 +1 @@ -Subproject commit 74ec891c2cf7fd992fbb6604eb3b56fb696bc4b1 +Subproject commit 3e5b83d1b9331c02db01dc52aebe9bc2e13372b7 diff --git a/src/main/java/forestry/arboriculture/EventHandlerArboriculture.java b/src/main/java/forestry/arboriculture/EventHandlerArboriculture.java index b05f9c119..2de087818 100755 --- a/src/main/java/forestry/arboriculture/EventHandlerArboriculture.java +++ b/src/main/java/forestry/arboriculture/EventHandlerArboriculture.java @@ -44,7 +44,7 @@ public class EventHandlerArboriculture { } } else if (tile instanceof IFruitBearer) { IFruitBearer bearer = (IFruitBearer) tile; - if (bearer.getRipeness() <= 1.0f) { + if (bearer.getRipeness() < 1.0f) { bearer.addRipeness(1.0f); event.setResult(Result.ALLOW); } diff --git a/src/main/java/forestry/arboriculture/FruitProviderNone.java b/src/main/java/forestry/arboriculture/FruitProviderNone.java index 34f32fafe..5cfc10f52 100755 --- a/src/main/java/forestry/arboriculture/FruitProviderNone.java +++ b/src/main/java/forestry/arboriculture/FruitProviderNone.java @@ -50,7 +50,7 @@ public class FruitProviderNone implements IFruitProvider { private final String key; private final IFruitFamily family; - int ripeningPeriod = 10; + protected int ripeningPeriod = 10; OverlayType overlay = null; diff --git a/src/main/java/forestry/arboriculture/gadgets/ForestryBlockLeaves.java b/src/main/java/forestry/arboriculture/gadgets/ForestryBlockLeaves.java index 8d13d3fd9..42640a364 100644 --- a/src/main/java/forestry/arboriculture/gadgets/ForestryBlockLeaves.java +++ b/src/main/java/forestry/arboriculture/gadgets/ForestryBlockLeaves.java @@ -42,6 +42,7 @@ import forestry.api.core.IToolScoop; import forestry.api.core.Tabs; import forestry.api.lepidopterology.EnumFlutterType; import forestry.api.lepidopterology.IButterfly; +import forestry.arboriculture.items.ItemLeavesBlock; import forestry.core.proxy.Proxies; import forestry.core.render.TextureManager; import forestry.core.utils.StackUtils; @@ -68,16 +69,10 @@ public class ForestryBlockLeaves extends BlockNewLeaf implements ITileEntityProv return null; } - private static NBTTagCompound getTagCompoundForTree(IBlockAccess world, int x, int y, int z) { + private static NBTTagCompound getTagCompoundForLeaves(IBlockAccess world, int x, int y, int z) { TileLeaves leaves = getLeafTile(world, x, y, z); - ITree tree = leaves.getTree(); - NBTTagCompound nbttagcompound = new NBTTagCompound(); - if (tree == null) { - return nbttagcompound; - } - - tree.writeToNBT(nbttagcompound); + leaves.writeToNBT(nbttagcompound); return nbttagcompound; } @@ -87,11 +82,15 @@ public class ForestryBlockLeaves extends BlockNewLeaf implements ITileEntityProv public void getSubBlocks(Item item, CreativeTabs tab, List list) { for (ITree tree : PluginArboriculture.treeInterface.getIndividualTemplates()) { - NBTTagCompound treeNBT = new NBTTagCompound(); - tree.writeToNBT(treeNBT); + TileLeaves leaves = new TileLeaves(); + leaves.setDecorative(); + leaves.setTree(tree); + + NBTTagCompound leavesNBT = new NBTTagCompound(); + leaves.writeToNBT(leavesNBT); ItemStack itemStack = new ItemStack(item, 1, 0); - itemStack.setTagCompound(treeNBT); + itemStack.setTagCompound(leavesNBT); list.add(itemStack); } @@ -167,8 +166,8 @@ 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 treeNBT = getTagCompoundForTree(world, x, y, z); - itemStack.setTagCompound(treeNBT); + NBTTagCompound leavesNBT = getTagCompoundForLeaves(world, x, y, z); + itemStack.setTagCompound(leavesNBT); return itemStack; } @@ -181,9 +180,12 @@ 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 treeNBT = getTagCompoundForTree(world, x, y, z); + NBTTagCompound leavesNBT = getTagCompoundForLeaves(world, x, y, z); for (ItemStack stack : ret) { - stack.setTagCompound(treeNBT); + if (stack.getItem() instanceof ItemLeavesBlock) { + NBTTagCompound leavesNBTCopy = (NBTTagCompound) leavesNBT.copy(); + stack.setTagCompound(leavesNBTCopy); + } } return ret; diff --git a/src/main/java/forestry/arboriculture/gadgets/TileLeaves.java b/src/main/java/forestry/arboriculture/gadgets/TileLeaves.java index 2e5386ba6..37695f7c5 100755 --- a/src/main/java/forestry/arboriculture/gadgets/TileLeaves.java +++ b/src/main/java/forestry/arboriculture/gadgets/TileLeaves.java @@ -10,9 +10,9 @@ ******************************************************************************/ package forestry.arboriculture.gadgets; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.EnumSet; import net.minecraft.block.Block; @@ -31,20 +31,24 @@ import net.minecraftforge.common.EnumPlantType; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; -import forestry.api.arboriculture.IAlleleFruit; +import forestry.api.arboriculture.IAlleleTreeSpecies; import forestry.api.arboriculture.IFruitProvider; import forestry.api.arboriculture.ILeafTickHandler; import forestry.api.arboriculture.ITree; +import forestry.api.arboriculture.ITreeGenome; +import forestry.api.arboriculture.ITreekeepingMode; import forestry.api.core.EnumHumidity; import forestry.api.core.EnumTemperature; import forestry.api.core.IErrorState; import forestry.api.genetics.AlleleManager; +import forestry.api.genetics.IAllele; import forestry.api.genetics.IEffectData; import forestry.api.genetics.IFruitBearer; import forestry.api.genetics.IFruitFamily; import forestry.api.genetics.IIndividual; import forestry.api.genetics.IPollinatable; import forestry.api.lepidopterology.IButterfly; +import forestry.api.lepidopterology.IButterflyGenome; import forestry.api.lepidopterology.IButterflyNursery; import forestry.api.lepidopterology.IButterflyRoot; import forestry.arboriculture.network.PacketLeafUpdate; @@ -62,6 +66,8 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru private int colourLeaves; private int colourFruits; + private String speciesUID; + private short textureIndexFancy = 48; private short textureIndexPlain = 64; private short textureIndexFruits = -1; @@ -69,12 +75,13 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru private boolean isFruitLeaf; private boolean isPollinatedState; private int ripeningTime; + private short ripeningPeriod = Short.MAX_VALUE - 1; // set true when placed by a player private boolean isDecorative = false; private int maturationTime; - private int encumbrance; + private int damage; private BiomeGenBase biome; @@ -92,25 +99,38 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru public void readFromNBT(NBTTagCompound nbttagcompound) { super.readFromNBT(nbttagcompound); - isFruitLeaf = nbttagcompound.getBoolean("FL"); - ripeningTime = nbttagcompound.getInteger("RT"); - - encumbrance = nbttagcompound.getInteger("ENC"); + 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")); } - isDecorative = nbttagcompound.getBoolean("Decorative"); + + if (nbttagcompound.hasKey("Decorative")) { //legacy + isDecorative = nbttagcompound.getBoolean("Decorative"); + } else { + isDecorative = nbttagcompound.getBoolean("DEC"); + } + + if (nbttagcompound.hasKey("UID")) { + speciesUID = "forestry." + nbttagcompound.getString("UID"); + } + + ITree tree = getTree(); + if (tree != null) { + setTree(tree); + } else if (speciesUID != null) { + setTree(speciesUID, false); + } } @Override public void writeToNBT(NBTTagCompound nbttagcompound) { super.writeToNBT(nbttagcompound); - nbttagcompound.setBoolean("FL", isFruitLeaf); - nbttagcompound.setInteger("RT", ripeningTime); - nbttagcompound.setInteger("ENC", encumbrance); + nbttagcompound.setInteger("RT", getRipeningTime()); + nbttagcompound.setInteger("ENC", damage); if (caterpillar != null) { nbttagcompound.setInteger("CATMAT", maturationTime); @@ -119,7 +139,12 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru caterpillar.writeToNBT(subcompound); nbttagcompound.setTag("CATER", subcompound); } - nbttagcompound.setBoolean("Decorative", isDecorative); + nbttagcompound.setBoolean("DEC", isDecorative); + + if (speciesUID != null) { + String strippedUID = speciesUID.replace("forestry.", ""); + nbttagcompound.setString("UID", strippedUID); + } } @Override @@ -128,13 +153,16 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru updateBiome(); } - if (isDecorative || getTree() == null) { + ITree tree = getTree(); + if (isDecorative || tree == null) { return; } - boolean isDestroyed = isDestroyed(); - for (ILeafTickHandler tickHandler : getTree().getGenome().getPrimary().getRoot().getLeafTickHandlers()) { - if (tickHandler.onRandomLeafTick(getTree(), worldObj, biome.biomeID, xCoord, yCoord, zCoord, isDestroyed)) { + ITreeGenome genome = tree.getGenome(); + + boolean isDestroyed = isDestroyed(tree, damage); + for (ILeafTickHandler tickHandler : genome.getPrimary().getRoot().getLeafTickHandlers()) { + if (tickHandler.onRandomLeafTick(tree, worldObj, biome.biomeID, xCoord, yCoord, zCoord, isDestroyed)) { return; } } @@ -143,13 +171,14 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru return; } - if (encumbrance > 0) { - encumbrance--; + if (damage > 0) { + damage--; } - if (hasFruit() && ripeningTime < Short.MAX_VALUE - 1) { - float sappiness = getTree().getGenome().getSappiness() - * PluginArboriculture.treeInterface.getTreekeepingMode(worldObj).getSappinessModifier(this.getTree().getGenome(), 1f); + if (hasFruit() && getRipeningTime() < ripeningPeriod) { + ITreekeepingMode treekeepingMode = PluginArboriculture.treeInterface.getTreekeepingMode(worldObj); + float sappinessModifier = treekeepingMode.getSappinessModifier(genome, 1f); + float sappiness = genome.getSappiness() * sappinessModifier; if (worldObj.rand.nextFloat() < sappiness) { ripeningTime++; @@ -161,44 +190,95 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru matureCaterpillar(); } - effectData = getTree().doEffect(effectData, worldObj, biome.biomeID, xCoord, yCoord, zCoord); + effectData = tree.doEffect(effectData, worldObj, biome.biomeID, xCoord, yCoord, zCoord); + } + + public void setTree(String speciesUID, boolean isPollinatedState) { + ITree tree = getTree(); + if (tree != null && tree.getIdent().equals(speciesUID)) { + return; + } + + IAllele[] treeTemplate = PluginArboriculture.treeInterface.getTemplate(speciesUID); + if (treeTemplate != null) { + ITree newTree = PluginArboriculture.treeInterface.templateAsIndividual(treeTemplate); + if (isPollinatedState) { + newTree.mate(newTree); + } + setTree(newTree); + } } @Override public void setTree(ITree tree) { super.setTree(tree); + ITreeGenome genome = tree.getGenome(); + IAlleleTreeSpecies species = genome.getPrimary(); + if (tree.canBearFruit()) { - isFruitLeaf = tree.getGenome().getFruitProvider().markAsFruitLeaf(tree.getGenome(), worldObj, xCoord, yCoord, zCoord); + IFruitProvider fruitProvider = genome.getFruitProvider(); + + isFruitLeaf = fruitProvider.markAsFruitLeaf(genome, worldObj, xCoord, yCoord, zCoord); + if (isFruitLeaf) { + // Hardcoded because vanilla oak trees don't show fruits. + if ((species == Allele.treeOak) && (fruitProvider == Allele.fruitApple.getProvider())) { + textureIndexFruits = -1; + } else { + textureIndexFruits = fruitProvider.getIconIndex(genome, worldObj, xCoord, yCoord, zCoord, getRipeningTime(), true); + } + + ripeningPeriod = (short) tree.getGenome().getFruitProvider().getRipeningPeriod(); + } + } else { + isFruitLeaf = false; + textureIndexFruits = -1; } - textureIndexFancy = determineTextureIndex(true); - textureIndexPlain = determineTextureIndex(false); - textureIndexFruits = determineOverlayIndex(); - colourLeaves = determineFoliageColour(); - colourFruits = determineFruitColour(); + textureIndexFancy = species.getLeafIconIndex(tree, true); + textureIndexPlain = species.getLeafIconIndex(tree, false); + + colourLeaves = species.getLeafColour(tree); + if (isDestroyed(tree, damage)) { + colourLeaves = Utils.addRGBComponents(colourLeaves, 92, 61, 0); + } else if (caterpillar != null) { + colourLeaves = Utils.multiplyRGBComponents(colourLeaves, 1.5f); + } + + speciesUID = tree.getIdent(); } /* INFORMATION */ - private boolean isDestroyed() { - if (getTree() == null) { + private static boolean isDestroyed(ITree tree, int damage) { + if (tree == null) { return false; } - return encumbrance > getTree().getResilience(); + return damage > tree.getResilience(); } public boolean isPollinated() { - return !isDestroyed() && getTree() != null && getTree().getMate() != null; + ITree tree = getTree(); + return tree != null && !isDestroyed(tree, damage) && tree.getMate() != null; } public int getFoliageColour(EntityPlayer player) { - return isPollinatedState & GeneticsUtil.hasNaturalistEye(player) ? 0xffffff : colourLeaves; + return (isPollinatedState && GeneticsUtil.hasNaturalistEye(player)) ? 0xffffff : colourLeaves; } public int getFruitColour() { + if (colourFruits == 0 && hasFruit()) { + colourFruits = determineFruitColour(); + } return colourFruits; } + private int determineFruitColour() { + ITree tree = getTree(); + ITreeGenome genome = tree.getGenome(); + IFruitProvider fruit = genome.getFruitProvider(); + return fruit.getColour(genome, worldObj, xCoord, yCoord, zCoord, getRipeningTime()); + } + @SideOnly(Side.CLIENT) public IIcon getIcon(boolean fancy) { if (fancy) { @@ -218,13 +298,11 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru } public int getRipeningTime() { - return ripeningTime; + return isDecorative ? ripeningPeriod : ripeningTime; } public void setDecorative() { isDecorative = true; - ripeningTime = getTree().getGenome().getFruitProvider().getRipeningPeriod(); - colourFruits = determineFruitColour(); } public boolean isDecorative() { @@ -274,56 +352,8 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru return getTree(); } - public int determineFoliageColour() { - - if (getTree() == null) { - return PluginArboriculture.proxy.getFoliageColorBasic(); - } - - int colour = getTree().getGenome().getPrimary().getLeafColour(getTree()); - - if (isDestroyed()) { - return Utils.addRGBComponents(colour, 92, 61, 0); - } else if (caterpillar != null) { - return Utils.multiplyRGBComponents(colour, 1.5f); - } - return colour; - } - - public int determineFruitColour() { - if (getTree() == null) { - return 0xffffff; - } - - IFruitProvider fruit = getTree().getGenome().getFruitProvider(); - return fruit.getColour(getTree().getGenome(), worldObj, xCoord, yCoord, zCoord, getRipeningTime()); - } - - public short determineTextureIndex(boolean fancy) { - if (getTree() != null) { - return getTree().getGenome().getPrimary().getLeafIconIndex(getTree(), fancy); - } - - return 0; - } - - public short determineOverlayIndex() { - if (getTree() == null) { - return -1; - } - if (!hasFruit()) { - return -1; - } - - IFruitProvider fruit = getTree().getGenome().getFruitProvider(); - - // Hardcoded because vanilla oak trees don't show fruits. - if (getTree().getGenome().getPrimary() == Allele.treeOak && fruit == ((IAlleleFruit) Allele.fruitApple).getProvider()) { - return -1; - } else { - return fruit.getIconIndex(getTree().getGenome(), worldObj, xCoord, yCoord, zCoord, getRipeningTime(), true); - } - + public String getUnlocalizedName() { + return getTree().getGenome().getPrimary().getUnlocalizedName(); } /* NETWORK */ @@ -334,30 +364,43 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru @Override public void sendNetworkUpdate() { - Proxies.net.sendNetworkPacket(new PacketLeafUpdate(this), xCoord, yCoord, zCoord); + PacketLeafUpdate leafUpdate = new PacketLeafUpdate(this); + Proxies.net.sendNetworkPacket(leafUpdate, xCoord, yCoord, zCoord); } private void sendNetworkUpdateRipening() { - Proxies.net.sendNetworkPacket(new PacketRipeningUpdate(this, determineFruitColour()), xCoord, yCoord, zCoord); + int newColourFruits = determineFruitColour(); + if (newColourFruits == colourFruits) { + return; + } + colourFruits = newColourFruits; + + PacketRipeningUpdate ripeningUpdate = new PacketRipeningUpdate(this); + Proxies.net.sendNetworkPacket(ripeningUpdate, xCoord, yCoord, zCoord); } @Override public void fromPacket(ForestryPacket packetRaw) { - super.fromPacket(packetRaw); - PacketLeafUpdate packet = (PacketLeafUpdate) packetRaw; isFruitLeaf = packet.isFruitLeaf(); isPollinatedState = packet.isPollinated(); - textureIndexFancy = packet.textureIndexFancy; - textureIndexPlain = packet.textureIndexPlain; - textureIndexFruits = packet.textureIndexFruit; - colourLeaves = packet.colourLeaves; - colourFruits = packet.colourFruits; + + colourFruits = packet.getColourFruits(); + + speciesUID = packet.getSpeciesUID(); + setTree(speciesUID, isPollinatedState); + + worldObj.func_147479_m(xCoord, yCoord, zCoord); } public void fromRipeningPacket(PacketRipeningUpdate packet) { - colourFruits = packet.colourFruits; + int newColourFruits = packet.getColourFruits(); + if (newColourFruits == colourFruits) { + return; + } + colourFruits = newColourFruits; + worldObj.func_147479_m(xCoord, yCoord, zCoord); } /** @@ -372,47 +415,48 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru /* IFRUITBEARER */ @Override public Collection pickFruit(ItemStack tool) { - if (!hasFruit() || isDecorative || getTree() == null) { - return new ArrayList(); + ITree tree = getTree(); + if (tree == null || !hasFruit() || isDecorative) { + return Collections.emptyList(); } - ArrayList picked = new ArrayList(Arrays.asList(getTree().produceStacks(worldObj, xCoord, yCoord, zCoord, getRipeningTime()))); + ItemStack[] produceStacks = tree.produceStacks(worldObj, xCoord, yCoord, zCoord, getRipeningTime()); ripeningTime = 0; sendNetworkUpdateRipening(); - return picked; + return Arrays.asList(produceStacks); } @Override public IFruitFamily getFruitFamily() { - if (getTree() == null) { + ITree tree = getTree(); + if (tree == null) { return null; } - return getTree().getGenome().getFruitProvider().getFamily(); + return tree.getGenome().getFruitProvider().getFamily(); } @Override public float getRipeness() { - if (getTree() == null) { - return 0f; - } - int ripeningPeriod = getTree().getGenome().getFruitProvider().getRipeningPeriod(); if (ripeningPeriod == 0) { return 1.0f; } - return (float) ripeningTime / ripeningPeriod; + if (getTree() == null) { + return 0f; + } + return (float) getRipeningTime() / ripeningPeriod; } @Override public boolean hasFruit() { - return isFruitLeaf && !isDestroyed(); + return isFruitLeaf && !isDestroyed(getTree(), damage); } @Override public void addRipeness(float add) { - if (getTree() == null || isDecorative) { + if (getTree() == null || !isFruitLeaf || getRipeningTime() >= ripeningPeriod || isDecorative) { return; } - ripeningTime += getTree().getGenome().getFruitProvider().getRipeningPeriod() * add; + ripeningTime += ripeningPeriod * add; sendNetworkUpdateRipening(); } @@ -422,12 +466,14 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru private void matureCaterpillar() { maturationTime++; - boolean wasDestroyed = isDestroyed(); - encumbrance += caterpillar.getGenome().getMetabolism(); - wasDestroyed = !wasDestroyed && isDestroyed(); + ITree tree = getTree(); + boolean wasDestroyed = isDestroyed(tree, damage); + damage += caterpillar.getGenome().getMetabolism(); - if (maturationTime >= (float) caterpillar.getGenome().getLifespan() / (caterpillar.getGenome().getFertility() * 2) - && caterpillar.canTakeFlight(worldObj, xCoord, yCoord, zCoord)) { + IButterflyGenome caterpillarGenome = caterpillar.getGenome(); + int caterpillarMatureTime = Math.round((float) caterpillarGenome.getLifespan() / (caterpillarGenome.getFertility() * 2)); + + if (maturationTime >= caterpillarMatureTime && caterpillar.canTakeFlight(worldObj, xCoord, yCoord, zCoord)) { if (worldObj.isAirBlock(xCoord - 1, yCoord, zCoord)) { attemptButterflySpawn(worldObj, caterpillar, xCoord - 1, yCoord, zCoord); } else if (worldObj.isAirBlock(xCoord + 1, yCoord, zCoord)) { @@ -438,13 +484,14 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru attemptButterflySpawn(worldObj, caterpillar, xCoord, yCoord, zCoord + 1); } setCaterpillar(null); - } else if (wasDestroyed) { + } else if (!wasDestroyed && isDestroyed(tree, damage)) { sendNetworkUpdate(); } } private void attemptButterflySpawn(World world, IButterfly butterfly, double x, double y, double z) { - if (((IButterflyRoot) AlleleManager.alleleRegistry.getSpeciesRoot("rootButterflies")).spawnButterflyInWorld(world, butterfly.copy(), x, y + 0.1f, z) != null) { + IButterflyRoot butterflyRoot = (IButterflyRoot) AlleleManager.alleleRegistry.getSpeciesRoot("rootButterflies"); + if (butterflyRoot.spawnButterflyInWorld(world, butterfly.copy(), x, y + 0.1f, z) != null) { Proxies.log.finest("A caterpillar '%s' hatched at %s/%s/%s.", butterfly.getDisplayName(), x, y, z); } } @@ -534,7 +581,8 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru @Override public boolean canNurse(IButterfly butterfly) { - return !isDecorative && !isDestroyed() && caterpillar == null; + ITree tree = getTree(); + return !isDecorative && !isDestroyed(tree, damage) && caterpillar == null; } /* IHousing */ diff --git a/src/main/java/forestry/arboriculture/gadgets/TileTreeContainer.java b/src/main/java/forestry/arboriculture/gadgets/TileTreeContainer.java index 24db29ef4..1ba767008 100755 --- a/src/main/java/forestry/arboriculture/gadgets/TileTreeContainer.java +++ b/src/main/java/forestry/arboriculture/gadgets/TileTreeContainer.java @@ -73,9 +73,6 @@ public abstract class TileTreeContainer extends TileEntity implements INetworked /* CONTAINED TREE */ public void setTree(ITree tree) { this.containedTree = tree; - if (tree != null) { - sendNetworkUpdate(); - } } public ITree getTree() { diff --git a/src/main/java/forestry/arboriculture/genetics/Tree.java b/src/main/java/forestry/arboriculture/genetics/Tree.java index b6fbeed5a..7d55ed631 100755 --- a/src/main/java/forestry/arboriculture/genetics/Tree.java +++ b/src/main/java/forestry/arboriculture/genetics/Tree.java @@ -44,30 +44,31 @@ import forestry.api.genetics.IChromosome; import forestry.api.genetics.IEffectData; import forestry.api.genetics.IFruitFamily; import forestry.api.genetics.IGenome; -import forestry.api.world.ITreeGenData; +import forestry.arboriculture.gadgets.ForestryBlockLeaves; +import forestry.arboriculture.gadgets.TileLeaves; +import forestry.core.config.Defaults; +import forestry.core.config.ForestryBlock; import forestry.core.genetics.Allele; import forestry.core.genetics.Chromosome; import forestry.core.genetics.Individual; import forestry.core.utils.StringUtil; import forestry.plugins.PluginArboriculture; -public class Tree extends Individual implements ITree, ITreeGenData, IPlantable { +public class Tree extends Individual implements ITree, IPlantable { private ITreeGenome genome; private ITreeGenome mate; + private EnumSet plantTypes; + private EnumPlantType plantType; + /* CONSTRUCTOR */ public Tree(NBTTagCompound nbttagcompound) { readFromNBT(nbttagcompound); } public Tree(ITreeGenome genome) { - this.genome = genome; - } - - public Tree(World world, ITreeGenome genome) { - this.genome = genome; - + setGenome(genome); } /* SAVING & LOADING */ @@ -77,10 +78,11 @@ public class Tree extends Individual implements ITree, ITreeGenData, IPlantable super.readFromNBT(nbttagcompound); if (nbttagcompound.hasKey("Genome")) { - genome = new TreeGenome(nbttagcompound.getCompoundTag("Genome")); + setGenome(new TreeGenome(nbttagcompound.getCompoundTag("Genome"))); } else { - genome = PluginArboriculture.treeInterface.templateAsGenome(TreeTemplates.getOakTemplate()); + setGenome(PluginArboriculture.treeInterface.templateAsGenome(TreeTemplates.getOakTemplate())); } + if (nbttagcompound.hasKey("Mate")) { mate = new TreeGenome(nbttagcompound.getCompoundTag("Mate")); } @@ -170,8 +172,6 @@ public class Tree extends Individual implements ITree, ITreeGenData, IPlantable return false; } - private EnumPlantType plantType; - @Override public EnumPlantType getPlantType(IBlockAccess world, int x, int y, int z) { return plantType; @@ -220,7 +220,38 @@ public class Tree extends Individual implements ITree, ITreeGenData, IPlantable @Override public void setLeaves(World world, GameProfile owner, int x, int y, int z) { - PluginArboriculture.treeInterface.setLeaves(world, this, owner, x, y, z); + setLeaves(world, owner, x, y, z, false); + } + + @Override + public void setLeavesDecorative(World world, GameProfile owner, int x, int y, int z) { + setLeaves(world, owner, x, y, z, true); + } + + private void setLeaves(World world, GameProfile owner, int x, int y, int z, boolean decorative) { + boolean placed = ForestryBlock.leaves.setBlock(world, x, y, z, 0, Defaults.FLAG_BLOCK_SYNCH); + if (!placed) { + return; + } + + if (!ForestryBlock.leaves.isBlockEqual(world, x, y, z)) { + world.setBlockToAir(x, y, z); + return; + } + + TileLeaves tileLeaves = ForestryBlockLeaves.getLeafTile(world, x, y, z); + if (tileLeaves == null) { + world.setBlockToAir(x, y, z); + return; + } + + tileLeaves.setOwner(owner); + if (decorative) { + tileLeaves.setDecorative(); + } + tileLeaves.setTree(this.copy()); + + world.markBlockForUpdate(x, y, z); } @Override @@ -248,7 +279,13 @@ public class Tree extends Individual implements ITree, ITreeGenData, IPlantable /* INFORMATION */ @Override public ITreeGenome getGenome() { - return this.genome; + return genome; + } + + private void setGenome(ITreeGenome genome) { + this.genome = genome; + plantTypes = genome.getPlantTypes(); + plantTypes.add(genome.getPrimary().getPlantType()); } @Override @@ -270,9 +307,7 @@ public class Tree extends Individual implements ITree, ITreeGenData, IPlantable @Override public EnumSet getPlantTypes() { - EnumSet tolerated = genome.getPlantTypes(); - tolerated.add(genome.getPrimary().getPlantType()); - return tolerated; + return plantTypes; } @Override @@ -349,7 +384,7 @@ public class Tree extends Individual implements ITree, ITreeGenData, IPlantable } if (mutated != null) { - return new Tree(world, new TreeGenome(mutated)); + return new Tree(new TreeGenome(mutated)); } for (int i = 0; i < parent1.length; i++) { @@ -358,7 +393,7 @@ public class Tree extends Individual implements ITree, ITreeGenData, IPlantable } } - return new Tree(world, new TreeGenome(chromosomes)); + return new Tree(new TreeGenome(chromosomes)); } private IChromosome[] mutateSpecies(World world, int x, int y, int z, IGenome genomeOne, IGenome genomeTwo) { diff --git a/src/main/java/forestry/arboriculture/genetics/TreeHelper.java b/src/main/java/forestry/arboriculture/genetics/TreeHelper.java index 8cde054aa..336cf2268 100755 --- a/src/main/java/forestry/arboriculture/genetics/TreeHelper.java +++ b/src/main/java/forestry/arboriculture/genetics/TreeHelper.java @@ -217,29 +217,22 @@ public class TreeHelper extends SpeciesRoot implements ITreeRoot { @Override public boolean setLeaves(World world, IIndividual tree, GameProfile owner, int x, int y, int z, boolean decorative) { - boolean placed = ForestryBlock.leaves.setBlock(world, x, y, z, 0, Defaults.FLAG_BLOCK_SYNCH); - if (!placed) { - return false; + if (decorative) { + ((ITree) tree).setLeavesDecorative(world, owner, x, y, z); + } else { + ((ITree) tree).setLeaves(world, owner, x, y, z); } if (!ForestryBlock.leaves.isBlockEqual(world, x, y, z)) { return false; } - TileEntity tile = ForestryBlockLeaves.getLeafTile(world, x, y, z); - if (tile == null) { + TileLeaves tileLeaves = ForestryBlockLeaves.getLeafTile(world, x, y, z); + if (tileLeaves == null) { world.setBlockToAir(x, y, z); return false; } - TileLeaves tileLeaves = (TileLeaves) tile; - tileLeaves.setOwner(owner); - tileLeaves.setTree((ITree) tree.copy()); - if (decorative) { - tileLeaves.setDecorative(); - } - world.markBlockForUpdate(x, y, z); - return true; } diff --git a/src/main/java/forestry/arboriculture/items/ItemLeavesBlock.java b/src/main/java/forestry/arboriculture/items/ItemLeavesBlock.java index 99020e820..057cbf327 100644 --- a/src/main/java/forestry/arboriculture/items/ItemLeavesBlock.java +++ b/src/main/java/forestry/arboriculture/items/ItemLeavesBlock.java @@ -5,11 +5,9 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.world.World; -import com.mojang.authlib.GameProfile; - -import forestry.api.arboriculture.ITree; -import forestry.arboriculture.genetics.Tree; +import forestry.arboriculture.gadgets.TileLeaves; import forestry.core.items.ItemForestryBlock; +import forestry.core.proxy.Proxies; import forestry.core.utils.StringUtil; import forestry.plugins.PluginArboriculture; @@ -25,37 +23,46 @@ public class ItemLeavesBlock extends ItemForestryBlock { if (!itemstack.hasTagCompound()) { return type; } - ITree tree = getTree(itemstack); - String customTreeKey = "trees.custom.leaves." + tree.getGenome().getPrimary().getUnlocalizedName().replace("trees.species.", ""); + + TileLeaves tileLeaves = new TileLeaves(); + tileLeaves.readFromNBT(itemstack.getTagCompound()); + + String unlocalizedName = tileLeaves.getUnlocalizedName(); + + String customTreeKey = "trees.custom.leaves." + unlocalizedName.replace("trees.species.", ""); if (StringUtil.canTranslate(customTreeKey)) { return StringUtil.localize(customTreeKey); } - String grammar = StringUtil.localize("trees.grammar.leaves"); - return grammar.replaceAll("%SPECIES", tree.getDisplayName()).replaceAll("%TYPE", type); + String grammar = StringUtil.localize("trees.grammar.leaves"); + String localizedName = StringUtil.localize(unlocalizedName); + + return grammar.replaceAll("%SPECIES", localizedName).replaceAll("%TYPE", type); } @Override - public int getColorFromItemStack(ItemStack itemstack, int renderPass) { - if (!itemstack.hasTagCompound()) { + public int getColorFromItemStack(ItemStack itemStack, int renderPass) { + if (!itemStack.hasTagCompound()) { return PluginArboriculture.proxy.getFoliageColorBasic(); } - ITree tree = getTree(itemstack); - return tree.getGenome().getPrimary().getLeafColour(tree); + + TileLeaves tileLeaves = new TileLeaves(); + tileLeaves.readFromNBT(itemStack.getTagCompound()); + + return tileLeaves.getFoliageColour(Proxies.common.getPlayer()); } @Override - public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int metadata) { - if (!stack.hasTagCompound()) { + public boolean placeBlockAt(ItemStack itemStack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int metadata) { + if (!itemStack.hasTagCompound()) { return false; } - ITree tree = getTree(stack); - GameProfile owner = player.getGameProfile(); - return PluginArboriculture.treeInterface.setLeaves(world, tree, owner, x, y, z, true); - } - private ITree getTree(ItemStack itemStack) { - return new Tree(itemStack.getTagCompound()); + TileLeaves tileLeaves = new TileLeaves(); + tileLeaves.readFromNBT(itemStack.getTagCompound()); + tileLeaves.getTree().setLeavesDecorative(world, player.getGameProfile(), x, y, z); + + return true; } } diff --git a/src/main/java/forestry/arboriculture/network/PacketLeafUpdate.java b/src/main/java/forestry/arboriculture/network/PacketLeafUpdate.java index 8ace878ec..363e0cf80 100755 --- a/src/main/java/forestry/arboriculture/network/PacketLeafUpdate.java +++ b/src/main/java/forestry/arboriculture/network/PacketLeafUpdate.java @@ -14,28 +14,35 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; + +import forestry.api.arboriculture.ITree; import forestry.arboriculture.gadgets.TileLeaves; +import forestry.core.network.ForestryPacket; +import forestry.core.network.ILocatedPacket; import forestry.core.network.PacketIds; -import forestry.core.network.PacketTileNBT; -public class PacketLeafUpdate extends PacketTileNBT { +public class PacketLeafUpdate extends ForestryPacket implements ILocatedPacket { - private static short hasFruitFlag = 1; - private static short isPollinatedFlag = 1 << 1; + private static final short hasFruitFlag = 1; + private static final short isPollinatedFlag = 1 << 1; + + private int posX, posY, posZ; private byte leafState = 0; - public short textureIndexFancy; - public short textureIndexPlain; - public short textureIndexFruit; - - public int colourLeaves; - public int colourFruits; + private int colourFruits = -1; + private String speciesUID = ""; public PacketLeafUpdate() { } public PacketLeafUpdate(TileLeaves leaves) { - super(PacketIds.LEAF_UPDATE, leaves); + super(PacketIds.LEAF_UPDATE); + + posX = leaves.getXCoord(); + posY = leaves.getYCoord(); + posZ = leaves.getZCoord(); leafState = 0; if (leaves.hasFruit()) { @@ -44,34 +51,34 @@ public class PacketLeafUpdate extends PacketTileNBT { if (leaves.isPollinated()) { leafState |= isPollinatedFlag; } - textureIndexFancy = leaves.determineTextureIndex(true); - textureIndexPlain = leaves.determineTextureIndex(false); - textureIndexFruit = leaves.determineOverlayIndex(); - colourLeaves = leaves.determineFoliageColour(); - colourFruits = leaves.determineFruitColour(); + + if (leaves.hasFruit()) { + colourFruits = leaves.getFruitColour(); + } + + ITree tree = leaves.getTree(); + if (tree != null) { + speciesUID = tree.getIdent(); + } } @Override public void writeData(DataOutputStream data) throws IOException { - super.writeData(data); - + data.writeShort(posX); + data.writeShort(posY); + data.writeShort(posZ); data.writeByte(leafState); - data.writeShort(textureIndexFancy); - data.writeShort(textureIndexPlain); - data.writeShort(textureIndexFruit); - data.writeInt(colourLeaves); + data.writeUTF(speciesUID); data.writeInt(colourFruits); } @Override public void readData(DataInputStream data) throws IOException { - super.readData(data); - + posX = data.readShort(); + posY = data.readShort(); + posZ = data.readShort(); leafState = data.readByte(); - textureIndexFancy = data.readShort(); - textureIndexPlain = data.readShort(); - textureIndexFruit = data.readShort(); - colourLeaves = data.readInt(); + speciesUID = data.readUTF(); colourFruits = data.readInt(); } @@ -82,4 +89,17 @@ public class PacketLeafUpdate extends PacketTileNBT { public boolean isPollinated() { return (leafState & isPollinatedFlag) > 0; } + + public int getColourFruits() { + return colourFruits; + } + + public String getSpeciesUID() { + return speciesUID; + } + + @Override + public TileEntity getTarget(World world) { + return world.getTileEntity(posX, posY, posZ); + } } diff --git a/src/main/java/forestry/arboriculture/network/PacketRipeningUpdate.java b/src/main/java/forestry/arboriculture/network/PacketRipeningUpdate.java index 5f5db8d32..04c28b822 100644 --- a/src/main/java/forestry/arboriculture/network/PacketRipeningUpdate.java +++ b/src/main/java/forestry/arboriculture/network/PacketRipeningUpdate.java @@ -23,18 +23,18 @@ import forestry.core.network.PacketIds; public class PacketRipeningUpdate extends ForestryPacket { - public int colourFruits; + private int colourFruits; private int posX, posY, posZ; public PacketRipeningUpdate() { } - public PacketRipeningUpdate(TileLeaves leaves, int fruitColour) { + public PacketRipeningUpdate(TileLeaves leaves) { super(PacketIds.RIPENING_UPDATE); posX = leaves.xCoord; posY = leaves.yCoord; posZ = leaves.zCoord; - colourFruits = fruitColour; + colourFruits = leaves.getFruitColour(); } @Override @@ -57,6 +57,10 @@ public class PacketRipeningUpdate extends ForestryPacket { colourFruits = data.readInt(); } + public int getColourFruits() { + return colourFruits; + } + public TileEntity getTarget(World world) { return world.getTileEntity(posX, posY, posZ); } diff --git a/src/main/java/forestry/arboriculture/render/LeavesRenderingHandler.java b/src/main/java/forestry/arboriculture/render/LeavesRenderingHandler.java index 9d7a29628..5d513bac3 100755 --- a/src/main/java/forestry/arboriculture/render/LeavesRenderingHandler.java +++ b/src/main/java/forestry/arboriculture/render/LeavesRenderingHandler.java @@ -22,10 +22,8 @@ import net.minecraftforge.client.IItemRenderer; import org.lwjgl.opengl.GL11; -import forestry.api.arboriculture.ITree; import forestry.arboriculture.gadgets.ForestryBlockLeaves; import forestry.arboriculture.gadgets.TileLeaves; -import forestry.arboriculture.genetics.Tree; import forestry.arboriculture.items.ItemLeavesBlock; import forestry.core.proxy.Proxies; import forestry.core.render.OverlayRenderingHandler; @@ -46,7 +44,7 @@ public class LeavesRenderingHandler extends OverlayRenderingHandler implements I public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId, RenderBlocks renderer) { TileLeaves tile = ForestryBlockLeaves.getLeafTile(world, x, y, z); - if (tile == null || tile.getTree() == null) { + if (tile == null) { return false; } @@ -55,9 +53,9 @@ public class LeavesRenderingHandler extends OverlayRenderingHandler implements I // Render overlay for fruit leaves. IIcon fruitIcon = tile.getFruitTexture(); - int fruitColor = tile.getFruitColour(); if (fruitIcon != null) { + int fruitColor = tile.getFruitColour(); renderFruitOverlay(world, block, x, y, z, renderer, fruitIcon, fruitColor); } @@ -132,24 +130,20 @@ public class LeavesRenderingHandler extends OverlayRenderingHandler implements I public void renderItem(ItemRenderType type, ItemStack item, Object... data) { switch (type) { case ENTITY: - renderLeafBlock((RenderBlocks) data[0], item, 0f, 0f, 0f); + renderItem((RenderBlocks) data[0], item, 0f, 0f, 0f); break; case EQUIPPED: case EQUIPPED_FIRST_PERSON: - renderLeafBlock((RenderBlocks) data[0], item, 0.5f, 0.5f, 0.5f); + renderItem((RenderBlocks) data[0], item, 0.5f, 0.5f, 0.5f); break; case INVENTORY: - renderLeafBlock((RenderBlocks) data[0], item, 0f, 0f, 0f); + renderItem((RenderBlocks) data[0], item, 0f, 0f, 0f); break; default: } } - private ITree getTree(ItemStack itemStack) { - return new Tree(itemStack.getTagCompound()); - } - - private void renderLeafBlock(RenderBlocks renderer, ItemStack itemStack, float x, float y, float z) { + private void renderItem(RenderBlocks renderer, ItemStack itemStack, float x, float y, float z) { Tessellator tessellator = Tessellator.instance; Block block = StackUtils.getBlock(itemStack); @@ -157,22 +151,16 @@ public class LeavesRenderingHandler extends OverlayRenderingHandler implements I return; } - ITree tree = getTree(itemStack); - if (tree == null) { - return; - } + TileLeaves leaves = new TileLeaves(); + leaves.readFromNBT(itemStack.getTagCompound()); GL11.glEnable(GL11.GL_BLEND); - TileLeaves leaves = new TileLeaves(); - leaves.setTree(tree); - leaves.setDecorative(); - IIcon leavesIcon = leaves.getIcon(Proxies.render.fancyGraphicsEnabled()); if (leavesIcon == null) { return; } - int color = leaves.determineFoliageColour(); + int color = leaves.getFoliageColour(Proxies.common.getPlayer()); float r1 = (float) (color >> 16 & 255) / 255.0F; float g1 = (float) (color >> 8 & 255) / 255.0F; @@ -218,11 +206,11 @@ public class LeavesRenderingHandler extends OverlayRenderingHandler implements I return; } - int fruitColor = leaves.getFruitColour(); IIcon fruitTexture = leaves.getFruitTexture(); if (fruitTexture == null) { return; } + int fruitColor = leaves.getFruitColour(); float r2 = (float) (fruitColor >> 16 & 255) / 255.0F; float g2 = (float) (fruitColor >> 8 & 255) / 255.0F; diff --git a/src/main/java/forestry/core/gui/GuiForestry.java b/src/main/java/forestry/core/gui/GuiForestry.java index 194a26f23..7cab9a082 100755 --- a/src/main/java/forestry/core/gui/GuiForestry.java +++ b/src/main/java/forestry/core/gui/GuiForestry.java @@ -14,7 +14,6 @@ import java.awt.Color; import java.util.Collection; import java.util.List; -import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.renderer.OpenGlHelper; @@ -456,7 +455,7 @@ public abstract class GuiForestry ext @Override public ToolTip getToolTip() { - EntityPlayer player = Minecraft.getMinecraft().thePlayer; + EntityPlayer player = Proxies.common.getPlayer(); ToolTip tip = new ToolTip(); tip.add(itemStack.getTooltip(player, false)); return tip; diff --git a/src/main/java/forestry/core/utils/GeneticsUtil.java b/src/main/java/forestry/core/utils/GeneticsUtil.java index 1560ec027..fe746c009 100644 --- a/src/main/java/forestry/core/utils/GeneticsUtil.java +++ b/src/main/java/forestry/core/utils/GeneticsUtil.java @@ -61,9 +61,22 @@ public class GeneticsUtil { } public static boolean hasNaturalistEye(EntityPlayer player) { - ItemStack armorItem = player.inventory.armorInventory[3]; - return armorItem != null && armorItem.getItem() instanceof IArmorNaturalist - && ((IArmorNaturalist) armorItem.getItem()).canSeePollination(player, armorItem, true); + if (player == null) { + return false; + } + + ItemStack armorItemStack = player.inventory.armorInventory[3]; + if (armorItemStack == null) { + return false; + } + + Item armorItem = armorItemStack.getItem(); + if (!(armorItem instanceof IArmorNaturalist)) { + return false; + } + + IArmorNaturalist armorNaturalist = (IArmorNaturalist) armorItem; + return armorNaturalist.canSeePollination(player, armorItemStack, true); } public static boolean canNurse(IButterfly butterfly, World world, final int x, final int y, final int z) { @@ -105,7 +118,7 @@ public class GeneticsUtil { ITree pollen = getErsatzPollen(world, x, y, z); if (pollen != null) { - PluginArboriculture.treeInterface.setLeaves(world, pollen, owner, x, y, z); + pollen.setLeaves(world, owner, x, y, z); return (IPollinatable) world.getTileEntity(x, y, z); }