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

How do set it up to where an item gives an effect while in hand or on the player

Discussion in 'Development' started by Vincent, Jan 14, 2018.

  1. Vincent

    Vincent Silverfish

    Messages:
    16
    GitHub:
    vincent
    So i am working on a plugin that needs to detect whether a player is holding an item or wearing it...
     
  2. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Task
     
  3. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PlayerItemHeldEvent.

    You can save the player's previous effects in an array and then do checks whenever the event gets called.

    WARNING:
    I'm not sure if this event gets called when the held item is moved to cursor inventory. If it doesn't get called, the player can be stuck with an infinite effect. But you can verify that fact... maybe?
    Confirmed, it works with every transaction. It's the best option to use in your case.

    PHP:
    /** @var int[] */
    private $enchantFx = [];

    public function 
    onItemHold(PlayerItemHeldEvent $event) : void{
        
    $item $event->getItem();
        
    $player $event->getPlayer();
        if(isset(
    $this->enchantFx[$id $player->getId()])){//remove effects if any
            
    foreach($this->enchantFx[$id] as $effectId){
                
    $player->removeEffect($effectId);
            }
            unset(
    $this->enchantFx[$id]);
        }
        if(
    $item->hasEnchantment(Enchantment::SHARPNESS)){
            
    $effect Effect::getEffect(Effect::HASTE)->setDuration(INT32_MAX);
            if(
    $player->addEffect($effect)){
                
    $this->enchantFx[$id][] = $effect->getId();
            }else{
                
    //unsuccessful attempt to give player an effect, probably because they already have the same effect with a better level of amplifier
            
    }
        }
    }
     
    Last edited: Jan 17, 2018
    Ayush likes this.
  4. kenygamer

    kenygamer Banned Banned

    Messages:
    106
    GitHub:
    kenygamer
    Clearly isn't, since you must be holding the item for the PlayerItemHeldEvent to be fired.
     
  5. kenygamer

    kenygamer Banned Banned

    Messages:
    106
    GitHub:
    kenygamer
    You can listen to the EntityArmorChangeEvent
    Source: https://forums.pmmp.io/threads/wearing-item.4836/

    Other reference:
    PHP:
    Player->getInventory()->
    getHelmet() # or
    getChestplate() # or
    getLeggings() # or
    getBoots()
    returns an Item object.
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    That's the whole point, read the thread once again.
     
  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.