[Chat] Kill counter prefix

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

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

    historio

    I need some tips/help on how to make a kill counter prefix

    Example: [<Amount of kills>] <PlayerName>

    Here's what I know so far:
    1.It's something todo with PlayerChatEvent or AsyncPlayerChatEvent and needs counter and ints/numbers also to do with player.setFormat but that's all I know so I need help with the rest.
     
  2. Offline

    autoit4you

    Code:java
    1. int kills = <Amount of kills>;
    2. String format = event.getFormat();
    3. event.setFormat("[" + kills + "] " + format);
     
  3. Offline

    historio

    How would a make this go up when the player kills a player ?

    Something todo with PlayerDeathEvent and kills + 1?

    Example: A player has [0] the player kills a other player they now have [1] then the player that has [1] gets killed so the player now has [0] again so the player that killed this player now has [1]. Hope this makes sense if it doesn't please tell me.

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

    Devil0s

    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent event) {
    3. if(event.getEntity().getKiller() instanceof Player) {
    4. Player killer = event.getEntity().getKiller();
    5. //TODO: Get kills of killer, increment the value by one and save the new value.
    6. }
    7. }


    Take a look to this Tutorial. It's about saving and loading of YAML files.
    To save the values permanently you have to use files/databases and I think the easiest and best way is to use a YAML file.
     
    historio likes this.
  5. Offline

    historio

    @Devils0s thanks I'll do this now.
     
  6. Offline

    Devil0s

    Please use the thanks button. :p
     
  7. Offline

    historio

    @DevilOs So i would add something like this?
    Code:java
    1. killer.setDisplayName(ChatColor.GRAY + "{ " + ChatColor.GOLD + kills
    2. + ChatColor.GRAY + " }");


    How do you spell your username? :D

    Then add some like kills +1 somewhere?

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

    Alxlre

    historio Yes. You can now easily kills++;.
     
  9. Offline

    historio

    I did this:
    Code:java
    1. killer.setDisplayName(ChatColor.GRAY + "{ " + ChatColor.GOLD + kills + 1
    2. + ChatColor.GRAY + " }");


    How would I keep this repeating and how would I remove it if the player dies?

    Bump.

    This is all my code so far (hope this helps you help me):
    Code:java
    1. import org.bukkit.ChatColor;
    2. import org.bukkit.entity.Player;
    3. import org.bukkit.event.EventHandler;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.event.entity.PlayerDeathEvent;
    6. import org.bukkit.event.player.AsyncPlayerChatEvent;
    7. import org.bukkit.event.player.PlayerJoinEvent;
    8.  
    9. public class ChatListener implements Listener {
    10.  
    11. public int kills = 0;
    12.  
    13. @EventHandler
    14. public void OnPlayerChat(AsyncPlayerChatEvent e) {
    15. int kills = 0;
    16. String format = e.getFormat();
    17. e.setFormat("[" + kills + "] " + format);
    18. }
    19.  
    20. @EventHandler
    21. public void onPlayerjoin(PlayerJoinEvent e) {
    22. Player p = e.getPlayer();
    23. p.setDisplayName(ChatColor.GRAY + "{ " + ChatColor.GOLD + kills
    24. + ChatColor.GRAY + " }");
    25. }
    26.  
    27. @EventHandler
    28. public void PlayerDeath(PlayerDeathEvent e) {
    29. if(e.getEntity().getKiller() instanceof Player) {
    30. Player killer = e.getEntity().getKiller();
    31. killer.setDisplayName(ChatColor.GRAY + "{ " + ChatColor.GOLD + kills + 1
    32. + ChatColor.GRAY + " }");
    33.  
    34. }
    35.  
    36. }
    37. }
    38.  


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

    Devil0s

    Code:java
    1. Player victim = event.getEntity();
    Get the victim and set the victims kills to 0.​
    That only set it to 0 if the victim is killed by a player.​
    Accidents or killed by NPCs don't count.​
    You have to save the players and their kills (I would prefer a HashMap) and then read out and update the values.​

    Code:java
    1.  
    2.  
    3. public HashMap<Player, Integer> kills = new HashMap<Player, Integer>();
    4.  
    5. @EventHandler
    6. public void onPlayerDeath(PlayerDeathEvent event) {
    7. if(event.getEntity().getKiller() instanceof Player) {
    8. Player killer = event.getEntity().getKiller();
    9. Player victim = event.getEntity();
    10. if(!kills.containsKey(killer)) kills.put(killer, 0);
    11. if(!kills.containsKey(victim)) kills.put(victim, 0);
    12.  
    13. kills.put(killer, kills.get(killer)+1);
    14. kills.put(victim, 0);
    15. }
    16. }


    That should work.
    To get the amount of a player use the kills.get(player); method that returns the current kills value.

    Code:java
    1. if(!kills.containsKey(player)) kills.put(player, 0);
    2. int killAmount = kills.get(player);


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  11. Using a hash map would work, but personally I'd work straight out a yml file. Someone already linked to the tutorial about configuration files.
    On player death, int kills = config.getInt(playerName)
    Config.set(playerName, kills++)

    Same for player that died, but then kills--
     
  12. Offline

    beastman3226

    historio
    If you want it to be {Kills} <name> then you should do this aswell:
    Code:java
    1. public HashMap<String, int> nameKills = new HashMap<>();
    2. @EventHandler
    3. public void playerDeath(PlayerDeathEvent e) {
    4. if(e.getEntity().getKiller() instanceof Player) {
    5. Player killer = e.getEntity().getKiller();
    6. killer.setDisplayName(ChatColor.GRAY + "{" + (nameKills.get(killer.getName) + 1) + "}" + killer.getName());
    7. nameKills.put(killer.getName(), nameKills.get(killer.getName) + 1);
    8. }
    9. }
     
  13. Offline

    autoit4you

    Resolved?
     
  14. Offline

    historio

    autoit4you Only justed tested it now and I'm getting errors

    [​IMG]

    The other errors are the same too.

    [​IMG]
     
  15. Offline

    autoit4you

    replace
    Code:text
    1. public int kills = 0;

    with
    Code:text
    1. public HashMap<String, int> kills = new HashMap<String, int>();


    also remove the setdisplayname in onPlayerJoin as the kills are should be shown on every chat event(with the OnPlayerChat).
     
  16. Offline

    historio

    autoit4you I get this error on the int it says "Syntax error on token "int", Dimensions expected after this "
    Code:java
    1. public HashMap<String, int> kills = new HashMap<String, int>();
     
  17. Offline

    autoit4you

    Okay change int to Integer
     
  18. Offline

    historio

    autoit4you Now "put" is giving me an error "The method put(String, Integer) in the type HashMap<String,Integer> is not applicable for the arguments (Player, int)"

    Code:java
    1. if (!kills.containsKey(killer))
    2. kills.put(killer, 0);
    3. if (!kills.containsKey(victim))
    4. kills.put(victim, 0);
    5.  
    6. kills.put(killer, kills.get(killer) + 1);
    7. kills.put(victim, 0);


    autoit4you Sorry if I'm being annoying :confused:

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

    autoit4you

    replace killer with killer.getName()
     
  20. Offline

    historio

    @aurotoit4you My server console now gives me this error:
    Code:
    14:11:44 [SEVERE] Could not pass event PlayerDeathEvent to AdvancedKitPvP v3.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.TimedRegisteredListener.callEvent(TimedRegisteredLi
    stener.java:30)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:478)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:463)
            at org.bukkit.craftbukkit.v1_5_R3.event.CraftEventFactory.callPlayerDeat
    hEvent(CraftEventFactory.java:340)
            at net.minecraft.server.v1_5_R3.EntityPlayer.die(EntityPlayer.java:294)
            at net.minecraft.server.v1_5_R3.EntityLiving.damageEntity(EntityLiving.j
    ava:764)
            at net.minecraft.server.v1_5_R3.EntityHuman.damageEntity(EntityHuman.jav
    a:684)
            at net.minecraft.server.v1_5_R3.EntityPlayer.damageEntity(EntityPlayer.j
    ava:359)
            at net.minecraft.server.v1_5_R3.EntityHuman.attack(EntityHuman.java:874)
     
            at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java
    :1124)
            at net.minecraft.server.v1_5_R3.Packet7UseEntity.handle(SourceFile:36)
            at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:218
    )
            at net.minecraft.server.v1_5_R3.PlayerConnection.d(PlayerConnection.java
    :115)
            at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
            at org.spigotmc.MultiplexingServerConnection.b(MultiplexingServerConnect
    ion.java:72)
            at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:5
    83)
            at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:2
    27)
            at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:4
    72)
            at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java
    :404)
            at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:5
    73)
    Caused by: java.lang.NullPointerException
            at me.historio13.AdvancedKitPvp.ChatListener.onPlayerDeath(ChatListener.
    java:35)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
            ... 21 more
     
  21. Offline

    autoit4you

    What is in the line 35?
     
  22. Offline

    historio

  23. Offline

    autoit4you

    Code:
    at me.historio13.AdvancedKitPvp.ChatListener.onPlayerDeath(ChatListener.
    java:35)
    Just give me the current 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
  24. Offline

    historio

    autoit4you

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


    and here is my main class code:
    Code:java
    1. package me.historio13.AdvancedKitPvp;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.plugin.PluginDescriptionFile;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public final class AdvancedKitPvp extends JavaPlugin implements Listener {
    10.  
    11. public final Logger logger = Logger.getLogger("minecraft");
    12. public static AdvancedKitPvp Plugin;
    13.  
    14. @Override
    15. public void onEnable() {
    16. getServer().getPluginManager().registerEvents(this, this);
    17. getCommand("akp").setExecutor(new AdvancedKitPvpCommandExecutor());
    18. getServer().getPluginManager()
    19. .registerEvents(new Eventlistener(), this);
    20. getServer().getPluginManager().registerEvents(new ChatListener(), this);
    21. }
    22.  
    23. @Override
    24. public void onDisable() {
    25. PluginDescriptionFile pdfFile = this.getDescription();
    26. this.logger.info(pdfFile.getName() + " has been Disabled!");
    27. }
    28. }
     
  25. Offline

    autoit4you

    try this:
    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()) + 1);
    12. kills.put(victim.getName(), 0);
    13. }
    14. }
     
    xTrollxDudex likes this.
  26. Offline

    historio

    autoit4you I have no errors now but its not doing anything when a player dies.
     
  27. Offline

    autoit4you

    are you sure?

    replace the OnPlayerChat function with 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. e.setFormat(ChatColor.GRAY + "{ " + ChatColor.GOLD + kills
    8. + ChatColor.GRAY + " } " + ChatColor.RESET + format);
    9. }


    Then kill another player and then write something in the chat.
     
  28. Offline

    historio

    autoit4you It looks like this
    [​IMG]

    How would I make it look like this?

    {1} <Player>
    {0} historio13

    autoit4you Again I'm sorry if I'am being annoying.

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

    autoit4you

    You did nothing wrong :) I just forgot to change kills to killAmount:
    Code:java
    1. e.setFormat(ChatColor.GRAY + "{ " + ChatColor.GOLD + killAmount
    2. + ChatColor.GRAY + " } " + ChatColor.RESET + format);


    Edit:
    You do not annoy me :)
     
  30. Offline

    historio

    autoit4you Thanks :D now I'm going to have a go at saving these kills.

    autoit4you would I do somelike this to save the kills?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
Thread Status:
Not open for further replies.

Share This Page