Solved Not updating inventory after right click

Discussion in 'Plugin Development' started by voltywolty, Sep 15, 2021.

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

    voltywolty

    Hey everyone. I'm currently trying to make it where a player right clicks to use a book to be granted items, however they need a certain amount of an item. Currently, it seems to check it only once, but not when they gain that item again. Struggling on why it isn't working so if someone could help me out, that'd be awesome.

    Code:
    @EventHandler
        public void onTailorBookRightClick(PlayerInteractEvent event) {
            if (event.getAction() == Action.RIGHT_CLICK_AIR) {
                if (event.getItem() != null) {
                    if (event.getItem().getItemMeta().equals(ItemManager.dwarfTailorBook.getItemMeta())) {
                        Player player = event.getPlayer();
                       
                        player.updateInventory();
                       
                        if (bookCooldown.containsKey(player.getName())) {
                            if (bookCooldown.get(player.getName()) > System.currentTimeMillis()) {
                                long secondsLeft = (bookCooldown.get(player.getName()) - System.currentTimeMillis()) / 1000;
                                player.sendMessage(ChatColor.BLUE + "Spell is on cooldown for " + secondsLeft + " seconds.");
                                return;
                            }
                        }
                       
                        bookCooldown.put(player.getName(), System.currentTimeMillis() + (15 * 1000));
                       
                        // Function isn't reading players inventory every time. So once the player starts, it doesn't update the inventory to see if they have that required item.
                        ItemStack dyeCheck = new ItemStack(Material.ORANGE_DYE);
                       
                        if (player.getInventory().contains(dyeCheck, 10)) {
                            List <String> armorItems = DvZ.getInstance().getConfig().getStringList("TailorArmors");
                            int index = new Random().nextInt(armorItems.size());
                            String items = armorItems.get(index);
                           
                            ItemStack newItem = new ItemStack(Material.getMaterial(items.toUpperCase()));
                           
                            player.getWorld().dropItem(player.getEyeLocation(), new ItemStack(Material.GOLD_ORE, 10));
                            player.getWorld().dropItem(player.getEyeLocation(), newItem);
                           
                            player.getInventory().removeItem(new ItemStack(Material.ORANGE_DYE, 10));
                        }
                        else {
                            player.sendMessage(ChatColor.BLUE + "You do not have 10 orange dye in your inventory! Use bonemeal and craft orange dye.");
                        }
                    }
                }
            }
        }
    Again, trying to make it where it reads the players inventory for a specific item every time.
     
  2. Offline

    man_in_matrix

    So is this function supposed to run until they have the number of items required?
    Just try to clarify.
    Anyways I think I have a solution

    Create a bukkit runnable that has all of the code to give the items and take the items inside of it and leave the dyeCheck Method.

    When the dyeCheck Is fulfilled then run the runnable for the player It should work. Reply for updates :D
     
  3. Offline

    voltywolty

    I have managed to figure it out! Thanks!
     
Thread Status:
Not open for further replies.

Share This Page