Put certain amount

Discussion in 'Spigot Plugin Development' started by Snowmite, Jul 2, 2018.

  1. I am making a backpack plugin that can store up to 4 different types of keys. You can deposit every key in your inventory. However, I don't want the key amounts to go over the max limit of the backpack. What I want to do is lets say my backpack has a max storage of 10 keys. I want to input 20 keys, so it takes 10, then leaves another 10. How would I do this?

    My Current Code:
    Code (Text):
                        if(Backpack.getAllKeysInInventory(player) + backpack.getAllKeysCombined()>backpack.getMaxItems()){
                            int amount = 0;
                            if(Backpack.getAllKeysInInventory(player) + backpack.getAllKeysCombined()>backpack.getMaxItems()){
                                amount = Backpack.getAllKeysInInventory(player) - backpack.getMaxItems();
                            }
                            else if(backpack.getMaxItems()>Backpack.getAllKeysInInventory(player) + backpack.getAllKeysCombined()){
                                amount = backpack.getMaxItems() - (Backpack.getAllKeysInInventory(player) + backpack.getAllKeysCombined());
                            }
                            Bukkit.broadcastMessage("" + amount);
                                for(ItemStack items : player.getInventory().getContents()) {
                                    if (items == null) continue;
                                    if (items.hasItemMeta() && items.getItemMeta().hasDisplayName()) {
                                        if (items.getItemMeta().getDisplayName().equals(Core.getString("items.mineKey.name"))) {
                                                amount = amount - 1;
                                                backpack.setMineKeys(backpack.getMineKeys() + items.getAmount());
                                                items.setAmount(0);
                                                if (items.getAmount() == 0) {
                                                    player.getInventory().removeItem(items);
                                                }
                                                if (amount == 0) break;
                                            }
                                    }
                                }
                            return;
                        }
     
  2. The easiest way to do this would be to listen for the inventory click event, do some maths to figure out how many more keys can be added to the clicked itemstack/backpack cancel the event and set the clicked itemstack to the max it can be then set the oncursor itemstack to the itemstack of keys minus the amount that you added to the stack in the backpack.
     
  3. I don't need help with the event, I need help with the math.
     
  4. Do something like this to get how many of that key there currently is in the inventory
    Honestly that confuses me too. I have done something similar before but not exactly the same, here the code I used it might help you figure it out.

    Code (Text):
        int amount = weapon.getMaxAmmo()-Util.getAmmo(item);
            if(args.length > 0 && Util.isInt(args[0]) && Integer.valueOf(args[0]) <= weapon.getMaxAmmo())
                amount = Integer.valueOf(args[0]);

            int totalAmmo = getAmmo(inv);
            if(!(totalAmmo >= amount))
            {
                player.sendMessage(MessageManager.noAmmo);
                return true;
            }

            int totalLoaded = Util.getAmmo(item)+amount;

            if(totalLoaded > weapon.getMaxAmmo())
                amount = weapon.getMaxAmmo()-Util.getAmmo(item);


            ItemStack loadedWeapon = weapon.setNbtData(Util.getUses(item), Util.getAmmo(item)+amount);
            inv.setItemInHand(loadedWeapon);
            removeItemFromInv(inv, amount);
            String ammoLoaded = MessageManager.ammoLoaded;
            ammoLoaded = ammoLoaded.replaceAll("%amount%", Integer.toString(amount));
            player.sendMessage(ammoLoaded);
            player.updateInventory();
            return true;
        }

        private int getAmmo(Inventory inv)
        {
            ItemStack ammo = Ammo.getAmmoItem();
            int i = 0;
            for(ItemStack item : inv.getContents())
            {
                if(item == null || item.getType().equals(Material.AIR))
                    continue;

                if(!item.hasItemMeta() || !item.getItemMeta().hasLore())
                    continue;

                if(item.getItemMeta().getLore().equals(ammo.getItemMeta().getLore()))
                    i = i + item.getAmount();
            }
            return i;
        }

        private void removeItemFromInv(PlayerInventory inv, int amt) {
            ItemStack item = Ammo.getAmmoItem();
            int amount = 0;
            for (final ItemStack i : inv.getContents())
                if (i != null && i.getType() == item.getType())
                    amount += i.getAmount();
            inv.remove(item.getType());
            if (amount > amt) {
                item.setAmount(amount - amt);
                inv.addItem(item);
            }
        }