The Bug I Really Cant Squash!!!!

Discussion in 'Plugin Development' started by SkyGlue, Dec 14, 2014.

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

    SkyGlue

    Hello im learning java and i am making this plugin that when a player tries to throw an egg they get kicked (Just Trying Random Stuff) This here is my code...

    This is The main class

    Code:
    package me.SkyGlue;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class EventHandle extends JavaPlugin {
    
    @Override
    public void onEnable() {
        new PlayerListener(this);
    }
    
    @Override
    public void onDisable() {
    
    }
    
    }
    
    
    This Is the PlayerListener class

    Code:
    package me.SkyGlue;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerEggThrowEvent;
    
    public class PlayerListener implements Listener {
       
        public PlayerListener(EventHandle plugin) {
         plugin.getPluginLoader().registerEvents(this, plugin);
           
        }
       
        @EventHandler
        public void onThrow(PlayerEggThrowEvent event) {
           
            Player player = event.getPlayer();
                   
                player.sendMessage(ChatColor.RED + "Uh-Oh. You Threw A Egg. That Is Prohibited On This Server!");
                player.kickPlayer("Woopsy's. You Threw An Egg. You May Rejoin. You Have Been Warned! ");
                                   
        }
       
    }
    

    This is the error i get

    The method registerEvents(PlayerListener, EventHandle) is undefined for the type PluginLoader
     
  2. Offline

    pookeythekid

    @SkyGlue I would suggest registering your events in your onEnable with:
    Code:
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvents(new PlayerListener(), this); // I suggest removing parameters and code from your constructor.
    Also, someone tell me if I'm wrong, but doesn't it do nothing if you just create a random instance of EventHandle in your constructor without it relating to your main class?
     
  3. Offline

    mythbusterma

    @pookeythekid

    Yes, you're wrong on both accounts, there's no reason to use your code over his.


    @SkyGlue

    You need the PluginManager, not the PluginLoader.
     
  4. Offline

    DeadlyScone

    Code:java
    1. package me.SkyGlue;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.PlayerEggThrowEvent;
    8.  
    9. public class PlayerListener implements Listener {
    10.  
    11. public PlayerListener(EventHandle plugin) {
    12. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    13.  
    14. }
    15.  
    16. @EventHandler
    17. public void onThrow(PlayerEggThrowEvent event) {
    18.  
    19. Player player = event.getPlayer();
    20.  
    21. player.sendMessage(ChatColor.RED + "Uh-Oh. You Threw A Egg. That Is Prohibited On This Server!");
    22. player.kickPlayer("Woopsy's. You Threw An Egg. You May Rejoin. You Have Been Warned! ");
    23.  
    24. }
    25.  
    26. }
    27.  
     
    Last edited: Dec 14, 2014
  5. Offline

    SkyGlue

    Ok i have done that but now it gives me this error...
    The method getPluginManager() is undefined for the type EventHandle

    @mythbusterma
     
  6. Offline

    DeadlyScone

    @SkyGlue
    look at my post, I edited it. Should work now.
     
  7. Offline

    Rocoty

    @mythbusterma I can think of several reasons as to why it would be better to register events in onEnable rather than scattering them all over the place.
     
  8. Offline

    Dragonphase

    @Rocoty

    That all comes down to coding preference and standards.
     
  9. Offline

    Rocoty

    @Dragonphase The way I see it it comes down to structure and maintainability. Keeping the registering in a centralised place makes for a tidier structure and it would be easier to maintain in the future.
     
    Last edited: Dec 14, 2014
    nlthijs48, pookeythekid and caelum19 like this.
  10. Offline

    pookeythekid

    *facepalm* Right...
     
  11. Offline

    3ShotGod

  12. Offline

    mythbusterma

    @Rocoty

    I would have them each register in their constructor, just for the cleanliness of the JavaPlugin class, but as long as you're consistent, it isn't really an issue either way.
     
    teej107 likes this.
  13. Offline

    Rocoty

    @mythbusterma That's what I would call giving the object too much work. It would be doing more than it's supposed to. A listener object is only supposed to listen for events and handle them. Giving it the power to register itself defies the Single Responsibility Principle (the 'S' in SOLID). Also, if allowing the object to register itself when it is created is a must, then this should be clearly documented in comments or JavaDocs, as it would by no means denote obvious behavior.

    If you give the main class the responsibility to register all of its own listeners, not only do you abide by the SRP and make your code more maintaniable, you also make your intent a whole lot clearer, which is always a good thing.
     
  14. Offline

    mythbusterma

    @Rocoty

    Fair enough, that's how I've always done it anyway. You make a good argument.
     
    Rocoty likes this.
  15. Offline

    DeadlyScone

  16. Offline

    mythbusterma

    @DeadlyScone

    The point being, Bukkit isn't infallible, and, programmatically, it makes no difference.

    Rocoty brought the idea of limited scope of responsibility of each class, that is it should serve one or a few clearly defined purposes, with a Listener being a class that should listen for events and (optionally) act upon said events within a limited scope. Having it register itself as a listener falls outside that scope, and is much more in line with the goal of your Main class, which, among other things, is to manage dependant classes.
     
  17. Offline

    DeadlyScone

    @mythbusterma
    I indeed follow the argument at foot and understand what you are justifying. I just thought it would be a valid point, or at least an interesting fact to ponder.
     
Thread Status:
Not open for further replies.

Share This Page