GUI won't perform actions by click

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

Thread Status:
Not open for further replies.
  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:
    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:
    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.
     
    Last edited: Aug 3, 2020
  2. Offline

    caderapee

    @abstractionAlpha

    launchGUI gui = new launchGUI();

    Create this in a global variable in your main class, andv dun register twice the same class.

    then you can do on your command : player.openInventory(gui.main)

    And remove the keyword final in the listener method
     
Thread Status:
Not open for further replies.

Share This Page