Sell items from inventory (get amount and remove)

Discussion in 'Plugin Development' started by TGamingStudio, May 19, 2018.

Thread Status:
Not open for further replies.
  1. Hi i want to make prison to my server but now i got to selling mined items so i dont know how to do that.. How can i test how much items player has and how to remove it .
    for example
    Code:
    for(inv.amountof   : mat.COBBLESTONE)
    remove
    int amount += 1
    int money + = 1 // cuz cobble
    ...
    so i want it compatible with more items .
     
  2. Offline

    Zombie_Striker

    @TGamingStudio
    Close:
    1. Create an int. This represents the amount of an item the player has.
    2. For loop through all of the inventory.
    3. Check if the item is what you are looking for (by checking material name, duribility, name, ect, or by simply using isSimilar(*).).
    4. if it is, get the amount and add it to the int.
    5. Outside of the for loop, call "Inventory#removeAll(...)" to then remove all of the items of that type.
    6. Finally, add the amount of money you want based off of the amount from #1.
     
  3. I said i want more items so Amount wont work becouse i want to sell diamond for more than cobblestone xd . it must be changed . and how to loop the inventory right ?
     
  4. Offline

    timtower Administrator Administrator Moderator

    Doesn't mean that you can't count per type, give money and continue with the next type.
     
  5. @timtower @Zombie_Striker
    i think i fixed the problem .
    Code:
    if(cmd.getName().equals("sell"))
               {
                   int sold = 0;
                   int amount = 0;
                   for (ItemStack item : p.getInventory().getContents()) {
                       if(item != null)
                       {
                           if(item.getType() == Material.COBBLESTONE)
                           {
                               amount += item.getAmount();
                               sold += item.getAmount() *1;
                               p.getInventory().remove(Material.COBBLESTONE);
                           }
                           if(item.getType() == Material.COAL)
                           {
                              
                               amount += item.getAmount();
                               sold += item.getAmount() *8;
                               p.getInventory().remove(Material.COAL);
                           }
                           if(item.getType() == Material.IRON_ORE)
                           {
                              
                               amount += item.getAmount();
                               sold += item.getAmount() *20;
                               p.getInventory().remove(Material.IRON_ORE);
                           }
                           if(item.getType() == Material.GOLD_ORE)
                           {
                              
                               amount += item.getAmount();
                               sold += item.getAmount() *40;
                               p.getInventory().remove(Material.GOLD_ORE);
                           }
                           if(item.getType() == Material.LAPIS_ORE)
                           {
                              
                               amount += item.getAmount();
                               sold += item.getAmount() *100;
                               p.getInventory().remove(Material.LAPIS_ORE);
                           }
                       }
                      
                   }
                   if(amount >= 1)
                   {...
    But how can i set Material for Lapis ? i could do it for material test but not for remove method .
     
  6. Offline

    Zombie_Striker

    @TGamingStudio
    Instead of removing a material, remove an ItemStack with Material.INK_SAC and the ID for lapis.
     
  7. Where have i put the id ? something like Material.INK_SACK (4) or material ,(4) doesnt work
     
  8. @TGamingStudio
    Instead of if its Material.LAPIS_ORE do if(item.getType() == Material.INK_SACK && item.getData == 4)
    getData returns the material data for that itemstack, so 4 is lapis lazuli
     
  9. it doesnt work .
    1. its getData()
    2. and i cant test for ItemData with int .
     
  10. Offline

    KarimAKL

  11. Ok that works for testing but how to remove item with durability?
     
  12. Offline

    KarimAKL

    Last edited by a moderator: May 22, 2018
  13. @TGamingStudio
    Ah so it was getDurability() not getData() my bad. So in the if statement if the item is ink sack and durability is 4 you know item is lapis so just do p.getInventory ().remove (item);
     
Thread Status:
Not open for further replies.

Share This Page