[Solved][WorldGuard] Get the region a player is standing in.

Discussion in 'Plugin Development' started by Shuski, Dec 19, 2011.

Thread Status:
Not open for further replies.
  1. Title pretty much says it all.

    How do you check if a player is within a region?
     
  2. Offline

    halley

    Checking if a player is IN a region is a different question than your title. A player may be IN six overlapping regions, and you have to figure out all the consequences of that.

    Have you looked into whether WorldGuard has a public API to answer this question?
     
  3. Offline

    hatstand

    WorldGuard's API can do this, though there isn't extensive documentation on it. Basic stuff, like hooking into it, is covered on SK's wiki.
     
  4. Offline

    halley

    Code:
    import com.sk89q.worldguard.protection.managers.RegionManager;
    import com.sk89q.worldguard.protection.ApplicableRegionSet;
    import static com.sk89q.worldguard.bukkit.BukkitUtil.*;
    
    ...
    
    public boolean isWithinRegion(Player player, String region)
        { return isWithinRegion(player.getLocation(), region); }
    
    public boolean isWithinRegion(Block block, String region)
        { return isWithinRegion(block.getLocation(), region); }
    
    public boolean isWithinRegion(Location loc, String region)
    {
        WorldGuardPlugin guard = getWorldGuard();
        Vector v = toVector(loc);
        RegionManager manager = guard.getRegionManager(loc.getWorld());
        ApplicableRegionSet set = manager.getApplicableRegions(v);
        for (ProtectedRegion each : set)
            if (each.getId().equalsIgnoreCase(region))
                return true;
        return false;
    }
    
    Untested, and adapted from code found on the WorldGuard API wiki page and the WorldGuard Javadocs.
     
    rsod likes this.
  5. This worked great!! Thank you very much!

    Another question @halley


    Instead of the player's position. How would you check if a WorldEdit's selection (or selected blocks) are within the region?

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

    halley

    You are asking if two shapes overlap or intersect: the selected cuboid and the region. It would be easy to make partial solutions for just cuboids. If you want to handle all possible regions, because regions can be more complicated than a cuboid, a complete solution would have to visit every block of the cuboid. To find out if any of the selection is in the given region, visit every block and stop as soon as you find any block inside the region. To find out if the whole selection fits within the region, you would do the same, but you can stop as soon as you find any block outside the region.
     
  7. Yeah. I was going to mention. Since it's only cuboids being handled; should make this easier.

    So you can check if a WorldEdit's (cuboid) selection is within a WG (cuboid) region?
    If I read that correctly. If so, how? If you don't mind sharing.

    Check 1 little cuboid 50 x 50 x VERT is inside 1 big region. 1000 x 1000 x VERT
     
  8. Offline

    halley

    Finding if cuboids overlap is pretty easy if you think about it. Draw it out on graph paper, and come up with some basic rules. This is pseudocode, I'm not going to code it all out for you.

    Code:
    figure out the lowest and highest X of the selection's two corners
    figure out the lowest and highest X of the region's two corners
    if the cuboid's highest X is less than the region's lowest X, they don't overlap at all
    if the cuboid's lowest X is greater than the region's highest X, they don't overlap at all
    otherwise they overlap on the X axis
    
    If they overlap on the X and on the Z and on the Y axes, then they overlap.
     
  9. Sweet. I'll give it a go. Cheers for the help.
     
Thread Status:
Not open for further replies.

Share This Page