Adding a block populator to default chunk generator

Discussion in 'Plugin Development' started by FTWinston, Oct 26, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    FTWinston

    Hey, I'd like to add a custom block populator to the default chunk generator before generating the world.

    I can easily add one after generating the world, like so:
    Code:
    WorldCreator wc = new WorldCreator("worldname").environment(Environment.NORMAL);
    World myWorld = getServer().createWorld(wc);
    // spawn is generated before the next line is called
    myWorld.getPopulators().add(new CustomBlockPopulator());
    
    If I do this, I can see the effects of my custom block populator if I walk far enough away from the spawn, but (obviously) not around the spawn itself, because that terrain was generated prior to adding the custom block populator.

    I couldn't find anything on this elsewhere in the forums - or rather, I found a few people saying it was possible, but no actual examples as to how.

    When the default generator is to be used, the generator() method of the WorldCreator returns null, so you can't just do wc.generator().getDefaultPopulators().add(new CustomBlockPopulator());

    Any ideas?

    Many thanks,
    Winston
     
  2. if you have your own world generator, you can add it before generation , if you haven't it, it there is no way whitout using reflection
     
  3. Offline

    FTWinston

    Hmm, ok thanks. I'll look into doing it with reflection. Any pointers, before I reinvent the wheel?

    Ok, well as I don't see any way of doing this "nicely", I ended up creating a copy of the CraftServer.createWorld function. This takes an extra parameter, namely a List of extra BlockPopulators to use. It then adds only a single line of code. There's also an overload of the function in there, taking only a single extra BlockPopulator. Note that it'll only work if you're using the CraftBukkit jar, instead of the Bukkit one.

    It seems ridiculous to have to do this, and now in case anything changes, this function will need to be checked/updated with every subsequent Bukkit release.

    Edit: Ok, no java syntax highlighting on here (that figures...)
    Here it is on pastebin, instead:
    http://pastebin.com/QXjm0URp

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  4. Offline

    jtjj222

    You need to hook the WorldInitEvent, then do event.getWorld().getPopulators.add(...).
    :D
     
    fromgate likes this.
  5. Offline

    FTWinston

    Aha! Thanks, that involves a lot less code.

    Although, my plugin has a whole selection of world generation options, and it depends on what the user selects whether I even want to do anything or not...

    So when the relevant world generation option is selected, I guess I need to register an event listener that listens for WorldInitEvent, create the world, then unregister the event listener again.

    It'd be a lot neater if the WorldCreator class had a list of extra block populators. I think I'll make a pull request for that...

    Edit: Here's the code, for anyone wanting to replicate this
    http://pastebin.com/DdXx5zWy

    Ok, umm, question:

    I want to make a pull request that adds to the WorldCreator class (which is in the bukkit project), and also changes how that is used in the CraftServer class (which is in the CraftBukkit project).

    Is there any fancy, clever way of doing that, or should I just make two pull requests, with a note to the effect that you shouldn't use one without the other?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
Thread Status:
Not open for further replies.

Share This Page