How would I iterate through a CuboidRegion?

Discussion in 'Plugin Development' started by Paul122, Mar 17, 2021.

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

    Paul122

    I've already given a shot, though the method I'm using seems to be causing severe server lag until it crashes. Here is the method I have tried:

    Code:
      public boolean isClear(Location loc, int size) {
            int odX = loc.getBlockX();
            int odY = loc.getBlockY() - 1;
            int odZ = loc.getBlockZ();
            int cornerX = loc.getBlockX() + (size + 1);
            int cornerY = loc.getBlockY() - size - 1;
            int cornerZ = loc.getBlockZ() - (size + 1);
    
            for (int x = odX; x < cornerX; x++) {
                for (int y = odY; y > cornerY; y++) {
                    for (int z = odZ; z > cornerZ; z++) {
                        Block b = new Location(loc.getWorld(), x, y, z).getBlock();
                        if (b.getType() == Material.CAULDRON) {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
    As you can (hopefully) tell, I'm trying to determine whether or not there are any blocks of a certain type within a cuboid region. Unfortunately, this method does cause my server + client to crash moments after it is called. The size integer that is being passed in is the dimensions of the cuboid (every side is the same). Generally speaking, the regions that I'm trying to iterate through tend to be anywhere from 45 blocks to 100 blocks in size.
     
  2. Offline

    davidclue

    It's because you are iterating through a list too big in 1 tick and it can't iterate through it within 1 minute so the server crashes. You will need to use a repeating task that goes through a certain portion of the list every tick, or simply don't call this method with a large size variable. I believe there are more efficient and better ways but this is my idea to you.
     
Thread Status:
Not open for further replies.

Share This Page