Remove certain items from drops on death

Discussion in 'Plugin Development' started by tylerthecreeper1, Mar 13, 2016.

Thread Status:
Not open for further replies.
  1. I'm trying to figure out how to keep 2 items in the players inventory on death and they lose all the other drops. I'm just really stuck and I'm having trouble doing this. If anyone could help me as to where to start with this, that would be great. I've tried multiple things without success.

    I have fixed my previous issue, but have another one. I've updated the title.

    I'm trying to remove certain items when the player dies, in this case the items from the starter kit. I used an iterator and I don't get an error in console and the items still drop. I'm really not sure why..

    Code inside PlayerDeathEvent:

    Code:
                  List <ItemStack> list = e.getDrops();
                  Iterator<ItemStack> i = list.iterator();
                
                      while(i.hasNext()){
                      ItemStack item = i.next();
                      if(item.getType() == Material.PRISMARINE_SHARD) {
                          i.remove();
                   }
            }
     
    Last edited: Mar 14, 2016
  2. Offline

    Zombie_Striker

    @tylerthecreeper1
    Please post your code/what you tried and we will help you from there. Do you want the "saved items" to be random items from the inventory, or from two specific spots?
     
  3. I want them to be 2 specific items, like they will be prismarine shards and sunflower (double_plant). They are sort of like a currency / reward thing for my pvp server.
     
  4. Offline

    Scorpionvssub

    loop through their inventory check if those items are in there, get its slot and its amount store it into the players hashmap and on respawn give it back

    if that doesn't work try the drop and check the dropped items
     
  5. This is something I have tried, but it is not working, no errors it just doesn't save the items.

    This is inside my playerdeathevent..

    Code:
                ArrayList<Map<String, Object>> keep = new ArrayList<Map<String, Object>>();
                for (ItemStack i : new ArrayList<ItemStack>(e.getDrops())) {
                    if (!(i.equals(Material.PRISMARINE_SHARD) || i.equals(Material.DOUBLE_PLANT))) {
                        e.getDrops().remove(i);
                    }
                    if (i.equals(Material.PRISMARINE_SHARD) || i.equals(Material.DOUBLE_PLANT)) {
                        keep.add(i.serialize());
                    }
                }
                @SuppressWarnings("unchecked")
                Map<String, Object>[] arr = new Map[keep.size()];
                keep.toArray(arr);
                this.dropOnRespawn.put(e.getEntity().getName(), arr);
            
        }
      
        @EventHandler
        public void onPlayerRespawn(PlayerRespawnEvent event) {
            if (this.dropOnRespawn.containsKey(event.getPlayer().getName())) {
                for (Map<String, Object> i : dropOnRespawn.get(event.getPlayer().getName())) {
                    event.getPlayer().getInventory().addItem(ItemStack.deserialize(i));
                }
            }
        }
     
  6. Offline

    Zombie_Striker

    You should not have collections of collections. Instead, create your own class that stores this data.


    1. You are testing if an Itemstack is equal to a Material.
    2. If you want to compare itemstacks, use isSimular when comparing itemstacks.
    3. If you want to compare materials, use "==" and use "i.getType"
     
  7. how would i do this?

    I also changed i.equals to i.getType() == material, but I'm getting an NPE on:
    Code:
                this.dropOnRespawn.put(e.getEntity().getName(), arr);
     
    Last edited: Mar 13, 2016
  8. Offline

    Gonmarte

    Instead of looping and using a mqap, why not make an PlayerDeathEvent and check if the player had that items before died , if so add him to an array list. Then make the event PlayerRespawnEvent and check if the player is in the array list, if so give him that 2 items and remove him from the array list.
     
  9. thank you, i've fixed it doing this. however i have another problem maybe you can help me with..

    I'm trying to remove certain items when the player dies, in this case the items from the starter kit. I used an interator and I don't get an error in console and the items still drop. I'm really not sure why..

    Code inside PlayerDeathEvent:

    Code:
                  List <ItemStack> list = e.getDrops();
                  Iterator<ItemStack> i = list.iterator();
                 
                      while(i.hasNext()){
                      ItemStack item = i.next();
                      if(item.getType() == Material.PRISMARINE_SHARD) {
                          i.remove();
                   }
            }
     
  10. bump, still need help :/
     
  11. Offline

    Zombie_Striker

    @tylerthecreeper1
    This is because you are not removing them from the list. Instead, you are just destroying the itemstack instance. use list.remove(i) to remove drops.
     
  12. tried that, the items are still dropped and there's no errors..
     
  13. Offline

    Zombie_Striker

  14. Offline

    Gonmarte

    Do you know that items that u want to remove? If so u can just check them like i sad above and remove them
     
  15. Offline

    Zombie_Striker

    But that's his problem. His problem is that he can't/does not know how to remove them.
     
  16. It's the same thing, just with list.remove(i)

    Code:
                  List <ItemStack> list = e.getDrops();
                  Iterator<ItemStack> i = list.iterator();
                
                      while(i.hasNext()){
                      ItemStack item = i.next();
                      if(item.getType() == Material.PRISMARINE_SHARD) {
                          list.remove(i);
                   }
                 
            }
    I've also tried list.remove(item);
     
  17. Offline

    Zombie_Striker

    @tylerthecreeper1
    But that is not the case. You are removing I from the list, the block iterator. What you want to remove is Item, which is the itemstack instance.
     
  18. I said I tried that, and I just tried it again, still drops the item :/
     
  19. Offline

    mcdorli

    So, you typed in list.remove(item)?

    Is there a e.setDrops() method?
     
  20. Offline

    Edvio

    I think you can do for(Item blablabla or whatever you have to type)
    and then e.getDrops.clear();
    Idk if it clears all items or one special but try it out :)
     
  21. Offline

    mcdorli

    It clears all of them, he only wants to delete 1
     
  22. there is not an e.setDrops() :/
     
  23. bump please help me..
     
  24. Offline

    Zombie_Striker

    @tylerthecreeper1
    Have you tried debugging? Are you sure that if statement is ever true (is the item a PRISMARINE_SHARD)? Are you sure the while loop is even looping through the itemstacks? Are you sure any itemstacks can be removed from the List?
     
  25. I've tried debugging. Why wouldn't it be? Not sure what I could do instead of the while loop. The itemstacks should be able to be removed from the itemstack list.
     
  26. Offline

    Zombie_Striker

    In regards to all three sentances, you do not have anything that checks it.

    1. It not about whether or not it should be true, it about if it ever actually is true. Add a debug message that says that the if statement is true and test if it.
    2. That's not what I meant, How do you know the while loop is even running? Print out a message showing what itemstacks it is looping through.
    3. You only tried removing that one type of itemstack. Try removing either all or another specific itemstack that you know is in the List, and see if using that method removes the itemstack (showing it does work).
     
Thread Status:
Not open for further replies.

Share This Page