Jump to content

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


Recommended Posts

Posted (edited)

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
Posted

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

Posted (edited)

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/

Posted

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.

Posted

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.)

Posted

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/

Posted

Thanks, that worked perfectly! (Though I did need to delay the event by a few ticks.) Now I just need to make sure it only runs once, then I should be good to go.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • When I first heard about Bitcoin back in 2018, I was skeptical. The idea of a decentralized, digital currency seemed too good to be true. But I was intrigued as I learned more about the technology behind it and its potential. I started small, investing just a few hundred dollars, dipping my toes into the cryptocurrency waters. At first, it was exhilarating to watch the value of my investment grow exponentially. I felt like I was part of the future, an early adopter of this revolutionary new asset. But that euphoria was short-lived. One day, I logged into my digital wallet only to find it empty - my Bitcoin had vanished without a trace. It turned out that the online exchange I had trusted had been hacked, and my funds were stolen. I was devastated, both financially and emotionally. All the potential I had seen in Bitcoin was tainted by the harsh reality that with decentralization came a lack of regulation and oversight. My hard-earned money was gone, lost to the ether of the digital world. This experience taught me a painful lesson about the price of trust in the uncharted territory of cryptocurrency. While the technology holds incredible promise, the risks can be catastrophic if you don't approach it with extreme caution. My Bitcoin investment gamble had failed, and I was left to pick up the pieces, wiser but poorer for having placed my faith in the wrong hands. My sincere appreciation goes to MUYERN TRUST HACKER. You are my hero in recovering my lost funds. Send a direct m a i l ( muyerntrusted ( @ ) mail-me ( . )c o m ) or message on whats app : + 1 ( 4-4-0 ) ( 3 -3 -5 ) ( 0-2-0-5 )
    • You could try posting a log (if there is no log at all, it may be the launcher you are using, the FAQ may have info on how to enable the log) as described in the FAQ, however this will probably need to be reported to/remedied by the mod author.
    • So me and a couple of friends are playing with a shitpost mod pack and one of the mods in the pack is corail tombstone and for some reason there is a problem with it, where on death to fire the player will get kicked out of the server and the tombstone will not spawn basically deleting an entire inventory, it doesn't matter what type of fire it is, whether it's from vanilla fire/lava, or from modded fire like ice&fire/lycanites and it's common enough to where everyone on the server has experienced at least once or twice and it doesn't give any crash log. a solution to this would be much appreciated thank you!
    • It is 1.12.2 - I have no idea if there is a 1.12 pack
  • Topics

×
×
  • Create New...

Important Information

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