Solved Check if enter world guard region

Discussion in 'Spigot Plugin Development' started by TheSupreme, Apr 17, 2017.

  1. How can I check and see if a player enters a world guard region without PlayerMoveEvent?
     
  2. HashMap with uuid and current regions -> bukkit runnable: check player location region -> check if it is different from their past regions -> do something -> update their current region in the map.
     
    • Funny Funny x 1
  3. Unfortunately I'm not quite sure of the worldguard API. Would someone mind providing an example of checking to see if a player is in a region?
     
  4. http://wiki.sk89q.com/wiki/WorldGuard/Regions/API

    All you had to do was google worldguard API...
     
  5. I feel like, as a programmer, you should know that Google is your best friend and how to use it for research. These kind of forums are great, but questions like "Unfortunately I'm not quite sure of the worldguard API. Would someone mind providing an example of checking to see if a player is in a region?" is just silly. APIs are usually very well documented, and a simple google search should give you a link (or multiple) where all the information you need is.
     
    • Like Like x 1
    • Agree Agree x 1
  6. I know that and trust me I have googled.
    When I said I'm unsure of the api I simply mean I don't understand it enough to use it.

    A good example of what I mean is this: You read the Manuel for your car. You get an idea of how it's supposed to work but until you actually put it into practice you really won't know.

    An example of checking a world guard region would be enough for me to put the missing piece of a puzzle in place. I'm not a good programmer but at the same time I am not a bad programmer.
     
  7. Fair enough. The first thing you want to find on an API to know how to use it is its documentation. An API without documentation is not really an API for others to use (although you could always look into the source code and figure it out yourself, but that's just nono). So, google for it. First link and you find the documentation on the regions. At the top, it is a disclaimer which redirects you to a proper documentation for WG 6+. And just by looking for 5 minutes in there, I managed to scrap this together:

    Hastebin or Pastebin or raw code below:
    Code (Text):
    import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
    import com.sk89q.worldguard.protection.managers.RegionManager;
    import com.sk89q.worldguard.protection.regions.ProtectedRegion;
    import org.bukkit.Bukkit;
    import org.bukkit.World;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;

    /**
    * Project created by ExpDev
    */

    public class WgTest {

        /**
         * Executes when the programs runs
         */
        public static void main(String[] strings) {
            // initializing class so I can use the getWorldGuard method
            WgTest wgTest = new WgTest();

            // creating a Player object from my name :)
            Player p = Bukkit.getPlayer("ExpDev");
            // checking if the player exists/is online
            if(p == null) {
                // they aren't. aborttttt
                System.out.println("Player is not online or does not exist");
                return;
            }

            // getting the world guard plugin
            WorldGuardPlugin wg = wgTest.getWorldGuard();
            // checking whether or not the dependency is installed
            if(wg == null) {
                // it ain't. you might wanna disable the plugin then
                System.out.println("World Guard plugin is not to be found. Disable plugin if it is dependant on it.");
                return;
            }

            // the world we will be looking in
            World w = Bukkit.getWorld("world");

            // getting the region manager
            RegionManager manager = manager = wg.getRegionManager(w);;
            // the region you want to look for the player in. "region" is the id/name of the region you are looking for. would be the one used to create it
            ProtectedRegion region = manager.getRegion("region");

            // checking if the given region exists or not
            if(region == null) {
                // doesn't exist. do nothing
                System.out.println("That region does not exist.");
                return;
            }

            // our true/false result of whether or not the player's position is contained in the region
            boolean inRegion = region.contains(wg.wrapPlayer(p).getPosition());
            // printing out whether it is or not
            System.out.println("Player " + p.getName() + " is in region " + region.getId() + ": " + inRegion);
        }

        /**
         * Get the world guard plugin instance
         * @return The world guard plugin instance (null if not found)
         */
        private WorldGuardPlugin getWorldGuard() {
            Plugin plugin = Bukkit.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;
        }

    }
    Please note that I have not tested this but it should work alright!

    Hope this helped. And please do read the comments so you can understand it.
     
    • Winner Winner x 1
    • Useful Useful x 1
  8. That is precisely what I needed, Thank you!
    Edit: Actually, it is and it isn't. Is there no way to find the ID/Name of the region you're currently standing in?
     
    #8 TheSupreme, Apr 17, 2017
    Last edited: Apr 17, 2017
  9. I would assume so, one second. Checking for it now.

    @TheSupreme this would suffice:
    Code (Java):
    // wrapping the player and getting its position again
    Vector pos = wg.wrapPlayer(p).getPosition();

    // pre initialization of the region
    ProtectedRegion isIn = null;

    // method 1
    ApplicableRegionSet regionSet = manager.getApplicableRegions(pos);
    for(ProtectedRegion r : regionSet) {
        if(r.contains(pos)) {
            isIn = r;
            break;
        }
    }

    if(isIn == null) {
        // simply meaning that regionSet is empty. Player is in no region i'd assume
        System.out.println("Could not find region player is located in");
        return;
    }

    System.out.println("Player is in region: " + isIn.getId());
    However, you have to take into consideration that the player might be located in multiple regions at the same time (all which are applicable), hence a set. So doing the same, but looping through the RegionManager#getRegions() map would probably be more accurate. I can't completely remember, but think it might return the parent of all the regions the player is in.

    However, you could always just call ProtectedRegion#getParent() on isIn as that would return the parent ProtectedRegion. Using ProtectedRegion#getId() on that new ProtectedRegion should give you the name/id :).

    EDIT:
    This is just pure spoon feeding. There might be a easier approach though, like a method to get all the regions the player is in. However, this is just that, kinda. Instead we just compare its position and get all the applicable regions, looping through them and getting the name of the parent.

    EDIT2:
    So by using this, you wouldn't really need that previous code. As this would return your region. Initially I thought you just wanted to check if a player is in a specific region (hence using the id/name). But with this, you will get the region the player is currently in, which is what you wanted, right?
     
    #9 ExpDev, Apr 17, 2017
    Last edited: Apr 17, 2017
    • Winner Winner x 1
  10. PERFECT !

    Thank you so much, very appreciated. :D

    I have a long list of region names that I need to check, so I will be comparing the region they're currently in with one in a list. :)
     
  11. That's the way to go. No problem! Glad to help.
     
  12. inTerritory is always equal to null.

    Code (Text):
        public static Boolean inTerritory(Player player)
        {  
            Vector pos = Plugins.getWorldGuard().wrapPlayer(player).getPosition();
            World w = Bukkit.getWorld("world");
            com.sk89q.worldguard.protection.managers.RegionManager manager = manager = Plugins.getWorldGuard().getRegionManager(w);
            // pre initialization of the region

            ProtectedRegion inTerritory = null;

            ApplicableRegionSet regionSet = manager.getApplicableRegions(pos);
            for(ProtectedRegion r : regionSet) {
                if(r.contains(pos)) {
                    inTerritory = r;
                    break;
                }
            }
           
            PlayerObject playerObject = Main.Players.get(player.getName());

            if(inTerritory == null) {
                if (playerObject.lastTerritory != "")
                    {
                    player.sendMessage("Leaving Territory " + playerObject.lastTerritory + "!");
                    playerObject.lastTerritory = "";
                    }
                return false;
            }

            /* Check region */
            if (!TerritoryManager.Territories.containsKey(inTerritory.getId()))
                return false;
           
          /* Get Territory*/
            TerritoryObject territoryObject = TerritoryManager.Territories.get(inTerritory.getId());
           
            if (playerObject.lastTerritory != territoryObject.getName())
                player.sendMessage("Entering " + territoryObject.getName() + "!");
           
            playerObject.lastTerritory = territoryObject.getName();
           
            if (playerObject.getClanId() == territoryObject.getOwner())
            {
                playerObject.canBuild = true;
                playerObject.inOwnTerritory = true;
                return true;
            }
           
            playerObject.canBuild = false;
            playerObject.inOwnTerritory = false;
            return true;
        }
    Code (Text):
    public class Plugins {

        public static WorldGuardPlugin getWorldGuard()
        {
            Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("WorldGuard");
           
            if (plugin == null || !(plugin instanceof WorldGuardPlugin)){
                return null;
            }
            return (WorldGuardPlugin) plugin;
        }
       
        public static WorldEditPlugin getWorldEdit()
        {
            Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
           
            if (plugin == null || !(plugin instanceof WorldEditPlugin)){
                return null;
            }
            return (WorldEditPlugin) plugin;
        }
    }
    inTerritory is being called here.
    Code (Text):
    public class Update {

        public static void update()
        {
            for(Player p : Bukkit.getServer().getOnlinePlayers()) {
                RegionManager.isIn(p);
                PlayerObject playerObject = Main.Players.get(p.getName());
               
                if (!ClanManager.Clans.containsKey(playerObject.getClanId())){
                    playerObject.setClan("");
                }
            }
        }
    }
     
    Update is called here.
    Code (Text):
    public class Main extends JavaPlugin implements Listener {
       
        @Override
        public void onEnable()
        {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this, this);
           
             new BukkitRunnable() {
         
                    @Override
                    public void run() {
                        Update.update();
                    }
                }.runTaskTimer(this, 0, 20);
        }
    }
     
  13. Could be because world guard is null. Do some debugging. Check if the regionmanager is null for example.
     
  14. Edit: Code Removed.
     
    #14 TheSupreme, Apr 17, 2017
    Last edited: Apr 18, 2017
  15. Found my issue, it was pathetically simple. Thanks again for your help!