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.
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
Code:java @EventHandler public void onDeath(PlayerDeathEvent deathevent) { String killed = deathevent.getEntity().getName(); String killer = deathevent.getEntity().getKiller().getName(); Player killer1 = deathevent.getEntity().getKiller(); Player killer2 = deathevent.getEntity(); } I dont know how to do this
sipsi133 Player killer = deathevent.getEntity().getKiller(); ItemStack itemKilledWith = killer.getItemInHand(); Use the itemKilledWith and check its lore, then continue to do what you wanted
sipsi133 I'd reccomend you find a tutorial on it and learn more about how to manipulate ItemStacks and their metadata
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);
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
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
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?
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
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>?
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
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.
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 import java.util.ArrayList; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.Material;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.entity.EntityDamageByEntityEvent;import org.bukkit.event.player.PlayerJoinEvent;import org.bukkit.inventory.ItemStack;import org.bukkit.plugin.java.JavaPlugin; public class KillCount extends JavaPlugin implements Listener { public ItemStack kill = new ItemStack(Material.STONE_SWORD); //make the itemprivate ArrayList<String> killer1 = new ArrayList<String>(); //make the lore Arraylist public void onEnable() {Bukkit.getServer().getPluginManager().registerEvents(this, this); //register your events} @EventHandlerpublic void onPlayerJoin(PlayerJoinEvent e) { //adds the item to the playere.getPlayer().getInventory().contains(kill);e.getPlayer().getInventory().addItem(kill); } @EventHandlerpublic void onPlayerKill(EntityDamageByEntityEvent e) { Player damager = (Player) e.getDamager(); //get the damager if (e.getDamager() instanceof Player) { //cheks if the damager an player isdamager.getInventory().contains(kill);damager.getInventory().addItem(kill);}if (e.getEntity() instanceof Player) {if (e.getEntity().isDead()) { //checks if the player is dead by the damagerkiller1.add(ChatColor.GREEN + "Kills: " + ChatColor.RED + +1 ); //adds this to the lore for the damager their item//TODO: Update the lore//TODO: Set the lore to killer }}} } EDIT by Moderator: merged posts, please use the edit button instead of double posting.
jusjus112 It didnt work, so i tried this: Code:java package io.github.sipsi133; import java.util.ArrayList;import org.bukkit.Bukkit;import org.bukkit.entity.Entity;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.entity.EntityDeathEvent;import org.bukkit.inventory.ItemStack;import org.bukkit.plugin.java.JavaPlugin; public class KillCount extends JavaPlugin implements Listener {private ArrayList<String> killer1 = new ArrayList<String>(); public void onEnable() {Bukkit.getServer().getPluginManager().registerEvents(this, this);}@SuppressWarnings("unused")@EventHandlerpublic void onPlayerKill(EntityDeathEvent e) { Player killer = e.getEntity().getKiller();Entity killed = e.getEntity();ItemStack is = killer.getItemInHand();if(killer.getItemInHand() != null); {if(e.getEntity().isDead()) {killer1.add("Kills:" + +1);is.getItemMeta().setLore(killer1);}}if(killer.getItemInHand() == null); {if(e.getEntity().isDead()); {killer.sendMessage("Please use sword or bow !");}}}} 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.
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.
@jusjus112 Code:java package io.github.sipsi133; import java.util.ArrayList; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.entity.Entity;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.entity.EntityDeathEvent;import org.bukkit.inventory.ItemStack;import org.bukkit.plugin.java.JavaPlugin; public class KillCount extends JavaPlugin implements Listener {private ArrayList<String> killer1 = new ArrayList<String>(); //make the lore Arraylist public void onEnable() {Bukkit.getServer().getPluginManager().registerEvents(this, this);}@EventHandlerpublic void onEntityKill(EntityDeathEvent e) {@SuppressWarnings("unused")Entity killed = (Entity) e.getEntity();Player killer = (Player) e.getEntity().getKiller();ItemStack is = killer.getInventory().getItemInHand();if(is != null); {if (e.getEntity() instanceof Entity) {if (e.getEntity().getKiller() instanceof Player) {if (e.getEntity().isDead()) {killer1.add(ChatColor.GREEN + "Kills: " + ChatColor.RED + +1 );killer.sendMessage("Lore set"); //TODO: Update the lore//TODO: Set the lore to killer}}}}}
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.
jusjus112 Still not working. I tried many methods. Sometimes it shows error when zombie dies on lava, fire etc.
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.