1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

How to update item name

Discussion in 'Development' started by Levi, Jun 25, 2018.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    I got a sword that counts how many kills you got with the sword and I tried
    PHP:
    setCustomName($currentItemName." ".$player->getKills()
     
  2. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    PHP:
    $item Item::get(27601);
    $item->setCustomName($item->getName() . " " $player->getKills());
    $player->getInventory()->setItemInHand($item);
     
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    ... what?
     
  4. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    Do it on task or event check if player is holding sword name sword kills if not than set it
     
  5. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    Thats would be laggy, what i tried is update itemInHand name on PlayerDeathEvent but the previous kills they have stays there
     
  6. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    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.
     
  7. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    It like ends up like this:


     
  8. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    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.
     
  9. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    But i wish to use the custom one
     
  10. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    Use a config file.
     
  11. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    Do it on Player hold item event idk event
     
  12. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    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.
     
  13. KHAV

    KHAV Baby Zombie

    Messages:
    152
    GitHub:
    xkhhv
    it is PlayerItemHeldEvent am i right?
     
    xXNiceAssassinlo YT likes this.
  14. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    This is being called after the player holds the item.
     
  15. KHAV

    KHAV Baby Zombie

    Messages:
    152
    GitHub:
    xkhhv
    Yeah you right
     
  16. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
  17. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    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.
     
  18. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    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.
     
  19. KHAV

    KHAV Baby Zombie

    Messages:
    152
    GitHub:
    xkhhv
    What about if he make when the player kill someone remove the item from the player and add it again
     
  20. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    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.
     
    KHAV likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.