Registering Events

Discussion in 'Spigot Plugin Development' started by _Unbannable, Jul 4, 2015.

  1. Hello! I need some help with a plugin that I'm currently creating.

    In my main class this is how I call all of my other classes.
    Code (Text):
        public void onEnable(){
            instance = this;
            getLogger().info("Plugin has been enabled!");
            getServer().getPluginManager().registerEvents(new PlayerJoin(), getInstance());
            getServer().getPluginManager().registerEvents(new PlayerListener(), getInstance());
            getServer().getPluginManager().registerEvents(new AnimalGUI(), getInstance());
            getServer().getPluginManager().registerEvents(new PlayerInvis(), getInstance());
            Bukkit.getPluginManager().registerEvents(this, this);

    And in a Listener class that I made I'm trying to run Runnable.
    Code (Text):
     List<Player> nametagCooldown = new ArrayList<Player>();

        public void onPInteract(PlayerInteractEvent e)

            final Player p = e.getPlayer();

            if (p.getItemInHand().getType() == Material.NAME_TAG) {
                if (!(nametagCooldown.contains(p)))
                    p.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Kappa" + ChatColor.AQUA + ChatColor.BOLD + " >> " + ChatColor.GOLD + "This feature has been temporarily disabled!");

                    Bukkit.getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {

                        public void run() {

                    }, 60L);

                }else p.sendMessage(ChatColor.RED + "You need to wait 3 seconds to do that!");

    But I get a red line under "plugin". I looked on video and the guy said use this line of code.
    Code (Text):
        Core plugin;
        public AnimalGUI(Core instance) {
            plugin = instance;
    and put it in the Listener class and that fixes the error but then I get a new error in the main class and it won't call the class anymore.

    So I guess the big question is how do I get them to connect so I don't get either error.
  2. JustThiemo

    JustThiemo Previously TrinityDEV

    It should work.
    I use it the same way:
    Code (Text):
    private MainClass plugin;

    public MyClass(MainClass instance) {
    this.plugin = instance
    But what error do you get?
  3. After you add the constructor in the listener classes that take your plugin as an argument, you need to actually pass the plugin to the class when you create an instance. E.g. In you main class, instead of new PlayerJoin(), you will need new PlayerJoin(this)