Search in sources :

Example 1 with SaplingGrowTreeEvent

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);
}
Also used : DecorateBiomeEvent(net.minecraftforge.event.terraingen.DecorateBiomeEvent) TreeRTG(rtg.api.world.gen.feature.tree.rtg.TreeRTG) Blocks(net.minecraft.init.Blocks) RTGConfig(rtg.RTGConfig) BiomeProviderRTG(rtg.world.biome.BiomeProviderRTG) BlockUtil(rtg.api.util.BlockUtil) Random(java.util.Random) BlockSapling(net.minecraft.block.BlockSapling) Decorate(net.minecraftforge.event.terraingen.DecorateBiomeEvent.Decorate) UtilityClass(rtg.api.util.UtilityClass) SaplingGrowTreeEvent(net.minecraftforge.event.terraingen.SaplingGrowTreeEvent) IRealisticBiome(rtg.api.world.biome.IRealisticBiome) WorldGenLiquids(net.minecraft.world.gen.feature.WorldGenLiquids) Event(net.minecraftforge.fml.common.eventhandler.Event) DecoBase(rtg.api.world.deco.DecoBase) World(net.minecraft.world.World) Collection(java.util.Collection) ChunkPos(net.minecraft.util.math.ChunkPos) BlockPos(net.minecraft.util.math.BlockPos) Logger(rtg.api.util.Logger) Collectors(java.util.stream.Collectors) IBlockState(net.minecraft.block.state.IBlockState) EventPriority(net.minecraftforge.fml.common.eventhandler.EventPriority) List(java.util.List) MinecraftForge(net.minecraftforge.common.MinecraftForge) RTGAPI(rtg.api.RTGAPI) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent) IBlockState(net.minecraft.block.state.IBlockState) BiomeProviderRTG(rtg.world.biome.BiomeProviderRTG) World(net.minecraft.world.World) IRealisticBiome(rtg.api.world.biome.IRealisticBiome) Random(java.util.Random) TreeRTG(rtg.api.world.gen.feature.tree.rtg.TreeRTG) BlockSapling(net.minecraft.block.BlockSapling) BlockPos(net.minecraft.util.math.BlockPos) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 2 with SaplingGrowTreeEvent

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;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) SaplingGrowTreeEvent(net.minecraftforge.event.terraingen.SaplingGrowTreeEvent) BlockPos(net.minecraft.util.math.BlockPos)

Aggregations

IBlockState (net.minecraft.block.state.IBlockState)2 BlockPos (net.minecraft.util.math.BlockPos)2 SaplingGrowTreeEvent (net.minecraftforge.event.terraingen.SaplingGrowTreeEvent)2 Collection (java.util.Collection)1 List (java.util.List)1 Random (java.util.Random)1 Collectors (java.util.stream.Collectors)1 BlockSapling (net.minecraft.block.BlockSapling)1 Blocks (net.minecraft.init.Blocks)1 ChunkPos (net.minecraft.util.math.ChunkPos)1 World (net.minecraft.world.World)1 WorldGenLiquids (net.minecraft.world.gen.feature.WorldGenLiquids)1 MinecraftForge (net.minecraftforge.common.MinecraftForge)1 DecorateBiomeEvent (net.minecraftforge.event.terraingen.DecorateBiomeEvent)1 Decorate (net.minecraftforge.event.terraingen.DecorateBiomeEvent.Decorate)1 Event (net.minecraftforge.fml.common.eventhandler.Event)1 EventPriority (net.minecraftforge.fml.common.eventhandler.EventPriority)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1 RTGConfig (rtg.RTGConfig)1 RTGAPI (rtg.api.RTGAPI)1