Solved KillStreaks

Discussion in 'Plugin Development' started by ajs333, Dec 13, 2013.

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

    boysnnoco

    ajs333 you can also do
    Code:java
    1. Player killer = (Player)e.getEntity().getKiller()

    Just be sure to check the the person who died is a player and the killer himself using what AoH_Ruthless says :)
     
  2. Offline

    ajs333

    boysnnoco
    Thank you for fixing that issue but now I get a internal error that says:

    15:43:23 [SEVERE] Could not pass event PlayerDeathEvent to sKits v1
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callPlayerDeathEvent(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.java:710)
    at net.minecraft.server.v1_6_R3.EntityHuman.damageEntity(EntityHuman.java:714)
    at net.minecraft.server.v1_6_R3.EntityPlayer.damageEntity(EntityPlayer.java:383)
    at net.minecraft.server.v1_6_R3.EntityHuman.attack(EntityHuman.java:884)
    at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:1121)
    at net.minecraft.server.v1_6_R3.Packet7UseEntity.handle(SourceFile:36)
    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:30)
    at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
    at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
    at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
    at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
    at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.NullPointerException
    at sKits.events.KillStreak.onPlayerDeath1(KillStreak.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)

    ... 20 more

    Here is line 36:
    Code:
    int kills = killstreak.get(killer.getName());
     
  3. Offline

    boysnnoco

    ajs333 make sure there is a killer by doing a simple check
    Code:java
    1. if(killer != null){
    2. //Code
    3. }
     
  4. Offline

    ajs333

  5. Offline

    boysnnoco

    ajs333 make sure that the killer is in/ has a number in the array list
    so do
    Code:java
    1. if(killstreak.get(killer.getName() != null){
    2. //Code
    3. }
     
  6. Offline

    ajs333

    boysnnoco
    Just to make sure I am understanding what you are telling me.. can you check this..
    Code:
    @EventHandler
        public void onPlayerDeath1(PlayerDeathEvent e) {
            if(e.getEntity().getKiller() instanceof Player) {
                Player killer = (Player) e.getEntity().getKiller();
                Player killed = e.getEntity();
                if(killer != null){
                    if(killstreak.get(killer.getName()) != null){
                        if(!killstreak.containsKey(killer.getName())) {
                            killstreak.put(killer.getName(), 0);
                        }
                        killstreak.put(killer.getName(),
                                killstreak.get(killer.getName() +1));
                        if(killstreak.containsKey(killed.getName())) {
                            killstreak.remove(killed.getName());
                        }
                        int kills = killstreak.get(killer.getName());
                        if(kills == 1) {
                            Bukkit.broadcastMessage(ChatColor.GOLD + killer.getName() + " has a 1 killstreak!");
                        }
                    }
                }
            }
        }
     
  7. Offline

    boysnnoco

    ajs333 you want to check if it is null then add them to the hashmap my bad
     
  8. Offline

    ajs333

    boysnnoco
    There are no error in my code nor are there any internal errors but it doesn't broadcast my message...

    Here is my whole class then:
    Keep in mind that my main class is called sKits
    Code:
    package sKits.events;
     
    import java.util.HashMap;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
     
    import sKits.sKits;
     
    public class KillStreak implements Listener{
       
        private HashMap<String, Integer> killstreak = new HashMap<String, Integer>();
       
        sKits kits;
        public KillStreak(sKits plugin) {
            kits = plugin;
        }
       
        @EventHandler
        public void onPlayerDeath1(PlayerDeathEvent e) {
            if(e.getEntity().getKiller() instanceof Player) {
                Player killer = (Player) e.getEntity().getKiller();
                Player killed = e.getEntity();
                if(killer != null){
                    if(killstreak.get(killer.getName()) != null){
                        if(!killstreak.containsKey(killer.getName())) {
                            killstreak.put(killer.getName(), 0);
                        }
                        killstreak.put(killer.getName(),
                                killstreak.get(killer.getName() +1));
                        if(killstreak.containsKey(killed.getName())) {
                            killstreak.remove(killed.getName());
                        }
                        int kills = killstreak.get(killer.getName());
                        if(kills == 1) {
                            Bukkit.broadcastMessage(ChatColor.GOLD + killer.getName() + " has a 1 killstreak!");
                        }
                        if(kills == 3) {
                            Bukkit.broadcastMessage(ChatColor.GOLD + killer.getName() + " has a 3 killstreak!");
                        }
                    }
                }
            }
        }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  9. Offline

    boysnnoco

    ajs333 are you registering your events?
    if so try this (I basically rebuilt your listener):
    Code:java
    1. @EventHandler
    2. public void onPlayerDeath1(PlayerDeathEvent e) {
    3. if(e.getEntity().getKiller() instanceof Player) {
    4. Player killer = (Player) e.getEntity().getKiller();
    5. Player killed = e.getEntity();
    6. if(killer != null){
    7. if(killstreak.get(killer.getName()) != null){
    8. if(killstreak.get(killer.getName()) != null){
    9. int k = killstreak.get(killer.getName()) + 1;
    10. killstreak.put(killer.getName(),k);
    11. if(killstreak.containsKey(killed.getName())) {
    12. killstreak.remove(killed.getName());
    13. }
    14. int kills = killstreak.get(killer.getName());
    15. }else{
    16. if(kills == 1) {
    17. Bukkit.broadcastMessage(ChatColor.GOLD + killer.getName() + " has a 1 killstreak!");
    18. } killstreak.put(killer.getName(), 1);
    19. }
    20. }
    21. }
    22. }
    23. }

    I don't know if it works :p
     
  10. Offline

    ajs333

    boysnnoco
    Wait, this is how you register events right?
    Code:
    pm.registerEvents(new KillStreak(), this);
     
  11. Offline

    boysnnoco

    ajs333 yeah (as long as it is in your onEnable() )
     
  12. Offline

    ajs333

    boysnnoco
    Yea, in my main class under the onEnable()

    I tested it and it still doesn't work...
    Is there a way I can test to see if the event is being called?
     
  13. Offline

    boysnnoco

    add a debug message like Bukkit.broadcastMessage("FIRED");
    at the top of your event
    ajs333
     
  14. Offline

    ajs333

    boysnnoco
    Nope the event isn't being called... :/
     
  15. Offline

    boysnnoco

    ajs333 show me your main class please
     
  16. Offline

    ajs333

    boysnnoco
    Oh, wait I forgot one thing... but when I kill a player it now says HI!

    Code:
    @EventHandler
        public void onPlayerDeath1(PlayerDeathEvent e) {
            Bukkit.broadcastMessage("Hi");
            if(e.getEntity().getKiller() instanceof Player) {
                Player killer = (Player) e.getEntity().getKiller();
                Player killed = e.getEntity();
                if(killer != null){
                    if(killstreak.get(killer.getName()) != null){
                        if(killstreak.get(killer.getName()) != null){
                            int k = killstreak.get(killer.getName()) + 1;
                            killstreak.put(killer.getName(),k);
                            if(killstreak.containsKey(killed.getName())) {
                                killstreak.remove(killed.getName());
                            }
                        }else{
                            int kills = killstreak.get(killer.getName());
                            if(kills == 1) {
                                Bukkit.broadcastMessage(ChatColor.GOLD + killer.getName() + " has a 1 killstreak!");
                            } killstreak.put(killer.getName(), 1);
                        }
                    }
                }
            }
        }
    Edit:
    When ever anyone dies the server says "HI"
     
  17. Offline

    boysnnoco

    ajs333 that's because your saying 'hi' when someone dies
     
  18. Offline

    ajs333

    boysnnoco
    Ik that but it doesn't broadcast the KillStreak message...
     
  19. Offline

    boysnnoco

    ajs333 this is what I have (and it seems to work)
    Code:java
    1. public final HashMap<String, Integer> killstreak = new HashMap<String, Integer>();
    2. @EventHandler
    3. public void onPlayerDeath1(PlayerDeathEvent e) {
    4. Bukkit.broadcastMessage("Hi");
    5. if(e.getEntity().getKiller() instanceof Player) {
    6. Player killer = (Player) e.getEntity().getKiller();
    7. Player killed = e.getEntity();
    8. if(killstreak.get(killer.getName()) != null){
    9. int k = killstreak.get(killer.getName()) + 1;
    10. killstreak.put(killer.getName(),k);
    11. if(killstreak.containsKey(killed.getName())) {
    12. killstreak.remove(killed.getName());
    13. }
    14. }else{
    15. killstreak.put(killer.getName(), 1);
    16. Bukkit.broadcastMessage(ChatColor.GOLD + killer.getName() + " has a 1 killstreak!");
    17. }
    18. }
    19. }
     
  20. Offline

    ajs333

    boysnnoco
    The issue is that it isn't removing their killstreak once they die...
     
  21. Offline

    boysnnoco

    ajs333 do killstreak.remove(killed.getName());
     
  22. Offline

    ajs333

    boysnnoco
    Its in there so I wasn't sure why it wasn't removing the killed person...
    Code:
    if(killstreak.containsKey(killed.getName())) {
                        killstreak.remove(killed.getName());
                    }
    Edit:
    Hmm what could fix that issue?
     
  23. Offline

    boysnnoco

    ajs333 the only thing I could think of is to make a seperate event just for removing people out of the hashmap :)
     
  24. Offline

    ajs333

  25. Offline

    Pizza371

    boysnnoco why would he need to create a seperate event? :confused:
     
  26. Offline

    ajs333

    Pizza371
    If you were to test what boysnnoco gave me, you would notice that once the player dies it doesn't remover their killstreak so i'm not sure how to do that... He suggested to make a new Event... How would you do it?
     
  27. Offline

    boysnnoco

    Pizza371 I was running out of ideas, I didn't quite understand why it wasn't removing the player but wth... lol

    ajs333
    Code:java
    1. @EventHandler
    2. public void onKillStreakRemove(PlayerDeathEvent e){
    3. //Code that removes the player
    4. }


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

    Pizza371

    boysnnoco ajs333 looks like you two's are nesting the the removal inside an if statement that has nothing to do with it, yet again, I could be reading the code wrong..
    here:
    Code:
     @EventHandler
        public void onPlayerDeath1(PlayerDeathEvent e) {
            Bukkit.broadcastMessage("Hi");
            Player killed = e.getEntity();
            if(killstreak.containsKey(killed.getName())) killstreak.remove(killed.getName());
            if(e.getEntity().getKiller() instanceof Player) {
                Player killer = (Player) e.getEntity().getKiller();
                if(killstreak.get(killer.getName()) != null){
                    int k = killstreak.get(killer.getName()) + 1;
                    killstreak.put(killer.getName(),k);
                }else{
                    killstreak.put(killer.getName(), 1);
                    Bukkit.broadcastMessage(ChatColor.GOLD + killer.getName() + " has a 1 killstreak!");
                }
            }
        }
     
  29. Offline

    boysnnoco

    @Pizzaa371 that must be it im really tired and I didn't notice it lol
     
  30. Offline

    AoH_Ruthless

    boysnnoco ajs333 Pizza371
    I did not read the last 2 posts, so this may be a repeat ...
    The formatting of the if/else blocks is pretty off.
    In particular, it's this chunk:
    Code:java
    1. if(killstreak.get(killer.getName()) != null){
    2. int k = killstreak.get(killer.getName()) + 1;
    3. killstreak.put(killer.getName(),k);
    4. if(killstreak.containsKey(killed.getName())) {
    5. killstreak.remove(killed.getName());
    6. }
    7. }else{
    8. killstreak.put(killer.getName(), 1);
    9. Bukkit.broadcastMessage(ChatColor.GOLD + killer.getName() + " has a 1 killstreak!");
    10. }


    It's much more efficient to use this:
    Code:java
    1. if(!killstreak.containsKey(killer.getName())) killstreak.put(killer.getName(), 0) //If they aren't in the killstreak map, add them with score 0.
    2. Bukkit.broadcastMessage("debug1"); //Add debug messages to see how far it gets
    3. int kills = killstreak.get(killer.getName()); //Casts a variable (this is a safe place because it ensures the killer is in the map)
    4. killstreak.put(killer.getName(), kills + 1); //Either way it will add 1 to the killstreak.
    5. Bukkit.broadcastMessage("debug2");
    6. if (kills == 1) {
    7. Bukkit.broadcastMessage("debug3");
    8. }


    Just edit out the chunk of code I was talking about and paste that in (understand it first!). I freehanded it on the forums so it may be a little off so be wary of that :)
     
Thread Status:
Not open for further replies.

Share This Page