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

Item Effects

Discussion in 'Development' started by Vincent, Dec 27, 2017.

  1. Vincent

    Vincent Silverfish

    Messages:
    16
    GitHub:
    vincent
    Im a new php dev... In training... I would like to know how to set it up to where when u are holding or wearing an item it gives u effects... Such as haste ect.


    I figured it out
    $player->addEffect(Effect::getEffect(Effect::REGENERATION)->setAmplifier(3)->setDuration(100 * 20));
     
    Last edited: Dec 27, 2017
  2. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    It is better to use an event to hold an item in your hand,

    PHP:
    /**
         * @param PlayerItemHeldEvent $event
         */
        
    public function onPlayerHeld(PlayerItemHeldEvent $event) {
            
    // 280 = stick
            
    if($event->getItem()->getId() == 280) {
                
    $event->getPlayer()->addEffect(Effect::getEffect(Effect::REGENERATION));
            }
        }
    but if a player has to wear a item, is a better schedule repeating task.

    PHP:
    <?php

    /**
     * Class CheckPlayerTask
     */
    class CheckPlayerTask extends \pocketmine\scheduler\PluginTask {

        
    /**
         * @param int $currentTick
         */
        
    public function onRun(int $currentTick) {
            foreach (\
    pocketmine\Server::getInstance()->getOnlinePlayers() as $player) {
                if(
    $player->getInventory()->contains(\pocketmine\item\Item::get(\pocketmine\item\Item::CARROT))) {
                    
    $player->addEffect(\pocketmine\entity\Effect::getEffect(\pocketmine\entity\Effect::JUMP)->setDuration(20));
                }
            }
        }
    }
     
  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.