WorldGuardAPI getting if a player is in a region ?

Discussion in 'Plugin Development' started by iAmGuus, Nov 7, 2014.

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


    Hi Guys,
    I got a question about the worldguard api, how do i get all the players that are in a region ?
    So far i got this already :
    2. @EventHandler(priority = EventPriority.HIGHEST)
    3. public void onInteractBlock(PlayerInteractEvent e) {
    4. Player p = e.getPlayer();
    5. WorldGuardPlugin guard = Main.getWorldGuard();
    6. RegionManager manager = guard.getRegionManager(p.getWorld());
    7. ApplicableRegionSet region = manager.getApplicableRegions(p.getLocation());
    8. if (isKitItem(p.getItemInHand())) {
    9. for (ProtectedRegion set : region) {
    10. if(set.getId() == "spawn") {
    11. e.setCancelled(true);
    12. }
    13. }
    14. }
    15. }

  2. Offline


    iAmGuus i recommend checking the java docs at

    First you need to get the plugin since you seem to already have implemented it in your plugin and can access it via Main.getWorldGuard() this is already done.

    To access a region you need to get the RegionManager. That you already did but you used p.getWorld() which is not the region id.

    If i understand you correctly you want to check if there is someone in a specific reagion called spawn. and if the have a kitItem in hand then the event PlayerInteractEvent should be canceled.

    You seem to not have read the WorldGuard Wiki which is really bad...

    Do that now and you will find your answer. Make use of RegionManager and its flags. If you want to restrict "use" of a specific item or all interaction with doors for example you can simply set a flag for the region when you create it. For that you dont need a plugin!

    WorldGuard API is used, when you want to control WorldGuard behavior like players can interact with specific items in a region that has the flag set "pvp false" which would not allow pvp.

    You would write a plugin that temporarily sets the flag to a player (checkout Region flags) so he can do pvp in a specific area when he enters a command or attacs the owner for example.

    Just some thoughts on the code you posted. Dont take this personally its just something you should consider.

    Your code would always fetch the instance of worldguard when someone interacts with something and fetch the RegionManager from it ... this could produce lag.

    If you need to check that constantly then you should consider passing the RegionManager directly to the event so its available inside that class. That way you would not waste time to do that on interact everytime only once when the class is created.

    Lets say you have a Event class called PlayerListener ... modify the constructor so that it would look something like this...

    public class PlayerListener implements Listener {
        private RegionManager regionManager;
        public PlayerListener(RegionManager regionManager) {
            this.regionManager = regionManager;
        @EventHandler(priority = EventPriority.NORMAL)
        public void onPlayerInteractEvent(PlayerInteractEvent e) {
            this.regionManager.getRegion(id).getMembers().contains(playerName); // checks if a specific playerName is in a specific regionId (Note: id and playerName have to be replaced with the correct assignment. This is just pseudocode
    This is just written from my mind so please check that with the current API. Also do not use HIGHEST since you need to give other plugin a chance to react to the event. Usually NORMAL is ok. If you want highest you want to override all other plugins which is bad in my opinion.

    For further details check the bukkit wiki on how to create propper EventListeners.

    Anyways the idea is to use WorldGuard so what i described here would not really make use of the WorldGuard API. WorldGuard can handle it on its own since each region has states so called "flags set". Those are either per region or can be set per player so what you need is to simply not allow "use" in that reagion. Either for a specific item or for everything. "use" is mapped to interact and controls if you can use doors or buttons for example.

    So checkout the wiki ... Yes you need to read the API since it has many features build in that you need. If you use the API always check first if the API already can do that for you instead of trying to write your own.

    If you want to disallow "use" in an area spawn then you might just want to set the flag for the region not the player. This is something you would do only once when you install worldguard.
    flash1110 and 97WaterPolo like this.
  3. Offline



    +1 Best response I've read ever.
    lycano likes this.
  4. Offline


    Thanks for the reply lycano, I'd understand what you have just written there, I'll try to fix it and hopefully it works. :D

    The thing is, the people cant hit each other in the spawn area (done with a WG Flag), i just want them to not let them use their kit items in a spawn area. So that's what i was trying to do :D
Thread Status:
Not open for further replies.

Share This Page