WorldGuard check "pvp" flag?

Discussion in 'Plugin Development' started by isleepzzz, Jan 12, 2015.

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

    isleepzzz

    Hey guys!
    I am using WorldGuard and I want to check if the player entity is inside a region and if the region has "pvp" disabled.
    I have no idea how to do this! 0.0
    Can someone help me please? :c
     
  2. Offline

    unrealdesign

    First you need to get the WorldGuard plugin. Here is a simple method to get it.
    Code:
    private WorldGuardPlugin getWorldGuard() {
        Plugin plugin = getServer().getPluginManager().getPlugin("WorldGuard");
        // WorldGuard may not be loaded
        if (plugin == null || !(plugin instanceof WorldGuardPlugin)) {
            return null; // Maybe you want throw an exception instead
        }
        return (WorldGuardPlugin) plugin;
    }
    Next you use a simple method to get another WorldGuard class that is iteratable
    Code:
    ApplicableRegionSet arSet = WGBukkit.getRegionManager(world).getApplicableRegions(location);
    Then you have to iterate through all the regions then iterate through all the players and see if it's inside the region.
    Code:
    for(ProtectedRegion pRegion : arSet)
    {
        BlockVector min = pRegion.getMinimumPoint();
        BlockVector max = pRegion.getMaximumPoint();
       
        for(Player p  : Bukkit.getServer().getOnlinePlayer())
        {
            Location loc = p.getLocation();
            if(min.getX() <= loc.getX() && max.getX() >= loc.getX())
                if(min.getY() <= loc.getY() && max.getY() >= loc.getY())
                    if(min.getZ() <= loc.getZ() && max.getZ() >= loc.getZ())
                        //Player is inside the region
        }
    }
    
    If you want to get a region at a specific point, you'd use
    Code:
    Vector pt = toVector(block); // This also takes a location
    
    RegionManager regionManager = worldGuard.getRegionManager(world);
    ApplicableRegionSet set = regionManager.getApplicableRegions(pt);
    
    I tested nothing and is all just an example. This will give you all the needed information for what is needed.
     
  3. Offline

    NathanWolf

    I will add that I recently ran into an issue in WG6 ("allows" is deprecated now), I think this is now the best way to query for the PVP flag:

    Code:
    set.queryState(null, DefaultFlag.PVP) != StateFlag.State.DENY
    This queries for the PVP state - if you have a Player instance there is a way to pass it in in case you have player-specific flags set. Or you can pass in null just to check a region.

    This will assume PvP is allowed inside a region without the flag set at all- which is probably what you want.

    I'll also comment that I don't think you need the middle part of unrealdesign's suggestion, iterating over all players and all regions like that. That's probably something you should avoid doing for performance reasons!

    I think if you want to check for player-specific flags, you can look into the first parameter to queryState, but I am not sure because I haven't done that myself.
     
Thread Status:
Not open for further replies.

Share This Page