InventoryClickEvents breaking...?

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

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

    Rprrr

    Hi,

    I need some help; I've tried and tried, but I just can't figure this out.

    I'm trying to create a GUI by making making a custom workbench screen. This screen will be shown when a player clicks an item in his crafting result slot - this item appears on every inventory click if the 4 crafting slots in the player's inventory are empty.

    This works, but only a little. The point is: when a player joins and he clicks in his inventory (and the crafting slots are empty), the menu item pops up in his result slot. But then, when he has finally closed the custom menu (the workbench inventory), and he tries to open the menu again (by clicking the item in the result slot), the item in the result slot does not pop up. His inventory seems to be 'broken', because when clicking in it, it does not fire InventoryClickEvents. The only way to solve this is by relogging (at least, for as far as I know - if there's any other ways I'd love to know, because that would solve this problem).

    I heard that you can't close and open an inventory at the same time; there has to be at least 1 tick delay in it (@nisovin told me this and he seemed to be right). That's why I put delay here and there to make sure it's not that causing it. There must be something else that's causing the InventoryClickEvents not to be fired - something else must be 'breaking' the inventory, but I do not know what it is. That's why I'm here.

    I will post all important methods that I'm using for the menu / GUI. Things like 'iCreativeWorld()', 'iOpenMenu()', 'iCloseMenu()' et cetera represent items. 'iOpenMenu()' is in the player's inventory's crafting result slot; 'iCloseMenu()' is in the result slot of the custom workbench.

    Code:
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e){
            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() !=null && e.getCurrentItem().equals(iOpenMenu())){
                    openMenu(p);
                    e.setCancelled(true);
                }
            }
            else if (e.getInventory().getType().equals(InventoryType.WORKBENCH)){
                final Player p = (Player) e.getWhoClicked();
               
                if (e.getInventory().getItem(0) !=null){
                    if (e.getInventory().getItem(0).equals(iCloseMenu())){
                        e.setCancelled(true);
                    }
                }
               
                if (e.getCurrentItem().equals(iCloseMenu())){
                    e.setCancelled(true);
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                        @Override
                        public void run() {
                            closeMenu(p);                   
                        }
                    }, 3L);
                }
                else if (e.getCurrentItem().equals(iSurvivalWorld())){
                    p.sendMessage(ChatColor.GOLD + "This is a testing version of the menu. It is not functional yet. Please wait for renewal of Le Serveur. :)");
                    e.setCancelled(true);
                }
                else if (e.getCurrentItem().equals(iSpawnWorld())){
                    p.sendMessage(ChatColor.GOLD + "This is a testing version of the menu. It is not functional yet. Please wait for renewal of Le Serveur. :)");
                    e.setCancelled(true);
                }
                else if (e.getCurrentItem().equals(iCreativeWorld())){
                    p.sendMessage(ChatColor.GOLD + "This is a testing version of the menu. It is not functional yet. Please wait for renewal of Le Serveur. :)");
                    e.setCancelled(true);
                }
            }
        }
       
        //For when a player closes by 'E' or 'Esc'.
        @EventHandler
        public void onInventoryClose(InventoryCloseEvent e){
            if (e.getInventory().getType().equals(InventoryType.WORKBENCH)){
                if (e.getInventory().getItem(0) !=null){
                    if (e.getInventory().getItem(0).equals(iCloseMenu())){
                        e.getInventory().setItem(0, new ItemStack(Material.AIR, 1));
                        e.getInventory().setItem(1, new ItemStack(Material.AIR, 1));
                        e.getInventory().setItem(2, new ItemStack(Material.AIR, 1));
                        e.getInventory().setItem(3, new ItemStack(Material.AIR, 1));
                        e.getInventory().setItem(4, new ItemStack(Material.AIR, 1));
                        e.getInventory().setItem(5, new ItemStack(Material.AIR, 1));
                        e.getInventory().setItem(6, new ItemStack(Material.AIR, 1));
                        e.getInventory().setItem(7, new ItemStack(Material.AIR, 1));
                        e.getInventory().setItem(8, new ItemStack(Material.AIR, 1));
                    }
                }
            }
        }
       
        public void openMenu(final Player p){
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                @Override
                public void run() {
                    p.closeInventory();
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                        @Override
                        public void run() {
                            p.openWorkbench(null, true);
                            Inventory inv = p.getOpenInventory().getTopInventory();
                            inv.setItem(4, iSurvivalWorld());
                            inv.setItem(5, iCreativeWorld());
                            inv.setItem(6, iSpawnWorld());
                            inv.setItem(0, iCloseMenu());
                           
                            //Inventory --- org.bukkit.Server --- createInventory (InventoryHolder owner, InventoryType type)
                            //Creates an empty inventory of the specified type.
                           
                            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                                @SuppressWarnings("deprecation")
                                @Override
                                public void run() {
                                    p.updateInventory();
                                }
                            }, 1L);
                        }
                    }, 1L);
                }
            }, 1L);
        }
       
        //For when a player closes by pressing the 'close' item.
        public void closeMenu(final Player p){       
            p.getOpenInventory().setItem(0, new ItemStack(Material.AIR, 1));
            p.getOpenInventory().setItem(1, new ItemStack(Material.AIR, 1));
            p.getOpenInventory().setItem(2, new ItemStack(Material.AIR, 1));
            p.getOpenInventory().setItem(3, new ItemStack(Material.AIR, 1));
            p.getOpenInventory().setItem(4, new ItemStack(Material.AIR, 1));
            p.getOpenInventory().setItem(5, new ItemStack(Material.AIR, 1));
            p.getOpenInventory().setItem(6, new ItemStack(Material.AIR, 1));
            p.getOpenInventory().setItem(7, new ItemStack(Material.AIR, 1));
            p.getOpenInventory().setItem(8, new ItemStack(Material.AIR, 1));               
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                @Override
                public void run() {
                    p.closeInventory();                       
                }
            }, 5L);
        }
        
    Please tell me if you know what is causing this strange error. If there's a way to 'fix' a player's inventory without having to relog, I would also love to know.

    Thank you for your time.

    Anyone?

    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