how to make confirm in inventory?

Discussion in 'Plugin Development' started by dekrosik, Aug 17, 2015.

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

    dekrosik

    hello i have this method
    Code:
        public static void openConfirmInventory(Player player, ItemStack itemConfirm) {
            ItemStack yes = new ItemStack(Material.EMERALD_BLOCK);
            ItemMeta im = yes.getItemMeta();
            im.setDisplayName(ChatColor.GREEN + String.valueOf(ChatColor.BOLD) + "TAK");
            yes.setItemMeta(im);
            ItemStack no = new ItemStack(Material.REDSTONE_BLOCK);
            ItemMeta imN = no.getItemMeta();
            imN.setDisplayName(ChatColor.RED + String.valueOf(ChatColor.BOLD) + "NIE");
            no.setItemMeta(imN);
            Inventory inventory = Bukkit.createInventory(null, 36, "POTWIERDZENIE");
            inventory.setItem(9, yes);
            inventory.setItem(10, yes);
            inventory.setItem(18, yes);
            inventory.setItem(19, yes);
            inventory.setItem(16, no);
            inventory.setItem(17, no);
            inventory.setItem(25, no);
            inventory.setItem(26, no);
            inventory.setItem(4, itemConfirm);
            player.openInventory(inventory);
        }
    
    and when i have this code in event
    Code:
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            ItemStack clicked = e.getCurrentItem();
            Inventory inventory = e.getInventory();
            Player player = (Player) e.getWhoClicked();
            if (clicked == null || clicked.getType() == Material.AIR || inventory.getName() == null) {
                return;
            } else if (inventory.getName().equalsIgnoreCase(Configuration.npcName)) {
                player.closeInventory();
                Utils.openConfirmInventory(player, clicked);
            } else if (inventory.getName().equalsIgnoreCase("POTWIERDZENIE")) {
                player.sendMessage("in confirm");
                ItemStack clicked1 = e.getCurrentItem();
                Inventory inventory1 = e.getInventory();
                if (clicked1.getType() == Material.EMERALD_BLOCK
                        && clicked1.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + String.valueOf(ChatColor.BOLD) + "TAK")) {
                    player.sendMessage("click");
                    Offer off = new Offer(clicked1.getType());
                    if (!player.getInventory().containsAtLeast(Utils.HunterHead(), off.getPrize())) {
                        player.sendMessage(ChatColor.RED + "Nie masz tyle glow.");
                        player.closeInventory();
                    }
                    if (player.getInventory().firstEmpty() == -1) {
                        player.sendMessage(ChatColor.RED + "Nie masz wolnego miejsca w inventory.");
                        player.closeInventory();
                    }
                    player.closeInventory();
                    player.getInventory().remove(Utils.HunterHead(off.getPrize()));
                    player.getInventory().addItem(new ItemStack(clicked.getType(), off.getAmount(), (short) off.getDate()));
                    player.sendMessage(ChatColor.GREEN + "Brawo kupiles itemek");
                }
            }
        }
    
    and when i click on item np. diamond confirmInventory was opened but when i click on emerald block nothink to do;/
     
  2. Offline

    SkyleTyler1337

    @dekrosik most likely because your getting the inventory two times and the item that you clicking.

    where it's at (copied code from yours)
    Code:
    ItemStack clicked = e.getCurrentItem();
            Inventory inventory = e.getInventory();
            Player player = (Player) e.getWhoClicked();
            if (clicked == null || clicked.getType() == Material.AIR || inventory.getName() == null) {
                return;
            } else if (inventory.getName().equalsIgnoreCase(Configuration.npcName)) {
                player.closeInventory();
                Utils.openConfirmInventory(player, clicked);
            } else if (inventory.getName().equalsIgnoreCase("POTWIERDZENIE")) {
                player.sendMessage("in confirm");
                ItemStack clicked1 = e.getCurrentItem();
                Inventory inventory1 = e.getInventory();

    sorry if it's not formatted correctly.
     
  3. You should notice that you should get the title, not the name.
    So you should do something like:
    Code:
    if (inventory.getTitle.equalsIgnoreCase("POTWIERDZENIE")) {
    //whatever
    }
     
  4. Offline

    Synapz

    @dekrosik You are using
    Code:
    if (inventory.getName().equalsIgnoreCase(...) {}
    This doesnt work because the inventory referenc you have stored has been closed. After you close it you have to make a new reference or set the inventory object to the new player.getInventory(), and that will update it to the current inventory and check its name
     
    Last edited: Aug 19, 2015
  5. And i didnt See that you have e.setCancelled(true);
    somewhere.
    That may not relate to the problem but i recommend using that to make sure the player doesnt have The item in bis cursor floating around.
     
  6. Offline

    LolDragonFire

    You could take a look at my plugin Bukkit GUI Lib which can be used as a library and already supports confirm gui's (Called YesNoGUI in the code) and a lot of other things.

    Code example:
    Code:
    YesNoGUI prompt = new YesNoGUI(new YesNoCallback() {
        public void onResult(boolean result) {
            if (result) { //If the player clicked the "Yes" button
                gui.delete();
                guis.remove(name);
                player.sendMessage("Deleted the gui!");
            }
        }
    }, "Do you really want to delete the \"" + name + "\" gui?", "Delete gui");
    
    BGUILib.showGUI(prompt, player); //Used to show a inventory gui to the specified player 
     
  7. @IlluminatiGaming @Synapz
    Code:
    //Directly pulled from the CraftInventory class where the getTitle() and getName() methods are.
    public String getTitle()
      {
        return getInventory().getName();
      }
    public String getName()
      {
        return getInventory().getName();
      }
    
     
  8. Offline

    Synapz

    @megamichiel no that's not what I'm saying

    When you use Inventory inventory = e.getInventory(); it points to the current inventory with the diamond in it. Then later you added player.closeInventory()... So your inventory reference STILL is pointing to the inventory you closed and not the new one with the emerald in it. You need to update the inventory reference or make a new one when you close the inventory. This way it gets updated and it points to your new inventory, not the old one that closed.
     
    Last edited: Aug 19, 2015
  9. @Synapz
    What are you talking about? When an Inventory event is called the getInventory() refers to the currently opened inventory, not the one recently closed. When the event is called and the person did inventory = e.getInventory(), how would it even refer to the old inventory?
     
    Synapz likes this.
Thread Status:
Not open for further replies.

Share This Page