Solved Can you make 2 guis in 1 class?

Discussion in 'Spigot Plugin Development' started by WeedMan420, Nov 15, 2019.

  1. i tried doing this, both gui's have setcancelled on them, but only the first gui works, the second gui for some reason "works" but the set cancelled on them does not, so i can just grab the items.
     
  2. SteelPhoenix

    Moderator

    Yea we can't exactly guess the error without seeing any code and if possible stack traces.
     
  3. Please post your code and any errors you get.
    Put it in a code block. -> https://i.imgur.com/ChCPWNd.png
    Click the little plus and hit code. Paste it in there.
     
  4. Code (Java):
        @EventHandler
        public void onInvClick(InventoryClickEvent e) {
            openFirstPage(e);
            openSecondPage(e);
        }
    Code (Java):
        private void createMenu2(Player player) {
            Inventory particleInv2 = Bukkit.getServer().createInventory(null, 54, "§8§l-=-§6§lParticle Inventory 2§8§l-=-");

            ItemStack item29 = new ItemStack(Material.GRAVEL);
            ItemMeta meta29 = item29.getItemMeta();
            ArrayList<String> item29Lore = new ArrayList<>();
            meta29.setDisplayName("§7§lLarge Smoke Particle");
            item29Lore.add(ChatColor.RED + "");
            meta29.setLore(item29Lore);
            item29.setItemMeta(meta29);
            particleInv2.setItem(10, item29);
    }
    Code (Java):
        public void openSecondPage(InventoryClickEvent e) {
            Player player = (Player) e.getWhoClicked();
            if (!e.getInventory().getName().equalsIgnoreCase("§8§l-=-§6§lParticle Inventory 2§8§l-=-")) {
                return;
            }

            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("§7§lLarge Smoke Particle"))
            {
                e.setCancelled(true);
                smokelarge.add(player.getUniqueId());
                player.closeInventory();
            }
    }
    this is the second page of the gui. which is the one in which event.setCancelled does not seem to work. its layed out exactly the same way the first page is so i cant see what the problem is.
     
  5. You need to have a condition which chooses between the 2 options.
    As your onInvClick(InventoryClickEvent e) will immediatly process openFirstPage(e); and then openSecondPage(e);

    Thus seemingly ignoring openFirstPage(e);
     
  6. this is how i choose between the 2 guis:
    Code (Java):
     @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player player = (Player) sender;
            if (command.getName().equalsIgnoreCase("particle")) {
                createMenu(player);
            }

            return super.onCommand(sender, command, label, args);
        }
    Code (Java):
     private void createMenu(Player player) {
            Inventory particleInv = Bukkit.getServer().createInventory(null, 54, "§8§l -=-§6§lParticle Inventory§8§l-=-");

            ItemStack nextItem1 = new ItemStack(Material.STAINED_GLASS_PANE, 1, (byte) 5);
            ItemMeta nextMeta1 = nextItem1.getItemMeta();
            nextMeta1.setDisplayName("§a§lNext Page");
            nextItem1.setItemMeta(nextMeta1);
            particleInv.setItem(53, nextItem1);
    }
    Code (Text):
        public void openFirstPage(InventoryClickEvent e) {
            Player player = (Player) e.getWhoClicked();
            if (!e.getInventory().getName().equalsIgnoreCase("§8§l -=-§6§lParticle Inventory§8§l-=-")) {
                return;
            }

            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("§a§lNext Page"))
            {
                player.closeInventory();
                createMenu2(player);
            }
    }
     
  7. your logic makes sense, but i need the inventoryclickevent on the second page as well how do i split them apart from the same method yet still make it work.
     
  8. Where’s the @EventHandler annotation on the second event?
     
  9. well im stupid... thanks, i did not know you need to add @EventHandler to even child methods of an event.