WorldGuard API ApplicableRegions for a chunk

Discussion in 'Spigot Plugin Development' started by Pyrictus, May 1, 2015.

  1. I'm able to get an ApplicableRegionSet of all regions at one specific location. But is it possible to get all ApplicableRegions for a chunk, without having to iterate through every block in the (in this case 2-dimensional) chunk?

    Obviously, rgManager.getApplicableRegions(p.getLocation().getChunk()); does not work.


    Thanks in advance :)
     
  2. gigosaurus

    Supporter

    Looking at the API, there is a getApplicableRegions(ProtectedRegion) method, so that just means we need to create our own ProtectedRegion, which would be the entire chunk.

    We want a ProtectedCuboidRegion (because chunks are cuboid), and that has a constructor (String id, BlockVector pt1, BlockVector pt2). That means we need to create a BlockVector at the lowest point of the chunk, and another at the highest.

    Doing (chunk.getX() << 4) will get the actual X location of the chunk, then adding 15 to that will get the max X point in the chunk.

    Repeat for Z, create the BlockVectors from that, create the ProtectedRegion and pass into the rgManager method, and it will return a ApplicableRegionSet containing all the regions that intersect with this chunk.

    Code (Java):

    import org.bukkit.Chunk;
    import com.sk89q.worldedit.BlockVector;
    import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;

    ...

    Chunk chunk = p.getLocation().getChunk();
    int bx = chunk.getX() << 4;
    int bz = chunk.getZ() << 4;
    BlockVector pt1 = new BlockVector(bx, 0, bz);
    BlockVector pt2 = new BlockVector(bx + 15, 256, bz + 15);
    ProtectedCuboidRegion region = new ProtectedCuboidRegion("ThisIsAnId", pt1, pt2);
    ApplicableRegionSet regions = rgManager.getApplicableRegions(region);
     
     
    #2 gigosaurus, May 1, 2015
    Last edited: May 2, 2015
    • Like Like x 1
    • Winner Winner x 1
    • Informative Informative x 1
  3. Thanks, Sir, it works like a charm!