Hello, so basically I want to update the lore of an item the player is holding in his hand after he kills an enemy. Im doing it like this: PHP: $itemInHand->setLore([$text]);$killer->getInventory()->setItemInHand($itemInHand);$killer->sendMessage(TextFormat::LIGHT_PURPLE . "Collected a Soul!"); (Killer is the player) However: If I kill someone in creative mode the lore updates successfully and in survival mode it doesnt. I am really confused and have no idea what is going on.
Here is the whole function for context. PHP: public function onEntityDeath(EntityDeathEvent $event){ //Get killed entity to access cause $entity = $event->getEntity(); //Get cause to access killer $cause = $entity->getLastDamageCause(); //Only increase Soul count only if the killed entity was a player if($cause instanceof EntityDamageByEntityEvent and ($entity instanceof Player)){ $killer = $cause->getDamager(); //Only player has an inventory if($killer instanceof Player){ $itemInHand = $killer->getInventory()->getItemInHand(); $lore = $itemInHand->getLore(); //Check if a Soul Tracker has been held in hand for($i = 0; $i<count($lore); ++$i){ if(strpos($lore[$i], 'Souls')) { $text = $lore[$i]; //Split string to access counter $parts = str_split($text, 19); //Increase counter $number = (string) (intval(str_replace(' ', '', $parts[1])) + 1); $text = $parts[0] . " " . $number; $itemInHand->setLore([$text]); //Refresh item $killer->getInventory()->setItemInHand($itemInHand); //Notify player $killer->sendMessage(TextFormat::LIGHT_PURPLE . "Collected a Soul!"); } } } } }
What do you mean by single player? (btw: You can use [php] instead of [code] to get syntax highlighting on the forums.)
I'd recommend to use foreach instead of for() with count() .. Then you can try to change $event to $cause. However I can't find any errors. Maybe try add some debug to your code.
@GamakCZ I already added some debug messages. The spilts at the correct part, the count increases but the setLore() call seems to fail. But only in survival. What do you mean by change $event to $cause?