Advertisement
Guest User

World Guard check if player is in region

a guest
Apr 17th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
  2. import com.sk89q.worldguard.protection.managers.RegionManager;
  3. import com.sk89q.worldguard.protection.regions.ProtectedRegion;
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.World;
  6. import org.bukkit.entity.Player;
  7. import org.bukkit.plugin.Plugin;
  8.  
  9. /**
  10.  * Project created by ExpDev
  11.  */
  12.  
  13. public class WgTest {
  14.  
  15.     /**
  16.      * Executes when the programs runs
  17.      */
  18.     public static void main(String[] strings) {
  19.         // initializing class so I can use the getWorldGuard method
  20.         WgTest wgTest = new WgTest();
  21.  
  22.         // creating a Player object from my name :)
  23.         Player p = Bukkit.getPlayer("ExpDev");
  24.         // checking if the player exists/is online
  25.         if(p == null) {
  26.             // they aren't. aborttttt
  27.             System.out.println("Player is not online or does not exist");
  28.             return;
  29.         }
  30.  
  31.         // getting the world guard plugin
  32.         WorldGuardPlugin wg = wgTest.getWorldGuard();
  33.         // checking whether or not the dependency is installed
  34.         if(wg == null) {
  35.             // it ain't. you might wanna disable the plugin then
  36.             System.out.println("World Guard plugin is not to be found. Disable plugin if it is dependant on it.");
  37.             return;
  38.         }
  39.  
  40.         // the world we will be looking in
  41.         World w = Bukkit.getWorld("world");
  42.  
  43.         // getting the region manager
  44.         RegionManager manager = manager = wg.getRegionManager(w);;
  45.         // the region you want to look for the player in
  46.         ProtectedRegion region = manager.getRegion("region");
  47.  
  48.         // checking if the given region exists or not
  49.         if(region == null) {
  50.             // doesn't exist. do nothing
  51.             System.out.println("That region does not exist.");
  52.             return;
  53.         }
  54.  
  55.         // our true/false result of whether or not the player's position is contained in the region
  56.         boolean inRegion = region.contains(wg.wrapPlayer(p).getPosition());
  57.         // printing out whether it is or not
  58.         System.out.println("Player " + p.getName() + " is in region " + region.getId() + ": " + inRegion);
  59.     }
  60.  
  61.     /**
  62.      * Get the world guard plugin instance
  63.      * @return The world guard plugin instance (null if not found)
  64.      */
  65.     private WorldGuardPlugin getWorldGuard() {
  66.         Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("WorldGuard");
  67.  
  68.         // WorldGuard may not be loaded
  69.         if (plugin == null || !(plugin instanceof WorldGuardPlugin)) {
  70.             return null; // Maybe you want throw an exception instead
  71.         }
  72.         return (WorldGuardPlugin) plugin;
  73.     }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement