Amount of blocks in a ProtectedRegion (WorldGuard)

Discussion in 'Spigot Plugin Development' started by GermanElectronix, May 28, 2015.

  1. Hello,

    currently I'm working on a plots plugin with per-block-prices.
    To calculate these prices I have to know how many blocks (including AIR) there are in a
    Does anyone know how to realize this? :)
    • Like Like x 1
    • Agree Agree x 1
  2. @GermanElectronix you can probably still compute the surface from the points, and multiply it with the height. How? No clue, google probably knows :p.
  3. Actually I just need the area. I already found this method:
    Code (Text):
    int blocks = ProtectedRegion.volume() / 256;
    It's working for cuboid but for polygonal regions it returns 0. I could calculate the area with vectors, but I have no idea how I could do that ^^
    #5 GermanElectronix, May 28, 2015
    Last edited: May 28, 2015
  4. That's not working for me. It returns wrong values.
    Code (Text):
    ProtectedRegion reg;
            float sum_but_no_result=0;

            for(int i=0; i<(reg.getPoints().size()-1); i++) {
                sum_but_no_result += reg.getPoints().get(i).getBlockX() * reg.getPoints().get(i + 1).getBlockZ() + reg.getPoints().get(i).getBlockZ() * reg.getPoints().get(i + 1).getBlockX();
            sum_but_no_result += reg.getPoints().get(reg.getPoints().size()-1).getBlockX() * reg.getPoints().get(0).getBlockZ() + reg.getPoints().get(reg.getPoints().size()-1).getBlockZ() * reg.getPoints().get(0).getBlockX();
            float sum = (Math.abs(sum_but_no_result) / 2.0f) / 256;
            System.out.println("BLOCKS: " + String.valueOf(sum));
  5. @GermanElectronix you could always pick an arbitrary point in the polygon, split the polygon up in triangles, compute the area of each triangle and sum the results.

    Or (and this is probably a pretty precise but slow method) recursively find all blocks in a slice of the polygon (so where all y is equal).
    Code (pseudo (Unknown Language)):
    // This assumes the initial call uses a block inside the polygon
    findArea(block, visited)
        found = 1
        for each neighbour
            if visited contains neighbour
            add neighbour to visited
            found = found + findArea(neighbour, visited)
        return found
    Note: Java wise, visited should be a HashSet for best performance
    #8 DarkSeraphim, May 28, 2015
    Last edited: May 28, 2015
  6. I finally found a way to compute the area of polygons (learned at school ^^):
    Code (Text):
    public static int getArea(final List<BlockVector2D> points){
            int[] x = new int[points.size()];
            int[] z = new int[points.size()];
            for(int i = 0; i<points.size(); i++){
                x[i] = points.get(i).getBlockX();
                z[i] = points.get(i).getBlockZ();
            int firstSum = 0;
            for(int i = 0; i<points.size()-1; i++)
                firstSum = x[i]*z[i+1];
            int secondSum = 0;
            for(int i = 0; i<points.size()-1; i++)
                secondSum = z[i]*x[i+1];
            firstSum -= secondSum;
            return firstSum / 2;
    The problem is, that this method is returning wrong values. Maybe, ProtectedRegion.getPoints(); is returning an unsorted list?