Removing Items with custom names when a player dies

Discussion in 'Plugin Development' started by DJCowGaming, Oct 20, 2019.

  1. Offline

    DJCowGaming

    I am trying to remove ItemStacks with custom names from drops when a player dies.
    What I have done is created a listener for PlayerDeathEvent, I iterated through all the drops, checked if they have the custom name, and removed them if they do. Whenever someone dies with the item in their inventory, it removes it and throws an error in the console, pointing to my for loop.

    Code:
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event) {
            for (ItemStack items : event.getDrops()) {
                String name = items.getItemMeta().getDisplayName();
    //            World world = event.getEntity().getWorld();
                // Weak Armor
                if (name.equals(ArcaneArmor.weakHelm().getItemMeta().getDisplayName())) {
                    event.getDrops().remove(ArcaneArmor.weakHelm());
                }
     
  2. Offline

    ThePandaPlayer

    It would be nice to see what the error is, as we have no context as to what the problem actually is. All we know is that there's code causing an exception in the console. Does the code perform its intended purpose? If not, what's the error? I simply need more information to help you.
     
  3. Offline

    DJCowGaming

    This is the error that is thrown to the console.

    Runtime error in the console (open)

    [11:06:31] [Server thread/ERROR]: Could not pass event PlayerDeathEvent to ArcaneWeapons v1.0
    org.bukkit.event.EventException: null
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:514) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at org.bukkit.craftbukkit.v1_14_R1.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:726) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.EntityPlayer.die(EntityPlayer.java:520) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.EntityLiving.damageEntity(EntityLiving.java:1169) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.EntityHuman.damageEntity(EntityHuman.java:769) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.EntityPlayer.damageEntity(EntityPlayer.java:675) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.EntityLiving.killEntity(EntityLiving.java:174) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.CommandKill.a(SourceFile:30) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.CommandKill.a(SourceFile:23) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at com.mojang.brigadier.CommandDispatcher.execute(CommandDispatcher.java:262) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at com.mojang.brigadier.CommandDispatcher.execute(CommandDispatcher.java:176) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.CommandDispatcher.a(CommandDispatcher.java:172) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at org.bukkit.craftbukkit.v1_14_R1.command.VanillaCommandWrapper.execute(VanillaCommandWrapper.java:45) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchCommand(CraftServer.java:710) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.PlayerConnection.handleCommand(PlayerConnection.java:1641) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:1481) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) [spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:910) [spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:903) [spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:887) [spigot.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:820) [spigot.jar:git-Spigot-9de398a-9c887d4]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_201]
    Caused by: java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source) ~[?:1.8.0_201]
    at java.util.ArrayList$Itr.next(Unknown Source) ~[?:1.8.0_201]
    at net.salvosmc.arcane.listeners.player.PlayerDeath.onPlayerDeath(PlayerDeath.java:13) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_201]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_201]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_201]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_201]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
    ... 32 more
    [11:06:31] [Server thread/INFO]: DJCowGaming fell out of the world


    Line 13 that the for loop.
     
  4. Offline

    KarimAKL

    You are trying to remove something from the collection you are iterating; try iterating over a new list that contains the elements from the list you want to iterate over, and then remove them from the original list.
     
  5. Offline

    DJCowGaming

    I changed it to this,
    Code:
            List<ItemStack> drops = event.getDrops();
            for (ItemStack items : drops) {
                String name = items.getItemMeta().getDisplayName();
    //            World world = event.getEntity().getWorld();
                // Weak Armor
                if (name.equals(ArcaneArmor.weakHelm().getItemMeta().getDisplayName())) {
                    event.getDrops().remove(ArcaneArmor.weakHelm());
                }
    though it still is throwing the same event exception. Should I remove the custom item from the drops List then event.setDrops to the dropsList? edit: It turns out there isn't a set drops method.
     
    Last edited: Oct 20, 2019
  6. Offline

    KarimAKL

    @DJCowGaming You are still iterating over the same list, make a new one, and then set it's contents.
     
  7. Offline

    DJCowGaming

    Do you mean like
    Code:
    List<ItemStack>  items = new ArrayList<ItemStack>();
    for (ItemStack i : event.getDrops()) {
         items.add(i)
    }
    Then
    Code:
    if (name.equals(// ItemStack)) {
         event.getDrops().remove(// ItemStack)
    }
     
  8. Offline

    KarimAKL

  9. Offline

    Strahan

    Java has an object specifically meant for this task; Iterator:

    Code:
    Iterator iter = e.getDrops().iterator();
    while (iter.hasNext()) {
        ItemStack i = (ItemStack)iter.next();
        if (!i.hasItemMeta() ||
            !i.getItemMeta().hasDisplayName() ||
            !i.getItemMeta().getDisplayName().equalsIgnoreCase("whatever")) continue;
        iter.remove();
    }
     
    KarimAKL likes this.
  10. Offline

    DJCowGaming

    Thank you guys for the help, it was much appreciated.
     

Share This Page