Basic void generator?

Discussion in 'Plugin Development' started by flyingtacoz, Sep 2, 2012.

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

    flyingtacoz

    What should a chunk generator that only generates void/air (no terrain.. just nothing) look like?
    Thank you.
     
  2. Offline

    Courier

    Code:java
    1. return new byte[16][];
    EDIT: That is in the generateBlockSections() method.
     
  3. Offline

    escortkeel

    CleanRoomGenerator is your friend.
     
  4. Offline

    flyingtacoz

    Rather have the generator built in my plugin, then using another one. :)
     
  5. Offline

    flyingtacoz

    bump, help please?
     
  6. Offline

    MrFigg

    Code:
    import java.util.Random;
     
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.generator.ChunkGenerator;
     
    public class VoidChunkGenerator extends ChunkGenerator {
        private byte[] chunk = new byte[65536];
       
        public VoidChunkGenerator(String worldname, String uid) {
            for(int x = 0; x < 16; x++) {
                for(int z = 0; z < 16; z++) {
                    for(int y = 0; y < 256; y++) {
                        chunk[(x*16+z)*256+y] = (byte)Material.AIR.getId();
                    }
                }
            }
        }
       
        public byte[] generate(World world, Random random, int cx, int cz) {
            return chunk.clone();
        }
       
        @Override
        public Location getFixedSpawnLocation(World world, Random random) {
            return new Location(world, 0, 128 ,0);
        }
    }
     
  7. Offline

    Courier

    You don't even need to do that much. Your chunk generation method can just return "new byte[16][]". Empty sections of the chunk are treated as air. Also, the generate() method is deprecated. You need to use generateBlockSections().

    Here is the entire class.
    Code:java
    1. import java.util.Random;
    2.  
    3. import org.bukkit.World;
    4. import org.bukkit.generator.ChunkGenerator;
    5.  
    6. public class VoidGenerator extends ChunkGenerator
    7. {
    8. @Override
    9. public byte[][] generateBlockSections(World world, Random random, int x, int z, BiomeGrid biomes)
    10. {
    11. return new byte[16][];
    12. }
    13. }
     
  8. Offline

    flyingtacoz

    Ok thanks guys! Is there a way to set the chunk to a certain biome, when it generates?
     
  9. Offline

    MrFigg

    Courier

    Thanks, been a while sense I even looked at world generation.
     
  10. Offline

    Courier

    Put this in the generateBlockSections() method:
    Code:java
    1. for(int bx = 0; bx < 16; ++bx)
    2. for(int bz = 0; bz < 16; ++bz)
    3. biomes.setBiome(bx, bz, Biome.PLAINS);
    Replace PLAINS with whichever type you want.
    Yeah, MC now deals with chunks in vertical sections of 16 blocks.
     
  11. Offline

    flyingtacoz

    I love you.
    Like this?
    Code:
    @Override
    public byte[][] generateBlockSections(World world, Random random, int x, int z, BiomeGrid biomes) {
    for(int bx = 0; bx < 16; ++bx)
    for(int bz = 0; bz < 16; ++bz)
    biomes.setBiome(bx, bz, Biome.TAIGA);
    {
        return new byte[16][];
    }
    }
    }
     
  12. Offline

    Courier

    Your braces are a little off. Like this:
    Code:java
    1. @Override
    2. public byte[][] generateBlockSections(World world, Random random, int x, int z, BiomeGrid biomes) {
    3. for(int bx = 0; bx < 16; ++bx)
    4. for(int bz = 0; bz < 16; ++bz)
    5. biomes.setBiome(bx, bz, Biome.TAIGA);
    6. return new byte[16][];
    7. }
     
  13. Offline

    flyingtacoz

    Thanks! I was wondering, I do this all the time. Does it/can it affect anything?
     
  14. Offline

    Courier

    Can what affect anything? Do you mean your braces? Well, originally, you had an odd brace at the end. You code looked like this (when formatted with Allman style braces):
    Code:java
    1. public byte[][] generateBlockSections(World world, Random random, int x, int z, BiomeGrid biomes)
    2. {
    3. for(int bx = 0; bx < 16; ++bx)
    4. for(int bz = 0; bz < 16; ++bz)
    5. biomes.setBiome(bx, bz, Biome.TAIGA);
    6. { //<--you don't need this
    7. return new byte[16][];
    8. } //<--and this
    9. }
    10. }//<--the extra brace
    If you have a brace mismatch, like you do because of your last end brace, your code won't even compile. The two braces surrounding your return statement are useless. Standalone braces inside a method are used to define scope; variables declared inside the braces cannot be accessed outside of the braces. There are two reasons you might want to do this:
    1) It can be useful to help the compiler know when you don't need variables anymore, although I have a feeling the Java compiler is intelligent enough that it doesn't usually matter.
    2) It can make code more readable (if done correctly and in moderation), and lets you re-use variable names.
    Example of re-use:
    Code:java
    1. {
    2. int x = 5;
    3. int y = 12;
    4. Bukkit.broadcastMessage(Integer.toString(x * y));
    5. }
    6. {
    7. double x = 5.5;
    8. double y = 11.5;
    9. Bukkit.broadcastMessage(Double.toString(x * y));
    10. }
    Honestly, the feature is not used very often. Most people probably don't even know about it.

    Standalone braces outside of a method are different. The code inside them is executed before every constructor.
    Standalone braces outside a method with the keyword static in front of them are used to run code once the class is first loaded into the JVM.
     
    gamerzap and flyingtacoz like this.
  15. Offline

    Mr. X

    Hey guys!
    I followd these "steps" but my server dont generate void but a normal world!
    I think i have some more to do as only create these classes but what?
     
Thread Status:
Not open for further replies.

Share This Page