diff --git a/src/main/java/forestry/api/genetics/AlleleManager.java b/src/main/java/forestry/api/genetics/AlleleManager.java index ad3c69005..1700b72cd 100644 --- a/src/main/java/forestry/api/genetics/AlleleManager.java +++ b/src/main/java/forestry/api/genetics/AlleleManager.java @@ -6,7 +6,9 @@ package forestry.api.genetics; import java.util.HashMap; +import java.util.Map; +import net.minecraft.block.Block; import net.minecraft.item.ItemStack; /** @@ -17,18 +19,22 @@ public class AlleleManager { * Main access point for all things related to genetics. See {@link IAlleleRegistry} for details. */ public static IAlleleRegistry alleleRegistry; + /** * Translates plain leaf blocks into genetic data. Used by bees and butterflies to convert and pollinate foreign leaf blocks. */ - public static HashMap ersatzSpecimen = new HashMap<>(); + public static Map leafTranslators = new HashMap<>(); + /** * Translates plain saplings into genetic data. Used by the treealyzer and the farm to convert foreign saplings. */ - public static HashMap ersatzSaplings = new HashMap<>(); + public static Map saplingTranslation = new HashMap<>(); + /** * Queryable instance of an {@link IClimateHelper} for easier implementation. */ public static IClimateHelper climateHelper; + /** * Creates Forestry alleles. */ diff --git a/src/main/java/forestry/api/genetics/ILeafTranslator.java b/src/main/java/forestry/api/genetics/ILeafTranslator.java new file mode 100644 index 000000000..f91d5e1a1 --- /dev/null +++ b/src/main/java/forestry/api/genetics/ILeafTranslator.java @@ -0,0 +1,17 @@ +package forestry.api.genetics; + +import javax.annotation.Nullable; + +import net.minecraft.block.state.IBlockState; + +import forestry.api.arboriculture.ITree; + +/** + * Translates blockStates for a single plain leaf block into genetic data. + * Used by bees and butterflies to convert and pollinate foreign leaf blocks. + * Kept in a map with Block keys: {@link AlleleManager#leafTranslators} + */ +public interface ILeafTranslator { + @Nullable + ITree getTreeFromLeaf(IBlockState leafBlockState); +} diff --git a/src/main/java/forestry/apiculture/genetics/Bee.java b/src/main/java/forestry/apiculture/genetics/Bee.java index e9186ce5c..c1d6d57eb 100644 --- a/src/main/java/forestry/apiculture/genetics/Bee.java +++ b/src/main/java/forestry/apiculture/genetics/Bee.java @@ -692,13 +692,13 @@ public class Bee extends IndividualLiving implements IBee { BlockPos blockPos = VectUtil.add(housingPos, randomPos, offset); TileEntity tile = world.getTileEntity(blockPos); - if (tile instanceof IPollinatable) { - IPollinatable pitcher = (IPollinatable) tile; + if (tile instanceof ICheckPollinatable) { + ICheckPollinatable pitcher = (ICheckPollinatable) tile; if (genome.getFlowerProvider().isAcceptedPollinatable(world, pitcher)) { pollen = pitcher.getPollen(); } } else { - pollen = GeneticsUtil.getErsatzPollen(world, blockPos); + pollen = GeneticsUtil.getPollen(world, blockPos); } if (pollen != null) { diff --git a/src/main/java/forestry/arboriculture/PluginArboriculture.java b/src/main/java/forestry/arboriculture/PluginArboriculture.java index 2d43bc8ef..a07e4f32d 100644 --- a/src/main/java/forestry/arboriculture/PluginArboriculture.java +++ b/src/main/java/forestry/arboriculture/PluginArboriculture.java @@ -16,6 +16,9 @@ import java.util.List; import java.util.Set; import net.minecraft.block.Block; +import net.minecraft.block.BlockNewLeaf; +import net.minecraft.block.BlockOldLeaf; +import net.minecraft.block.state.IBlockState; import net.minecraft.entity.passive.EntityVillager; import net.minecraft.init.Blocks; import net.minecraft.init.Items; @@ -37,9 +40,11 @@ import net.minecraftforge.oredict.OreDictionary; import forestry.api.arboriculture.EnumGermlingType; import forestry.api.arboriculture.EnumWoodType; import forestry.api.arboriculture.IAlleleFruit; +import forestry.api.arboriculture.ITree; import forestry.api.arboriculture.TreeManager; import forestry.api.core.ForestryAPI; import forestry.api.genetics.AlleleManager; +import forestry.api.genetics.ILeafTranslator; import forestry.api.recipes.RecipeManagers; import forestry.api.storage.ICrateRegistry; import forestry.api.storage.StorageManager; @@ -376,19 +381,47 @@ public class PluginArboriculture extends BlankForestryPlugin { } private static void registerErsatzGenomes() { - AlleleManager.ersatzSpecimen.put(new ItemStack(Blocks.LEAVES, 1, 0), TreeDefinition.Oak.getIndividual()); - AlleleManager.ersatzSpecimen.put(new ItemStack(Blocks.LEAVES, 1, 1), TreeDefinition.Spruce.getIndividual()); - AlleleManager.ersatzSpecimen.put(new ItemStack(Blocks.LEAVES, 1, 2), TreeDefinition.Birch.getIndividual()); - AlleleManager.ersatzSpecimen.put(new ItemStack(Blocks.LEAVES, 1, 3), TreeDefinition.Jungle.getIndividual()); - AlleleManager.ersatzSpecimen.put(new ItemStack(Blocks.LEAVES2, 1, 0), TreeDefinition.AcaciaVanilla.getIndividual()); - AlleleManager.ersatzSpecimen.put(new ItemStack(Blocks.LEAVES2, 1, 1), TreeDefinition.DarkOak.getIndividual()); + AlleleManager.leafTranslators.put(Blocks.LEAVES, new ILeafTranslator() { + @Override + public ITree getTreeFromLeaf(IBlockState leafBlockState) { + if (!leafBlockState.getValue(BlockOldLeaf.DECAYABLE)) { + return null; + } + switch (leafBlockState.getValue(BlockOldLeaf.VARIANT)) { + case OAK: + return TreeDefinition.Oak.getIndividual(); + case SPRUCE: + return TreeDefinition.Spruce.getIndividual(); + case BIRCH: + return TreeDefinition.Birch.getIndividual(); + case JUNGLE: + return TreeDefinition.Jungle.getIndividual(); + } + return null; + } + }); + AlleleManager.leafTranslators.put(Blocks.LEAVES2, new ILeafTranslator() { + @Override + public ITree getTreeFromLeaf(IBlockState leafBlockState) { + if (!leafBlockState.getValue(BlockNewLeaf.DECAYABLE)) { + return null; + } + switch (leafBlockState.getValue(BlockNewLeaf.VARIANT)) { + case ACACIA: + return TreeDefinition.AcaciaVanilla.getIndividual(); + case DARK_OAK: + return TreeDefinition.DarkOak.getIndividual(); + } + return null; + } + }); - AlleleManager.ersatzSaplings.put(new ItemStack(Blocks.SAPLING, 1, 0), TreeDefinition.Oak.getIndividual()); - AlleleManager.ersatzSaplings.put(new ItemStack(Blocks.SAPLING, 1, 1), TreeDefinition.Spruce.getIndividual()); - AlleleManager.ersatzSaplings.put(new ItemStack(Blocks.SAPLING, 1, 2), TreeDefinition.Birch.getIndividual()); - AlleleManager.ersatzSaplings.put(new ItemStack(Blocks.SAPLING, 1, 3), TreeDefinition.Jungle.getIndividual()); - AlleleManager.ersatzSaplings.put(new ItemStack(Blocks.SAPLING, 1, 4), TreeDefinition.AcaciaVanilla.getIndividual()); - AlleleManager.ersatzSaplings.put(new ItemStack(Blocks.SAPLING, 1, 5), TreeDefinition.DarkOak.getIndividual()); + AlleleManager.saplingTranslation.put(new ItemStack(Blocks.SAPLING, 1, 0), TreeDefinition.Oak.getIndividual()); + AlleleManager.saplingTranslation.put(new ItemStack(Blocks.SAPLING, 1, 1), TreeDefinition.Spruce.getIndividual()); + AlleleManager.saplingTranslation.put(new ItemStack(Blocks.SAPLING, 1, 2), TreeDefinition.Birch.getIndividual()); + AlleleManager.saplingTranslation.put(new ItemStack(Blocks.SAPLING, 1, 3), TreeDefinition.Jungle.getIndividual()); + AlleleManager.saplingTranslation.put(new ItemStack(Blocks.SAPLING, 1, 4), TreeDefinition.AcaciaVanilla.getIndividual()); + AlleleManager.saplingTranslation.put(new ItemStack(Blocks.SAPLING, 1, 5), TreeDefinition.DarkOak.getIndividual()); } @Override diff --git a/src/main/java/forestry/arboriculture/items/ItemGermlingGE.java b/src/main/java/forestry/arboriculture/items/ItemGermlingGE.java index e304ab45e..32565f6d8 100755 --- a/src/main/java/forestry/arboriculture/items/ItemGermlingGE.java +++ b/src/main/java/forestry/arboriculture/items/ItemGermlingGE.java @@ -10,6 +10,7 @@ ******************************************************************************/ package forestry.arboriculture.items; +import javax.annotation.Nonnull; import java.util.List; import net.minecraft.block.state.IBlockState; @@ -17,13 +18,13 @@ import net.minecraft.client.renderer.ItemMeshDefinition; import net.minecraft.client.renderer.block.model.ModelResourceLocation; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Blocks; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.util.ActionResult; import net.minecraft.util.EnumActionResult; -import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.RayTraceResult; import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; @@ -144,67 +145,74 @@ public class ItemGermlingGE extends ItemGE implements IVariableFermentable, ICol } @Override - public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { - ITree tree = TreeManager.treeRoot.getMember(stack); - if (tree == null) { - return EnumActionResult.PASS; + public ActionResult onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand) { + RayTraceResult raytraceresult = this.rayTrace(worldIn, playerIn, true); + + if (raytraceresult != null && raytraceresult.typeOfHit == RayTraceResult.Type.BLOCK) { + BlockPos pos = raytraceresult.getBlockPos(); + + ITree tree = TreeManager.treeRoot.getMember(itemStackIn); + if (tree != null) { + if (type == EnumGermlingType.SAPLING) { + return onItemRightClickSapling(itemStackIn, worldIn, playerIn, pos, tree); + } else if (type == EnumGermlingType.POLLEN) { + return onItemRightClickPollen(itemStackIn, worldIn, playerIn, pos, tree); + } + } + } + return new ActionResult<>(EnumActionResult.PASS, itemStackIn); + } + + @Nonnull + private static ActionResult onItemRightClickPollen(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, BlockPos pos, ITree tree) { + ICheckPollinatable checkPollinatable = GeneticsUtil.getCheckPollinatable(worldIn, pos); + if (checkPollinatable == null || !checkPollinatable.canMateWith(tree)) { + return new ActionResult<>(EnumActionResult.FAIL, itemStackIn); } - if (type == EnumGermlingType.SAPLING) { - // x, y, z are the coordinates of the block "hit", can thus either be the soil or tall grass, etc. - IBlockState hitBlock = worldIn.getBlockState(pos); - if (!hitBlock.getBlock().isReplaceable(worldIn, pos)) { - if (!worldIn.isAirBlock(pos.up())) { - return EnumActionResult.FAIL; - } - pos = pos.up(); - } - - if (!tree.canStay(worldIn, pos)) { - return EnumActionResult.FAIL; - } - - if (TreeManager.treeRoot.plantSapling(worldIn, tree, playerIn.getGameProfile(), pos)) { - if (!playerIn.capabilities.isCreativeMode) { - stack.stackSize--; - } - return EnumActionResult.SUCCESS; - } else { - return EnumActionResult.FAIL; - } - } else if (type == EnumGermlingType.POLLEN) { - - ICheckPollinatable checkPollinatable = GeneticsUtil.getCheckPollinatable(worldIn, pos); - - if (checkPollinatable == null) { - return EnumActionResult.PASS; - } - - if (!checkPollinatable.canMateWith(tree)) { - return EnumActionResult.FAIL; - } - - IPollinatable pollinatable = GeneticsUtil.getOrCreatePollinatable(playerIn.getGameProfile(), worldIn, pos); - - if (!pollinatable.canMateWith(tree)) { - return EnumActionResult.FAIL; - } - + IPollinatable pollinatable = GeneticsUtil.getOrCreatePollinatable(playerIn.getGameProfile(), worldIn, pos); + if (pollinatable == null || !pollinatable.canMateWith(tree)) { + return new ActionResult<>(EnumActionResult.FAIL, itemStackIn); + } + + if (worldIn.isRemote) { + return new ActionResult<>(EnumActionResult.SUCCESS, itemStackIn); + } else { pollinatable.mateWith(tree); - PacketFXSignal packet = new PacketFXSignal(PacketFXSignal.VisualFXType.BLOCK_BREAK, PacketFXSignal.SoundFXType.BLOCK_BREAK, pos, Blocks.LEAVES.getDefaultState()); + IBlockState blockState = worldIn.getBlockState(pos); + PacketFXSignal packet = new PacketFXSignal(PacketFXSignal.VisualFXType.BLOCK_BREAK, PacketFXSignal.SoundFXType.BLOCK_BREAK, pos, blockState); Proxies.net.sendNetworkPacket(packet, worldIn); if (!playerIn.capabilities.isCreativeMode) { - stack.stackSize--; + itemStackIn.stackSize--; } - return EnumActionResult.SUCCESS; - - } else { - return EnumActionResult.PASS; + return new ActionResult<>(EnumActionResult.SUCCESS, itemStackIn); } } + @Nonnull + private static ActionResult onItemRightClickSapling(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, BlockPos pos, ITree tree) { + // x, y, z are the coordinates of the block "hit", can thus either be the soil or tall grass, etc. + IBlockState hitBlock = worldIn.getBlockState(pos); + if (!hitBlock.getBlock().isReplaceable(worldIn, pos)) { + if (!worldIn.isAirBlock(pos.up())) { + return new ActionResult<>(EnumActionResult.FAIL, itemStackIn); + } + pos = pos.up(); + } + + if (tree.canStay(worldIn, pos)) { + if (TreeManager.treeRoot.plantSapling(worldIn, tree, playerIn.getGameProfile(), pos)) { + if (!playerIn.capabilities.isCreativeMode) { + itemStackIn.stackSize--; + } + return new ActionResult<>(EnumActionResult.SUCCESS, itemStackIn); + } + } + return new ActionResult<>(EnumActionResult.FAIL, itemStackIn); + } + @Override public float getFermentationModifier(ItemStack itemstack) { ITree tree = TreeManager.treeRoot.getMember(itemstack); diff --git a/src/main/java/forestry/arboriculture/tiles/TileLeaves.java b/src/main/java/forestry/arboriculture/tiles/TileLeaves.java index 39f22cca0..249e3e89f 100644 --- a/src/main/java/forestry/arboriculture/tiles/TileLeaves.java +++ b/src/main/java/forestry/arboriculture/tiles/TileLeaves.java @@ -291,7 +291,9 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru } tree.mate(individual); - worldObj.markBlockRangeForRenderUpdate(getPos(), getPos()); + if (!worldObj.isRemote) { + sendNetworkUpdate(); + } } @Override diff --git a/src/main/java/forestry/core/utils/GeneticsUtil.java b/src/main/java/forestry/core/utils/GeneticsUtil.java index 89a8e6b43..6c63bbfb2 100644 --- a/src/main/java/forestry/core/utils/GeneticsUtil.java +++ b/src/main/java/forestry/core/utils/GeneticsUtil.java @@ -10,6 +10,7 @@ ******************************************************************************/ package forestry.core.utils; +import javax.annotation.Nullable; import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -17,7 +18,6 @@ import java.util.Set; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Blocks; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; @@ -35,6 +35,7 @@ import forestry.api.genetics.IAlleleSpecies; import forestry.api.genetics.ICheckPollinatable; import forestry.api.genetics.IChromosomeType; import forestry.api.genetics.IIndividual; +import forestry.api.genetics.ILeafTranslator; import forestry.api.genetics.IMutation; import forestry.api.genetics.IPollinatable; import forestry.api.lepidopterology.IButterfly; @@ -45,25 +46,6 @@ import forestry.core.genetics.ItemGE; public class GeneticsUtil { - private static Set ersatzSpecimenBlocks; - - private static Set getErsatzBlocks() { - if (ersatzSpecimenBlocks == null) { - ersatzSpecimenBlocks = new HashSet<>(); - for (ItemStack ersatzSpecimen : AlleleManager.ersatzSpecimen.keySet()) { - Block ersatzBlock = ItemStackUtil.getBlock(ersatzSpecimen); - if (ersatzBlock != null) { - ersatzSpecimenBlocks.add(ersatzBlock); - } - } - } - return ersatzSpecimenBlocks; - } - - private static boolean isErsatzBlock(Block block) { - return block != null && getErsatzBlocks().contains(block); - } - public static boolean hasNaturalistEye(EntityPlayer player) { if (player == null) { return false; @@ -91,7 +73,7 @@ public class GeneticsUtil { } // vanilla leaves can always be converted and then nurse - return getErsatzPollen(world, pos) != null; + return getPollen(world, pos) != null; } /** @@ -105,7 +87,7 @@ public class GeneticsUtil { return (IPollinatable) tile; } - ITree pollen = getErsatzPollen(world, pos); + ITree pollen = getPollen(world, pos); if (pollen != null) { return new CheckPollinatableTree(pollen); } @@ -124,7 +106,7 @@ public class GeneticsUtil { } if (Config.pollinateVanillaTrees) { - ITree pollen = getErsatzPollen(world, pos); + ITree pollen = getPollen(world, pos); if (pollen != null) { pollen.setLeaves(world, owner, pos); return (IPollinatable) world.getTileEntity(pos); @@ -134,33 +116,29 @@ public class GeneticsUtil { return null; } - public static ITree getErsatzPollen(World world, final BlockPos pos) { + /** + * Gets pollen from a location. Does not affect the pollen source. + */ + @Nullable + public static ITree getPollen(World world, final BlockPos pos) { + TileEntity tile = world.getTileEntity(pos); + + if (tile instanceof ICheckPollinatable) { + return ((ICheckPollinatable) tile).getPollen(); + } + IBlockState blockState = world.getBlockState(pos); + if (blockState == null) { + return null; + } Block block = blockState.getBlock(); - if (!isErsatzBlock(block)) { + + ILeafTranslator leafTranslator = AlleleManager.leafTranslators.get(block); + if (leafTranslator == null) { return null; } - int meta = block.getMetaFromState(blockState); - - if (Blocks.LEAVES == block || Blocks.LEAVES2 == block) { - if ((meta & 4) != 0) { - // no-decay vanilla leaves. http://minecraft.gamepedia.com/Data_values#Leaves - // Treat them as decorative and don't pollinate. - return null; - } - if (block == Blocks.LEAVES2) { - meta = meta + 4; //Dark Oak and Acacia are their own leaf block, but added on the end of sapling - } - block = Blocks.SAPLING; - } - ItemStack itemStack = new ItemStack(block, 1, meta); - IIndividual tree = getGeneticEquivalent(itemStack); - if (tree instanceof ITree) { - return (ITree) tree; - } - - return null; + return leafTranslator.getTreeFromLeaf(blockState); } public static IIndividual getGeneticEquivalent(ItemStack itemStack) { @@ -172,7 +150,7 @@ public class GeneticsUtil { return ((ItemGE) item).getIndividual(itemStack); } - for (Map.Entry entry : AlleleManager.ersatzSaplings.entrySet()) { + for (Map.Entry entry : AlleleManager.saplingTranslation.entrySet()) { if (ItemStackUtil.isIdenticalItem(itemStack, entry.getKey())) { return entry.getValue().copy(); } diff --git a/src/main/java/forestry/lepidopterology/items/ItemButterflyGE.java b/src/main/java/forestry/lepidopterology/items/ItemButterflyGE.java index 54e72bdc8..52cd89c12 100755 --- a/src/main/java/forestry/lepidopterology/items/ItemButterflyGE.java +++ b/src/main/java/forestry/lepidopterology/items/ItemButterflyGE.java @@ -235,7 +235,7 @@ public class ItemButterflyGE extends ItemGE implements ISpriteRegister, IColored if(tileEntity instanceof IButterflyNursery){ nursery = (IButterflyNursery) tileEntity; }else{ - ITree treeLeave = GeneticsUtil.getErsatzPollen(worldIn, pos); + ITree treeLeave = GeneticsUtil.getPollen(worldIn, pos); if(treeLeave != null){ treeLeave.setLeaves(worldIn, playerIn.getGameProfile(), pos);