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:
    61
    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 Zombie Pigman

    Messages:
    687
    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
     
    Ayush likes this.
  3. Ayush

    Ayush Witch

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

    minijaham Zombie Pigman

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

    Ayush Witch

    Messages:
    61
    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 Zombie Pigman

    Messages:
    687
    GitHub:
    minijaham
    Check if the item is not the item you want.
    And simply remove it
     
  7. Ayush

    Ayush Witch

    Messages:
    61
    How?
     
  8. minijaham

    minijaham Zombie Pigman

    Messages:
    687
    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
     
  9. Ayush

    Ayush Witch

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

    minijaham Zombie Pigman

    Messages:
    687
    GitHub:
    minijaham
    Well I think that problem is a different story.
    You can cancel consume event for regen potion :shrug:
     

Share This Page

  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.