use of net.minecraftforge.event.terraingen.SaplingGrowTreeEvent in project Realistic-Terrain-Generation by Team-RTG.
the class EventHandlerCommon method saplingGrowTreeRTG.
// TERRAIN_GEN_BUS
@SuppressWarnings("deprecation")
@SubscribeEvent
public static void saplingGrowTreeRTG(SaplingGrowTreeEvent event) {
final World world = event.getWorld();
// skip if RTG saplings are disabled or this world does not use BiomeProviderRTG
if (!RTGConfig.rtgTreesFromSaplings() || !(world.getBiomeProvider() instanceof BiomeProviderRTG)) {
Logger.debug("[SaplingGrowTreeEvent] Aborting: RTG trees are disabled, or not an RTG dimension");
return;
}
final BlockPos pos = event.getPos();
final IBlockState saplingBlock = world.getBlockState(pos);
Logger.trace("Handling SaplingGrowTreeEvent in dim: {}, at: {}, for: {}", world.provider.getDimension(), pos, saplingBlock);
// Are we dealing with a sapling? Sounds like a silly question, but apparently it's one that needs to be asked.
if (!(saplingBlock.getBlock() instanceof BlockSapling)) {
Logger.debug("[SaplingGrowTreeEvent] Aborting: Sapling is not a sapling block ({})", saplingBlock.getBlock().getClass().getName());
return;
}
final Random rand = event.getRand();
// Should we generate a vanilla tree instead?
int chance = RTGConfig.rtgTreeChance();
if (rand.nextInt(chance < 1 ? 1 : chance) != 0) {
Logger.debug("[SaplingGrowTreeEvent] Aborting RTG tree generation: random chance");
return;
}
final IRealisticBiome rtgBiome = RTGAPI.getRTGBiome(world.getBiome(pos));
Collection<TreeRTG> biomeTrees = rtgBiome.getTrees();
if (biomeTrees.isEmpty()) {
Logger.debug("[SaplingGrowTreeEvent] Aborting RTG tree generation: No RTG trees to generate in Biome: {}", rtgBiome.baseBiomeResLoc());
return;
}
// First, let's get all of the trees in this biome that match the sapling on the ground.
List<TreeRTG> validTrees = biomeTrees.stream().filter(tree -> saplingBlock.getBlock() == tree.getSaplingBlock().getBlock() && BlockUtil.getTypeFromSapling(saplingBlock) == BlockUtil.getTypeFromSapling(tree.getSaplingBlock())).collect(Collectors.toList());
// Abort if there are no valid trees.
if (validTrees.isEmpty()) {
Logger.debug("[SaplingGrowTreeEvent] No RTG trees found for sapling, so generating original tree instead");
return;
}
// Get a random tree from the list of valid trees.
TreeRTG tree = validTrees.get(rand.nextInt(validTrees.size()));
// Set the trunk size if min/max values have been set.
if (tree.getMinTrunkSize() > 0 && tree.getMaxTrunkSize() > tree.getMinTrunkSize()) {
tree.setTrunkSize(DecoBase.getRangedRandom(rand, tree.getMinTrunkSize(), tree.getMaxTrunkSize()));
}
// Set the crown size if min/max values have been set.
if (tree.getMinCrownSize() > 0 && tree.getMaxCrownSize() > tree.getMinCrownSize()) {
tree.setCrownSize(DecoBase.getRangedRandom(rand, tree.getMinCrownSize(), tree.getMaxCrownSize()));
}
int treeHeight = tree.getTrunkSize() + tree.getCrownSize();
if (treeHeight < 1) {
Logger.debug("[SaplingGrowTreeEvent] Unable to grow RTG tree with no height: {}[logblock={}, leafblock={}, saplingblock={}]", tree.getClass().getSimpleName(), tree.getLogBlock(), tree.getLeavesBlock(), tree.getSaplingBlock());
return;
}
if (!BlockUtil.checkVerticalMaterials(BlockUtil.MatchType.ALL_IGNORE_REPLACEABLE, world, pos.up(), treeHeight - 1)) {
Logger.debug("[SaplingGrowTreeEvent] Aborting RTG tree generation: not enough space above");
return;
}
/*
* Set the generateFlag to what it needs to be for growing trees from saplings,
* generate the tree, and then set it back to what it was before.
*
* TODO: Does this affect the generation of normal RTG trees? - Pink
*/
int oldFlag = tree.getGenerateFlag();
tree.setGenerateFlag(3);
boolean generated = tree.generate(world, rand, pos);
tree.setGenerateFlag(oldFlag);
if (generated) {
event.setResult(Event.Result.DENY);
// Sometimes we have to remove the sapling manually because some trees grow around it, leaving the original sapling.
if (world.getBlockState(pos) == saplingBlock) {
world.setBlockState(pos, Blocks.AIR.getDefaultState(), 2);
}
}
Logger.trace("Finished handling SaplingGrowTreeEvent in Biome: {}, with sapling: {}", rtgBiome.baseBiomeResLoc(), saplingBlock);
}
use of net.minecraftforge.event.terraingen.SaplingGrowTreeEvent in project GregTech by GregTechCEu.
the class WorldGenRubberTree method grow.
public boolean grow(World world, BlockPos pos, Random random) {
if (world == null) {
return false;
}
SaplingGrowTreeEvent event = new SaplingGrowTreeEvent(world, random, pos);
MinecraftForge.TERRAIN_GEN_BUS.post(event);
if (event.getResult() == Event.Result.DENY) {
return false;
}
IBlockState woodBlock = MetaBlocks.RUBBER_LOG.getDefaultState().withProperty(NATURAL, true);
IBlockState leaves = MetaBlocks.RUBBER_LEAVES.getDefaultState();
int height = getGrowHeight(world, pos);
if (height < 2)
return false;
height -= random.nextInt(height / 2 + 1);
height = Math.max(5, height);
BlockPos.MutableBlockPos tmpPos = new BlockPos.MutableBlockPos();
for (int cHeight = 0; cHeight < height; cHeight++) {
BlockPos cPos = pos.up(cHeight);
setBlockAndNotifyAdequately(world, cPos, woodBlock);
if ((height < 7 && cHeight > 1) || cHeight > 2) {
for (int cx = pos.getX() - 2; cx <= pos.getX() + 2; cx++) {
for (int cz = pos.getZ() - 2; cz <= pos.getZ() + 2; cz++) {
int chance = Math.max(1, cHeight + 4 - height);
int dx = Math.abs(cx - pos.getX());
int dz = Math.abs(cz - pos.getZ());
if ((dx <= 1 && dz <= 1) || (dx <= 1 && random.nextInt(chance) == 0) || (dz <= 1 && random.nextInt(chance) == 0)) {
tmpPos.setPos(cx, pos.getY() + cHeight, cz);
if (world.isAirBlock(tmpPos)) {
setBlockAndNotifyAdequately(world, new BlockPos(tmpPos), leaves);
}
}
}
}
}
}
for (int i = 0; i <= height / 4 + random.nextInt(2); i++) {
tmpPos.setPos(pos.getX(), pos.getY() + height + i, pos.getZ());
if (world.isAirBlock(tmpPos))
setBlockAndNotifyAdequately(world, new BlockPos(tmpPos), leaves);
}
return true;
}
Aggregations