[Chat] Kill counter prefix

Discussion in 'Plugin Development' started by historio, Jul 15, 2013.

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

    autoit4you

    I would only save them in the onDisable() and load them in the onEnable()
     
  2. Offline

    historio

    autoit4you Okay and is there any chance that can you give me some tips on how do save this?
     
  3. Offline

    autoit4you

    This should work, if you put it into the main class:
    Code:java
    1. public void saveKills(HashMap<String, Integer> kills) {
    2. for(String name : kills.keySet()) {
    3. getConfig().set("kills." + name, kills.get(name));
    4. }
    5. saveConfig();
    6. }
    7.  
    8. public HashMap<String, Integer> getKills() {
    9. HashMap<String, Integer> kills = new HashMap<String, Integer>();
    10. ConfigurationSection players = getConfig().getConfigurationSection("kills");
    11. if(players == null)
    12. return kills;
    13. for(String name : players.getKeys()) {
    14. kills.put(name, (Integer)getConfig().getInt("kills." + name));
    15. }
    16. return kills;
    17. }
     
  4. Offline

    historio

    autoit4you What do I do for the OnDisable and OnEnable?

    autoit4you If a player has { 0 } then if gets killed it looks like {-1} how would I change this so if the player has {0} it cant go any lower?

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

    autoit4you

    Something like this:
    Code:java
    1. private ChatListener cl = null;
    2.  
    3. public void onEnable() {
    4. cl = new ChatListener();
    5. cl.kills = getKills();
    6. getServer().getPluginManager().registerEvents(cl, this);
    7. }
    8.  
    9. public void onDisable() {
    10. saveKills(cl.kills);
    11. }


    post the code of the onPlayerDeath function

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

    historio

    autoit4you
    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST)
    2. public void onPlayerDeath(PlayerDeathEvent event) {
    3. if (event.getEntity().getKiller() instanceof Player) {
    4. Player killer = event.getEntity().getKiller();
    5. Player victim = event.getEntity();
    6. if (!kills.containsKey(killer.getName()))
    7. kills.put(killer.getName(), 0);
    8. if (!kills.containsKey(victim.getName()))
    9. kills.put(victim.getName(), 0);
    10.  
    11. kills.put(killer.getName(), kills.get(killer.getName()) + 2);
    12. kills.put(victim.getName(), - 4);

    and now the prefix shows up like this
    [​IMG]
     
  7. Offline

    autoit4you

    Are you sure you do not set a displayname?
     
  8. Offline

    ReviveDept

    You can also use "p.setPrefix()" instead of "p.setDisplayName()"
     
  9. Offline

    historio

    Code:java
    1.  
    2. import java.util.HashMap;
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.EventPriority;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.entity.PlayerDeathEvent;
    10. import org.bukkit.event.player.AsyncPlayerChatEvent;
    11.  
    12. public class ChatListener implements Listener {
    13.  
    14. public HashMap<String, Integer> kills = new HashMap<String, Integer>();
    15.  
    16. @EventHandler(priority = EventPriority.HIGHEST)
    17. public void OnPlayerChat(AsyncPlayerChatEvent e) {
    18. int killAmount = 0;
    19. if (kills.containsKey(e.getPlayer().getName()))
    20. killAmount = kills.get(e.getPlayer().getName());
    21. String format = e.getFormat();
    22. e.setFormat(ChatColor.GRAY + "{ " + ChatColor.GOLD + killAmount
    23. + ChatColor.GRAY + " } " + ChatColor.RESET + format);
    24. }
    25.  
    26. @EventHandler(priority = EventPriority.HIGHEST)
    27. public void onPlayerDeath(PlayerDeathEvent event) {
    28. if (event.getEntity().getKiller() instanceof Player) {
    29. Player killer = event.getEntity().getKiller();
    30. Player victim = event.getEntity();
    31. if (!kills.containsKey(killer.getName()))
    32. kills.put(killer.getName(), 0);
    33. if (!kills.containsKey(victim.getName()))
    34. kills.put(victim.getName(), 0);
    35.  
    36. kills.put(killer.getName(), kills.get(killer.getName()) + 2);
    37. kills.put(victim.getName(), - 4);
    38. }
    39. }
    40. }
    41.  
     
  10. Offline

    autoit4you

    add a System.out.println("AsyncChatEvent"); to the onPlayerChat function
    And then chat ingame and copy the console output.
     
  11. Offline

    historio

    autoit4you

    [INFO] AsyncChatEvent
    [INFO] AsyncChatEvent
    [INFO] { 0 } { 2 } <historio13> t
     
  12. Offline

    autoit4you

    looks like the AsyncPlayerChatEvent gets fired two times

    Code:
    if(!format.startsWith(ChatColor.GRAY + "{") {
      //the e.setFormat stuff
    }
     
  13. Offline

    historio

    autoit4you Fixed and how do I stop {0} from going like {-4}
     
  14. Offline

    autoit4you

    You currently set it for everyone who is killed to -4.
    Do you want to set it for everyone who is killed by a player to 0?
    Or do you want to substract 4 from the current kills the player has?
     
  15. Offline

    historio

    autoit4you substract 4 from the current kills the player has

    Code:java
    1. kills.put(killer.getName(), kills.get(killer.getName()) + 2);
    2. kills.put(victim.getName()kills.get(victim.getName()) - 4);
    3. }


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

    autoit4you

    If you want that the kills number does not go under 0 use this:
    Code:java
    1. if(kills.get(victim.getName()) -4 < 0) {
    2. kills.put(victim.getName(), 0);
    3. } else {
    4. kills.put(victim.getName(), kills.get(victim.getName()) - 4);
    5. }
     
  17. Offline

    historio

    autoit4you Thanks :D

    autoit4you How would I do this a player reaches Gray{ Gold 100 Gray} then his/her prefix changes colour
    to like Green{ Dark_Green100 Green} ?

    Hope this makes sense.

    P.s sorry autoit for asking you more questions D:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
    autoit4you likes this.
  18. Offline

    autoit4you

    get the kills and if he has 100 or more kills it uses format a else it uses format b
     
  19. Offline

    historio

    autoit4you Like this? :

    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST)
    2. public void OnPlayerChat(AsyncPlayerChatEvent e) {
    3. int killAmount = 0;
    4. if (kills.containsKey(e.getPlayer().getName()))
    5. killAmount = kills.get(e.getPlayer().getName());
    6. String format = e.getFormat();
    7. if (!format.startsWith(ChatColor.GRAY + "{")) {
    8. e.setFormat(ChatColor.GRAY + "{ " + ChatColor.GOLD + killAmount
    9. + ChatColor.GRAY + " } " + ChatColor.RESET + format);
    10. } else {
    11.  
    12. if(killAmount = int 100);
    13. e.setFormat(ChatColor.BLACK + "{ " + ChatColor.GREEN + killAmount
    14. + ChatColor.BLACK + " } " + ChatColor.RESET + format);
    15. }
    16. }


    But (Line 28) " if(killAmount = int 100);" gives me this error "Type mismatch: cannot convert from int to boolean"
     
  20. Offline

    autoit4you

    historio nearly.
    Code:java
    1. if (!format.startsWith(ChatColor.GRAY + "{")) {
    2. if(killAmount >= 100) {
    3. e.setFormat(ChatColor.BLACK + "{ " + ChatColor.GREEN + killAmount + ChatColor.BLACK + " } " + ChatColor.RESET + format);
    4. } else {
    5. e.setFormat(ChatColor.GRAY + "{ " + ChatColor.GOLD + killAmount + ChatColor.GRAY + " } " + ChatColor.RESET + format);
    6. }


    killAmount >= 100 means killAmount equals 100 or is higher than 100
     
  21. Offline

    historio

    autoit4you Thanks I'll test it tomorrow because I have to go now Bye

    autoit4you How would I make a command that would add kills to a player.

    I have done this so far:

    Code:java
    1. if (commandLabel.equalsIgnoreCase("Addkills")) {
    2. Player p = (Player) sender;
    3. if(p.hasPermission("AdvancedKitPvp.addkills"))
    4. kills.put(p.getName(), kills.get(p.getName()));


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

    autoit4you

    check if the arguments list is > 0
    then parse argument0 to an Integer
    than add the parsed number to the kills the player already has
     
Thread Status:
Not open for further replies.

Share This Page