Assigning Certain Listeners to Certain Players

Discussion in 'Plugin Development' started by LukesComputers, Apr 27, 2015.

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

    LukesComputers

    Hi I am creating a plugin where listeners have to be assigned to a certain class. I know that a simple solution would be to make a "global" listener class but it would be ineffective for what I am doing. For some reason when I install this on my test server the listeners act as if they are ignored.

    This is my main class:
    Code:
    public class Main extends JavaPlugin {
        public static Main plugin;
    
        public void onEnable() {
        }
    
        public void onDisable() {
    
        }
    
        public boolean onCommand(CommandSender sender, CommandSender command,
                String commandLabel, String[] args) {
            if (commandLabel.equals("test")) {
                    UniqueListener v = new UniqueListener((Player) sender);
            }
            return false;
        }
    }
    Then my UniqueListener class
    Code:
    public class UniqueListener implements Listener {
    
    public UniqueListener(Player player) {
        Bukkit.getServer().getPluginManager().registerEvents(this, Main.plugin);
        }
    @EventHandler
        public void onEntityDamage(EntityDamageByEntityEvent e) {
               e.getEntity().sendMessage("Listener is working");
    
         }
    }
    
    Now when I install the plugin on my server I can assure that the command works but when a player damages a entity it does not display the message
     
  2. Offline

    Koobaczech

    Hey man, register your listener in your OnEnable. Its something like this
    Code:
    Bukkit.getServer().getPluginManager().registerEvents(UniqueListener, this);
     
  3. Offline

    sgavster

    Register your events,
    Use an ArrayList<String> to store the players name check if they are in it to make things only apply for them.
     
Thread Status:
Not open for further replies.

Share This Page