Solved Don't Drop non-enchanted items

Discussion in 'Plugin Development' started by Dobbermann2, Oct 30, 2014.

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

    Dobbermann2

    Hello,
    How can i do on playerdeath that if a sword(unenchanted)/bow(unenchanted)/arrow/armor(unenchanted) is dropped that it despawns? I can't fix it :(

    Greetings
     
  2. Offline

    Unica

    Dobbermann2

    • Listen for PlayerDeathEvent
      Code:java
      1. @EventHandler
      2. public void onDeath(PlayerDeathEvent e){
      3. }
    • Loop through the drops
      Code:java
      1. for(ItemStack drop : e.getDrops()){
      2. }
    • Check if the drop contains an enchantment
      Code:java
      1. for(ItemStack drop : e.getDrops()){
      2. if(!drop.getEnchantments().isEmpty()){ //Not sure if there is another way to check enchantments on an itemstack
      3. //Set it to air r something
      4. }
      5. }
     
    Dobbermann2 likes this.
  3. Offline

    Dobbermann2

    If there is enchanted stuff the non ehcnated stuff needs to be despawned
    But the enchanted stuff not
     
  4. Offline

    Unica

    Dobbermann2
    drop.getEnchantments() returns a list with all enchantments on that itemstack.
    If the list is empty, thus it doesn't have any enchantments, then u set the material to air so it will be removed.
     
  5. Offline

    Dobbermann2

    So if there is dropped 2 sword unenchanted and 1 enchanted the unenchanted will despawn and the enchanted will drop?
     
  6. Offline

    Unica

  7. Offline

    Dobbermann2

    Unica How can i set that item to air or despawn it?

    Any?

    ?

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

    fireblast709

    It returns a Map, doesn't it :p.
    Dobbermann2 you need to remove it from the drops (do note that you can't do that whilst iterating over a List, you will need an Iterator for that)
     
    Hawktasard likes this.
  9. Offline

    Dobbermann2

  10. Offline

    mythbusterma

  11. Offline

    fireblast709

    mythbusterma the Map is immutable :p
    Dobbermann2 PlayerDeathEvent has a method getDrops() which returns a List<ItemStack>. Simply invoke iterator() to get an Iterator<ItemStack>. For further information about Iterators, read the link posted by mythbusterma (or google around, if you google 'java iterator' you will find a few examples)
     
  12. Offline

    mythbusterma

    fireblast709

    Oh sorry, I was talking about the items returned by get drop.....I thought that was a Map for some reason (which wouldn't even make any sense.....) I was completely off topic. I meant iterate over the the drops like you said.
     
  13. Offline

    Dobbermann2

    fireblast709 Sorry but i am dutch i dont understand things from that page :( I can use translate but...
     
  14. Offline

    zackpollard

    You don't understand the things from that page, but you understand what we are saying here....
    Enough said.
     
  15. Offline

    Dobbermann2

    I dont know how i can do it! I TRIED EVERYTHING
     
  16. Offline

    fireblast709

  17. Offline

    Dobbermann2

  18. Offline

    fireblast709

  19. Offline

    Dobbermann2

    fireblast709 i removed it because it isn't working

    fireblast709 Do you know it?

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

    fireblast709

    Dobbermann2 Well I am willing to help you, not spoonfeed you :p
     
  21. Offline

    Dobbermann2

  22. Offline

    fireblast709

    Dobbermann2 this thread already has a lot of answers, try writing the code, and let me know of you run into any issues (and I expect you to first write code before you run into issues)
     
  23. Offline

    mythbusterma

    Dobbermann2

    Like we said before, take an iterator from the drops and remove the items you don't want (assuming getDrops() returns a mutable list).
     
  24. Offline

    Dobbermann2

    @myhtbusterma tommorow i wil test it
     
  25. Offline

    Dobbermann2

    fireblast709 mythbusterma This is my code
    Code:java
    1. public void onPlayerDeath(PlayerDeathEvent e) {
    2. Iterator<ItemStack> iterator = e.getDrops().iterator();
    3. while(iterator.hasNext()) {
    4. ItemStack item = iterator.next();
    5. if(item.getEnchantments().isEmpty()) {
    6. item.setType(Material.AIR);
    7.  
    8.  
    9.  
    10.  
    11.  
    12.  
    13. }
    14.  
    15. }
    16. }
     
  26. Offline

    fireblast709

    Dobbermann2 now instead of item.setType(), just remove the ItemStack completely by using .remove() (on the Iterator) and you should be done :D.
     
  27. Offline

    Dobbermann2

Thread Status:
Not open for further replies.

Share This Page