Getting if a player is in a WorldGuard region

Discussion in 'Plugin Development' started by eleectricman226, Jan 3, 2015.

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

    eleectricman226

    I've recently been trying to revive someone's old plugin by request of someone else. I've never used the WorldGuard API, and the wiki doesn't explain it very well. Most of the answers on bukkit were from years ago, could anyone help me?
     
  2. Offline

    nj2miami

    Its a many-fold answer honestly and took me much trial and error to do what you are asking. This is ripped from code I wrote for a 1.7.9 plugin, but *should* work on any version.

    You need to hook into WorldGuard first.

    public static WorldGuardPlugin wg = null;

    onEnable() set wg to the result of the following: (you can remove the console spam if you like). It either returns null or the instance of WorldGuard.

    Code:
        private WorldGuardPlugin getWorldGuard() {
            Plugin w = getServer().getPluginManager().getPlugin("WorldGuard");
    
            if ((w == null) || (!(w instanceof WorldGuardPlugin))) {
                //Failed to hook into WorldGuard
                return null;
            }
           //Hooked into WorldGuard
            return (WorldGuardPlugin)w;
        }
    The variable "loc" below is simply the player location you want to check for.

    Code:
            Vector v = new Vector(loc.getX(), loc.getBlockY(), loc.getZ());
            ApplicableRegionSet set = YOUR_PLUGIN.wg.getRegionManager(loc.getWorld()).getApplicableRegions(v);
            Iterator<ProtectedRegion> iter = set.iterator();
            ProtectedRegion region = null;
    
            while(iter.hasNext()) {
                ProtectedRegion nextRegion = iter.next();
                if(region == null || region.getPriority() > region.getPriority()) region = nextRegion;
            }
    The result will populate the variable "region" with the highest priority region a player is in. You can get the region name by calling "region.getId()"

    Hope this helps some.
     
    Last edited by a moderator: Jan 5, 2015
    fromgate likes this.
  3. Offline

    timtower Administrator Administrator Moderator

    Cleaned offtopic conversation.
    Modified code to stop a new offtopic conversation from appearing.
    Keep it nice and on topic please
     
  4. Offline

    DukerHD

    I created a plugin using the WorldGuard API. The way I did this was to check if the region contains the players location. I can't remember my code, but I will try to find it and put it here.
     
  5. Offline

    1Rogue

    @nj2miami

    You shouldn't be storing the WorldGuardPlugin reference in a public static variable. Make it protected or private with an accessor, and consider adding it to a manager (not the main class).
     
  6. Offline

    nj2miami

    That is how the plugin dev for WorldGuard has it in their example.

    @1Rogue What truly is the difference if I make it a private static and reference it through a Getter versus making it publicly accessible? Nothing is going to change the object.
     
Thread Status:
Not open for further replies.

Share This Page