1.16.5 GUI won't run commands on click

Discussion in 'Spigot Plugin Development' started by abstractionAlpha, Aug 3, 2020.

  1. I'm trying to create a pretty simple GUI. It works when I type the command "/gui", but once in the GUI, it doesn't run the command I have specified that gives back the raw slot of the item - instead, I just pick up the item with my cursor where it becomes draggable. I'm using the SpigotMC "Creating a GUI Inventory" tutorial for reference.

    Here's my main class:

    Code (Text):
    public final class MapsGUI extends JavaPlugin {

        @Override
        public void onEnable() {
            saveDefaultConfig();
            getServer().getPluginManager().registerEvents(new Listeners(), this);
            getServer().getPluginManager().registerEvents(new launchGUI(), this);
        }

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("gui")) {
                if (getConfig().getBoolean("mapChosen") == true) {
                    sender.sendMessage(ChatColor.RED + "Map already chosen!");
                } else {
                    launchGUI gui = new launchGUI();
                    gui.openInventory((HumanEntity) sender);
                    return true;
                }
            }
            return false;
        }

        @Override
        public void onDisable() {
            saveConfig();
        }
    }
    Here's my launchGUI class:

    Code (Text):
    public class launchGUI implements Listener {
        private final Inventory main;

        public launchGUI() {
            main = Bukkit.createInventory(null, 9, "Main");
            initializeItems();
        }

        public void initializeItems() {
            main.addItem(createGuiItem(Material.OAK_SAPLING, "Map Selection", "┬žaVote on which map you'd like to play"));
            main.addItem(createGuiItem(Material.GLASS_PANE, "Map Size", "┬žaVote on how tightly packed you'd like the map to be"));
        }

        protected ItemStack createGuiItem(final Material material, final String name, final String... lore) {
            final ItemStack item = new ItemStack(material, 1);
            final ItemMeta meta = item.getItemMeta();

            meta.setDisplayName(name);
            meta.setLore(Arrays.asList(lore));
            item.setItemMeta(meta);
            return item;
        }

        public void openInventory(final HumanEntity ent) {
            ent.openInventory(main);
        }

        @EventHandler
        public void onInventoryClick(final InventoryClickEvent e) {
            if (e.getInventory() != main) return;
            e.setCancelled(true);
            final ItemStack clickedItem = e.getCurrentItem();

            if (clickedItem == null || clickedItem.getType() == Material.AIR) return;
            final Player p = (Player) e.getWhoClicked();

            p.sendMessage("You clicked at slot " + e.getRawSlot());
        }

        @EventHandler
        public void onInventoryClick(final InventoryDragEvent e) {
            if (e.getInventory() == main) {
                e.setCancelled(true);
            }
        }

    }
    If someone could point to what I'm doing wrong, I'd appreciate it. The guide might just be outdated and I'm too new to this to know the current standards.
     
  2. You need to register the created 'launchGUI' instance as listener via the PluginManager. Otherwise your event handler methods won't get called on inventory events.
     
  3. Declare the 'LaunchGUI' instance that you register in onEnable as a global variable.
    Then you can use this global variable in the onCommand method to open the inventory for the player.

    It is important that you are only using one instance because otherwise your check "e.getInventory() != main" is failing.