Using custom ChunkGenerators

Discussion in 'Plugin Development' started by Assist, Nov 9, 2013.

Thread Status:
Not open for further replies.
  1. Hey, I followed the tutorial in the Resources section about ChunkGenerators, and came up with this code.
    Code:java
    1. public byte[][] generateBlockSections(World world, Random random, int chunkX, int chunkZ, BiomeGrid biomeGrid) {
    2. byte result[][] = new byte[world.getMaxHeight() / 16][];
    3.  
    4. for (int x = 0; x < 60; x++) {
    5. for (int y = 1; y < 10; y++) {
    6. for (int z = 0; z < 60; z++) {
    7. setBlock(result, x, y, z, (byte) Material.DIRT.getId());
    8. setBlock(result, x, 11, z, (byte) Material.GRASS.getId());
    9. }
    10. }
    11. }
    12.  
    13. return result;
    14. }
    15.  
    16. void setBlock(byte[][] result, int x, int y, int z, byte blkid) {
    17. if (result[y >> 4] == null) {
    18. result[y >> 4] = new byte[4096];
    19. }
    20.  
    21. result[y >> 4][((y & 0xF) << 8) | (z << 4) | x] = blkid;
    22. }

    Now how would I create a world using this ChunkGenerator? I basically just want to create a world with a dirt box, nothing else. I've never worked with ChunkGenerators, BlockPopulators or WorldGenerators before.

    In the thread, it told me to create a method in the main class called getDefaultWorldGenerator, which would return my custom generator, but I haven't done that yet since I'm not sure what it does, and I don't wanna mess up mine, and everyone else's worlds.

    Thanks in advance.
     
  2. Offline

    GusGold

    Assist
    Add in this
    Code:java
    1. public ChunkGenerator getDefaultWorldGenerator(String worldName, String id){
    2. return new Generator();
    3. }
    Where Generator is your generator class
    Then you will need to tell bukkit to use the generator for certain worlds, so in your server's bukkit.yml you need to add
    Code:text
    1. worlds:
    2. MyWorldName:
    3. generator: MyPluginName
    Finally, because you want this to affect world gen, which is done before plugins are normally loaded, you will need to add to your plugin.yml:
    Code:text
    1. load: STARTUP
    to load the plugin almost immediately. More info on loading here.
     
  3. GusGold
    That's what I thought, but the problem is, I can't yet know the names of the worlds. I guess I could just locate the bukkit.yml file in their server folder, and add the world name in there (using FileConfiguration in my plugin) before generating it. Would that be a possibility?
     
  4. Offline

    iFamasssxD

    You can assign the Generator on world creation if this is more what you are looking for.
    Code:
    World world = Bukkit.getServer().createWorld(new WorldCreator("World").environment(Environment.NORMAL).generator(new CustomChunkGen()));
     
  5. iFamasssxD
    Hmm.. I'll try that. Thanks for the suggestion, I'll update this post with the result.
     
  6. Offline

    GusGold

    Assist
    Yeah, well and truly possible, but I would suggest making a server admin do it, as you would need to reload the server after setting it, and other inconvenient stuff. Just put a warning saying what to put into their bukkit.yml on load up.

    iFamasssxD
    Will this overwrite an existing world of the same name? or will it create another one?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  7. Offline

    iFamasssxD

    Not 100% sure. I dont think this will overwrite a current worlds.
     
  8. Offline

    GusGold

    iFamasssxD
    So if bukkit.yml world is set to 'world' and I use
    Code:java
    1. World world = Bukkit.getServer().createWorld(new WorldCreator("world").environment(Environment.NORMAL).generator(new CustomChunkGen()));
    on a startup loading plugin, will Bukkit generate the world, 'world', based on CustomChunkGen()?
     
  9. GusGold
    Well as I said, I (nor the server owners) know the names of the worlds yet, I'm generating new worlds using the plugin.
     
  10. Offline

    GusGold

    Assist
    At what point do you know the name, because then iFamasssxD 's method sounds the way to go.
     
  11. GusGold
    I know the name when I create the world. This is what I'm using at the moment, though I haven't tested it yet since I'm trying to figure out something else.
    Code:java
    1. String worldName = "null";
    2.  
    3. public RealmCreator(String worldName) {
    4. this.worldName = worldName;
    5. }
    6.  
    7. public void createWorld() {
    8. if (!(worldName.equalsIgnoreCase("") || worldName.equalsIgnoreCase("null"))) {
    9. if (Bukkit.getWorld(worldName) != null) {
    10. Bukkit.getServer().createWorld(new WorldCreator(worldName).environment(Environment.NORMAL).generator(new RealmGenerator()));
    11. } else {
    12. Bukkit.getLogger().severe("Unable to generate realm: world already exists.");
    13. }
    14.  
    15. } else {
    16. Bukkit.getLogger().severe("Unable to generate realm: world name is null.");
    17. }
    18. }
    19.  
    20. public String getName() {
    21. return worldName;
    22. }
     
  12. Offline

    GusGold

    Assist
    Just a note
    Code:java
    1. null != "null"
    , so where you check if your world name is equal to "null", you should be check that
    Code:java
    1. worldName == null


    Assist
    But that code looks functional enough, though I have never used the .createWorld() before :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  13. Oops :oops:. Thanks.

    Edit: I assume you're talking about this line:
    Code:java
    1. if (Bukkit.getWorld(worldName) != null) {
    That was wrong, but the line above it was correct (hence the !)
     
Thread Status:
Not open for further replies.

Share This Page