1.15.2 PlayerMendingEvent

Discussion in 'Spigot Plugin Development' started by Richardjmanning, Jan 19, 2020.

  1. Hello guys, I am trying to listen to the player mending event for when an Item get well mended.
    My code below should just print out a count of EXP but does nothing.

    I am not sure why, the plugin says it is loaded.
    but when exp changes nothing happens and when an item get mended nothing happens.

    Code (Text):
    public class BetterMending extends JavaPlugin implements Listener{
        private static Plugin plugin;
          public static Economy econ = null;

        public void onEnable() {
            plugin = (Plugin) this;
            RegisterEcon();
            registerEvents((Plugin) this, new Listener[] {

            });
        }

        public void onDisable() {
            plugin = null;
        }

        public static void registerEvents(Plugin plugin, Listener... listeners) {
            Listener[] arrayOfListener;
            int j = (arrayOfListener = listeners).length;
            for (int i = 0; i < j; i++) {
                Listener listener = arrayOfListener[i];
                Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
            }
        }

        public static Plugin getPlugin() {
            return plugin;
        }
       
       
        public static void RegisterEcon() {
            if (Bukkit.getPluginManager().getPlugin("Vault") != null) {
                RegisteredServiceProvider<Economy> service = Bukkit.getServicesManager().getRegistration(Economy.class);
                if (service != null)
                    econ = (Economy) service.getProvider();
            }
        }
       

        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerItemMend(PlayerItemMendEvent event) {
            System.out.println("Count:" + event.getExperienceOrb().getExperience());
        }
       
       

        @EventHandler(priority = EventPriority.HIGHEST)
        void onPlayerExpChange(PlayerExpChangeEvent event) {
            System.out.println("EXP Count:" + event.getAmount());
        }
     
  2. You are calling your register-method, but there's nothing in it... you are passing an empty array. Also, you can write that method a lot simpler:

    Code (Java):
    public NOSTATICPLEASE void registerEvents(JavaPlugin plugin, Listener... listeners) {
        for (Listener listener: listeners) {
            Bukst.getServer().getPluginManager().registerEvents(listener, plugin);
        }

    }
    In your onEnable-method, you want to exchange this:

    Code (Java):
    registerEvents((Plugin) this, new Listener[] {

            });
    by this:
    Code (Text):
    registerEvents((Plugin) this, this);
    I would recommend creating a new class for listeners that belong together.
    Also, in your onDisable-method, you don't need to set your plugin to null. Event priority Highest should probably not be used, since there is no real need, but if you want it, go for it.
     
  3. I've cleaned up your code a little bit.

    Code (Java):
    public class BetterMending extends JavaPlugin implements Listener {

        private Economy economy;

        public void onEnable() {
            if (this.getServer().getPluginManager().getPlugin("Vault") != null) {
                RegisteredServiceProvider<Economy> service = this.getServer().getServicesManager().getRegistration(Economy.class);
                if (service != null) {
                    this.economy = (Economy) service.getProvider();
                }
            }
            this.getServer().getPluginManager().registerEvents(this, this);
        }

        public void onDisable() {
            this.economy = null;
        }

        @EventHandler
        public void onPlayerItemMend(PlayerItemMendEvent event) {
            System.out.println("Count:" + event.getExperienceOrb().getExperience());
        }

        @EventHandler
        public void onPlayerExpChange(PlayerExpChangeEvent event) {
            System.out.println("EXP Count:" + event.getAmount());
        }
    }
    This should be all you need for the plugin that you are making...

    If you need to expand it to have multiple listeners. Create a static instance variable and provide a getter method, also initialize that instance variable in your onEnable() and set it to null in your onDisable()
    Code (Java):

    private static BetterMending instance;

    public void onEnable() {
        instance = this;
    }

    public void onDisable() {
        instance = null;
    }

    public static BetterMending getInstance() {
        return instance;
    }
     
     
    #3 ceezuns, Jan 19, 2020
    Last edited: Jan 19, 2020
    • Agree Agree x 1