Problem registering events

Discussion in 'Spigot Plugin Development' started by suitinacow, Jul 10, 2015.

  1. Code (Text):
      getServer().getPluginManager().registerEvents(new AutoRespawn(pluginHandler));
    this is in the onEnable()
    and the autorespawn class looks like:
    Code (Text):
    package events;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;

    import io.github.suitinacow.spawntypes.PluginHandler;

    public class AutoRespawn implements Listener{
     
        private final PluginHandler pluginHandler;
        private final JavaPlugin plugin;
     
        public AutoRespawn(final JavaPlugin plugin, final PluginHandler pluginHandler)
        {
            this.pluginHandler = pluginHandler;
            this.plugin = plugin;
        }
       
    it gives me an error on the registerEvents..


    EDIT: my source code is here: https://drive.google.com/file/d/0B8p1H7uShx32WUpPbUVoTmlsSWM/view?usp=sharing
     
    #1 suitinacow, Jul 10, 2015
    Last edited: Jul 11, 2015
  2. JamesJ

    Supporter

    Needs to be
    Code (Text):
      getServer().getPluginManager().registerEvents(new AutoRespawn(this, pluginHandler));
     
  3. I think you've missed an argument. Try:
    getServer().getPluginManager().registerEvents(listener, plugin);

    Where listener & plugin the objects you want, plz don't put listener and plugin in your code xd
     
  4. when i change it to yours it asks me to do what @devcexx said and then it doesnt work because i have an instance of pluginHandler instead of plugin.. because im using pluginHandler for a class
     
  5. But what type is PluginHandler?? Because to registerEvents method the first parameter should be implementation of Listener and the second one a class that inherits JavaPlugin
     
  6. PluginHandler is a class in my plugin
     
  7. But now what parameters are passing to registerEvents?
     
  8. I might have confused myself because for my commands i use
    Code (Text):
    getCommand("spawn").setExecutor(new Spawn(pluginHandler));
    the spawn class has the oncommand call and pluginhandler has the rest of the methods and it works fine that way.. I'm trying to do the same thing with the event listener
     
  9. javaPlugin and pluginHandler if i do it this way:
    Code (Text):
    getServer().getPluginManager().registerEvents(new AutoRespawn(null, pluginHandler), null);
    according to quick fix
     
  10. It could be. Both methods don't have the same syntax
     
  11. getServer().getPluginManager().registerEvents(new {ClassName}(this),this);



     
  12. The second null should be an indtancd of your plugin, an instance of the main class defined in your plugin.yml
     
  13. that still, for some reason, tells me to change it back to
    Code (Text):
    getServer().getPluginManager().registerEvents(new AutoRespawn(this, pluginHandler),this);
    which may work??
     
  14. I think so. If you arent getting any compilation issues give it a try. Quickly im running out of battery xD
     
  15. try it! If it does then congrats it works ^_^
     
  16. it didnt crash on load but i cant recieve any damage and /kill caused an error
    Code (Text):
    java.lang.IllegalArgumentException: Plugin cannot be null
     
    ... im pretty sure a registerEvent method wouldn't cause this
     
  17. my class for event listener looks like:
    Code (Text):
    package events;

    import org.bukkit.entity.Player;  
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;

    import io.github.suitinacow.spawntypes.PluginHandler;

    public class AutoRespawn implements Listener{
       
        private final PluginHandler pluginHandler;
        private final JavaPlugin plugin;
       
        public AutoRespawn(final JavaPlugin plugin, final PluginHandler pluginHandler)
        {
            this.pluginHandler = pluginHandler;
            this.plugin = plugin;
        }  
       
        @EventHandler(priority = EventPriority.HIGHEST)
        public void damage(final PlayerDeathEvent event){
            try{
                if(event.getEntity() instanceof Player){
                        final Player player = (Player) event.getEntity();
                        if(player.isDead())
                        {
                            player.spigot().respawn();
                            new BukkitRunnable() {
                            public void run() {
                                player.teleport(pluginHandler.getSpawn(player.getWorld()));
                                player.sendMessage("You fucking died");
                            }                  
                        }.runTaskLater(plugin, 20);
                    }
                }
            }catch (Exception x){
                x.printStackTrace();              
            }  
        }
    }
     
    main looks like:
    Code (Text):
    package io.github.suitinacow.spawntypes;
    import org.bukkit.plugin.java.JavaPlugin;
    import commands.SetSpawn;
    import commands.Spawn;
    import commands.Wild;
    import events.AutoRespawn;

    public class Main extends JavaPlugin{  
       
        private PluginHandler pluginHandler;
       
        public void onEnable(){  
            saveDefaultConfig();
            getConfig().options().copyDefaults(true);
              saveConfig();
             
            getCommand("wild").setExecutor(new Wild(pluginHandler));
            getCommand("setspawn").setExecutor(new SetSpawn(pluginHandler));
            getCommand("spawn").setExecutor(new Spawn(pluginHandler));
            getServer().getPluginManager().registerEvents(new AutoRespawn(this, pluginHandler),this);
        }
       
        public void onDisable(){
            pluginHandler = null;
            saveConfig();      
        }

    }
     
    anything look incorrect?
     
  18. Yes if you're passing a null object. But with your code it shoudn't occur
     
  19. Could u update us on ur class?
     
  20. A question... where are you setting the value of pluginHandler?