Solved How to remove ONE lore item from player's inventory

Discussion in 'Plugin Development' started by Colby l, Feb 17, 2014.

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

    Colby l

    I'm trying to remove one lore item from a player's inventory, and the code below removes one lore item from every stack in a player's inventory. If I try and make a new itemstack, then the data isn't verified to be lore item data.

    Code:
    Code:java
    1. //TODO TEST
    2. p.sendMessage("Test 5");
    3. //TEST
    4. // iterates through player's inventory
    5. int counter = 0;
    6. int itemAmount = 0;
    7. for (ItemStack is : inventory) {
    8.  
    9. if (is != null && !is.equals(Material.AIR)) {
    10. //TODO TEST
    11. p.sendMessage("Test 6");
    12. //TEST
    13. if (is.hasItemMeta()) {
    14. //TODO TEST
    15. p.sendMessage("Test 7");
    16. //TEST
    17.  
    18. ItemMeta itemmeta = is.getItemMeta();
    19. List<String> lore = itemmeta.getLore();
    20. //checks if lore is the same as config
    21. if (lore.contains(config.getItemMetaTag(tier))) {
    22. //TODO TEST
    23. p.sendMessage("Test 8");
    24. //TEST
    25. if (counter < itemChargeAmount){
    26. //TODO TEST
    27. p.sendMessage("Test 9");
    28. //TEST
    29. //sets item stack amount
    30.  
    31. //removes item from player's inventory
    32. //TODO TEST
    33. p.sendMessage("Test 11" + is.getAmount());
    34. //TEST
    35. // is.setAmount(is.getAmount() - 1);
    36. inventory.remove(new ItemStack(is.getType(), 1));//removeItem(is);
    37. // inventory.removeItem(is);
    38. }
    39. counter++;
    40. }
    41.  
    42. }
    43. }
    44. }


    Item lore and lore removal in general is hell. There needs to be an easier way...

    Thanks for the help!
     
  2. Offline

    AmShaegar

    I would do it this way:
    Code:java
    1. public void removeItem(Inventory inventory) {
    2. int counter = 0;
    3. int itemAmount = 0;
    4. for (ItemStack is : inventory) {
    5.  
    6. if (is == null || is.equals(Material.AIR)) {
    7. continue;
    8. }
    9. if (!is.hasItemMeta()) {
    10. continue;
    11. }
    12. ItemMeta itemmeta = is.getItemMeta();
    13. List<String> lore = itemmeta.getLore();
    14. if (!lore.contains(config.getItemMetaTag(tier))) {
    15. continue;
    16. }
    17. if (counter >= itemChargeAmount) {
    18. counter++;
    19. continue;
    20. }
    21. is.setAmount(is.getAmount()-1);
    22. break;
    23. }
    24. }
     
  3. Offline

    Colby l

    That's much neater,thanks!

    However, if a player has a stack of the material separated, it will remove one item from each stack.

    Also, if the itemChargeAmount is 2 or more, the loop still only removes one item.
     
  4. Offline

    AmShaegar

    Well, I don't know exactly what your code is supposed to do, but you can easily add a parameter for the amount to be removed.

    This code does not remove an Item of each stack because of the break in line 22. After one fitting Item stack is found the for loop is interrupted.
     
  5. Offline

    Colby l

    Oh, I forgot to add the 'break' line. So, that is solved.

    But the itemChargeAmount is still bugged.

    itemChargeAmount is an int retrieved from a config file. Depending on the config, the plugin will remove the specified amount of items with a specific lore, set in the config.
     
  6. Offline

    AmShaegar

    well, then you should adjust the code like that. just replace my 1 with what you want the code to remove. Although this will only work if the necessary amount is at one single stack.
     
  7. Offline

    Colby l

    So I got it working by replacing '1' with itemChargeAmount, however now I need to use the deprecated method 'p.updateInventor()'. This is not a huge issue, but is there a new method that accomplishes this task?

    Code:
    Code:java
    1. is.setAmount(is.getAmount() - itemChargeAmount);
    2. p.updateInventory();
    3. break;
     
  8. Offline

    AmShaegar

    Colby l
    Sorry, I missed that one. No, there is currently no alternative. Usually, you shouldn't need to invoke updateInventory(). But it doesn't always work. Although it does not hurt to use it, as far as I know.
     
Thread Status:
Not open for further replies.

Share This Page