Registering events from items set in config

Discussion in 'Spigot Plugin Development' started by MrFishCakes, Apr 23, 2017.

  1. Hello,
    Thanks for the people that helped yesterday.
    I now have an issue when registering my events, it doesn't seem to register them for any method I have tried. If someone could direct me in the right area on how to do this that would be amazing. Here is the code I have so far:
    Code (Java):
    @EventHandler(priority = EventPriority.MONITOR)
        public void punish(InventoryClickEvent e) {
            Inventory inv = e.getInventory();
            Player p = (Player) e.getWhoClicked();
            if (!inv.getTitle().equals(config.getString("GUI.Main.Name"))
                    || !inv.getName().equals(config.getString("GUI.Main.Name"))) {
                return;

            } else if (inv.getTitle()
                    .equals(config.getString("GUI.Main.Name").replace("%target%", PunishCommand.target.getName())
                            .replace("%reason%", PunishCommand.reason).replace("&", "§"))) {
                e.setCancelled(true);
                ItemStack item = e.getCurrentItem();
                if (item == null || item.getType() == Material.AIR || !item.hasItemMeta()) {
                    p.closeInventory();
                    return;
                }
                for (int i = 54; i < 54; i++) {

                    String itemName = item.getItemMeta().getDisplayName();
                    if (config.getString("GUI.Items." + i + ".Name").equals(itemName)) {
                        String command = config.getString("GUI.Items." + e.getSlot() + 1 + ".Command")
                                .replace("%target%", PunishCommand.target.getName())
                                .replace("%reason%", PunishCommand.reason);
                        p.performCommand(command);
                        p.closeInventory();
                        e.setCancelled(true);
                    }
                }
            }
        }

    Thank you in advance.
     
  2. 1. Create Main Class where create method onEnable onDisable implement JavaPlugin..
    2. Create Events Class put your code + implement it listener
    3. Go to Main class put to onEnable
    Code (Text):
    Bukkit.getPluginManager().registerEvents(new Events(), this);
    Finish
     
  3. I've done that already. Nothing triggers
     
  4. Show me full code of Main class, Events class and plugin.yml
     
  5. Code (Java):
    package me.mrfishcakes.punishmentgui;

    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;

    import me.mrfishcakes.punishmentgui.command.PunishCommand;
    import me.mrfishcakes.punishmentgui.events.PunishEvent;

    public class PunishmentGUI extends JavaPlugin {
       
        public static PunishmentGUI plugin;
       
        public void onEnable() {
            plugin = this;
            getCommand("punish").setExecutor(new PunishCommand());
            getCommand("punishgui").setExecutor(new PunishCommand());
            Bukkit.getServer().getPluginManager().registerEvents(new PunishEvent(), this);
            saveDefaultConfig();
        }

    }
     
    Code (Java):
    package me.mrfishcakes.punishmentgui.events;

    import org.bukkit.Material;
    import org.bukkit.configuration.Configuration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;

    import me.mrfishcakes.punishmentgui.PunishmentGUI;
    import me.mrfishcakes.punishmentgui.command.PunishCommand;

    public class PunishEvent implements Listener {

        static Configuration config = PunishmentGUI.plugin.getConfig();

        @EventHandler(priority = EventPriority.MONITOR)
        public void punish(InventoryClickEvent e) {
            Inventory inv = e.getInventory();
            Player p = (Player) e.getWhoClicked();
            if (!inv.getTitle().equals(config.getString("GUI.Main.Name"))
                    || !inv.getName().equals(config.getString("GUI.Main.Name"))) {
                return;

            } else if (inv.getTitle()
                    .equals(config.getString("GUI.Main.Name").replace("%target%", PunishCommand.target.getName())
                            .replace("%reason%", PunishCommand.reason).replace("&", "§"))) {
                e.setCancelled(true);
                ItemStack item = e.getCurrentItem();
                if (item == null || item.getType() == Material.AIR || !item.hasItemMeta()) {
                    p.closeInventory();
                    return;
                }
                for (int i = 54; i < 54; i++) {

                    String itemName = item.getItemMeta().getDisplayName();
                    if (config.getString("GUI.Items." + i + ".Name").equals(itemName)) {
                        String command = config.getString("GUI.Items." + e.getSlot() + 1 + ".Command")
                                .replace("%target%", PunishCommand.target.getName())
                                .replace("%reason%", PunishCommand.reason);
                        p.performCommand(command);
                        p.closeInventory();
                        e.setCancelled(true);
                    }
                }
            }
        }

    }
     
    Code (YAML):
    main: me.mrfishcakes.punishmentgui.PunishmentGUI
    name
    : PunishmentGUI
    version
    : 5.0
    author
    : MrFishCakes
    description
    : Punish players in a fancy GUI.

    commands
    :
        punish
    :
          description
    : Open up the GUI.
          aliases
    : [p, punishment]
        punishgui
    :
          description
    : Main command.
     
  6. Error in console? so where is onDisable ?
     
  7. Forgot to add that in thanks but still doesn't work and no console errors.
     
  8. Extends :p

    Check if the listener catches the event.

    Code (Text):
    @EventHandler(priority = EventPriority.MONITOR)
        public void punish(InventoryClickEvent e) {
            System.out.println("yes, we catched it");
            Inventory inv = e.getInventory();
            Player p = (Player) e.getWhoClicked();
            if (!inv.getTitle().equals(config.getString("GUI.Main.Name"))
                    || !inv.getName().equals(config.getString("GUI.Main.Name"))) {
                return;

            } else if (inv.getTitle()
    If it is printed in the console, check if the plugin is enabled successfully.
     
  9. We are catching it, now I just need to have the command execute and we are all done. Best way to do this would be to do it how?
     
  10. getSever().dispatchCommand(getServer().getConsoleSender, "command goes here");
     
  11. Command not firing for some reason? We cancel the item moving and that is it. Here is the current code:
    https://paste.md-5.net/neqetekewa.avrasm
     
  12. Debug more. Does
    if (itemName.equals(name)) {

    ? To be sure, print something.


    if (itemName.equals(name)) {
    System.out.println("it does equal");
     
  13. Prints out nothing into console at that point.
     
  14. Try stripping the itemname of colors. ChatColor.stripColor(itemName).equals(name). The chatcolors might be conflicting.
     
  15. Striping still doesn't do anything. :/
     
  16. no real need for an else-if statement at the top. code would execute as follows. ex:

    Code (Java):

    if (statement == 2) {
         A();
         return;
    }
    if (statement == 3) {
         B();
         return;
    }

    or
    if (statement == 2) {
         A();
         return;
    }
    //anything that isn't 2
    B();
     
  17. I'm still not having the commands executed
     
  18. debug.

    check to what extent the code is running
    what is actually being fetched the from the config (the values)