Solved Killstreak help?

Discussion in 'Plugin Development' started by kreashenz, Mar 16, 2013.

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

    kreashenz

    I think this will include HashMaps, and I got no idea how to use them. What I need to do, is make killstreaks, and when a player hits a certain kill streak, they get items.. I don't know how to check or store how many kills a player has. I basically just need, how to check if a player has ? amount of kills. Thanks :)
     
  2. Offline

    ZeusAllMighty11

    Map<String, Integer>();

    Store in a file if necessary.


    if(map.getKey(pname) > 10{ // more than 10 kills }
     
  3. Offline

    CubixCoders

    Although ZeusAllMighty11 gives what you basically need, i will simplify it
    Code:java
    1.  
    2. //First we create the map.
    3. public Map<String, Integer> killstreaks = new HashMap<String, Integer>();
    4. //Now we want to listen for when a player dies using PlayerDeathEvent
    5. //Then we want to check if the killer is a player, then we add him to the map
    6. if(!killstreaks.containsKey(((Player)player.getKiller()).getName())) killstreaks.put(((Player)player.getKiller()).getName(), 0); // They were never in the map, so we set them to 0
    7. //Now we add 1 to their kills
    8. killstreaks.put(((Player)player.getKiller()).getName(), killstreaks.get(((Player)player.getKiller()).getName()) + 1); // Gets what their current killstreak is and adds 1
    9. //Now we check how many they have (same PlayerDeathEvent method, just after)
    10. if(killstreaks.get(((Player)player.getKiller()).getName()) == /*Number of kills they need*/){
    11. //Give them the rewards.
    12. }
    13.  

    Hope i helped.
     
  4. Offline

    lycano

    You may want to use a class for that.

    Code:
    public class KillStreak {
     
        protected Map<String, Integer> playerKills = new HashMap<String, Integer>();
       
        public KillStreak() {
        }
       
        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);
        }
    }
    
    if you need to reuse the killstreak amount somewhere in the code you may want to rewrite the class so that it gives you the state too instead of defining those states via methods.

    Code:
    public enum KillStreakState {
        NONE(0),
        KILLINGSPREE(3),
        RAMPAGE(5),
        GODLIKE(7);
       
        private int value;
     
        private KillStreakState(int value) {
            this.value = value;
        }
       
        public int getValue() {
            return this.value;
        }
    }
    
    add those to the previous class
    Code:
    protected Map<String, KillStreakState> killStreakStates = new HashMap<String, KillStreakState>();
     
    public boolean hasKillStreakState(String playerName) {
        return (this.killStreakStates.contains(playerName));
    }
     
    public boolean setKillStreakState(String playerName, KillStreakState killStreakState) {
        this.killStreakStates.put(playerName, killStreakState);
    }
     
    public KillStreakState getKillStreakState(String playerName) {
        return this.killStreakStates.get(playerName);
    }
    
    That way you can implement the logic in your code and set the state if the player reaches a certain value plus you can use switch instead of if then else.
     
    drtshock likes this.
  5. Offline

    kreashenz

    Sorry, I wanna revive this, because of what lycano said, I (just) found out I posted this a while ago, and I re-read over hes code, and found a little error in it, not sure if its a fault by me, but I'm getting an error on
    Code:
            public boolean hasKills(String playerName) {
                return playerKills.contains(playerName);
            }
    and the (error) message : The method contains(String) is undefined for the type Map<String,Integer>..
    Also, I was stuffing around, made my own thing, didn't really work, but I have a list of killstreak things I made, and they are not working.. Here's my whole killstreak (Rewards) class..
    Code:java
    1. package me.kreashenz.kitpvp;
    2.  
    3. import java.util.HashMap;
    4. import java.util.Map;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.entity.PlayerDeathEvent;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.inventory.meta.ItemMeta;
    14. import org.bukkit.potion.PotionEffect;
    15. import org.bukkit.potion.PotionEffectType;
    16.  
    17. public class Rewards implements Listener {
    18.  
    19. public HashMap<String,Integer> killstreak = new HashMap<String,Integer>();
    20. public int timer = 999999999;
    21. @EventHandler
    22. public void onKillStreakadd(PlayerDeathEvent e){
    23. int kills = killstreak.get(e.getEntity().getPlayer().getName());
    24. Player p = e.getEntity().getPlayer();
    25. if(e.getEntity() instanceof Player){
    26. Player killer = e.getEntity().getPlayer().getKiller();
    27. killstreak.put(p.getName(), 0);
    28. killstreak.put(killer.getName(), +1);
    29. }
    30.  
    31. if(kills == 3){
    32. if(timer < 999999999){
    33. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, timer, 0));
    34. }
    35. Bukkit.broadcastMessage("§a" + e.getEntity().getPlayer().getName() + "§6 is on a §a"
    36. + killstreak.get(p.getName()) + " §6kill streak! Someone end it now!");
    37. p.sendMessage("§aSpeed mode activate!");
    38. }
    39. if(kills == 5){
    40. if(timer < 999999999){
    41. p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, timer, 0));
    42. }
    43. Bukkit.broadcastMessage("§a" + e.getEntity().getPlayer().getName() + "§6 is on a §a"
    44. + killstreak.get(p.getName()) + " §6kill streak! Someone end it now!");
    45. p.sendMessage("§aStrength mode activate!");
    46. }
    47. if(kills == 7){
    48. ItemStack snowball = new ItemStack(Material.SNOW_BALL, 3);
    49. ItemMeta snowballMeta = snowball.getItemMeta();
    50. snowballMeta.setDisplayName("§rGrenades");
    51. snowball.setItemMeta(snowballMeta);
    52. p.getInventory().addItem(snowball);
    53. p.sendMessage("§aEnjoy your reward of a §6couple grenades§a. Use them wisely, though!");
    54. }
    55. if(kills == 10){
    56. if(timer < 999999999){
    57. p.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, timer, 0));
    58. }
    59. Bukkit.broadcastMessage("§a" + e.getEntity().getPlayer().getName() + "§6 is on a §a"
    60. + killstreak.get(p.getName()) + " §6kill streak! Someone end it now!");
    61.  
    62. ItemStack helm = p.getInventory().getHelmet();
    63. ItemStack chest = p.getInventory().getChestplate();
    64. ItemStack legs = p.getInventory().getLeggings();
    65. ItemStack boots = p.getInventory().getBoots();
    66.  
    67. helm.setDurability(helm.getType().getMaxDurability());
    68. chest.setDurability(chest.getType().getMaxDurability());
    69. legs.setDurability(legs.getType().getMaxDurability());
    70. boots.setDurability(boots.getType().getMaxDurability());
    71.  
    72. p.getInventory().addItem(helm);
    73.  
    74. p.getInventory().setHelmet(helm);
    75. p.getInventory().setChestplate(chest);
    76. p.getInventory().setLeggings(legs);
    77. p.getInventory().setBoots(boots);
    78. p.sendMessage("§aYou are on a great 10 kill streak!! Keep going!" +
    79. "You have been rewarded by having your §6armour fixed§a, and §6haste §aadded!");
    80. }
    81. if(kills == 15){
    82. if(timer < 999999999){
    83. p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, timer, 1));
    84. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, timer, 1));
    85. }
    86. Bukkit.broadcastMessage("§a" + e.getEntity().getPlayer().getName() + "§6 is on a §a"
    87. + killstreak.get(p.getName()) + " §6kill streak! Someone end it now!");
    88. p.sendMessage("§aYou are on a whopping 15 kill streak!!! Keep going!" +
    89. "You have been rewarded with double §6strength§a, and §6speed§a!");
    90. }
    91. }
    92. }
     
  6. Offline

    lycano

    kreashenz gosh, well you where right. Thanks for tagging me. I have fixed that issue. I was using Lists for too long i guess. "containsKey" is correct instead of contains.
     
  7. Offline

    kreashenz

    lycano Yeah, no problem, also, can you give me an example of how to use each of these methods, because I know they sound pretty self explanatory, but I just have a feeling I will screw it up..
     
  8. Offline

    lycano

    Also .. you did it without a class but should work also.

    A suggestion: You should use the enum i had posted as you can then use a switch instead of those if-constructs.
     
  9. Offline

    kreashenz

    lycano But how would I use the enums, Also without the if statements?
     
  10. Offline

    lycano

    kreashenz well you can't really screw up. Instead of the map you do

    Code:
    KillStreak killstreak;
    
    Then init in constructor of your listener.

    Use the methods to add, remove and check your states.

    Okay ... you merge the classes into one and thats it ;) Well i can give you an example later when im at home.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  11. Offline

    kreashenz

    lycano Also, would the HashMap (
    Code:
    public HashMap<String,Integer> killstreak = new HashMap<String,Integer>();
    ) be needed after this, for anything? This is the first one in the public class Rewards class.
     
  12. Offline

    lycano

    kreashenz This is handled inside the class so you wont need it in the listener. It would be replaced by the class definition.
     
  13. Offline

    kreashenz

    lycano I have this code, but its sending a NPE on line 26..
    Code:java
    1. /*line 26 : */ int kills = killStreak.getKills(p.getName());

    I think this is causing it not to give the player kills, or add them or whatever, but here's the whole code, now.
    Code:java
    1. package me.kreashenz.kitpvp;
    2.  
    3. import java.util.HashMap;
    4. import java.util.Map;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.entity.PlayerDeathEvent;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.inventory.meta.ItemMeta;
    14. import org.bukkit.potion.PotionEffect;
    15. import org.bukkit.potion.PotionEffectType;
    16.  
    17. public class Rewards implements Listener {
    18.  
    19. KillStreak killStreak;
    20. Rewards(KillStreak killStreak){this.killStreak=killStreak;}
    21. public int timer = 999999999;
    22.  
    23. @EventHandler
    24. public void onKillStreakadd(PlayerDeathEvent e){
    25. Player p = e.getEntity().getPlayer();
    26. int kills = killStreak.getKills(p.getName());
    27. if(e.getEntity() instanceof Player){
    28. Player killer = p.getKiller();
    29. killStreak.setKills(p.getName(), 0);
    30. killStreak.addKills(killer.getName(), killStreak.getKills(killer.getName()) +1);
    31. }
    32. if(kills == 3){
    33. if(timer < 999999999){
    34. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, timer, 0));
    35. }
    36. Bukkit.broadcastMessage("§a" + p.getName() + "§6 is on a §a"
    37. + killStreak.getKills(p.getName()) + " §6kill streak! Someone end it now!");
    38. p.sendMessage("§aSpeed mode activate!");
    39. }
    40. if(kills == 5){
    41. if(timer < 999999999){
    42. p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, timer, 0));
    43. }
    44. Bukkit.broadcastMessage("§a" + e.getEntity().getPlayer().getName() + "§6 is on a §a"
    45. + killStreak.getKills(p.getName()) + " §6kill streak! Someone end it now!");
    46. p.sendMessage("§aStrength mode activate!");
    47. }
    48. if(kills == 7){
    49. ItemStack snowball = new ItemStack(Material.SNOW_BALL, 3);
    50. ItemMeta snowballMeta = snowball.getItemMeta();
    51. snowballMeta.setDisplayName("§rGrenades");
    52. snowball.setItemMeta(snowballMeta);
    53. p.getInventory().addItem(snowball);
    54. p.sendMessage("§aEnjoy your reward of a §6couple grenades§a. Use them wisely, though!");
    55. }
    56. if(kills == 10){
    57. if(timer < 999999999){
    58. p.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, timer, 0));
    59. }
    60. Bukkit.broadcastMessage("§a" + e.getEntity().getPlayer().getName() + "§6 is on a §a"
    61. + killStreak.getKills(p.getName()) + " §6kill streak! Someone end it now!");
    62. ItemStack helm = p.getInventory().getHelmet();
    63. ItemStack chest = p.getInventory().getChestplate();
    64. ItemStack legs = p.getInventory().getLeggings();
    65. ItemStack boots = p.getInventory().getBoots();
    66. helm.setDurability(helm.getType().getMaxDurability());
    67. chest.setDurability(chest.getType().getMaxDurability());
    68. legs.setDurability(legs.getType().getMaxDurability());
    69. boots.setDurability(boots.getType().getMaxDurability());
    70. p.getInventory().setHelmet(helm);
    71. p.getInventory().setChestplate(chest);
    72. p.getInventory().setLeggings(legs);
    73. p.getInventory().setBoots(boots);
    74. p.sendMessage("§aYou are on a great 10 kill streak!! Keep going!" +
    75. "You have been rewarded by having your §6armour fixed§a, and §6haste §aadded!");
    76. }
    77. if(kills == 15){
    78. if(timer < 999999999){
    79. p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, timer, 1));
    80. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, timer, 1));
    81. }
    82. Bukkit.broadcastMessage("§a" + e.getEntity().getPlayer().getName() + "§6 is on a §a"
    83. + killStreak.getKills(p.getName()) + " §6kill streak! Someone end it now!");
    84. p.sendMessage("§aYou are on a whopping 15 kill streak!!! Keep going!" +
    85. "You have been rewarded with double §6strength§a, and §6speed§a!");
    86. }
    87. }
    88.  
    89. public class KillStreak {
    90.  
    91. protected Map<String, Integer> playerKills = new HashMap<String, Integer>();
    92.  
    93. public KillStreak() {}
    94.  
    95. public boolean hasKills(String playerName) {
    96. return playerKills.containsKey(playerName);
    97. }
    98.  
    99. public boolean isOnKillingSpree(String playerName) {
    100. return (this.getKills(playerName) > 3);
    101. }
    102.  
    103. public Integer getKills(String playerName) {
    104. if (this.hasKills(playerName))
    105. return this.playerKills.get(playerName);
    106.  
    107. return new Integer(0);
    108. }
    109.  
    110. public void setKills(String playerName, Integer kills) {
    111. this.playerKills.put(playerName, kills);
    112. }
    113.  
    114. public void addKills(String playerName, Integer kills) {
    115. this.setKills(playerName, this.getKills(playerName) + kills);
    116. }
    117. }
    118. }
    119.  
     
  14. Offline

    lycano

    You fogot to init your class. Either init it directly in L19 or in the Listeners constructor.

    Line 19 in your code should be

    Code:
    KillStreak killStreak = new KillStreak();
    
    Or add the following code at L22 and leave 19 untouched
    Code:
    public Rewards() {
      this.killStreak = new KillStreak();
    }
    
    I prefer the latter cause of readability.

    Oh wait .. i didnt got L20 ;)

    Replace line 20 with the last code i gave ya "public Rewards() ..."

    Also you should keep variables above methods as a coding standard.

    A class has a header and a body. Think that the header describes everything the class needs like variables and the body is what you want to do with the class (methods). So keeping variables in the header is a good way to keep readability.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  15. Offline

    kreashenz

    lycano I have this somewhere, doesn't this matter?
    Code:java
    1. Rewards(KillStreak killStreak){this.killStreak=killStreak;}
    2.  
     
  16. Offline

    lycano

    well you should declare a class constructor as public cause of coding standards (so that others can see what you want) and because you might not want to rely on java default behavior.

    Anyways that line you posted would set the KillStreak class correctly if you are using
    Code:
    registerEvent(new Rewards(new KillStreak()))
    
    somewhere. Normally you do that in your onEnable method but as i dont know your JavaPlugin class you might also set this somewhere else....
     
  17. Offline

    kreashenz

    lycano What...? Error on that line..
    Code:
    No enclosing instance of type Rewards is accessible. Must qualify the allocation with an enclosing instance of type Rewards (e.g. x.new A() where x is an instance of Rewards).
     
  18. Offline

    lycano

    Ok, let me start over. The code you pasted line 20...

    Somewhere in your plugin you have to register the Event through registerEvent method right? Can you post those code lines too?
     
  19. Offline

    kreashenz

    lycano Don't worry now, I have it sorted, thanks to a friend. [Solved] :)
     
  20. Offline

    lycano

    Nice! Have fun.
     
Thread Status:
Not open for further replies.

Share This Page