Jump to content

[1.12.2] [Solved] When Replacing Blocks During World Gen, Some Leaves Don't Get Replaced


StarV

Recommended Posts

As the title says, I am currently trying to replace leaves during world gen (among other blocks). My goal is to use the vanilla ChunkGeneratorOverworld for my custom dimension, but replace blocks on generation to create a visually different but topographically the same.

 

Spoiler

package com.starv.openworld.util.handler;

import com.starv.openworld.core.config.DimensionIDs;
import com.starv.openworld.registry.ModBlocks;

import net.minecraft.block.Block;
import net.minecraft.block.BlockDirt;
import net.minecraft.block.BlockNewLeaf;
import net.minecraft.block.BlockNewLog;
import net.minecraft.block.BlockOldLeaf;
import net.minecraft.block.BlockOldLog;
import net.minecraft.block.BlockPlanks;
import net.minecraft.block.BlockRedSandstone;
import net.minecraft.block.BlockSand;
import net.minecraft.block.BlockSandStone;
import net.minecraft.block.BlockStainedHardenedClay;
import net.minecraft.block.BlockStone;
import net.minecraft.block.BlockStoneBrick;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import net.minecraftforge.event.terraingen.PopulateChunkEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class WorldGenHandler {
    
    @SubscribeEvent
    public void populateChunkEvent(PopulateChunkEvent event)
    {
        if (event.getWorld().provider.getDimension() == DimensionIDs.SUPERSPACE)
        {
            Chunk chunk = event.getWorld().getChunkFromChunkCoords(event.getChunkX(), event.getChunkZ());
            
            for (ExtendedBlockStorage storage : chunk.getBlockStorageArray())
            {
                if (storage != null)
                {
                    for (int x = 0; x < 16; x++)
                    {
                        for (int y = 0; y < 16; y++)
                        {
                            for (int z = 0; z < 16; z++)
                            {
                                this.replaceBlock(storage, x, y, z, ModBlocks.superspace_grass, Blocks.GRASS.getDefaultState(), Blocks.DIRT.getDefaultState().withProperty(BlockDirt.VARIANT, BlockDirt.DirtType.PODZOL));
                                this.replaceBlock(storage, x, y, z, ModBlocks.superspace_dirt, Blocks.DIRT.getDefaultState(), Blocks.DIRT.getDefaultState().withProperty(BlockDirt.VARIANT, BlockDirt.DirtType.COARSE_DIRT));
                                this.replaceBlock(storage, x, y, z, ModBlocks.superspace_sand, Blocks.SAND.getDefaultState(), Blocks.SAND.getDefaultState().withProperty(BlockSand.VARIANT, BlockSand.EnumType.RED_SAND));
                                this.replaceBlock(storage, x, y, z, ModBlocks.superspace_sandstone, Blocks.SANDSTONE.getDefaultState(), Blocks.SANDSTONE.getDefaultState().withProperty(BlockSandStone.TYPE, BlockSandStone.EnumType.CHISELED), Blocks.SANDSTONE.getDefaultState().withProperty(BlockSandStone.TYPE, BlockSandStone.EnumType.SMOOTH), Blocks.RED_SANDSTONE.getDefaultState(), Blocks.RED_SANDSTONE.getDefaultState().withProperty(BlockRedSandstone.TYPE, BlockRedSandstone.EnumType.CHISELED), Blocks.RED_SANDSTONE.getDefaultState().withProperty(BlockRedSandstone.TYPE, BlockRedSandstone.EnumType.SMOOTH), Blocks.HARDENED_CLAY.getDefaultState(), Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockStainedHardenedClay.COLOR, EnumDyeColor.BLACK), Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockStainedHardenedClay.COLOR, EnumDyeColor.BLUE), Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockStainedHardenedClay.COLOR, EnumDyeColor.BROWN), Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockStainedHardenedClay.COLOR, EnumDyeColor.CYAN), Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockStainedHardenedClay.COLOR, EnumDyeColor.GRAY), Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockStainedHardenedClay.COLOR, EnumDyeColor.GREEN), Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockStainedHardenedClay.COLOR, EnumDyeColor.LIGHT_BLUE), Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockStainedHardenedClay.COLOR, EnumDyeColor.LIME), Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockStainedHardenedClay.COLOR, EnumDyeColor.MAGENTA), Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockStainedHardenedClay.COLOR, EnumDyeColor.ORANGE), Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockStainedHardenedClay.COLOR, EnumDyeColor.PINK), Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockStainedHardenedClay.COLOR, EnumDyeColor.PURPLE), Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockStainedHardenedClay.COLOR, EnumDyeColor.RED), Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockStainedHardenedClay.COLOR, EnumDyeColor.SILVER), Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockStainedHardenedClay.COLOR, EnumDyeColor.WHITE), Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockStainedHardenedClay.COLOR, EnumDyeColor.YELLOW));
                                this.replaceBlock(storage, x, y, z, ModBlocks.superspace_sandstone_stairs, Blocks.SANDSTONE_STAIRS.getDefaultState(), Blocks.RED_SANDSTONE_STAIRS.getDefaultState());
                                this.replaceBlock(storage, x, y, z, ModBlocks.superspace_gravel, Blocks.GRAVEL.getDefaultState());
                                this.replaceBlock(storage, x, y, z, ModBlocks.superspace_oak_log, Blocks.LOG.getDefaultState(), Blocks.LOG.getDefaultState().withProperty(BlockOldLog.VARIANT, BlockPlanks.EnumType.SPRUCE), Blocks.LOG.getDefaultState().withProperty(BlockOldLog.VARIANT, BlockPlanks.EnumType.BIRCH), Blocks.LOG.getDefaultState().withProperty(BlockOldLog.VARIANT, BlockPlanks.EnumType.JUNGLE), Blocks.LOG2.getDefaultState(), Blocks.LOG2.getDefaultState().withProperty(BlockNewLog.VARIANT, BlockPlanks.EnumType.ACACIA), Blocks.LOG2.getDefaultState().withProperty(BlockNewLog.VARIANT, BlockPlanks.EnumType.DARK_OAK));
                                this.replaceBlock(storage, x, y, z, ModBlocks.superspace_oak_planks, Blocks.PLANKS.getDefaultState());
                                this.replaceBlock(storage, x, y, z, ModBlocks.superspace_oak_leaves, Blocks.LEAVES.getDefaultState().withProperty(BlockOldLeaf.VARIANT, BlockPlanks.EnumType.OAK).withProperty(BlockOldLeaf.DECAYABLE, true), Blocks.LEAVES.getDefaultState().withProperty(BlockOldLeaf.VARIANT, BlockPlanks.EnumType.OAK).withProperty(BlockOldLeaf.DECAYABLE, false), Blocks.LEAVES.getDefaultState().withProperty(BlockOldLeaf.VARIANT, BlockPlanks.EnumType.SPRUCE).withProperty(BlockOldLeaf.DECAYABLE, true), Blocks.LEAVES.getDefaultState().withProperty(BlockOldLeaf.VARIANT, BlockPlanks.EnumType.SPRUCE).withProperty(BlockOldLeaf.DECAYABLE, false), Blocks.LEAVES.getDefaultState().withProperty(BlockOldLeaf.VARIANT, BlockPlanks.EnumType.BIRCH).withProperty(BlockOldLeaf.DECAYABLE, true), Blocks.LEAVES.getDefaultState().withProperty(BlockOldLeaf.VARIANT, BlockPlanks.EnumType.BIRCH).withProperty(BlockOldLeaf.DECAYABLE, false), Blocks.LEAVES.getDefaultState().withProperty(BlockOldLeaf.VARIANT, BlockPlanks.EnumType.JUNGLE).withProperty(BlockOldLeaf.DECAYABLE, true), Blocks.LEAVES.getDefaultState().withProperty(BlockOldLeaf.VARIANT, BlockPlanks.EnumType.JUNGLE).withProperty(BlockOldLeaf.DECAYABLE, false), Blocks.LEAVES2.getDefaultState().withProperty(BlockNewLeaf.VARIANT, BlockPlanks.EnumType.ACACIA).withProperty(BlockNewLeaf.DECAYABLE, true), Blocks.LEAVES2.getDefaultState().withProperty(BlockNewLeaf.VARIANT, BlockPlanks.EnumType.ACACIA).withProperty(BlockNewLeaf.DECAYABLE, false), Blocks.LEAVES2.getDefaultState().withProperty(BlockNewLeaf.VARIANT, BlockPlanks.EnumType.DARK_OAK).withProperty(BlockNewLeaf.DECAYABLE, true), Blocks.LEAVES2.getDefaultState().withProperty(BlockNewLeaf.VARIANT, BlockPlanks.EnumType.DARK_OAK).withProperty(BlockNewLeaf.DECAYABLE, false));
                                this.replaceBlock(storage, x, y, z, ModBlocks.superspace_stone, Blocks.STONE.getDefaultState(), Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.ANDESITE), Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.DIORITE), Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.GRANITE));
                                this.replaceBlock(storage, x, y, z, ModBlocks.superspace_stone_bricks, Blocks.STONEBRICK.getDefaultState(), Blocks.STONEBRICK.getDefaultState().withProperty(BlockStoneBrick.VARIANT, BlockStoneBrick.EnumType.CHISELED), Blocks.STONEBRICK.getDefaultState().withProperty(BlockStoneBrick.VARIANT, BlockStoneBrick.EnumType.CRACKED), Blocks.STONEBRICK.getDefaultState().withProperty(BlockStoneBrick.VARIANT, BlockStoneBrick.EnumType.MOSSY), Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.ANDESITE_SMOOTH), Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.DIORITE_SMOOTH), Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.GRANITE_SMOOTH));
                                this.replaceBlock(storage, x, y, z, ModBlocks.superspace_stone_bricks_stairs, Blocks.STONE_BRICK_STAIRS.getDefaultState());
                                this.replaceBlock(storage, x, y, z, ModBlocks.superspace_cobblestone, Blocks.COBBLESTONE.getDefaultState(), Blocks.MOSSY_COBBLESTONE.getDefaultState());
                                this.replaceBlock(storage, x, y, z, ModBlocks.superspace_cobblestone_stairs, Blocks.STONE_STAIRS.getDefaultState());
                            }
                        }
                    }
                }
            }
            chunk.setModified(true);
        }
    }
    
    private void replaceBlock(ExtendedBlockStorage storage, int x, int y, int z, Block blockOut, IBlockState... blockIn)
    {
        for (IBlockState block : blockIn)
        {
            if (storage.get(x, y, z).equals(block)) storage.set(x, y, z, blockOut.getDefaultState());
        }
    }
}

 

 

