Solved Taking items from inventory

Discussion in 'Plugin Development' started by raGan., Jan 10, 2013.

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

    raGan.

    Hello, I usually don't have such problems, but now I just can't solve this thing, I have a method that takes specified items from player's inventory, I'm sure they are there, because i'm running it on the copy of player's inventory before I take items. I'm also sure it runs, because I did some debugging. Problem is, that it sometimes removes items and sometimes not. I noticed that it actually matters what's in payer's hand when running this method. Here it is:
    Code:java
    1.  
    2. //Those are fields used in this method, not important but meh
    3. //private final Material material;
    4. //private final short data;
    5. //private final int amount;
    6. //private final Map<Integer, Integer> enchants;
    7.  
    8. public boolean takeInventory(Inventory inv) {
    9. int remain = amount;
    10. ItemStack[] contents = inv.getContents();
    11. for (int i = 0; i <contents.length; i++) {
    12. if (contents[i] != null) {
    13. boolean enchsOK = true;
    14. for(Integer e : enchants.keySet()) {
    15. if(enchants.get(e) != contents[i].getEnchantmentLevel(Enchantment.getById(e))) {
    16. enchsOK = false;
    17. break;
    18. }
    19. }
    20. if(enchsOK) {
    21. if (contents[i].getTypeId() == material.getId()) {
    22. if(data < 0 || contents[i].getDurability() == data) {
    23. if(remain >= contents[i].getAmount()) {
    24. remain -= contents[i].getAmount();
    25. // debug message here, always correct
    26. contents[i] = null;
    27. inv.clear(i);
    28. } else {
    29. contents[i].setAmount(contents[i].getAmount() - remain);
    30. remain = 0;
    31. break;
    32. }
    33. }
    34. }
    35. }
    36. }
    37. }
    38. return remain == 0;
    39. }[/i][/i][/i][/i][/i][/i][/i][/i][/i]

    Let's say I want to remove 10 pieces of paper. I put this 10 papers into hotbar in 7th slot from the left. When I hold it in the hand, it is successfully removed. Also when I hold item to it's left in the hand, it get's removed. But when I have some other slot in hand, it won't. When it won't get removed, I can scroll to put it in my hand, but I can't 'Q' drop it. It looks like item was removed server-side, but once I pick it with mouse and place it again, it becomes fully functional.

    Does anyone have similar problem or know what's going on here ? I believe it's bukkit problem, but I'm not sure where to look in CB code.


    It does not work in creative mode. In survival mode, items won't be resreshed neither, but will disappear on click.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 8, 2016
  2. Offline

    Cjreek

    I'm not sure but you could try player.updateInventory(). (altough it's deprecated) This MAY solve your problem but maybe it won't...^^)
     
  3. Offline

    raGan.

    No, I was using it before, but it was causing unwanted behavior in some cases, especially if called while player is dragging item with mouse or during crafting.
     
Thread Status:
Not open for further replies.

Share This Page