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

Give Player Health When When an item In Hand Of Player

Discussion in 'Development' started by Ayush, Sep 10, 2021.

  1. Ayush

    Ayush Witch

    Messages:
    62
    TODO:
    When Player Hold And Item In Hand It Gives Health After he hold anything else It Will remove Health but the problem Is I Want to set player's Max health from Nbt of that item!
     
  2. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    PHP:
    /** @var Item $item */
    $nbt $item->getNamedTag() ?? new CompoundTag("", []);
    $nbt->test = new IntTag("test"69);
    $item->setNamedTag($nbt);

    /** @var Item $item */
    if (isset($item->getNamedTag()->test)) {
        
    //$item has NBT tag: test.
        /** @var int $test */
        
    $test $item->getNamedTag()->test->getValue(); //69
    }
    Source: Muqsit
     
    NTT, Agent and Ayush like this.
  3. Ayush

    Ayush Witch

    Messages:
    62
    How to check if He Held And remove when he held something else?
    @minijaham
     
  4. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    There's an event that detects it. might want to look for that yourself
     
    Agent likes this.
  5. Ayush

    Ayush Witch

    Messages:
    62
    Yes
    PlayerItemHeldEvent but when he held that item it gives him effects but it won't remove when he held something else?

    @minijaham
     
  6. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Check if the item is not the item you want.
    And simply remove it
     
    Agent likes this.
  7. Ayush

    Ayush Witch

    Messages:
    62
    How?
     
  8. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    PHP:
    on item held event {
        if 
    event->getItem === Your item {
            
    Code to give effect
            
    return;
        }
        
    code to remove effect
    }
    Something like this should work
     
    Agent likes this.
  9. Ayush

    Ayush Witch

    Messages:
    62
    I should remove but if he use regeneration potion it also remove it!
     
  10. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Well I think that problem is a different story.
    You can cancel consume event for regen potion :shrug:
     
    Agent likes this.
  11. BloodyJohnRus

    BloodyJohnRus Silverfish

    Messages:
    19
    GitHub:
    GDSHNIK
    You can use PlayerMoveEvent or PlayerInteractEvent:
    PHP:
    use pocketmine\event\PlayerInteractEvent;
    public function 
    onInteract(PlayerInteractEvent $e)
    {
    $nbt $item->getNamedTag() ?? new CompoundTag("", []);
    $nbt->test = new IntTag("test"69);
    $item->setNamedTag($nbt);

    /** @var Item $item */
    if (isset($item->getNamedTag()->test)) {
        
    //$item has NBT tag: test.
        /** @var int $test */
        
    $test $item->getNamedTag()->test->getValue(); //69
    }
    }
    From 2-nd post.
    Also check code of PlayerInteractEvent on GitHub.
    https://github.com/pmmp/PocketMine-MP
     
  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.