Inventory Menu

Discussion in 'Plugin Development' started by MasterDoctor, Nov 7, 2015.

Thread Status:
Not open for further replies.
  1. Offline


    I opened an inventory menu but when I close it with ESC it doesn't destroy the listener - this causes me a problem! Any ideas?
  2. Offline


    Gee, it would be nice to know what the problem is.
    Jakeeeee and mcdorli like this.
  3. Offline


    Hmm... I think there is something wrong with your code. You might want to post it if you want help.
  4. Offline


  5. Offline


    Maybe you should try posting your code, only way I could help you.
  6. Offline


    if (event.getPosition() == 0 || event.getPosition() == 8 || event.getPosition() == 45
                            || event.getPosition() == 53) {
                        // Close Menu
                    } else {
                        // Do not close menu
    IconMenu class
    package com.goldblockstudios.masterdoctor;
    * IconMenu class by nisovin!
    import java.util.Arrays;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.HandlerList;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    public class IconMenu implements Listener {
        private String name;
        private int size;
        private OptionClickEventHandler handler;
        private Plugin plugin;
        private String[] optionNames;
        private ItemStack[] optionIcons;
        public IconMenu(String name, int size, OptionClickEventHandler handler, Plugin plugin) {
   = name;
            this.size = size;
            this.handler = handler;
            this.plugin = plugin;
            this.optionNames = new String[size];
            this.optionIcons = new ItemStack[size];
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        public IconMenu setOption(int position, ItemStack icon, String name, String... info) {
            optionNames[position] = name;
            optionIcons[position] = setItemNameAndLore(icon, name, info);
            return this;
        public void open(Player player) {
            Inventory inventory = Bukkit.createInventory(player, size, name);
            for (int i = 0; i < optionIcons.length; i++) {
                if (optionIcons[i] != null) {
                    inventory.setItem(i, optionIcons[i]);
        public void destroy() {
            handler = null;
            plugin = null;
            optionNames = null;
            optionIcons = null;
        @EventHandler(priority = EventPriority.MONITOR)
        void onInventoryClick(InventoryClickEvent event) {
            if (event.getInventory().getTitle().equals(name)) {
                int slot = event.getRawSlot();
                if (slot >= 0 && slot < size && optionNames[slot] != null) {
                    Plugin plugin = this.plugin;
                    OptionClickEvent e = new OptionClickEvent((Player) event.getWhoClicked(), slot, optionNames[slot]);
                    if (e.willClose()) {
                        final Player p = (Player) event.getWhoClicked();
                        Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                            public void run() {
                        }, 1);
                    if (e.willDestroy()) {
        public interface OptionClickEventHandler {
            public void onOptionClick(OptionClickEvent event);
        public class OptionClickEvent {
            private Player player;
            private int position;
            private String name;
            private boolean close;
            private boolean destroy;
            public OptionClickEvent(Player player, int position, String name) {
                this.player = player;
                this.position = position;
       = name;
                this.close = true;
                this.destroy = false;
            public Player getPlayer() {
                return player;
            public int getPosition() {
                return position;
            public String getName() {
                return name;
            public boolean willClose() {
                return close;
            public boolean willDestroy() {
                return destroy;
            public void setWillClose(boolean close) {
                this.close = close;
            public void setWillDestroy(boolean destroy) {
                this.destroy = destroy;
        private ItemStack setItemNameAndLore(ItemStack item, String name, String[] lore) {
            ItemMeta im = item.getItemMeta();
            return item;

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    EDIT: Sorry my web browser glitched out
  7. Offline


    @MasterDoctor The same thing happened to me before. It was because I was creating the inventory every time someone opened it, which means it registered a new listener every time someone opened it. To fix it, just have one single inventory, and show that when someone opens it.

    In short: Don't create the object each time you want to show it.
    MasterDoctor likes this.
  8. Offline


    Ohh, Thanks very much!
    EDIT: @567legodude where do I create the menu then?
    Last edited: Nov 7, 2015
Thread Status:
Not open for further replies.

Share This Page