Kill count

Discussion in 'Plugin Development' started by sipsi133, Dec 22, 2013.

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

    sipsi133

    How i can add kill count to item when player kills other player with it?
    Example if player1 kills player2 with sword, then sword lore should be Kills: 1 and for each kill +1

    help

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

    xTigerRebornx

    Use the PlayerDeathEvent, get the Killer of the Player that died, get the Killer's item in hand, get the data for it, get the lore, parse an int from it, add one to the int thats parsed, then set the lore to the new int
     
  3. Offline

    sipsi133

    Code:java
    1. @EventHandler
    2. public void onDeath(PlayerDeathEvent deathevent) {
    3. String killed = deathevent.getEntity().getName();
    4. String killer = deathevent.getEntity().getKiller().getName();
    5. Player killer1 = deathevent.getEntity().getKiller();
    6. Player killer2 = deathevent.getEntity();
    7. }


    I dont know how to do this
     
  4. Offline

    xTigerRebornx

    sipsi133
    Player killer = deathevent.getEntity().getKiller();
    ItemStack itemKilledWith = killer.getItemInHand();

    Use the itemKilledWith and check its lore, then continue to do what you wanted
     
  5. Offline

    sipsi133

    xTigerRebornx I dont know how to add +1 to lore and how to set lore
     
  6. Offline

    xTigerRebornx

    sipsi133 I'd reccomend you find a tutorial on it and learn more about how to manipulate ItemStacks and their metadata
     
  7. Offline

    sipsi133

    xTigerRebornx I can learn about it if you tell me how to do it :)
     
  8. Offline

    xTigerRebornx

    sipsi133 Once you get an ItemStack, you can do ItemMeta meta = <itemstack>.getItemMeta();
    <itemstack> is the name of the variable that you assigned to the itemstack. Once you do that you can use meta.getLore(); meta.setLore(); etc

    After you've set things, make sure you update the ItemMeta of the ItemStack using <itemstack>.setItemMeta(meta);
     
  9. Offline

    jusjus112

    sipsi133
    Try to update your lore!
    If player1 killed player2 set the lore to Kills: 1 and if they do antoher kill set the lore to Kills: 2
    Anyway, you can also use an hashmap! If an player has 2 kills, he is getting an sword with an lore Kills: 2
     
  10. Offline

    xTigerRebornx

    jusjus112 But if the server were to reset, that HashMap with the kills would be cleared, unless he saved it. I'm guessing that the OP just wants the kill count to be stored on the item itself, so he could check if the item has a kill lore, and if it does, get an int from it, then add one to that int, and set the new lore with the new int
     
  11. Offline

    jusjus112

    xTigerRebornx
    I am sure, about if you reload the server the arena/players would be cleared?
    Anyway, Did you tried to save your hashmap or players to an config?
     
  12. Offline

    xTigerRebornx

    jusjus112 He isn't using a Hashmap as far as we know, he wants to store the kill count on the item itself, not the player's kill count, but the number of kills with that item
     
  13. Offline

    jusjus112

    xTigerRebornx
    Yes, did you see what i said?
    If an player has an kill, and he has an item for 5 kills!
    The player is getting that item if an player has 5 kills!
    So use the HashMap<Kills,ItemStack>?
     
  14. Offline

    xTigerRebornx

    jusjus112 Why would there be a need for a HashMap at all? The data is all stored on the item itself. Storing and getting it from a hashmap is not needed, the OP is just asking how he would store the ammount of kills on an item, in which he could just set the lore to a number, and get that number everytime there is a kill with that weapon, then add one to it, and set the new number. I see no need for a HashMap
     
  15. Offline

    sipsi133

    xTigerRebornx Can you post me code how to set lore and add +1 to lore every time player kills player with that item.
    And if the lore is null when player kills player, then set lore to: "Kills: 1"
    And if you want, you can comment each line, so I can learn from it.
     
  16. Offline

    jusjus112

    xTigerRebornx
    I said that it maybe can use for it!
    You can use your idea and an more ideas!
    So if hey do not want an hashmap, good luck!

    sipsi133
    Try this? UNTESTED
    Code:java
    1. import java.util.ArrayList;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    10. import org.bukkit.event.player.PlayerJoinEvent;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class KillCount extends JavaPlugin implements Listener {
    15.  
    16. public ItemStack kill = new ItemStack(Material.STONE_SWORD); //make the item
    17. private ArrayList<String> killer1 = new ArrayList<String>(); //make the lore Arraylist
    18.  
    19. public void onEnable() {
    20. Bukkit.getServer().getPluginManager().registerEvents(this, this); //register your events
    21. }
    22.  
    23.  
    24. @EventHandler
    25. public void onPlayerJoin(PlayerJoinEvent e) { //adds the item to the player
    26. e.getPlayer().getInventory().contains(kill);
    27. e.getPlayer().getInventory().addItem(kill);
    28.  
    29. }
    30.  
    31.  
    32. @EventHandler
    33. public void onPlayerKill(EntityDamageByEntityEvent e) {
    34.  
    35. Player damager = (Player) e.getDamager(); //get the damager
    36.  
    37. if (e.getDamager() instanceof Player) { //cheks if the damager an player is
    38. damager.getInventory().contains(kill);
    39. damager.getInventory().addItem(kill);
    40. }
    41. if (e.getEntity() instanceof Player) {
    42. if (e.getEntity().isDead()) { //checks if the player is dead by the damager
    43. killer1.add(ChatColor.GREEN + "Kills: " + ChatColor.RED + +1 ); //adds this to the lore for the damager their item
    44. //TODO: Update the lore
    45. //TODO: Set the lore to killer
    46.  
    47.  
    48. }
    49. }
    50. }
    51.  
    52. }
    53.  


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

    sipsi133

    jusjus112 It didnt work, so i tried this:

    Code:java
    1. package io.github.sipsi133;
    2.  
    3. import java.util.ArrayList;
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.entity.Entity;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.entity.EntityDeathEvent;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class KillCount extends JavaPlugin implements Listener {
    14. private ArrayList<String> killer1 = new ArrayList<String>();
    15.  
    16. public void onEnable() {
    17. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    18. }
    19. @SuppressWarnings("unused")
    20. @EventHandler
    21. public void onPlayerKill(EntityDeathEvent e) {
    22.  
    23. Player killer = e.getEntity().getKiller();
    24. Entity killed = e.getEntity();
    25. ItemStack is = killer.getItemInHand();
    26. if(killer.getItemInHand() != null); {
    27. if(e.getEntity().isDead()) {
    28. killer1.add("Kills:" + +1);
    29. is.getItemMeta().setLore(killer1);
    30. }
    31. }if(killer.getItemInHand() == null); {
    32. if(e.getEntity().isDead()); {
    33. killer.sendMessage("Please use sword or bow !");
    34. }
    35. }
    36. }
    37. }

    but it didnt work.

    stack-trace:
    Code:
    11:27:12 [SEVERE] Could not pass event EntityDeathEvent to KillCount v0.0.2
    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_6_R3.event.CraftEventFactory.callEntityDeat
    hEvent(CraftEventFactory.java:344)
            at net.minecraft.server.v1_6_R3.EntityInsentient.dropDeathLoot(EntityIns
    entient.java:202)
            at net.minecraft.server.v1_6_R3.EntityLiving.die(EntityLiving.java:765)
            at net.minecraft.server.v1_6_R3.EntityLiving.damageEntity(EntityLiving.j
    ava:717)
            at net.minecraft.server.v1_6_R3.EntityBat.damageEntity(SourceFile:197)
            at net.minecraft.server.v1_6_R3.Entity.burn(Entity.java:802)
            at net.minecraft.server.v1_6_R3.Entity.move(Entity.java:705)
            at net.minecraft.server.v1_6_R3.EntityLiving.e(EntityLiving.java:1124)
            at net.minecraft.server.v1_6_R3.EntityLiving.c(EntityLiving.java:1451)
            at net.minecraft.server.v1_6_R3.EntityInsentient.c(EntityInsentient.java
    :306)
            at net.minecraft.server.v1_6_R3.EntityLiving.l_(EntityLiving.java:1284)
            at net.minecraft.server.v1_6_R3.EntityInsentient.l_(EntityInsentient.jav
    a:150)
            at net.minecraft.server.v1_6_R3.EntityBat.l_(SourceFile:107)
            at net.minecraft.server.v1_6_R3.World.entityJoinedWorld(World.java:1492)
     
            at net.minecraft.server.v1_6_R3.World.playerJoinedWorld(World.java:1467)
     
            at net.minecraft.server.v1_6_R3.World.tickEntities(World.java:1336)
            at net.minecraft.server.v1_6_R3.WorldServer.tickEntities(WorldServer.jav
    a:517)
            at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:5
    83)
            at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:2
    40)
            at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:4
    93)
            at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java
    :425)
            at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:5
    83)
    Caused by: java.lang.NullPointerException
            at io.github.sipsi133.KillCount.onPlayerKill(KillCount.java:25)
            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)
    It shows the message "Please use sword or bow!" everytime i kill zombie (or any mob) with sword or any item.
     
  18. Offline

    jusjus112

    sipsi133
    You must check if the e.getEntity an player is!
    with if "(e.getEntity instanceof Player)" or Player killed = (Player) e.getEntity

    sipsi133
    And BTW, Where do you get the itemstack from?
    You must have an itemstack to set the lore, you cant set the lore of an item in your hand!

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

    sipsi133

    @jusjus112
    Code:java
    1. package io.github.sipsi133;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.Entity;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.entity.EntityDeathEvent;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class KillCount extends JavaPlugin implements Listener {
    16. private ArrayList<String> killer1 = new ArrayList<String>(); //make the lore Arraylist
    17.  
    18. public void onEnable() {
    19. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    20. }
    21. @EventHandler
    22. public void onEntityKill(EntityDeathEvent e) {
    23. @SuppressWarnings("unused")
    24. Entity killed = (Entity) e.getEntity();
    25. Player killer = (Player) e.getEntity().getKiller();
    26. ItemStack is = killer.getInventory().getItemInHand();
    27. if(is != null); {
    28. if (e.getEntity() instanceof Entity) {
    29. if (e.getEntity().getKiller() instanceof Player) {
    30. if (e.getEntity().isDead()) {
    31. killer1.add(ChatColor.GREEN + "Kills: " + ChatColor.RED + +1 );
    32. killer.sendMessage("Lore set");
    33.  
    34. //TODO: Update the lore
    35. //TODO: Set the lore to killer
    36. }
    37. }
    38. }
    39. }
    40. }
     
  20. Offline

    jusjus112

  21. Offline

    sipsi133

    jusjus112 Its not setting the lore when i kill Entity

    jusjus112 It sends the "Lore set" message, but its not setting the lore.

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

    jusjus112

    sipsi133
    Try removing the +1 and looks if they update it?
    so the lore not without the +1
     
  23. Offline

    sipsi133

    jusjus112 Still not working. I tried many methods. Sometimes it shows error when zombie dies on lava, fire etc.
     
  24. Offline

    jusjus112

    sipsi133
    Can you send an pm for you full code?
     
  25. Offline

    sipsi133

  26. Offline

    Developing

    sipsi133 Bad practice. Never cast the entity to a player before you check if its a player. Even if you checked after casting , its pointless.
     
  27. Offline

    sipsi133

Thread Status:
Not open for further replies.

Share This Page