Solved KillStreaks

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

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

    ajs333

    Ok, I have created a class for KillStreaks and whenever I try to load the plugin I get an error on line 16 which is
    Code:
    KillStreak killStreak = new KillStreak();
    
    Here is my whole class:
    Code:
    package sKits.events;
     
    import java.util.HashMap;
    import java.util.Map;
     
    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;
     
    public class KillStreak implements Listener{
       
        Map<String, Integer> playerKills = new HashMap<String, Integer>();
       
        KillStreak killStreak = new KillStreak();
        public int timer = 999999999;
     
        public boolean hasKills(String playerName) {
            return playerKills.containsKey(playerName);
        }
     
        public boolean isOnKillingSpree(String playerName) {
            return (this.getKills(playerName) > 3);
        }
     
        public Integer getKills(String playerName) {
            if (this.hasKills(playerName))
                return this.playerKills.get(playerName);
     
            return new Integer(0);
        }   
     
        public void setKills(String playerName, Integer kills) {
            this.playerKills.put(playerName, kills);
        }
     
        public void addKills(String playerName, Integer kills) {
            this.setKills(playerName, this.getKills(playerName) + kills);
        }
     
     
        @EventHandler
        public void onKillStreakadd(PlayerDeathEvent e){
            Player p = e.getEntity().getPlayer();
            int kills = killStreak.getKills(p.getName());
            if(e.getEntity() instanceof Player){
                Player killer = p.getKiller();
                killStreak.setKills(p.getName(), 0);
                killStreak.addKills(killer.getName(), killStreak.getKills(killer.getName()) +1);
            }
            if(kills == 1) {
                Bukkit.broadcastMessage("§a" + p.getName() + "§6 is on a §a"
                        + killStreak.getKills(p.getName()) + " §6kill streak! Someone end it now!");
            }
            if(kills == 2){
                Bukkit.broadcastMessage("§a" + p.getName() + "§6 is on a §a"
                        + killStreak.getKills(p.getName()) + " §6kill streak! Someone end it now!");
            }
            if(kills == 5){
                Bukkit.broadcastMessage("§a" + e.getEntity().getPlayer().getName() + "§6 is on a §a"
                        + killStreak.getKills(p.getName()) + " §6kill streak! Someone end it now!");
            }
            if(kills == 10){
                Bukkit.broadcastMessage("§a" + e.getEntity().getPlayer().getName() + "§6 is on a §a"
                        + killStreak.getKills(p.getName()) + " §6kill streak! Someone end it now!");
            }
            if(kills == 15){
                Bukkit.broadcastMessage("§a" + e.getEntity().getPlayer().getName() + "§6 is on a §a"
                        + killStreak.getKills(p.getName()) + " §6kill streak! Someone end it now!");
            }
        }
    }
    
    How can I fix that one line so the plugin actually loads..??
     
  2. Offline

    pope_on_dope

  3. Offline

    ajs333

    pope_on_dope
    Hmm, ok...

    Ok, I'm confused... how would I do it..??

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

    pope_on_dope

    add this to your class:
    Code:
    public KillStreak() {};
    without it your class is private
     
  5. Offline

    Gater12

    ajs333
    Code:java
    1.  
    2. public KillStreak(){
    3. /* Code */
    4. }
    5.  
     
  6. Offline

    ajs333

    pope_on_dope Gater12
    Now I get an error on line 48 which is
    Code:
        int kills = killStreak.getKills(p.getName());
    What is the problem with that line?
     
  7. Offline

    pope_on_dope

    whats the error? provide a stacktrace
     
  8. Offline

    ajs333

  9. Offline

    iiHeroo


    Do you know what a Stacktrace is o.o
     
  10. Offline

    reider45

    ajs333 Go into the logs and copy the error message
     
  11. Offline

    ajs333

    Nope... :'(
    I feel dumb now...
     
  12. Offline

    Developing

    ajs333 Its what the error report says..
     
  13. Offline

    ajs333

    Developing reider45 iiHeroo


    08:55:29 [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 org.bukkit.craftbukkit.v1_6_R3.entity.CraftLivingEntity.setHealth(CraftLivingEntity.java:79)
    at org.bukkit.command.defaults.KillCommand.execute(KillCommand.java:33)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
    at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:523)
    at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:959)
    at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:877)
    at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:834)
    at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
    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.onKillStreakadd(KillStreak.java:53)
    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)
    ... 22 more

    >
     
  14. Offline

    iiHeroo

    ajs333

    Line 53 in your class, stack trace says this

    So line 53 in KillStreak.java
     
  15. Offline

    ajs333

    iiHeroo
    Yeah, so here is line 53:
    Code:
     int kills = killStreak.getKills(p.getName());
     
  16. Offline

    AoH_Ruthless

    ajs333
    First of all, your addKills and setKills method really have no purpose the way they are setup and are unnecessary.

    Secondarily, your error is coming from this:
    Never, ever, ever cast a variable to a player before checking if the entity is a player. This is your problem in most of the threads you post. You are assigning a variable to a player without checking if the entity really in fact is a player when it might not be, and that's dangerous.
     
    iiHeroo likes this.
  17. Offline

    ajs333

    AoH_Ruthless @iiHerro
    Ok, I'll do some more research and learn a bit more Java before I try to do killstreaks but if anyone of you can help me fix this that would be great because I'm making this plugin for private use of a private server that will only have a max of 6 players, its a thing I'm doing with my friends and I'm the only one who can someone code...
     
  18. Offline

    AoH_Ruthless

    ajs333
    Alright, I'll attempt to walk you through with what you need to do.
    First, we need a HashMap.
    Code:java
    1. private HashMap<String, Integer> killstreak = new HashMap<String, Integer>();

    Now, we want to check if a player died, and check if the killer was a player.
    Code:java
    1. @EventHandler //It's imperative we use this interface.
    2. public void onPlayerDeath(PlayerDeathEvent e) { //This is the event we are using.
    3. if(e.getKiller() instanceof Player) { //We want to check if the killer was a player, else might throw an internal server error.
    4. Player killer = e.getKiller(); //We can now define variables for the killer and the player who died.
    5. Player killed = e.getEntity();
    6. }
    7. }

    Now, we want to check if they are in the killstreak hashmap, and if not, add them. We also want to remove the player who died from the hashmap.
    Code:java
    1. @EventHandler //It's imperative we use this interface.
    2. public void onPlayerDeath(PlayerDeathEvent e) { //This is the event we are using.
    3. if(e.getKiller() instanceof Player) { //We want to check if the killer was a player, else might throw an internal server error.
    4. Player killer = e.getKiller(); //We can now define variables for the killer and the player who died.
    5. Player killed = e.getEntity();
    6. if(!killstreak.containsKey(killer.getName) { //If it doesn't contain the killer, add them and set their score to 0.
    7. killstreak.put(killer.getName(), 0);
    8. }
    9. killstreak.put(killer.getName(). killstreak.get(killer.getName() +1)); //No matter what, it will add one to the killstreak.
    10. if(killstreak.containsKey(killed.getName()) { //Check if the player who died is in the HashMap.
    11. killstreak.remove(killed.getName()); //We want to remove the killed player from the killstreak map.
    12. }
    13. //Give rewards to the killer
    14. }
    15. }
     
    elementalgodz11 likes this.
  19. Offline

    ajs333

    AoH_Ruthless
    I don't know how to get the Killer's KillStreak and broadcast a message to the server...
     
  20. Offline

    AoH_Ruthless

    ajs333
    That's extremely easy.
    to get the KillStreak:
    Code:java
    1. int kills = killstreak.get(killer.getName());
    2. if(kills.equals("Some integer here")) {
    3. //Code
    4. }

    To broadcast a message, just make a for loop that gets all the online players and sends them all a message.
     
  21. Offline

    ajs333

    AoH_Ruthless
    Yeah, that is what I had/came up with and I get an error on the "if(kills.equals("1")) {"

    Edit:
    Can I do
    Code:
    if (kills == 1) {
     
  22. Offline

    AoH_Ruthless

    ajs333
    Yeah, that should work. Sorry for using the wrong operand
     
  23. Offline

    ajs333

    AoH_Ruthless
    Is this correct? if so, it doesn't work...
    Code:
    @EventHandler
        public void onPlayerDeath1(PlayerDeathEvent e) {
            if(e.getEntity() instanceof Player) {
                Player killer = e.getEntity();
                Player killed = e.getEntity();
                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!");
                }
            }
        }
     
  24. Offline

    AoH_Ruthless

    ajs333
    You obviously didn't fully read my post.
    Scroll up to post #19. (the one with 4 edits)
     
  25. Offline

    Pizza371

    ajs333
    Code:
    @EventHandler
        public void onPlayerDeath1(PlayerDeathEvent e) {
            if(e.getEntity() instanceof Player) {
                Player killer = e.getEntity();
                Player killed = e.getEntity();
                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!");
                }
            }
        }
    quickly scanned the code and fixed one error:
    you're using killstreak.get(killer.getName()+1) when it should be outside of that bracket (see above)
    idk if there is more
     
  26. Offline

    ajs333

    Pizza371
    I understand what you did but AoH_Ruthless didn't tell me to do that...
    Also Aoh_Ruthless, what did I forget?
     
  27. Offline

    AoH_Ruthless

    ajs333
    Did you read it? You didn't follow my instructions to the letter (or the code).
    Because, what Pizza371 just mentioned (putting that line where he put it) is something I put in the code I gave you. I'm not going to spoonfeed you this stuff when the information is right there for you.
     
  28. Offline

    ajs333

    AoH_Ruthless
    I must be blind so let me relook at it...

    Edit: I see what I did wrong...
    Code:
    Player killer = e.getKiller();
    Player killed = e.getEntity();
    I put this:
    Code:
    Player killer = e.getEntity();
    Player killed = e.getEntity();
    Here is the thing though, I get an error on e.getKiller()

    AoH_Ruthless
    The error is:
    The method getKiller() is undefined for the type PlayerDeathEvent
     
  29. Offline

    AoH_Ruthless

    ajs333
    I don't know how much I can stress it. Read the post or any other post I have made on your recent threads: make sure the killer is a player before casting it!! Sorry to sound frustrated, but you really don't get it, do you?

    Before using:
    Code:java
    1. Player killer = e.getKiller();


    You have to use:
    Code:java
    1. if (e.getKiller() instanceof Player) {
     
  30. Offline

    ajs333

    AoH_Ruthless
    Ik that after you told me like 5 times... but I still get an error on that line...
    The error is:
    The method getKiller() is undefined for the type PlayerDeathEvent

    Here is the code if you don't believe me that I have
    Code:
    if (e.getKiller() instanceof Player) {
    Here it is:
    Code:
     @EventHandler
        public void onPlayerDeath1(PlayerDeathEvent e) {
            if(e.getKiller() instanceof Player) {
                Player killer = e.getKiller();
                Player killed = e.getEntity();
                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!");
                }
            }
        }
     
Thread Status:
Not open for further replies.

Share This Page