Solved How can I add Listener?

Discussion in 'Plugin Development' started by liaosunny, Aug 15, 2017.

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

    liaosunny

    I want to add the Listener.
    What should I do it?
    Should I create two classes?
    Except that things,I want to learn how to make a plug-in for my world. What should I do? Or what steps should be done?
    Thank you for your help.
     
  2. What Listener do you want?

    Normally, you would do something like this:

    Code:
    public class MainClass extends JavaPlugin implements Listener{
    }
    
    This will let you use code such as

    Code:
    @EventHandler
    public void onPlayerChat(AsyncPlayerChatEvent event){
    if(event.getMessage().contains("potato"){
    event.getPlayer().sendMessage("Potato");
    }
    }
    
    There is also something called a block listener. I recommend you research here.
     
    Last edited: Aug 15, 2017
  3. Offline

    A5H73Y

    Yes you would ideally have a separate class to separate the events logically, for example I have PlayerListener and VehicleListener etc; But you can be as specific as you'd like.
    Create your class that implements the Listener interface.
    Make sure you register that new class:
    Code:
    (plugin).getServer().getPluginManager().registerEvents(new PlayerListener(), (plugin)); 
    All events must have a @EventHandler annotation for them to work with Bukkit.
     
    Last edited by a moderator: Aug 15, 2017
  4. Offline

    liaosunny

    I want one to monitor what the player is doing, such as the player entering an instruction, or destroying the box, and passing it to the main class file.
    I use two classes to Distinguish.
    Then use
    getLogger().info();
    or other code?

    I know
    Code:
    (plugin).getServer().getPluginManager().registerEvents(new PlayerListener(), (plugin)); 
    But I can use it finish my idea.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 15, 2017
  5. Offline

    caderapee

  6. Offline

    liaosunny

Thread Status:
Not open for further replies.

Share This Page