How to fix bug when a tree spawns on the edge of a chunk BlockPopulators

Discussion in 'Plugin Development' started by cwhit0814, Jul 21, 2016.

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

    cwhit0814

    My Block Populator for spawning trees is messed up when a tree spawns on the edge of a chunk Like in the picture below. My grass populator overlays the tree when the tree hangs over another chunk. Is there a way to fix it.

    2016-07-21_18.12.56.png

    Code:
    public class TreePopulator extends BlockPopulator {
    
        public void populate(World world, Random random, Chunk chunk) {
            int realX = chunk.getX() * 16;
            int realZ = chunk.getZ() * 16;
            int realXOff = realX + random.nextInt(10);
            int realZOff = realZ + random.nextInt(10);
         
            world.generateTree(new Location(world, realXOff, world.getHighestBlockYAt(realXOff, realZOff), realZOff), TreeType.TREE);
         
        }
    
    }
    
    Code:
    public class GrassPopulator extends BlockPopulator {
    
        @Override
        public void populate(World world, Random random, Chunk chunk) {
            for (int x = 0; x < 16; x++) {
                for (int z = 0; z < 16; z++) {
                    int realX = x + chunk.getX() * 16; // find the world location of chunk location x
                    int realZ = z + chunk.getZ() * 16;
                    world.getBlockAt(realX, world.getHighestBlockYAt(realX, realZ) - 1, realZ).setType(Material.GRASS);
                    world.getBlockAt(realX, world.getHighestBlockYAt(realX, realZ) - 2, realZ).setType(Material.DIRT);
                    world.getBlockAt(realX, world.getHighestBlockYAt(realX, realZ) - 3, realZ).setType(Material.DIRT);
                }
            }
        }
    
    }
     
  2. Offline

    Zombie_Striker

    @cwhit0814
    There are two options:
    1. Make sure that each block popluator only genreate structures in their own chunk.
    2. Add a for loop check to see if there are any blocks at the locations where the new structure should be. If there are, move the new structure to a new location.
    BTW:
    These values will always be the same. No need to create this 16^2 times. Move them out of the for loops to save memory.
     
  3. Offline

    cwhit0814

    Thanks I will try those.

    Although for the first option how would I do that considering I am using a random to generate the tree because the tree generator generates the tree from the bottom block of wood and doesn't consider the leaves as part of the location.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  4. Offline

    Zombie_Striker

    @cwhit0814
    The average tree has a radius of ~2 blocks, leaves start at ~ 2 blocks above the ground, and is normally 7 blocks tall. You can try making two cuboid regions (the stem and the leaves) and check if there are any blocks within those two areas. For larger trees, those are rarer and are share the same structure as the smaller ones (I.e the math for the small tress should mostly work for the large trees aswell).
     
  5. Offline

    cwhit0814

    Could you give me some sort of example because I'm not totally understanding what you mean.
     
  6. Offline

    Zombie_Striker

    @cwhit0814
    This is psudocode, since we do not like to provide code that can be just copied and pasted (prevent noobs from making new threads because they have no idea what the code does/works.)
    Code:
    //The stem
    int x = x of base of stem
    int z = z of base of stem
    for y = base; y < 2; y++
    if there is a block there
      do not create tree. (return)
    
    //The leaves
    
    for X2 = x-2; X2 < x+2; X2++
    same for Z2
      for Y2 = base+2; Y2 < base+2+5; Y2++
       if there is block at that location
         do not create tree. (return)
    
    //There are no blocks there
    create tree
     
  7. Offline

    cwhit0814

    Alright thanks a ton I will test this now.

    @Zombie_Striker y is an int right so how do I get the Y of the X and Z coordinates would I use world#getHighestBlockYAt(...) to find it right?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 22, 2016
  8. Offline

    Zombie_Striker

  9. Offline

    cwhit0814

    @Zombie_Striker If I wanted to overlay the stone with grass without using a populator that would fix the problem right? Like set the blocks on the Y to Grass, Dirt, Dirt then make the tree populator generate the trees without problems because if I use the grass populator you can see the stone for a bit then it overlays with grass and dirt. Just noticed that kindof problem when generating new chunks.

    @Zombie_Striker

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

Share This Page