The other replaceBlock methods work as expected, but the leaves don't seem to always get replaced. Image attached to post for example. I believe it may have something to do with variants, but I have put in every combination of variants I could find for the leaves, and it just doesn't seem to work as intended, so I'm at a loss here.

screenshot.png

Edited by StarV
Link to comment
Share on other sites

Instanceof (or Class.isInstance()) doesn't seem to work. It either replaces everything with ModBlocks.superspace_cobblestone_stairs or doesn't do anything at all.

 

I did manage to fix the variant issue with this:

Spoiler

private void replaceBlock(ExtendedBlockStorage storage, int x, int y, int z, Block blockOut, Block... blockIn)
    {
        for (Block block : blockIn)
        {
            if (block.getClass().equals(storage.get(x, y, z).getBlock().getClass())) storage.set(x, y, z, blockOut.getDefaultState());
        }
    }

However, it only seems to run in certain chunks, which is odd. Do the trees generate in a different event?

screenshot.png

Link to comment
Share on other sites

That should do almost exactly the same thing as instanceof, although instanceof has the advantage that any subclasses (like if some other mod extends BlockLeaves) would be recognized. But if it works then alls good.

 

The main point to remember is that equals() depends on the implementation but often compares all the field values as well as the general class. If you want to match type you really want instance of, or compare actual class.

