Jump to content

How to optimize setBlock during chunk generation


Recommended Posts

Hello all,


Today I started to dive into modding Minecraft (with Forge 4).

I'm thinking of generating some pretty big structures, so I started messing around with a custom world generator. But one of my tests (simply adding some sort of brick stone roof with glass windows over the complete world) is generating really slow.


My IWorldGenerator implementation:


public class QWorldGenerator implements IWorldGenerator 
    protected WorldGenRoof roofGenerator = new WorldGenRoof();

    // left out some inherited methods

    private void generateSurface(World world, Random random, int x, int z) 
        roofGenerator.generate(world, random, x, 0, z);


The actual WorldGenerator:


public class WorldGenRoof extends WorldGenerator
    public boolean generate(World world, Random random, int chunkX, int chunkY, int chunkZ)
        int blockId = 0;
        for (int x = 0; x < 16; x++) {
            for (int z = 0; z < 16; z++) {
                blockId = Block.stoneBrick.blockID;
                if (x > 6 && x < 13 && z > 6 && z < 13) {
                    blockId = Block.glass.blockID;
                world.setBlock(chunkX + x, 100, chunkZ + z, blockId);
        return true;


The code works fine, but as I said, really slow. It takes like five minutes instead of the normal couple of seconds to generate a new world.

When I remove the world.setBlock(...) there is no real drop in performance.


So is there some kind of other process I should use when doing things like this? Or maybe some mode to set while setting blocks in a loop? (I tried world.editingBlocks, but that didn't really help :P)


Any ideas welcome! :)

Link to comment
Share on other sites

I'm not good at code, but could it be because generateSurface is private and WorldGenRoof is protected? I usually set all my code to public, and everything loads fine for me.


Those aren't really relevant to the problem (private/protected/package/public keywords only define where the properties can be accessed, and in this case private would be enough, since it's only the owner class accessing them). Stuff like this would also throw an exception when used incorrectly (I guess, long time since I programmed in Java).


My problem is just that world.setBlock seems to execute very slowly, possibly because it fires a whole range of things that need to be calculated for it. So the question here is whether there is a better way to do what I'm doing.

Link to comment
Share on other sites

There are variations of setBlock that do less stuff, as well as the ability to edit things directly in the chunks.

You'd have to look into what the best code path for you would be.



I'm guessing you're talking about setBlockAndNotify and the likes? Those weren't really faster. (If you meant those, but I doubt that.)


Editing chunks directly sounds more like it :D. But googling this stuff doesn't yield much results. I'll try some more later today (I'm at work now).

But if you or someone could just point me a bit further in the right direction (like with a link or method name), that would probably help a lot.


Most stuff that I find is coded roughly the same way as what I'm doing, so I'm guessing I'm missing an important keyword or something :D

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.

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.

  • Create New...

Important Information

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