how do i remove a x amount of itemstack from player inventory ?

Discussion in 'Plugin Development' started by iDemonTz, Mar 28, 2020.

Thread Status:
Not open for further replies.
  1. hello, i can remove a x amount of regular item but i don't knows how i can remove a x amount of itemstack.
    Exemple : i want to remove 128 itemstack from inventory.
    Pls help me! sorry for my english

    With Regular items :
    Code:java
    1.  
    2. if(inv2 != null && inv2.getName() != null && inv2.getName().equals("§7Pumpkin Craft")) {
    3. e.setCancelled(true);
    4. if(it != null) {
    5. if(it.getType() == Material.PUMPKIN && it.getItemMeta().getDisplayName().equals("§aGreat Pumpkin")) {
    6. int gamount = itemsinInventory(p.getInventory(), Material.PUMPKIN);
    7. int unamount = itemsinInventoryIT(p.getInventory(), Material.PUMPKIN);
    8. int amount = gamount - unamount;
    9. int recipe = 576;
    10. if(amount < recipe) {
    11. p.sendMessage("You don't have the require items");
    12. }else {
    13.  
    14. p.getInventory().removeItem(new ItemStack[] {
    15. new ItemStack(Material.PUMPKIN, recipe)
    16. });
    17. p.getInventory().addItem(CustomItemsforPumpkin.GREATPUMPKIN.getItems());
    18. }
    19.  
    20.  
    21. }
    22.  
    23.  
    24. }
    25.  


    Code:java
    1.  
    2. public int itemsinInventory(Inventory inv, Material... search) {
    3. List<Material> wanted = Arrays.asList(search);
    4. int found = 0;
    5.  
    6. for(ItemStack item : inv.getContents()) {
    7. if(item != null && wanted.contains(item.getType()))
    8. found += item.getAmount();
    9. }
    10.  
    11. return found;
    12.  
    13. }
    14. public int itemsinInventoryIT(Inventory inv, Material... search) {
    15. List<Material> wanted = Arrays.asList(search);
    16. int found = 0;
    17. for(ItemStack item : inv.getContents()) {
    18. if(item != null && wanted.contains(item.getType())) {
    19. if(item.getItemMeta().hasDisplayName()) {
    20. found += item.getAmount();
    21. }
    22. }
    23. }
    24. return found;
    25.  
    26. }
    27.  


    With ItemStack :
    Code:java
    1.  
    2. if(it.getType() == Material.PUMPKIN && it.getItemMeta().getDisplayName().equals("§2Giant Pumpkin")) {
    3. int amount = gpumpkin(p.getInventory(), Material.PUMPKIN);
    4. int recipe = 576;
    5. p.sendMessage(amount + "");
    6. if(amount < recipe) {
    7. p.sendMessage("You don't have the items");
    8. }else {
    9. //remove 576 custom pumpkin (ItemStack)
    10. p.getInventory().addItem(CustomItemsforPumpkin.GIANTPUMPKIN.getItems());
    11. }
    12. }
    13.  

    Code:java
    1.  
    2. ublic int gpumpkin(Inventory inv, Material... search) {
    3. List<Material> wanted = Arrays.asList(search);
    4. int found = 0;
    5. for(ItemStack item : inv.getContents()) {
    6. if(item != null && wanted.contains(item.getType())) {
    7. if(item.getItemMeta().getDisplayName().equals("§aGreat Pumpkin")) {
    8. found += item.getAmount();
    9. }
    10. }
    11. }
    12. return found;
    13.  
    14. }
    15.  
     
  2. Offline

    Dai_Kunai

Thread Status:
Not open for further replies.

Share This Page