Solved Swords Not Removing

Discussion in 'Plugin Development' started by will181, Jul 24, 2014.

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

    will181

    Hello.

    Bukkit is giving me an issue. I am trying to remove swords from player's inventories; however, the sword disappears from the inventory, but when I punch things, it still acts as if the sword is still in my hand. If I then relog, then the sword appears back in my inventory, exactly the same.

    Does anyone know why this happens?

    Code:
    int x = 0;
           
            for(@SuppressWarnings("unused") ItemStack itemStack : p.getInventory().getContents()) {
                if(is.getType().equals(Material.WOOD_SWORD) || is.getType().equals(Material.STONE_SWORD) || is.getType().equals(Material.IRON_SWORD) || is.getType().equals(Material.GOLD_SWORD) || is.getType().equals(Material.DIAMOND_SWORD))
                    p.getInventory().setItem(x, null);
               
                x++;
            }
    My original code was this, I just iadded that in an attempt to fix this issue.

    Code:
    p.getInventory().removeItem(is);
     
  2. Offline

    TheHandfish

    Yeah, there's some special way you need to remove items from inventories. I'll have to dig it back up.
     
  3. Offline

    DannyDog

    Player.getInventory().remove()?
    I think you're not suppose to be setting it to null..
    I think it's mean to be AIR...
     
  4. Offline

    will181

    DannyDog I tried remove, as well as the setting it to air, but neither worked.
     
  5. Offline

    Gater12

  6. Offline

    will181

  7. Offline

    skipperguy12

    Try calling
    Code:java
    1. p.getInventory().updateInventory();
    after setting it to air/removing it.
     
  8. Offline

    DannyDog

  9. Offline

    Gater12

    will181
    You are not using you're "itemstack" variable which you should.
     
  10. Offline

    will181

    Gater12 DannyDog skipperguy12 Not sure what I was doing there. I added the p.updateInventory(), but exactly the same thing is happening.
     
  11. Offline

    TheHandfish

    UpdateInventory is deprecated because it's no longer necessary. Things are deprecated for a reason. It means there is a better way to go about whatever you are trying to achieve.
     
    Dragonphase likes this.
  12. Offline

    DannyDog

    I've seen this issue once before on an old factions server, where if you hit your ally/clan member with a sword the durability will go down and eventually it breaks. But it doesn't because if you relog or press q on the slot the sword will reappear.
    So i'm guessing it's to do with the client?
     
  13. Offline

    Gater12

    will181
    What is "is" referencing to?
     
  14. Offline

    skipperguy12

    Gater12 is probably right. I didn't actually read all of it, I assumed you were removing incorrectly. Now that I read the code, it appears you did use some other ItemStack variable.
    Also, why are you manually incrementing variable x? http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
    This will provide an index while looping through each slot.
    Code:java
    1.  
    2. for (int i = 0; i < p.getInventory().getSize(); i++) {
    3. ItemStack slotItem = p.getInventory().getItem(i);
    4. if (slotItem.getType().equals(Material.WOOD_SWORD) ||
    5. slotItem.getType().equals(Material.STONE_SWORD) ||
    6. slotItem.getType().equals(Material.IRON_SWORD) ||
    7. slotItem.getType().equals(Material.GOLD_SWORD) ||
    8. slotItem.getType().equals(Material.DIAMOND_SWORD))
    9. p.getInventory().setItem(i, null);
    10. }

    TheHandfish Ok, I removed that from the above code. I searched for a bit and read that setItem and addItem will automatically update, so it is not required.
    However, if this still does not work, just use updateInventory(). It may be deprecated, but it's not going anywhere. It also doesn't have to be permanent, just remove it if you find a better solution.
     
  15. Offline

    Gater12

    skipperguy12
    Could use that or the enhanced for loop which the OP has already done.
     
  16. Offline

    skipperguy12

    Gater12 I guess it's just personal preference.
     
  17. Offline

    will181

    TheHandfish What is this better way of doing it then? I can't see any other way of doing this.
     
  18. Offline

    will181

    Bump? Also, extra bit of info, if I press 'Q', the item drops. So, it's essentially just turning invisible in the inventory.


    It is also still detected as being there in my inventory by my plugin.

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

    MomsKnife

    getItemInHand().setType(Material.AIR);
    p.updateInventory();

    Might work..
     
  20. Offline

    will181

    MomsKnife It's not limited to the item in their hand though. It could be anywhere in their inventory. I also tried setting it to air.

    Even if I set it to a stick or something, if I drop it, it still drops a sword

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

    Dragonphase

    will181

    Inventory's remove method will remove ALL instances of a specified ItemStack or Material:

    Code:java
    1. p.getInventory().remove(Material.WOOD_SWORD);
    2. p.getInventory().remove(Material.STONE_SWORD);
    3. p.getInventory().remove(Material.IRON_SWORD);
    4. p.getInventory().remove(Material.GOLD_SWORD);
    5. p.getInventory().remove(Material.DIAMOND_SWORD);
     
  22. Offline

    will181

    Dragonphase I only want to remove one instance of it, and that gives me the same issue.
     
  23. Offline

    Dragonphase

    will181

    Get the index of the items you want to remove and set their types to Material.AIR. There is no reason it shouldn't work, because I've been using these methods for a plugin I'm developing, and they work perfectly:

    Code:java
    1. for(int i = 0; i < p.getInventory().getSize(); i ++) {
    2. ItemStack item = p.getInventory().getItem(i);
    3. if(item.getType().equals(Material.WOOD_SWORD) || item.getType().equals(Material.STONE_SWORD) || item.getType().equals(Material.IRON_SWORD) || item.getType().equals(Material.GOLD_SWORD) || item.getType().equals(Material.DIAMOND_SWORD))
    4. p.getInventory().setItem(i, new ItemStack(Material.AIR));
    5. }


    Also, your original code was written as though you had the intentions of removing all swords.
     
  24. Offline

    will181

    Dragonphase I realise that it would have removed all swords, but that was my mistake. I appreciate that it should work, but it doesn't. Can I ask what build of bukkit you were using when it worked in your plugins?
     
  25. Offline

    Dragonphase

    will181
    I'm using the latest development build. I just tried it again, and it worked perfectly. I made the following method:

    Code:java
    1. public void removeItemFromIndex(Inventory inventory, int index, int count){
    2. if (inventory.getItem(index).getAmount()-count > 0) inventory.getItem(index).setAmount(inventory.getItem(index).getAmount()-count);
    3. else inventory.setItem(index, new ItemStack(Material.AIR));
    4. }


    But for your needs, you could remove count:

    Code:java
    1. public void removeItemFromIndex(Inventory inventory, int index){
    2. inventory.setItem(index, new ItemStack(Material.AIR));
    3. }


    You will also need to check that the item at the index is not null, otherwise it will return a NullPointerException.
     
  26. Offline

    TheHandfish

    ^ Why not use the latest recommended build? Isn't that the latest build period?

    I'll have to look at how I do this in WhymCraft.
     
  27. Offline

    ChipDev

    Set durability to 0.
     
  28. Offline

    will181

    The issue is that I had commented out the "event.setCancelled(true)" earlier while testing and forgot to uncomment it.
     
  29. Offline

    TheHandfish

  30. Offline

    Dragonphase

    TheHandfish
    It's a personal preference. The reasons I use the latest development build:
    • Getting used to the latest noticeable changes if any
    • Identifying bugs and issues if any
    will181
    Please mark the thread as Solved if the issue has been resolved.
     
Thread Status:
Not open for further replies.

Share This Page