Solved Trouble with method to register events

Discussion in 'Spigot Plugin Development' started by Ageforce, Jun 26, 2021.

  1. I'm currently attempting to make a method that registers events with the parameters from the registerEvents() method from bukkit.

    The purpose of the method is to eliminate the DRY usage of several methods.

    This is the code i have so far:
    Code (Java):
    package com.ageforce;

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


    public class Plugin extends JavaPlugin {
        Listener listener;
        Plugin plugin;

        public PluginManager registerEvent(Listener listener, Plugin plugin) {
            this.listener = listener;
            this.plugin = plugin;

            return getServer().getPluginManager().registerEvents(listener, plugin);
        }

        @Override
        public void onEnable() {

            System.out.println("Plugin activated!");

            registerEvent(new CreatureSpawn(), this);
            registerEvent(new PlayerJoin(), this);
        }

        @Override
        public void onDisable() {

            System.out.println("Plugin deactivated");
        }
    }
    upload_2021-6-26_21-22-48.png


    How i should approach the method is unclear to me. I am currently unaware what the return type should be, and what i should even return. Please guide me in the correct direction.

    Any reply is greatly appreciated. Thank you!
     
  2. SteelPhoenix

    Moderator

    The method you declare is supposed to return a PluginManager instance, but #registerEvents(...) returns void, so you're returning void instead of a PluginManager.
     
  3. I already know this, stating the obvious is not really going to help my situation.

    I'm sorry if that sounded rude, i did not intend it to.

    Thanks for the reply.
     
  4. So remove your return statement from that line, then on the next line, return the PluginManager instead. You get it in the first place with getServer().getPluginManager(), so now just do it again and return that.
     
  5. SteelPhoenix

    Moderator

    If you create a method, give it a return type, and then don't know what you should return, I'm lost. It can't be any clearer than that. Maybe go back to learn basics?
     
  6. Wait, if you already know this, why are you returning a method that returns void? As SteelPhoenix said, please learn the basics of Java before making a plugin. Not only will you save your own time in the long run, you save us time from trying to help you fix really basic mistakes.
     
  7. Thank you, your explanation made it much clearer.
     
  8. I have already learnt the basics of Java. It's my first day of trying to learn Spigot, and i just got a little thrown off while working with the Event API.

    If you don't believe me, go take a look at my trello:
    https://trello.com/b/L89PPDbv/programming-goals

    There's a reasonable explanation for my confusion, but it is not necessary to explain it here, since we would just go off topic.
     
  9. Relax man, they're not tryna dis you or making u look dumb, they just prefer u figuring it out instead of giving u the answer without explanation or in simple words they are just tryna help u.