Solved Need help with inventory editing

Discussion in 'Plugin Development' started by Smalltrout, Oct 31, 2014.

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

    Smalltrout

    Hello,

    How can i make this so it only removes a certain amount of items from the players inventory. Right now it removes the right amount but only when that material is in a stack of 3. When it is in a stack of 64 it gives you the chest but doesn't remove any items so you can just spam it and get an unlimited amount. I understand the reason is because i am using an ItemStack but you cannot remove a said amount of material from a players inventory.
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    2. ItemStack br = new ItemStack(Material.BLAZE_ROD, 3);
    3.  
    4. String nm = sender.getName();
    5.  
    6. Player player = (Player) sender;
    7. Inventory inv = player.getInventory();
    8.  
    9. ItemStack cp = new ItemStack(Material.CHEST, 1);
    10. ItemMeta cpMeta = cp.getItemMeta();
    11.  
    12. cpMeta.setDisplayName(ChatColor.GREEN + "-=[" + ChatColor.WHITE + "Care Package" + ChatColor.GREEN + "]=-");
    13. cp.setItemMeta(cpMeta);
    14.  
    15. if(commandLabel.equalsIgnoreCase("cp")){
    16. if(inv.containsAtLeast(br, 1)){
    17.  
    18. inv .addItem(cp);
    19.  
    20. inv.remove(br);
    21.  
    22. player.getServer().broadcastMessage(ChatColor.GREEN + "[" + ChatColor.WHITE + "DiamondTime" + ChatColor.GREEN + "] " + nm + ChatColor.GREEN + " Has recieved a Small Care Package!");
    23. }else{
    24. player.sendMessage(ChatColor.DARK_RED + "You do not have proper requirements!");
    25. }
    26.  
    27.  
    28. }
    29.  
    30.  
    31.  
    32. return false;
    33. }
    34.  


    Thanks!
     
  2. Offline

    Jimfutsu

    I take it you want to remove 3 "br" from the players inventory?
     
  3. Offline

    Smalltrout

    Jimfutsu Yes.

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  4. Offline

    Smalltrout

  5. Offline

    xTrollxDudex

    Why don't you just use Inventory#contains(Material)?

    What you are doing now checks for 3 item stacks in your inventory. Using contains(Material.BLAZE_ROD) would result in checking the inventory for blaze rods, regardless of whether it is in a stack, as long as it is at least 1.
     
  6. Offline

    Smalltrout

    Yeah, that is what I'm trying to do. I want to see if the have 3 or more blaze rods then only remove 3 from them because that is the challenge. You give up three blaze rods for one care package and that's where I'm getting stuck. If I use ItemStack in inv.remove(itemstack) then it only removes the 3 if it's in it's own stack of 3. It is checking the inventory properly just having problems with the inv.Remove() if the blazerods are in a stack of 64 it still gives you a care package (cp) but doesn't remove any blaze rods xTrollxDudex
     
  7. Offline

    xTrollxDudex

    Well, you could use Inventory#contains(Material, int).
     
    Hawktasard likes this.
  8. Offline

    Smalltrout

    Yes but that isn't what I need help with. I need to remove a specific amount of material. No matter what size stack it's in xTrollxDudex
     
  9. Offline

    xTrollxDudex

    Then replace the material with the amount subtracted by 3...
     
  10. Offline

    Smalltrout

    bump

    I tried this but it still doesn't work
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    2.  
    3. ItemStack br = new ItemStack(Material.BLAZE_ROD);
    4.  
    5. String nm = sender.getName();
    6.  
    7. Player player = (Player) sender;
    8. Inventory inv = player.getInventory();
    9.  
    10.  
    11. ItemStack cp = new ItemStack(Material.CHEST, 1);
    12. ItemMeta cpMeta = cp.getItemMeta();
    13.  
    14. cpMeta.setDisplayName(ChatColor.GREEN + "-=[" + ChatColor.WHITE + "Care Package" + ChatColor.GREEN + "]=-");
    15. cp.setItemMeta(cpMeta);
    16.  
    17. if(commandLabel.equalsIgnoreCase("cp")){
    18. if(inv.containsAtLeast(br, 5)){
    19.  
    20. inv.addItem(cp);
    21.  
    22. br.setAmount(br.getAmount()-5);
    23.  
    24. player.getServer().broadcastMessage(ChatColor.GREEN + "[" + ChatColor.WHITE + "DiamondTime" + ChatColor.GREEN + "] " + nm + ChatColor.GREEN + " Has recieved a Small Care Package!");
    25. }else{
    26. player.sendMessage(ChatColor.DARK_RED + "You do not have proper requirements!");
    27. }
    28.  
    29.  
    30. }
    31.  
    32.  
    33.  
    34. return false;
    35. }
    36.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  11. Offline

    fireblast709

    Smalltrout use Inventory#removeItem(ItemStack...items)
     
  12. Smalltrout Also, this is not urgent. Please don't do that. :)
     
  13. Offline

    Smalltrout

Thread Status:
Not open for further replies.

Share This Page