Killstreak annoucements (KitPvP)

Discussion in 'Plugin Development' started by Fhbgsdhkfbl, Apr 15, 2014.

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

    Fhbgsdhkfbl

    Hey bukkit forums!
    I am coding a kitpvp plugin (Still)
    And I am trying to make killstreak announcements, like when a player gets 3, 5, 10, etc etc, you get my point, it will broadcast (Player) has gotten a killstreak of 3.

    But this doesn't seem to work



    public static HashMap<String, Integer> killstreak = new HashMap<String, Integer>();
    Code:
    @EventHandler
        public void onDeath(PlayerDeathEvent e) {
            if (e.getEntity().getKiller() instanceof Player && e.getEntity() instanceof Player) {
                Player k = e.getEntity().getKiller();
                Player p = e.getEntity().getPlayer();
                if(killstreak.get(k.getName()) == 5){
                    Bukkit.broadcastMessage(k.getName() + ChatColor.GRAY + " Has a killstreak of 5!");
                }
                if(killstreak.get(k.getName()) == 8){
                    Bukkit.broadcastMessage(k.getName() + ChatColor.GRAY + " Has a killstreak of 8!");
                }
                if(killstreak.get(k.getName()) == 10){
                    Bukkit.broadcastMessage(k.getName() + ChatColor.GRAY + " Has a killstreak of 10!");
                }
                if(killstreak.get(k.getName()) == 15){
                    Bukkit.broadcastMessage(k.getName() + ChatColor.GRAY + " Has a killstreak of 15!");
                }
                if(killstreak.get(k.getName()) == 20){
                    Bukkit.broadcastMessage(k.getName() + ChatColor.GRAY + " Has a killstreak of 20!");
                }
                if(killstreak.get(k.getName()) == 25){
                    Bukkit.broadcastMessage(k.getName() + ChatColor.GRAY + " Has a killstreak of 25!");
                }
                if(killstreak.get(k.getName()) == 30){
                    Bukkit.broadcastMessage(k.getName() + ChatColor.GRAY + " Has a killstreak of 30!");
                }
                if(killstreak.get(k.getName()) == 35){
                    Bukkit.broadcastMessage(k.getName() + ChatColor.GRAY + " Has a killstreak of 35!");
                }
                if(killstreak.get(k.getName()) == 40){
                    Bukkit.broadcastMessage(k.getName() + ChatColor.GRAY + " Has a killstreak of 40!");
                }
                if(killstreak.get(k.getName()) == 50){
                    Bukkit.broadcastMessage(k.getName() + ChatColor.GRAY + " Has a killstreak of 50!");
                }
                if (killstreak.get(p.getName()) > 5) {
                    Bukkit.broadcastMessage(k.getName() + ChatColor.GRAY + " just ended" + p.getName() + " 's KillStreak");
                }
            }
        }
     
  2. Offline

    Garris0n

    Well are you ever increasing the value? Also, why not just put the variable in the string instead of using a ridiculous number of if statements?
     
  3. Offline

    Fhbgsdhkfbl

  4. Offline

    NonameSL

    Okay... This is a Sisyphean task.
    int i = killstreak.get(k.getName());
    if(i==5||i==8||i==10||i==15||i==20||i==25||i==30||i==35||i==40||i==50){
    is a much easier way.

    Plus, you are not modifying the players killstreak when they kill/get killed. The full code is:

    Code:java
    1.  
    2. public static HashMap<String, Integer> killstreak = new HashMap<String, Integer>();
    3. @EventHandler
    4. public void onDeath(PlayerDeathEvent e) {
    5. if (e.getEntity().getKiller() instanceof Player && e.getEntity() instanceof Player) {
    6. Player k = e.getEntity().getKiller();
    7. Player p = e.getEntity().getPlayer();
    8. killstreak.put(k.getName(), killstreak.get(k.getName())+1);
    9. killstreak.put(p.getName(), 0);
    10. int i = killstreak.get(k.getName());
    11. if(i==5||i==8||i==10||i==15||i==20||i==25||i==30||i==35||i==40||i==50){
    12. Bukkit.broadcastMessage(k.getName()+ChatColor.GRAY+" has a killstreak of "+i+"!");
    13. }
    14. if (killstreak.get(p.getName()) > 5) {
    15. Bukkit.broadcastMessage(k.getName() + ChatColor.GRAY + " just ended" + p.getName() + " 's KillStreak");
    16. }
    17. }
    18. }
    19.  
    20.  
     
  5. Offline

    Fhbgsdhkfbl

    NonameSL
    thank you so much, I will remember this in the future! :3

    NonameSL

    I must be an idiot or something but after a player gets 5 kills, it's not announcing that he/she got the kills.
    Idk why it's doing that.

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

    Garris0n

    Is the event registered?
     
  7. Offline

    Fhbgsdhkfbl

    Garris0n
    Well it's in my Main class, do I still need to register the events?
    Isn't it enabled?
    Code:
    @Override
        public void onEnable(){
            registerCommands();
            getServer().getPluginManager().registerEvents(this, this);
            setupEconomy();
        }
     
  8. Offline

    Garris0n

    Yes you need to register if it's in your main class, but it looks like you have done so if that's your onEnable method. Try adding some debug messages to see where it's failing.
     
  9. Offline

    GaaTavares

    TIP: If you are going to announce just mutiples of 5 killstreaks I would use (killstreak) % 5 == 0 to check if it's multiple of 5. Much more clean and efficient
     
  10. Offline

    Fhbgsdhkfbl

  11. Offline

    GaaTavares

    An example:
    Code:java
    1. int killstreak = 5 // let's say the killstreak is 5.
    2. // now we gotta check if the killstreak is a multiple of 5:
    3. if (killstreak % 5 == 0){
    4. // will return true because 5 does is a multiple of 5.
    5. }
    6. killstreak = 7; // now we re-set our killstreak to 7
    7. // then we do the same thing
    8. if (killstreak % 5 == 0){
    9. //it will return false because killstreak is not a multiple of 5.
    10. }
    11.  

    The most explained possible.
     
  12. Offline

    Fhbgsdhkfbl

    Garris0n
    You're not explaining why it's not broadcasting the message when a player gets 5 kills in a row
     
  13. Offline

    Garris0n

     
  14. Offline

    Fhbgsdhkfbl

    Garris0n
    Idk how to do debug messages
     
  15. Offline

    Anonomoose

    Add some system output messages after each if statement. If it doesn't reach message x, then it hasn't passed the statement, so you know where it's failing.
     
  16. Offline

    NonameSL

    x%y means the remnat of x/y, like the remnat of 5/2 is 1.
    GaaTavares I didnt think of that.
    Code:java
    1. public static HashMap<String, Integer> killstreak = new HashMap<String, Integer>();
    2. @EventHandler
    3. public void onDeath(PlayerDeathEvent e) {
    4. if (e.getEntity().getKiller() instanceof Player && e.getEntity() instanceof Player) {
    5. Player k = e.getEntity().getKiller();
    6. Player p = e.getEntity().getPlayer();
    7. killstreak.put(k.getName(), killstreak.get(k.getName())+1);
    8. killstreak.put(p.getName(), 0);
    9. int i = killstreak.get(k.getName());
    10. if(i%5==0||i==8){
    11. Bukkit.broadcastMessage(k.getName()+ChatColor.GRAY+" has a killstreak of "+i+"!");
    12. }
    13. if (killstreak.get(p.getName()) > 5) {
    14. Bukkit.broadcastMessage(k.getName() + ChatColor.GRAY + " just ended" + p.getName() + " 's KillStreak");
    15. }
    16. }
    17. }

    This should work. If not please send the full code.
     
Thread Status:
Not open for further replies.

Share This Page