I got a sword that counts how many kills you got with the sword and I tried PHP: setCustomName($currentItemName." ".$player->getKills()
PHP: $item = Item::get(276, 0, 1);$item->setCustomName($item->getName() . " " . $player->getKills());$player->getInventory()->setItemInHand($item);
Thats would be laggy, what i tried is update itemInHand name on PlayerDeathEvent but the previous kills they have stays there
Do you mean that it always displays 1 less kill than it should, or that it always displays 0 kills? Also, you never mentioned what about your original attempt isn't functioning as intended.
Ohh, I see. In your original code, what is $currentItemName? If I'm not mistaken, using $item->getName() . " " . $playerKills would solve the problem, since it uses the item's default name rather than the custom one.
I would do it on the entity damage by entity event. I would get the id of the item, make a new one and set its name to the damager's kills and set it to the slot the old item was.
Then you could mess around with turning the string into an array of words, search the array for "Kills", clear everything in the array after that, and place the new kill value there. Also, use PlayerDeathEvent, similar to what I did here. It'll be called a lot less, and is the simplest way to get the killer I know of. EntityDamageEvent will be called a lot more often, needlessly. Since you set the kills in the custom name, you can only change the number value by searching the existing string and replacing the number you searched for.
That doesn't make any sense, since he wants to do this after a player kills another player. You don't want to add a kill every time a player holds the item.
That's what they'll have to do, and I think OP understands that, but they're struggling with retaining the item's custom name, but also while changing that custom name. One workaround, like @MalakasPlayzMCPE mentioned, may be to store the item's custom name in a file, so that OP could use something more like their original code. I only want to know if OP plans on having a kill count on all items, or just specific ones, and if it needs to be done all the time, or just after a condition is met, like a command. If I knew that, I could be a bit more helpful.