Edited by jabelar

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Trees can generate across chunk boarders. 

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Well, I'm still stuck. Will I have to end up replacing the biome decorators and put in my own trees, or is there a way to do this? It looks like trees are generating during DecorateBiomeEvent.Decorate with the EventType of TREE.

 

Here in the genDecorations method of BiomeDecorator.class

Spoiler

        if(net.minecraftforge.event.terraingen.TerrainGen.decorate(worldIn, random, chunkPos, net.minecraftforge.event.terraingen.DecorateBiomeEvent.Decorate.EventType.TREE))
        for (int j2 = 0; j2 < k1; ++j2)
        {
            int k6 = random.nextInt(16) + 8;
            int l = random.nextInt(16) + 8;
            WorldGenAbstractTree worldgenabstracttree = biomeIn.getRandomTreeFeature(random);
            worldgenabstracttree.setDecorationDefaults();
            BlockPos blockpos = worldIn.getHeight(this.chunkPos.add(k6, 0, l));

            if (worldgenabstracttree.generate(worldIn, random, blockpos))
            {
                worldgenabstracttree.generateSaplings(worldIn, random, blockpos);
            }
        }

There are also some other plants that aren't working in certain chunks, such as custom tall grass, but fixing trees should fix those as well. I just don't know when to intercept the event (if I even need to.)

Link to comment
Share on other sites

instead of the populate chunk event you might have better luck with the chunk load event. I was looking at this recently (and will probably file an issue about it) but there really isn't a great event for replacing the results of generation. There are events where you can fully replace the entire chunk provider, or biome and such, but if you want to let the normal generation happen and then replace selective blocks the populate chunk happens too early (also seems you can run into issue with concurrent modifications) and the other events tend not to pass the chunk primer.

 

But I found that chunk load actually happens almost exactly where you need it to. So try that instead.

  • Like 1

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.