Integrate the terrain in the tree gen helper

This commit is contained in:
Nedelosk committed 2016-03-26 13:28:13 +01:00
1 parent 1c1dd4918a
commit 8a40dd6d27
5 files changed
+65 -20

No files matched your search

@@ -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);
}
}
@@ -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();
}
}
@@ -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);
}
}
}
@@ -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);
}
}
@@ -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) {