InventoryClickEvent in custom workbench?

Discussion in 'Plugin Development' started by Rprrr, Jan 24, 2013.

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

    Rprrr

    Hi,

    I'm opening a custom workbench for the player by using:
    Code:
        public void openMenu(final Player p){
            p.closeInventory();
            p.openWorkbench(null, true);
            Inventory inv = p.getOpenInventory().getTopInventory();
            inv.setItem(0, iCloseMenu());
           
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                @SuppressWarnings("deprecation")
                @Override
                public void run() {
                    p.updateInventory();
                }
            }, 1L);
        }
    But, when I'm trying to listen for events in that inventory (InventoryClickEvents), I notice they do not get called. Simply not at all.

    I've used debug messages to check this, and yes, I've registered my listener, and yes, I'm 100% the listener works, because I'm using it for other inventory activities aswell (opening the menu), and it works fine then. I basically put a debug-message at the top of the listener code (no 'if'-statements or whatever), and it doesn't even call that message.

    So how can I solve this? Because I really want to listen for those events... but they don't seem to get called. :s

    Thanks in advance!

    Anyone that has an idea? Thanks.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  2. Offline

    nisovin

    Where is this method being called from?
     
  3. Offline

    Rprrr

    nisovin
    It's called from this listener (I was making some kind of server menu by a GUI):
    Code:
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e){
            Bukkit.broadcastMessage("Click. Inventory type: " + e.getInventory().getType());
            if (e.getInventory().getType().equals(InventoryType.CRAFTING)){
                final Player p = (Player) e.getWhoClicked();
                Inventory inv = p.getOpenInventory().getTopInventory();
                if (inv.getItem(1) == null && inv.getItem(2) == null && inv.getItem(3) == null && inv.getItem(4) == null){
                    inv.setItem(0, iOpenMenu());
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                        @SuppressWarnings("deprecation")
                        @Override
                        public void run() {
                            p.updateInventory();
                        }
                    }, 1L);
                }
                if (e.getCurrentItem().equals(iOpenMenu())){
                    openMenu(p);
                    e.setCancelled(true);
                }
            }
            else if (e.getInventory().getType().equals(InventoryType.WORKBENCH)){
                Bukkit.broadcastMessage("It's a workbench! :D");
                final Player p = (Player) e.getWhoClicked();
                if (e.getCurrentItem().equals(iCloseMenu())){
                    Bukkit.broadcastMessage("And you just clicked the 'close' item!");
                    closeMenu(p);
                    e.setCancelled(true);
                }
            }
        }
    iCloseMenu() and iOpenMenu() are methods that return ItemStacks.
     
  4. Offline

    nisovin

    If you close an inventory from within the InventoryClickEvent it will break all future click events. You need to schedule it to close later, even just one tick is fine.
     
    Rprrr likes this.
  5. Offline

    Rprrr

    nisovin
    Alright. :) Thanks for the help! I'll try that.

    nisovin
    Okay, it works. Thanks a thousand times. :)
    I'm now trying to re-open the player's inventory when he's closing it, but that's causing some problems. :s So my closing method is:
    Code:
        public void closeMenu(final Player p){
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                @Override
                public void run() {
                    p.closeInventory();
                    p.openInventory(p.getInventory());
                    Inventory inv = p.getOpenInventory().getTopInventory();
                    inv.setItem(0, iOpenMenu()); 
                 
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                        @SuppressWarnings("deprecation")
                        @Override
                        public void run() {
                            p.updateInventory();
                        }
                    }, 1L);
                }
            }, 1L);
        }
    The point is, this creates an inventory that basically shows the player's inventory twice (one inventory at the top, one inventory at the bottom). What I actually want to show, is the player's inventory with all his armour slots, the crafting slots, et cetera. Do you know if this can be achieved by using different code? Thanks in advance!

    edit: I"m sorry, double post. :l

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
Thread Status:
Not open for further replies.

Share This Page