[Question] - Wildcard quantity for item stacks?

Discussion in 'Plugin Development' started by Shay Williams, Jun 8, 2012.

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

    Shay Williams

    Is it possible? Here's a snippet from some blacksmithing code of one of my plugins:

    My issue is that the check for iron_ingots only works if there is an itemstack with exactly 1 quantity, I.E. a single iron ingot, not a stack.

    Is there a way to get around this so that it will just take one iron ingot off any stack? Any help is appreciated, thanks.

    Code:
    if(i.getDurability() > 0 && p.getInventory().contains(new ItemStack(Material.IRON_INGOT, 1))){
                        ItemStack cost = new ItemStack(Material.IRON_INGOT, 1);
                        p.getInventory().removeItem(cost);
                        i.setDurability((short) 0);
                        plugin.addXp(p, "blacksmithing", (blevel * 2) + 50);
                        plugin.CheckForLevelUp(p, "blacksmithing");
                    }
                    else if(!(p.getInventory().contains(new ItemStack(Material.IRON_INGOT, 1)))){
                        p.sendMessage(ChatColor.RED + "You do not have the required material to repair this item.");
                        return;
                    }
                    else if(i.getDurability() == 0){
                        p.sendMessage(ChatColor.YELLOW + "This piece of equipment is already fully repaired.");
                        return;
                    }
     
  2. Offline

    Giant

    Not via the default inventory handling I am afraid :( There are ways to get around this issue, but they aren't exactly clean...

    The way I do it in GiantShop is via this...
     
  3. Offline

    Shay Williams

    I see. Would you mind if I used the methods depicted in your plugin for my own? I would give credit and it wouldn't be distributed.
     
  4. Offline

    Xx_^_Kai_^_xX

    Gah, Giant is... wrong :)
    Simply don't define a quantity to the ItemStack :D
     
  5. Offline

    Giant

    Not at all, go right ahead! :)
     
    Xx_^_Kai_^_xX likes this.
  6. Offline

    Xx_^_Kai_^_xX

    Code:
    if(i.getDurability() > 0 && p.getInventory().contains(new ItemStack(Material.IRON_INGOT, 1))){
                        ItemStack cost = new ItemStack(Material.IRON_INGOT, 1);
                        p.getInventory().removeItem(cost);
    Correct that to:

    Code:
    if(i.getDurability() > 0 && p.getInventory().contains(new ItemStack(Material.IRON_INGOT))){
                        ItemStack cost = new ItemStack(Material.IRON_INGOT, 1);
                        p.getInventory().removeItem(cost);
     
  7. Offline

    CorrieKay

    theres an easier way to do this.

    check if the inventory contains an item stack with material type of iron ingots.

    if it doesnt, return false
    if it does, find it, reduce its amount by one, and go on your merry way :3
     
  8. Offline

    Xx_^_Kai_^_xX

    Simplified version of my explanation. You always beat me CorrieKay xD

    P.S.: This is KaiBB
     
    CorrieKay likes this.
  9. Offline

    Shay Williams

    Where am I going wrong?

    Code:
    for (ItemStack it : p.getInventory().getContents()){
                            if(it.getType() == Material.IRON_INGOT){
                                it.setAmount(it.getAmount() - 1);
                                if(it.getAmount() == 0){
                                    it.setType(Material.AIR);
                                }
                                break;
                            }   
                            }
     
  10. Offline

    CorrieKay

    uh.. i dont know? you dont tell me whats going wrong :p looks fine from here :eek:
     
  11. Offline

    Shay Williams

    I had to change some stuff around, all fixed though.

    Here's the source so others don't need to suffer what I did:
    Code:
            if(p.getInventory().all(Material.IRON_INGOT).size() == 0){
                p.sendMessage(ChatColor.RED + "You do not have the required material to repair this item.");
                return;
            }
            if(i.getDurability() == 0){
                p.sendMessage(ChatColor.YELLOW + "This piece of equipment is already fully repaired.");
                return;
            }
                    if(i.getDurability() > 0 && p.getInventory().all(Material.IRON_INGOT).size() != 0){
                        HashMap<Integer, ? extends ItemStack> invItems = p.getInventory().all(Material.IRON_INGOT);
                       
                        for (Map.Entry<Integer, ? extends ItemStack> entry : invItems
                                .entrySet()) {
                            int index = entry.getKey();
                            ItemStack item = entry.getValue();
                            int stackAmount = item.getAmount();
                           
                            if(stackAmount == 1){
                                p.getInventory()
                                .setItem(
                                        index,
                                        new ItemStack(Material.AIR));
                            }
                           
                            if(stackAmount > 1){
                            p.getInventory()
                            .setItem(
                                    index,
                                    new ItemStack(Material.IRON_INGOT, stackAmount
                                            - 1));
                            }
                   
                            p.updateInventory();
                            break;
                        }
     
Thread Status:
Not open for further replies.

Share This Page