diff --git a/src/main/java/forestry/apiculture/worldgen/HiveGen.java b/src/main/java/forestry/apiculture/worldgen/HiveGen.java index 112fbf3e8..447f55c68 100644 --- a/src/main/java/forestry/apiculture/worldgen/HiveGen.java +++ b/src/main/java/forestry/apiculture/worldgen/HiveGen.java @@ -22,10 +22,6 @@ public abstract class HiveGen implements IHiveGen { @Override public boolean canReplace(World world, BlockPos pos) { - Block block = BlockUtil.getBlock(world, pos); - Material material = block.getMaterial(); - return block.isReplaceable(world, pos) && !material.isLiquid() || - block.isAir(world, pos) || - material == Material.plants; + return BlockUtil.canReplace(world, pos); } } diff --git a/src/main/java/forestry/apiculture/worldgen/HiveGenGround.java b/src/main/java/forestry/apiculture/worldgen/HiveGenGround.java index 2438c0a6d..78686fd23 100644 --- a/src/main/java/forestry/apiculture/worldgen/HiveGenGround.java +++ b/src/main/java/forestry/apiculture/worldgen/HiveGenGround.java @@ -38,13 +38,13 @@ public class HiveGenGround extends HiveGen { @Override public int getYForHive(World world, int x, int z) { - int y = world.getHeight(new BlockPos(x, 0, z)).getY(); // get to the ground - while (y >= 0 && (world.getBlockState(new BlockPos(x, y - 1, z)).getBlock().isLeaves(world, new BlockPos(x, y - 1, z)) || canReplace(world, new BlockPos(x, y - 1, z)))) { - y--; + BlockPos pos = world.getHeight(new BlockPos(x, 0, z)); + while (pos.getY() >= 0 && (world.getBlockState(pos).getBlock().isLeaves(world, pos) || canReplace(world, pos))) { + pos = pos.add(0, -1, 0); } - return y; + return pos.getY(); } } diff --git a/src/main/java/forestry/arboriculture/commands/TreeGenHelper.java b/src/main/java/forestry/arboriculture/commands/TreeGenHelper.java index cc2bac860..8de603655 100644 --- a/src/main/java/forestry/arboriculture/commands/TreeGenHelper.java +++ b/src/main/java/forestry/arboriculture/commands/TreeGenHelper.java @@ -10,8 +10,12 @@ ******************************************************************************/ package forestry.arboriculture.commands; +import net.minecraft.block.Block; +import net.minecraft.block.material.Material; +import net.minecraft.block.state.IBlockState; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.BlockPos; +import net.minecraft.world.World; import net.minecraft.world.gen.feature.WorldGenerator; import forestry.api.arboriculture.IAlleleTreeSpecies; @@ -22,6 +26,7 @@ import forestry.api.genetics.AlleleManager; import forestry.api.genetics.IAllele; import forestry.core.commands.SpeciesNotFoundException; import forestry.core.commands.TemplateNotFoundException; +import forestry.core.utils.BlockUtil; import forestry.core.worldgen.WorldGenBase; public final class TreeGenHelper { @@ -37,10 +42,20 @@ public final class TreeGenHelper { } public static void generateTree(WorldGenerator gen, EntityPlayer player, BlockPos pos) { - if (gen instanceof WorldGenBase) { - ((WorldGenBase) gen).generate(player.worldObj, pos, true); - } else { - gen.generate(player.worldObj, player.worldObj.rand, pos); + World world = player.worldObj; + + if(world.isAirBlock(pos.add(0, -1, 0))){ + pos = BlockUtil.getNextSolidDownPos(world, pos); + }else{ + pos = BlockUtil.getNextReplaceableUpPos(world, pos); + } + + if(BlockUtil.canPlaceTree(world, pos)){ + if (gen instanceof WorldGenBase) { + ((WorldGenBase) gen).generate(world, pos, false); + } else { + gen.generate(world, world.rand, pos); + } } } diff --git a/src/main/java/forestry/core/utils/BlockUtil.java b/src/main/java/forestry/core/utils/BlockUtil.java index 880f9be52..b1e31ea6c 100644 --- a/src/main/java/forestry/core/utils/BlockUtil.java +++ b/src/main/java/forestry/core/utils/BlockUtil.java @@ -17,6 +17,7 @@ import java.util.List; import net.minecraft.block.Block; import net.minecraft.block.BlockDirectional; import net.minecraft.block.BlockPlanks; +import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; import net.minecraft.item.ItemStack; @@ -290,4 +291,43 @@ public abstract class BlockUtil { return false; } } + + public static boolean canReplace(World world, BlockPos pos) { + Block block = getBlock(world, pos); + Material material = block.getMaterial(); + return material.isReplaceable() && block.isReplaceable(world, pos) && !material.isLiquid() || block.isAir(world, pos) || material == Material.plants; + } + + public static boolean canPlaceTree(World world, BlockPos pos){ + BlockPos downPos = pos.down(); + Block block = world.getBlockState(downPos).getBlock(); + if(block.isReplaceable(world, downPos) && block.getMaterial().isLiquid() || block.isLeaves(world, downPos) || block.isWood(world, downPos)){ + return false; + } + return true; + } + + public static BlockPos getNextReplaceableUpPos(World world, BlockPos pos){ + int x = pos.getX(); + int y = pos.getY(); + int z = pos.getZ(); + + do { + y++; + } while (!BlockUtil.canReplace(world, new BlockPos(x, y, z))); + + return new BlockPos(x, y, z); + } + + public static BlockPos getNextSolidDownPos(World world, BlockPos pos){ + int x = pos.getX(); + int y = pos.getY(); + int z = pos.getZ(); + + do { + y--; + } while (BlockUtil.canReplace(world, new BlockPos(x, y - 1, z))); + + return new BlockPos(x, y, z); + } } diff --git a/src/main/java/forestry/lepidopterology/genetics/ButterflyRoot.java b/src/main/java/forestry/lepidopterology/genetics/ButterflyRoot.java index 408aca301..7a95c7134 100644 --- a/src/main/java/forestry/lepidopterology/genetics/ButterflyRoot.java +++ b/src/main/java/forestry/lepidopterology/genetics/ButterflyRoot.java @@ -207,16 +207,10 @@ public class ButterflyRoot extends SpeciesRoot implements IButterflyRoot { do { y--; - } while (!canReplace(world, new BlockPos(x, y, z))); + } while (!BlockUtil.canReplace(world, new BlockPos(x, y, z))); return new BlockPos(x, y, z); } - - private boolean canReplace(World world, BlockPos pos) { - Block block = BlockUtil.getBlock(world, pos); - Material material = block.getMaterial(); - return block.isReplaceable(world, pos) && !material.isLiquid() || block.isAir(world, pos) || material == Material.plants; - } @Override public boolean isMated(ItemStack stack) {