How do I generate a sphere with a custom ChunkGenerator?

Discussion in 'Plugin Development' started by ocomobock, Nov 14, 2017.

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

    ocomobock

    I've been trying to solve this problem for days, so if anyone feels like reading through this and trying to help me I would be extremely appreciative.

    I want to make a cave with a ChunkGenerator (not a BlockPopulator.) I am going to do this by making a pattern of spheres. I just can't figure out how to even make a sphere using the ChunkGenerator class. Here's what I have:

    Code:
    
    public class CTChunkGenerator extends ChunkGenerator
    {
       Random r = new Random();
        void setBlock(int x, int y, int z, byte[][] chunk, Material material)
        {
            if (y < 256 && y >= 0 && x <= 64 && x >= 0 && z <= 64 && z >= 0)
            { 
                if (chunk[y >> 4] == null)
                chunk[y >> 4] = new byte[16 * 16 * 16];
                chunk[y >> 4][((y & 0xF) << 8) | (z << 4) | x] = (byte) material.getId();
            }
        }
        private short getBlock(int x, int y, int z, byte[][] chunk) {
            if (y < 256 && y >= 0 && x <= 16 && x >= 0 && z <= 16 && z >= 0) { 
                if (chunk[y >> 4] == null)
                    return 0;
                return chunk[y >> 4][((y & 0xF) << 8) | (z << 4) | x];
            }
            else return 0;
        }
       
    public byte[][] generateBlockSections(World world, Random rand, int ChunkX, int ChunkZ, BiomeGrid biome)
        {
            byte[][] chunk = new byte[world.getMaxHeight() / 16][];
            int rx = ChunkX*16;
                for (int x = 0; x < 16; x++)
                {
                    for (int z = 0; z < 16; z++)
                    {
                        for (int y = 0; y < 256; y++)
                        {
                            if(r.nextInt(20)==0 && x == 8 && z == 8)
                                genSphere(x, y, z, chunk, 4);
                        }
                    }
                }
            return chunk;
        }
    
       
        public void genSphere(int xx, int yy, int zz, byte[][] chunk, double radius)
        {
            for(int x = (int) (xx - radius); x < (int)(xx + radius); x++)
            {
                for(int z = (int) (zz - radius); z < (int)(zz + radius); z++)
                {
                    for(int y = (int) (yy - (radius / 2)); y < (int)(yy + (radius / 2) - 1); y++)
                    {
                        if(y >= 0 && y <= 255)
                        {
                            if(getBlock(x, y, z, chunk) !=7 && distance(x, y, z, xx, yy, zz) < radius)
                            {
                                setBlock(x, y, z,chunk,Material.AIR);
                            }
                            else if(getBlock(x,y,z,chunk)==7)
                                break;
                           
                           
                        }
                    }
                }
            }
        }
       
        public static double distance(int x1, int y1, int z1, int x2, int y2, int z2)
        {
            return Math.sqrt(Math.pow((x1-x2), 2) + Math.pow((y1-y2), 2) + Math.pow((z1-z2), 2));
        }
    So to quickly summarize all of that code, I've got the setBlock() and getBlock() methods which are self explanatory, I've got generateBlockSections() which generates each chunk, I've got the genSphere() method which actually makes the sphere, and I have the distance() method which is used in the genSphere() method.

    Now here's the problem: when I generate a sphere in generateBlockSections(), the sphere will usually go outside the chunk. It seems like everything in the generateBlockSections() method has to be generated inside the chunk, so it needs to be within x=0, x=16, z=0, and z=16. If I place anything outside of this range, it will cause an error.

    Here's the error I get:

    Code:
    [13:24:12] [Server thread/ERROR]: Error occurred while enabling Gloop v1.0 (Is it up to date?)
    net.minecraft.server.v1_12_R1.ReportedException: Exception generating new chunk
        at net.minecraft.server.v1_12_R1.WorldServer.a(WorldServer.java:917) ~[craftbukkit.jar:git-Bukkit-53fccdf]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.createWorld(CraftServer.java:892) ~[craftbukkit.jar:git-Bukkit-53fccdf]
        at ct.Main.onEnable(Main.java:41) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[craftbukkit.jar:git-Bukkit-53fccdf]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:329) [craftbukkit.jar:git-Bukkit-53fccdf]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:401) [craftbukkit.jar:git-Bukkit-53fccdf]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:370) [craftbukkit.jar:git-Bukkit-53fccdf]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:331) [craftbukkit.jar:git-Bukkit-53fccdf]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.reload(CraftServer.java:731) [craftbukkit.jar:git-Bukkit-53fccdf]
        at org.bukkit.Bukkit.reload(Bukkit.java:534) [craftbukkit.jar:git-Bukkit-53fccdf]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [craftbukkit.jar:git-Bukkit-53fccdf]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) [craftbukkit.jar:git-Bukkit-53fccdf]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:631) [craftbukkit.jar:git-Bukkit-53fccdf]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchServerCommand(CraftServer.java:617) [craftbukkit.jar:git-Bukkit-53fccdf]
        at net.minecraft.server.v1_12_R1.DedicatedServer.aP(DedicatedServer.java:408) [craftbukkit.jar:git-Bukkit-53fccdf]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:372) [craftbukkit.jar:git-Bukkit-53fccdf]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:651) [craftbukkit.jar:git-Bukkit-53fccdf]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:555) [craftbukkit.jar:git-Bukkit-53fccdf]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_144]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: -192
        at org.bukkit.craftbukkit.v1_12_R1.generator.CustomChunkGenerator$CustomBiomeGrid.setBiome(CustomChunkGenerator.java:29) ~[craftbukkit.jar:git-Bukkit-53fccdf]
        at ct.CTChunkGenerator.generateBlockSections(CTChunkGenerator.java:44) ~[?:?]
        at org.bukkit.craftbukkit.v1_12_R1.generator.CustomChunkGenerator.getOrCreateChunk(CustomChunkGenerator.java:105) ~[craftbukkit.jar:git-Bukkit-53fccdf]
        at net.minecraft.server.v1_12_R1.ChunkProviderServer.originalGetChunkAt(ChunkProviderServer.java:159) ~[craftbukkit.jar:git-Bukkit-53fccdf]
        at net.minecraft.server.v1_12_R1.ChunkProviderServer.getChunkAt(ChunkProviderServer.java:140) ~[craftbukkit.jar:git-Bukkit-53fccdf]
        at net.minecraft.server.v1_12_R1.ChunkProviderServer.getChunkAt(ChunkProviderServer.java:120) ~[craftbukkit.jar:git-Bukkit-53fccdf]
        at net.minecraft.server.v1_12_R1.ChunkProviderServer.getChunkAt(ChunkProviderServer.java:116) ~[craftbukkit.jar:git-Bukkit-53fccdf]
        at org.bukkit.craftbukkit.v1_12_R1.CraftWorld.loadChunk(CraftWorld.java:261) ~[craftbukkit.jar:git-Bukkit-53fccdf]
        at org.bukkit.craftbukkit.v1_12_R1.CraftWorld.loadChunk(CraftWorld.java:155) ~[craftbukkit.jar:git-Bukkit-53fccdf]
        at org.bukkit.craftbukkit.v1_12_R1.CraftWorld.getHighestBlockYAt(CraftWorld.java:104) ~[craftbukkit.jar:git-Bukkit-53fccdf]
        at org.bukkit.generator.ChunkGenerator.canSpawn(ChunkGenerator.java:270) ~[craftbukkit.jar:git-Bukkit-53fccdf]
        at net.minecraft.server.v1_12_R1.WorldServer.canSpawn(WorldServer.java:258) ~[craftbukkit.jar:git-Bukkit-53fccdf]
        at net.minecraft.server.v1_12_R1.WorldServer.b(WorldServer.java:980) ~[craftbukkit.jar:git-Bukkit-53fccdf]
        at net.minecraft.server.v1_12_R1.WorldServer.a(WorldServer.java:902) ~[craftbukkit.jar:git-Bukkit-53fccdf]
        ... 18 more
    [13:24:12] [Server thread/INFO]: [WorldEdit] Enabling WorldEdit v6.1.8-SNAPSHOT;cd4729f
    Again, if anybody has any insight into what I could try, that would be amazing. Thanks in advance
     
  2. Offline

    MightyOne

    @ocomobock didnt you figure it out yourself? you can not generate a chunk and set blocks outside of it. or?
     
  3. Offline

    ocomobock

    Yeah, basically I'm trying to figure out how to work around this though. How would I make a cave if that would mean going outside of the chunk? That's what I'm trying to figure out.

    I don't want to use a BlockPopulator, because ChunkGenerators are usually used for larger structures such as caves.
     
  4. Offline

    MightyOne

    well for the sphere you can just take (xxoryyorzz +- radius) and look if it is another chunk than (xxoryyorzz). every chunk coordinate can be found out with (int) (num / 16). I hope so. idk.. try it out?
     
  5. Offline

    Zombie_Striker

    @ocomobock
    You will need to set up some mathematical formula that turns the X and Z values into whether or not there is a cave at that location. That's what seeds are for.
     
  6. Offline

    ocomobock

    Yeah, I'll try something along these lines. Thank you

    I'm sorry but I don't understand this.
     
    Last edited: Nov 14, 2017
Thread Status:
Not open for further replies.

Share This Page