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

Help? Thanks

Discussion in 'Development' started by Vaxrp, Oct 14, 2017.

  1. Vaxrp

    Vaxrp Witch

    Messages:
    73
    GitHub:
    Vaxrp
    PHP:
    public function onHold(PlayerItemHeldEvent $event){
        
    $item $event->getItem();
        
    $player $event->getPlayer();
        if(
    $item->getId() === Item::BLAZE_ROD){
            
    $player->addEffect(Effect::getEffect(Effect::SPEED)->setDuration(20 10)->setVisible(false));
        }
    }
    this plugin works but only with one item at a time. Can you guys help me find a way to set multiple items with effect each or multiple effects each?
    THANKS! really need help .
     
  2. DayKoala

    DayKoala Silverfish

    Messages:
    22
    GitHub:
    daykoala
    You can use this for item with same effect:

    PHP:
    if($item->getId() === Item::BLAZE_ROD or $item->getId() === Item::YOUR_ITEM){
       
    #Your code here
    }
    or for other effect:

    PHP:
    if($item->getId() === Item::BLAZE_ROD){
       
    #Your code here
    }
    if(
    $item->getId() === Item::YOUR_ITEM){
       
    #Your code here
    }
     
  3. Vaxrp

    Vaxrp Witch

    Messages:
    73
    GitHub:
    Vaxrp
    I can create as many if events as I need?
     
  4. Vaxrp

    Vaxrp Witch

    Messages:
    73
    GitHub:
    Vaxrp
    if statements my bad im new xd
     
  5. DayKoala

    DayKoala Silverfish

    Messages:
    22
    GitHub:
    daykoala
    Yes, but try not to repeat the same many times so that you do not get any errors
     
  6. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    If there are lots of items, I'd avoid using if statements and try a switch statement:

    PHP:
    public function onHold(PlayerItemHeldEvent $event){
        
    $item $event->getItem();
        
    $player $event->getPlayer();
        
    $effectId null;
        switch(
    $item->getId()) {
            case 
    Item::BLAZE_ROD:
                
    $effectId Effect::SPEED;
                break;
            case 
    Item::DIAMOND_SWORD:
                
    $effectId Effect::STRENGTH;
                break;
            case 
    Item::DIAMOND_PICKAXE:
                
    $effectId Effect::HASTE;
                break;
        }

        if(
    $effectId !== null) {
            
    $player->addEffect(Effect::getEffect($effectId)->setDuration(20 10)->setVisible(false));
        }
    }
    or here's another alternative with arrays:
    PHP:
    public function onHold(PlayerItemHeldEvent $event){
        
    $item $event->getItem();
        
    $player $event->getPlayer();
        
    $items = [Item::BLAZE_ROD=>Effect::SPEEDItem::DIAMOND_SWORD=>Effect::STRENGTHItem::DIAMOND_PICKAXE=>Effect::HASTE];

        if(isset(
    $items[$item->getId()])) {
            
    $player->addEffect(Effect::getEffect($items[$item->getId()])->setDuration(20 10)->setVisible(false));
        }
    }
     
    Levi likes this.
  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.