PlayerDeathEvent problem

Discussion in 'Plugin Development' started by MordorKing78, Sep 10, 2016.

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

    MordorKing78

    Well, you might recall this an easy job but I can't get it done for some reason?
    It keeps throwing me the errors pointing towards the HashMaps, but I'm not sure what I did wrong?

    Here is the code:

    Code:
        HashMap<Player, Integer> playerScore = new HashMap<Player, Integer>();
        HashMap<Player, Integer> playerKills = new HashMap<Player, Integer>();
        HashMap<Player, Integer> playerDeaths = new HashMap<Player, Integer>();
    
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent e){
            Player p = e.getEntity();
            Player killer = e.getEntity().getKiller();
          
            playerDeaths.put(p, playerDeaths.get(p)+1);
    
            playerScore.put(killer, playerScore.get(killer)+3);
            playerKills.put(killer, playerKills.get(p)+1);
          
            if(playerScore.get(p) >= 5){
                playerScore.put(p, playerScore.get(killer)+-5);          
            }else{
                playerScore.put(p, 0);
            }
        }
    Here is the Error:

    Code:
    [21:35:58 ERROR]: Could not pass event EntityDeathEvent to MineHavocBattlePvP v1
    .0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:297) ~[craftbukkit.jar:git-Bukkit-33d5de3]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-33d5de3]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-33d5de3]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-33d5de3]
            at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.callEntityDeat
    hEvent(CraftEventFactory.java:346) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityLiving.die(EntityLiving.java:858)
    [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityLiving.damageEntity(EntityLiving.j
    ava:796) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityBat.damageEntity(SourceFile:190) [
    craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.Entity.K(Entity.java:297) [craftbukkit.j
    ar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityLiving.K(EntityLiving.java:158) [c
    raftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityInsentient.K(EntityInsentient.java
    :148) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.Entity.s_(Entity.java:230) [craftbukkit.
    jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityLiving.s_(EntityLiving.java:1427)
    [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityInsentient.s_(EntityInsentient.jav
    a:192) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityBat.s_(SourceFile:101) [craftbukki
    t.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.World.entityJoinedWorld(World.java:1374)
    [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.World.g(World.java:1352) [craftbukkit.ja
    r:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.World.tickEntities(World.java:1222) [cra
    ftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.WorldServer.tickEntities(WorldServer.jav
    a:539) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:7
    15) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:2
    84) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:6
    09) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :517) [craftbukkit.jar:git-Bukkit-33d5de3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R1.entity.C
    raftBat cannot be cast to org.bukkit.entity.Player
            at org.m.MineHavocPvP.Manager.GameManager.onPlayerDeath(GameManager.java
    :32) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _101]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _101]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_101]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_101]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:295) ~[craftbukkit.jar:git-Bukkit-33d5de3]
            ... 23 more
    >
     
  2. Offline

    JanTuck

    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R1.entity.C
    raftBat cannot be cast to org.bukkit.entity.Player

    Casting a Entity to a Player is impossible, therefor you have to check first.

    Example

    if ( YourEntity instanceof Player){
    Player YourVariable = (Player) YourEntity;
    }

    @Zombie_Striker
    Is this still Spoonfeeding?
     
    Last edited: Sep 10, 2016
  3. Offline

    Zombie_Striker

    Don't blidnly cast. On line 32 of GameManager, you are casting a bat to a player. Please post line 32, and check before you cast.
     
  4. Offline

    MordorKing78

    @Zombie_Striker Oh yeah, I think I might of found the issue, I'd just got to test it with someone. I think it's because the HashMap is looking for a player and I'm giving the Entity that killed me the score.

    Also line 32 was the line with (Player) I've now made it so it checks if the entity is a player.

    @Zombie_Striker Update: It gives me this error when I'm testing it.
    Code:
    [21:53:09 ERROR]: Could not pass event PlayerDeathEvent to MineHavocBattlePvP v1
    .0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:297) ~[craftbukkit.jar:git-Bukkit-33d5de3]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-33d5de3]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-33d5de3]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-33d5de3]
            at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.callPlayerDeat
    hEvent(CraftEventFactory.java:364) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityPlayer.die(EntityPlayer.java:392)
    [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityLiving.damageEntity(EntityLiving.j
    ava:796) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityHuman.damageEntity(EntityHuman.jav
    a:795) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityPlayer.damageEntity(EntityPlayer.j
    ava:471) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityMonster.r(EntityMonster.java:74) [
    craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityZombie.r(EntityZombie.java:226) [c
    raftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.PathfinderGoalMeleeAttack.e(SourceFile:1
    22) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.PathfinderGoalSelector.a(PathfinderGoalS
    elector.java:93) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityInsentient.doTick(EntityInsentient
    .java:460) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityLiving.m(EntityLiving.java:1601) [
    craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityInsentient.m(EntityInsentient.java
    :331) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityMonster.m(EntityMonster.java:22) [
    craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityZombie.m(EntityZombie.java:164) [c
    raftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityLiving.s_(EntityLiving.java:1465)
    [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityInsentient.s_(EntityInsentient.jav
    a:192) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityMonster.s_(EntityMonster.java:26)
    [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityZombie.s_(EntityZombie.java:222) [
    craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.World.entityJoinedWorld(World.java:1374)
    [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.World.g(World.java:1352) [craftbukkit.ja
    r:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.World.tickEntities(World.java:1222) [cra
    ftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.WorldServer.tickEntities(WorldServer.jav
    a:539) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:7
    15) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:2
    84) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:6
    09) [craftbukkit.jar:git-Bukkit-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :517) [craftbukkit.jar:git-Bukkit-33d5de3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.NullPointerException
            at org.m.MineHavocPvP.Manager.GameManager.onPlayerDeath(GameManager.java
    :40) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _101]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _101]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_101]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_101]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:295) ~[craftbukkit.jar:git-Bukkit-33d5de3]
            ... 30 more
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 10, 2016
  5. Offline

    kameronn

    Also I might want to add you should check if the killer is instance of player
     
  6. Offline

    MordorKing78

    @kameronn the killer does not have to be a player, in the updated code he doesn't have to be. I forgot to change that.
     
  7. Offline

    MordorKing78

    @AlvinB I can find that there is something null and that its going on at line 40, but I can't seem to figure out what's going on.
     
  8. Offline

    JanTuck

    Post code line 40?

    I believe one of these is line 40? with the null pointer? What if the hashmap hasnt got the playerDeaths.get(p) and you are null+1




    Moderators when does my approval go away?
     
    Last edited: Sep 10, 2016
  9. Offline

    Zombie_Striker

    @MordorKing78
    Assuming the lines stay the same, line 40 should be with the lines for the hashmap. Null check each value that gets returned and make sure the player is in the hashmap first before getting the value.
     
  10. Offline

    MordorKing78

    @Zombie_Striker Indeed, line 40 is
    Code:
                playerDeaths.put(p, playerDeaths.get(p)+1);
    
    It shouldn't be null because when I "start" the game I set all HashMaps for all online players to 0, this way they aren't null.

    EDIT: Just figured out by debugging (printIn) that the hashmaps are indeed null.
     
  11. Offline

    JanTuck

    Set the hashmaps on player Login?
     
  12. Offline

    kameronn

    Are you ever removing them for the hashmap

    EDIT: Now I see why, you're trying to add 1 to the hashmap, when you never set them in the hashmap, so basically when a player first dies you're doing null + 1 instead of 0 + 1. Check if they're not in the hashmap and if they arent put them in with the score of 0
     
  13. Offline

    MordorKing78

    @kameronn I'm removing them when the game ends.

    @Zombie_Striker @AlvinB Okay, so I'll show you guys what I'm doing because I can't seem to figure out what I'm doing wrong.

    First I make the startGame method:
    Code:
    public void startGame(){   
           
            Bukkit.broadcastMessage(plugin.MhString + "§e§lBattle§r§6§lPvP§r§e has started, you've got §r§6§l5§r§e minutes!");
    
            for(final Player all : Bukkit.getOnlinePlayers()){
                all.getWorld().playSound(all.getLocation(), Sound.LEVEL_UP, 5, 15);
               
                playerScore.put(all, 0);
                playerKills.put(all, 0);
                playerDeaths.put(all, 0);
    }
    Here I set everyone their scores to zero to make sure no one has the score of their last game.

    Then I call this method when the command is launched:

    Code:
    public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
            Player p = (Player)sender;
                   
            if(args.length == 0){
                p.sendMessage(plugin.MhString + "§cPlease use a valid argument!");
            }else if(args[0].equalsIgnoreCase("forcestart")){       
                plugin.manager.startGame();
            }
           
            return false;       
        }
    Now the game has begun! Now I want the scores to register work:

    Code:
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent e){       
            if(e.getEntity() instanceof Player){
                Player p = e.getEntity();
                //Player killer = e.getEntity().getKiller(); (Ignore this)
    
                Bukkit.broadcastMessage("" + playerDeaths.get(p));(Ignore this)
                playerDeaths.put(p, playerDeaths.get(p)+1);
               
                if(playerScore.get(p) >= 5){
                    playerScore.put(p, playerScore.get(p)+-5);           
                }else{
                    playerScore.put(p, 0);
                }
               
                //playerScore.put(killer, playerScore.get(killer)+3);(Ignore this)
                //playerKills.put(killer, playerKills.get(p)+1);(Ignore this)
            }   
        }
     
  14. @MordorKing78
    The only explanation I can see is that the HashMap is null. Can you show me where you define it?
     
  15. Offline

    MordorKing78

    @AlvinB on the top of the class:
    Code:
        public HashMap<Player, Integer> playerScore = new HashMap<Player, Integer>();
        public HashMap<Player, Integer> playerKills = new HashMap<Player, Integer>();
        public HashMap<Player, Integer> playerDeaths = new HashMap<Player, Integer>();
    I tried making them public but no difference.
     
  16. @MordorKing78
    Well, then the problem must be the "playerDeaths.get() + 1" line. Do you ever set the Integer value in the Hashmap before this?
     
  17. Offline

    MordorKing78

    @AlvinB well, yeah I set it to 0 when the game starts, and when a player dies he dies after the game starts so the hashmap should be 0, and 0+1=1 amirite, not null :3.
     
  18. Offline

    kameronn

    @MordorKing78
    Can you show us that? Also, check if they are in the hashmap when you're trying to add 1 to their current score to prevent npe exceptions
     
  19. Offline

    MordorKing78

    @kameronn Well, I tried placing the hashmaps as public in the Main class, for some reason that worked?
     
  20. @MordorKing78 Some suggestions:

    Save the UUID rather than Player
    Make an Object to hold UUID, score, kills and deaths. Then just make a list of the objects.
    Don't hardcode colour codes in, use ChatColor
     
  21. Offline

    MordorKing78

    @bwfcwalshy

    1. Yeah, I still need to change that.
    2. Aight, I'll look into that.
    3. I know it's bad to use §, but it's just so much easier if you need things like this: §e§lHello§r§6 World§r§l§m Sup
     
  22. @MordorKing78 You can always make a method to do ChatColor.translate... for you so you can have your message "&e&lHello &cWorld&5!"
     
Thread Status:
Not open for further replies.

Share This Page