Config & @EventHandler Help

Discussion in 'Spigot Plugin Development' started by ItsWill, Jun 13, 2015.

  1. Code (Text):
    NOT SOLVED
     
    #1 ItsWill, Jun 13, 2015
    Last edited: Jun 13, 2015
  2. Try adding debug messages.
     
  3. Is this your main class of commands class?
     
    #3 toxicmages, Jun 13, 2015
    Last edited: Jun 13, 2015
  4. Any other Ideas?
     
  5. @mansionwill your package name could be in a different project if u have multiple packages with the name 'Main' then it wont work.. (as i heard it)
     
  6. You could add log.info("Works"); in your EventHandlers like @Mysticate suggested. Check console for works
     
  7. Still doesn't work. And that's not the problem. As what you say I should have good practice it works. The problem is when I Extend JavaPlugin in my PlayerListener class (I need to add the JavaPlugin because of the config file) it doesn't work. If I just implement the Listener then it works.
     
  8. JavaPlugin can only be extended by your main class, if you need the config, simply pass your Main classes instance through a constructor to your PlayerListener. i.e

    Code (Text):
    public class PlayerListener implements Listener {
    private Main main;

    public PlayerListener(Main main) {
    this.main = main;
    }
    //main.getConfig() now works.

    }
     
    • Useful Useful x 1
  9. Yess! Thanks, I am going to try this. I sort of knew like it could only be used by the main class, but I didn't know how to use the config. Thanks A Million Man.
     
  10. No problem, let me know if it doesn't work.
     
  11. Code (Text):
    public class PlayerListener implements Listener {
        private Main main;
     
        public PlayerListener(Main main) {
        this.main = main;
        }
        @EventHandler
        public void Click(InventoryClickEvent e) {
            if(!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase(main.getConfig().getString("Wardrobe-Name")))
    Doesn't work:
    Code (Text):
    public class PlayerListener implements Listener {
        private Main main;

        public PlayerListener(Main main) {
        this.main = main;
        }
        @EventHandler
        public void Click(InventoryClickEvent e) {
            if(!ChatColor.stripColor(e.getInventory().getName())
    .equalsIgnoreCase(main.getConfig().getString("Wardrobe-Name")))
    getConfig() turns up red. The main part doesn't.
     
  12. What's the error?
     
  13. getConfig() turns up red
     
  14. Hover over it with your mouse and tell me the error message.
     
    • Useful Useful x 1
  15. The method getConfig() is undefined for the type Main
    Do I need to register events?
     
  16. Your Main class extends JavaPlugin, right? Also, is your main class called "Main"?

    EDIT: Yeah, you'd have to register Events. PluginManager#registerEvents(new PlayerListener(this), this);
     
    • Agree Agree x 1
  17. Yeah. Can you show me how I'd register events :p
    I don't know how to register events that are in a different class.
     
  18. Make sure you registered the event like I just showed, since you made a constructor you'll have to pass the Main class instance through that as well.
     
  19. How would you do that?
     
  20. In your onEnable() put the following.
    Bukkit.getServerr().getPluginManager().registerEvents(new PlayerListener(this), this);