Worldguard - Methods - Listener

Discussion in 'Plugin Development' started by Phibedy, Feb 22, 2012.

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

    Phibedy

    Hi,
    I am a new try and error developer, that´s why I got a silly Problem.
    I am writing an little bank plugin and now I want to add the locations.
    Problem:
    I am not able to use methods from worldguard,
    I want a variable like: Location loc = player.getLocation();
    In Worldguard it should be: Position pos = player.getPosition();
    afterwards it should check:
    if(pos.getApplicablaRegionsIDs().contains("MyRegion"{
    }
    But it just underlines getPosition(), buildpathes are Bukkit + Worldguard + Worldedit.
    I am developing for about 3 days, so please be patient.

    Which Listener would you use to get an output if someone enters a region?
    Is it bad to check every player every 2 seconds instead?


    best regards Phibedy
     
  2. Offline

    bassfader

    I am doing it like this:
    I am getting the instance of WorldGuard in my plugins onEnable() method and store it as public variable. In the Listener (or any other) Class I store an instance of my plugin ("handed over" to the Class via its constructor). Then I am able to access the WorldGuard instance and its methods.

    Here's a simple example which will tell you if a player is in a WorldGuard Region or not when you rightclick him:

    Main Class:
    PHP:
    public class MyPlugin extends JavaPlugin {
        
    // Get the logger
        
    public final Logger log Logger.getLogger("Minecraft");
       
        
    // public variable to store the WorldGuard instance
        
    public WorldGuardPlugin worldGuard null;
       
        @
    Override
        
    public void onDisable() {
            
    log.info("[MyPlugin] MyPlugin v" getDescription().getVersion() + " Disabled");
        }
     
        @
    Override
        
    public void onEnable() {
            
    // Get the WorldGuard instance
            
    worldGuard getWorldGuardPlugin();
           
            
    // Create instance of Listener Class and "hand over" an instance of this Class to it
            
    playerListener = new MyPluginListener(this);
           
            
    log.info("[MyPlugin] MyPlugin v" getDescription().getVersion() + " Enabled");
        }   
       
        private 
    WorldGuardPlugin getWorldGuardPlugin() {       
            
    // Return value variable, by default null
            
    WorldGuardPlugin plugin null;
           
            
    // Try to get the plugin instance
            
    Plugin p getServer().getPluginManager().getPlugin("WorldGuard");
           
            
    // Make sure its not null and its WorldGuard
            
    if ((!= null) && (instanceof WorldGuardPlugin)) {
                
    // Save the instance of WorldGuard in the return value variable;
                
    plugin = (WorldGuardPluginp;
                
    log.info("[MyPlugin] WorldGuard v" p.getDescription().getVersion() + " detected! Enabling WorldGuard Support.");
            }
           
            
    // Return the instance (or null if no WorldGuard found)
            
    return plugin;
        }   
    }
    Listener Class:
    PHP:
    public class MyPluginListener implements Listener {
        
    // instance of the main plugin class
        
    private final MyPlugin plugin;
     
        public 
    MyPluginListener(MyPlugin instance) {
            
    // Save the instance of the main plugin class
            
    plugin instance;
           
            
    // Register events
            
    plugin.getServer().getPluginManager().registerEvents(thisplugin);
        }
       
        @
    EventHandler(priority EventPriority.MONITOR)
        public 
    void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
            if (
    event.getRightClicked() instanceof Player) {
                
    // Tell the player if the RightClicked Player is in a region or not
                
    if (isPlayerInRegion((Playerevent.getRightClicked())) {
                    
    event.getPlayer().sendMessage("Player is in a WorldGuard Region");
                } else {
                    
    event.getPlayer().sendMessage("Player is NOT in a WorldGuard Region");
                }
            }
        }
       
        private 
    boolean isPlayerInRegion(Player player) {
            
    // Make sure that a WorldGuard instance was found
            
    if (plugin.worldGuard != null) {
                
    // Get the players position (as a WorldEdit Vector)
                
    Vector playerVector = new Vector(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ());       
               
                
    // Get WorldGuards Region Manager
                
    RegionManager rm plugin.worldGuard.getRegionManager(player.getWorld());
               
                
    // Make sure a RegionManager was found
                
    if (rm != null)    {
                    if (
    rm.getApplicableRegions(playerVector).size() > 0) return true;
                }
            }
            
    // No region found at players position
            
    return false;
        }
    }
     
  3. Offline

    Phibedy

    Thx helped me alot

    best regards Phibedy
     
Thread Status:
Not open for further replies.

Share This Page