On Death Drop issue

Discussion in 'Plugin Development' started by achap1989, Oct 29, 2013.

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

    achap1989

    Ok, so I am making part of this plugin to drop items from the hotbar on death however when I die I get this error and it basically duplicates the items in my inventory. Can someone show me what I have done wrong?
    Show Spoiler

    21:11:51 [SEVERE] Could not pass event PlayerDeathEvent to Hot Bar Drop v0.1
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
    at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callPlayerDeat
    hEvent(CraftEventFactory.java:344)
    at net.minecraft.server.v1_6_R3.EntityPlayer.die(EntityPlayer.java:312)
    at net.minecraft.server.v1_6_R3.EntityLiving.damageEntity(EntityLiving.j
    ava:710)
    at net.minecraft.server.v1_6_R3.EntityHuman.damageEntity(EntityHuman.jav
    a:714)
    at net.minecraft.server.v1_6_R3.EntityPlayer.damageEntity(EntityPlayer.j
    ava:383)
    at net.minecraft.server.v1_6_R3.EntityLiving.b(EntityLiving.java:851)
    at net.minecraft.server.v1_6_R3.EntityHuman.b(EntityHuman.java:1305)
    at net.minecraft.server.v1_6_R3.Entity.a(Entity.java:771)
    at net.minecraft.server.v1_6_R3.EntityLiving.a(EntityLiving.java:136)
    at net.minecraft.server.v1_6_R3.EntityPlayer.b(EntityPlayer.java:492)
    at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java
    :419)
    at net.minecraft.server.v1_6_R3.Packet10Flying.handle(SourceFile:136)
    at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296
    )
    at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java
    :116)
    at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
    at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:3
    0)
    at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:5
    92)
    at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:2
    27)
    at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:4
    88)
    at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java
    :421)
    at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:5
    83)
    Caused by: java.lang.IllegalArgumentException: Cannot drop a Null item.
    at org.apache.commons.lang.Validate.notNull(Validate.java:203)
    at org.bukkit.craftbukkit.v1_6_R3.CraftWorld.dropItem(CraftWorld.java:29
    4)
    at org.bukkit.craftbukkit.v1_6_R3.CraftWorld.dropItemNaturally(CraftWorl
    d.java:312)
    at achap1989.hotbardrop.HotBarDrop.onPlayerDeath(HotBarDrop.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
    ... 24 more


    Code:
    package achap1989.hotbardrop;
     
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class HotBarDrop extends JavaPlugin implements Listener {
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
       
        }
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent e) {
     
            e.getDrops().clear();
           
            Player p = e.getEntity();
            for(int i = 0; i <=8; i++){
                    for(ItemStack item : p.getInventory().getContents()){
                            p.getWorld().dropItemNaturally(p.getLocation(), p.getInventory().getItem(i));
                    }
            }
        }
     
    }
     
    
     
  2. Offline

    maxben34

    You are casting a player to an entity. You can't do that. You need to check
    Code:java
    1. if (e.getEntity() instanceof Player){
    2.  
    3. }
    4.  

    That alone is a problem in your code.
     
  3. Offline

    achap1989

    maxben34 It changes nothing. So it keeps everything the exact same i guess. xD

    Does anyone else know what is going on?

    SkillSam Well that works, well somewhat, now when i die the server still errors but items in the inventory are not dropped but completely dissapear

    SkillSam Neither of them work, it does the same thing each time I change the
    p.getWorld().dropItemNaturally(p.getLocation(), p.getInventory().getItem(i));

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

    xTrollxDudex

    achap1989 SkillSam
    Read the exception: NullPointerException. It can't drop an air item, add this:
    PHP:
    //loop through
        
    if(!= null) {
            
    p.getWorld().dropItemNaturally(p.getLocation(), i);
        }
    }
     
  5. Offline

    drtshock

    You are already getting the contents of the inventory once, don't get them twice.

    Just do p.getWorld().dropItemNaturally(p.getLocation(), item);
    I would also clear their inventory just to be safe ;)

    EDIT: Also null checks are best checks ^^

    That will only drop 8 items. Getting all items in the inventory would be easier and more reliable.

    Seems I failed to read the first line of his post ;3 Then yeah, you'd be correct :)

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

    achap1989

    SkillSam it is stoll removing items fromthe inventory and not dropping those even with the clear drops portain.

    Nothing atall SkillSam

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

    achap1989

    SkillSam The code I am using is below, when I die it del
    Code:java
    1. package achap1989.hotbardrop;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.entity.PlayerDeathEvent;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11.  
    12. public class HotBarDrop extends JavaPlugin implements Listener {
    13.  
    14. public void onEnable(){
    15. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    16.  
    17. }
    18. @EventHandler
    19. public void onPlayerDeath(PlayerDeathEvent e) {
    20. e.getDrops().clear();
    21. Player p = e.getEntity();
    22. for (int i = 0; i <= 8; i++) {
    23. ItemStack item = p.getInventory().getItem(i);
    24. if (item != null) {
    25. e.getDrops().add(item);
    26. }
    27. }
    28. }
    29.  
    30. }
    31.  
    32.  
     
  8. Offline

    achap1989

    SkillSam I went through several times, however even just itself it does the same thing. I think I located the main source. For some odd reason when I use the clear the drops it removes them from my inventory I even tried a death event with the clear drops only. Any Idea how to fix this?
     
  9. Offline

    achap1989

    SkillSam, Sorry it has been a while I have been busy with work and my son. But you misunderstand me, I want it to drop the items on the hotbar and keep all of the other items in the inventory. Something similar to turning on keep inventory but it keeps everything but the hotbar items
     
Thread Status:
Not open for further replies.

Share This Page