Listener class is empty?

Discussion in 'Spigot Plugin Development' started by Strainj1, May 26, 2017.

  1. ok so I've hit another snag - I found the following code to set up an Event Listener , but i can't seem to make Listener work

    Code (Text):

          import org.bukkit.event.Listener;

          public class MyListener implements Listener {
                @Override
                public void onEnable() {
                        getServer().getPluginManager().registerEvents(new MyListener(), this);
                }
          }
     
    Eclipse tells me that onEnable must override a supertype method - so I understand that to mean that Listerner needs to have a method called onEnable in it, in order to overwrite it - fair enough.... so I open the Listener class to see what i did wrong - and it seems to be empty, no methods or anything :

    Code (Text):

            package org.bukkit.event;

            /**
            * Simple interface for tagging all EventListeners
            */
            public interface Listener {}
     

    so it's almost like Listener doesn't even exist? There are no methods available when i open it - so maybe i don't have it implemented properly? - Odd cause the rest of the bukkit libraries seem to work fine? And Listener even comes up as a valid class to use, just with no methods....
     
  2. Choco

    Moderator

    It's because Listener is used to identify that your class is listening for events. It provides no methods that need to be implemented. The only reason it exists is to hold an instance of it through the use of Polymorphism with the rest of the listeners :D If you want access to onEnable(), your main class must extend JavaPlugin as well.
     
    • Like Like x 1
  3. Also, to explain, Eclipse is telling you that to override a method, there has to be a "supertype" method (a method of the same name and arguments in the super class, which in this case is Listener since it is being implemented). There is no such method as "onEnable" in the Listener class, and there is your error. "onEnable" is a method only of the JavaPlugin super class (when you extend it with your main class) and is why it works there and only there.
     
  4. The listeners in Main class dont work you need create a other class to use Events
     
  5. long story short, you need to extend JavaPlugin.
     
  6. ok so i have changed the class to

    Code (Text):
    import java.io.Console;

    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    public class MyListener extends JavaPlugin implements Listener {
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(new MyListener(), this);
         
            Bukkit.broadcastMessage("This is a global broadcast message");
         
        }
    }
    which has taken away my errors.

    So if i'm understanding correctly, Listener just detects usage of the particular object it has been implemented on? - So if this is the case, how do i make the main class talk to this? - how do i run things? from what i can tell - i can only make the main JavaPlugin do things using methods like onEnable and onCommand - but how do i "listen" to events, such as a player moving, or breaking a block or killing an animal or whatever....

    I was hoping that creating an instance of MyListener onEnable of the main class would do something, but it did not.

    Code (Text):

    public class main extends JavaPlugin{
        @Override
        public void onEnable() {
          MyListener listener = new MyListener();
        }

        @Override
        public void onDisable() {

        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
          if (command.getName().equalsIgnoreCase("/MyCommand")) {
            sender.sendMessage("Ok so you ran the /MyCommand command");
            return true;
          }
          return false;
        }
       
    }
     
    I assume i need a method in my main class that on some kind of action, lets say a player moves....... can create the listener object and then do something? but what is that method? in the JavaPlugin I don't see any applicable methods?
     
  7. Your dumb:
    Main
    Code (Text):
    public class Main extends JavaPlugin{
        @Override
        public void onEnable() {
          getServer().getPluginManager().registerEvents(new MyListener(), this);
        }

        @Override
        public void onDisable() {

        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
          if (command.getName().equalsIgnoreCase("MyCommand")) {
            sender.sendMessage("Ok so you ran the /MyCommand command");
            return true;
          }
          return false;
        }
     
    }
     
    MyListener
    Code (Text):
    import org.bukkit.event.Listener;

    public class MyListener implements Listener {

        @org.bukkit.event.EventHandler
        public void hello(org.bukkit.event.player.PlayerJoinEvent event) {
            for(int i = 0; i < 25; i++) event.getPlayer().sendMessage("You joined, now deal with this annoying message");
        }
    }
     
  8. Yes they do.

    Strainj1 http://bukkit.gamepedia.com/Event_API_Reference#Adding_the_listener
    Code (Text):
    public class MyPlugin extends JavaPlugin implements Listener {
      @Override
      public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
      }

      @EventHandler
      public void onLogin(PlayerLoginEvent event) {
        getLogger().log(Level.INFO, "Player " + event.getPlayer().getName() + " is logging in!");
      }
    }
     
  9. If your onDisable is empty you might aswell just not override it.
     
    • Agree Agree x 1
  10. Also, if you're going to call someone dumb, you should at least be able to use the English language correctly.

    @OP
    Your main class is the only class that needs to extend JavaPlugin. That is how your plugin communicates with Bukkit. You do not need two, nor will it work as intended.

    To explain more, the Listener class is empty because it allows the server to manage a bunch of different classes that are Listeners. The server can't possibly know every class that is going to be a Listener, so you implement Listener and the server knows when you register it. It has no methods because it calls the events through reflection. It looks through the class for methods that contain the handler "EventHandler" and has the arguments of one singular event. So if the server finds a method that meets those standards, it'll essentially "write it down" and call it whenever that particular event in the arguments is called.

    For the two classes to "communicate", that is a rather entry level of Java knowledge so you should probably learn more of the basics of Java. You need to store your secondary class (the Listener class) as a "global" variable in the main class. That will allow the main class to communicate with the secondary class. If you need it vice versa or both ways, you need to pass the main class through the constructor and store the argument from the constructor as a "global" variable in the secondary (the Listener) class.