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

Item->setLore() appears to be not working in survival mode

Discussion in 'Development' started by KlausKappa, Feb 18, 2020.

  1. KlausKappa

    KlausKappa Creeper

    Messages:
    4
    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.
     
    Last edited: Feb 19, 2020
  2. KlausKappa

    KlausKappa Creeper

    Messages:
    4
    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($text19);
                            
    //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!");
                        }
                    }
                }
            }
        }
     
    Last edited: Feb 19, 2020
  3. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    What do you mean by single player?

    (btw: You can use [php] instead of [code] to get syntax highlighting on the forums.)
     
  4. KlausKappa

    KlausKappa Creeper

    Messages:
    4
    I mean the gamemode a player has. I editied it out. Obviously i mean Survival mode :c
     
  5. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    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.
     
    HimbeersaftLP likes this.
  6. KlausKappa

    KlausKappa Creeper

    Messages:
    4
    @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?
     
  7. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    $cause = $event;
     
  8. TurtleSh0ck

    TurtleSh0ck Creeper

    Messages:
    1
    GitHub:
    MrHuskyy
    $ItemInHand->setLore([TF::GREEN . "HERE IS THE LORE"]);

    Use: use pocketmine\utils\TextFormat as TF;
     
  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.