Solved Setting effects

Discussion in 'Development' started by minijaham, Jul 29, 2020.

  1. minijaham

    minijaham Witch

    Messages:
    68
    GitHub:
    minijaham
    Hey, I encountered an error while making a custom potion plugin.
    Error:
    Code:
    Server thread/CRITICAL Error: "Call to undefined method pocketmine\entity\Effect::setDuration()" (EXCEPTION) in "plugins/CustomPotion/src/minijaham/CustomPotion/CustomPotionEvent" at line 41
    29.07 20:19:26 [Server] Server thread/CRITICAL #0 src/pocketmine/plugin/MethodEventExecutor(42): minijaham\CustomPotion\CustomPotionEvent->onConsume(object pocketmine\event\player\PlayerItemConsumeEvent)
    29.07 20:19:26 [Server] Server thread/CRITICAL #1 src/pocketmine/plugin/RegisteredListener(80): pocketmine\plugin\MethodEventExecutor->execute(object minijaham\CustomPotion\CustomPotionEvent, object pocketmine\event\player\PlayerItemConsumeEvent)
    29.07 20:19:26 [Server] Server thread/CRITICAL #2 src/pocketmine/event/Event(88): pocketmine\plugin\RegisteredListener->callEvent(object pocketmine\event\player\PlayerItemConsumeEvent)
    29.07 20:19:26 [Server] Server thread/CRITICAL #3 src/pocketmine/Player(2578): pocketmine\event\Event->call()
    29.07 20:19:26 [Server] Server thread/CRITICAL #4 src/pocketmine/network/mcpe/PlayerNetworkSessionAdapter(150): pocketmine\Player->handleInventoryTransaction(object pocketmine\network\mcpe\protocol\InventoryTransactionPacket)
    29.07 20:19:26 [Server] Server thread/CRITICAL #5 src/pocketmine/network/mcpe/protocol/InventoryTransactionPacket(173): pocketmine\network\mcpe\PlayerNetworkSessionAdapter->handleInventoryTransaction(object pocketmine\network\mcpe\protocol\InventoryTransactionPacket)
    29.07 20:19:26 [Server] Server thread/CRITICAL #6 src/pocketmine/network/mcpe/PlayerNetworkSessionAdapter(110): pocketmine\network\mcpe\protocol\InventoryTransactionPacket->handle(object pocketmine\network\mcpe\PlayerNetworkSessionAdapter)
    29.07 20:19:26 [Server] Server thread/CRITICAL #7 src/pocketmine/network/mcpe/protocol/BatchPacket(127): pocketmine\network\mcpe\PlayerNetworkSessionAdapter->handleDataPacket(object pocketmine\network\mcpe\protocol\InventoryTransactionPacket)
    29.07 20:19:26 [Server] Server thread/CRITICAL #8 src/pocketmine/network/mcpe/PlayerNetworkSessionAdapter(110): pocketmine\network\mcpe\protocol\BatchPacket->handle(object pocketmine\network\mcpe\PlayerNetworkSessionAdapter)
    29.07 20:19:26 [Server] Server thread/CRITICAL #9 src/pocketmine/Player(3247): pocketmine\network\mcpe\PlayerNetworkSessionAdapter->handleDataPacket(object pocketmine\network\mcpe\protocol\BatchPacket)
    29.07 20:19:26 [Server] Server thread/CRITICAL #10 src/pocketmine/network/mcpe/RakLibInterface(169): pocketmine\Player->handleDataPacket(object pocketmine\network\mcpe\protocol\BatchPacket)
    29.07 20:19:26 [Server] Server thread/CRITICAL #11 vendor/pocketmine/raklib/src/server/ServerHandler(99): pocketmine\network\mcpe\RakLibInterface->handleEncapsulated(string[18] 68.7.150.203 51523, object raklib\protocol\EncapsulatedPacket, integer 0)
    29.07 20:19:26 [Server] Server thread/CRITICAL #12 src/pocketmine/network/mcpe/RakLibInterface(109): raklib\server\ServerHandler->handlePacket()
    29.07 20:19:26 [Server] Server thread/CRITICAL #13 src/pocketmine/network/mcpe/RakLibInterface(99): pocketmine\network\mcpe\RakLibInterface->process()
    29.07 20:19:26 [Server] Server thread/CRITICAL #14 vendor/pocketmine/snooze/src/SleeperHandler(123): pocketmine\network\mcpe\RakLibInterface->pocketmine\network\mcpe\{closure}()
    29.07 20:19:26 [Server] Server thread/CRITICAL #15 vendor/pocketmine/snooze/src/SleeperHandler(85): pocketmine\snooze\SleeperHandler->processNotifications()
    29.07 20:19:26 [Server] Server thread/CRITICAL #16 src/pocketmine/Server(2157): pocketmine\snooze\SleeperHandler->sleepUntil(double 1596053966.3365)
    29.07 20:19:26 [Server] Server thread/CRITICAL #17 src/pocketmine/Server(1994): pocketmine\Server->tickProcessor()
    29.07 20:19:26 [Server] Server thread/CRITICAL #18 src/pocketmine/Server(1588): pocketmine\Server->start()
    29.07 20:19:26 [Server] Server thread/CRITICAL #19 src/pocketmine/PocketMine(273): pocketmine\Server->__construct(object BaseClassLoader, object pocketmine\utils\MainLogger, string[47] /, string[55] /plugins/)
    29.07 20:19:26 [Server] Server thread/CRITICAL #20 src/pocketmine/PocketMine(304): pocketmine\server()
    PHP:
    <?php

    namespace minijaham\CustomPotion;

    use 
    minijaham\Main;

    use 
    pocketmine\entity\Effect;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerItemConsumeEvent;
    use 
    pocketmine\event\player\PlayerItemHeldEvent;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\TextFormat as TF;

    class 
    CustomPotionEvent implements Listener {
       
        public function 
    onConsume(PlayerItemConsumeEvent $event) {
           
            
    $player $event->getPlayer();
           
            if(
    $event->getItem()->getId() === 373) {
           
                
    $damage $event->getItem()->getDamage();
               
                switch(
    $damage) {
                   
                    case 
    100:
                   
                    
    $player->addEffect(Effect::getEffect(Effect::SPEED)->setDuration(360)->setAmplifier(1));
                    
    $player->addEffect(Effect::getEffect(Effect::HASTE)->setDuration(360)->setAmplifier(2));
                    
    $player->addEffect(Effect::getEffect(Effect::NIGHT_VISION)->setDuration(180)->setAmplifier(1));
                   
                    
    $player->getInventory()->removeItem(Item::get(Item::POTION1001));
                    
    $player->getInventory()->addItem(Item::get(Item::GLASS_BOTTLE01));
                    
    $player->sendMessage("§7(§a!§7)§a Consummed Raiding potion!");
                    return 
    true;
                    break;
                   
                    case 
    101:
                   
                    
    $player->addEffect(Effect::getEffect(Effect::JUMP)->setDuration(180)->setAmplifier(3));
                    
    $player->addEffect(Effect::getEffect(Effect::STRENGTH)->setDuration(330)->setAmplifier(5));
                    
    $player->addEffect(Effect::getEffect(Effect::SPEED)->setDuration(360)->setAmplifier(2));
                    
    $player->addEffect(Effect::getEffect(Effect::FIRE_RESISTANCE)->setDuration(360)->setAmplifier(1));
                   
                    
    $player->getInventory()->removeItem(Item::get(Item::POTION1011));
                    
    $player->getInventory()->addItem(Item::get(Item::GLASS_BOTTLE01));
                    
    $player->sendMessage("§7(§a!§7)§a Consummed PvP potion!");
                    return 
    true;
                    break;
                   
                }
            }
        }
       
        public function 
    onHeld(PlayerItemHeldEvent $event) {
           
            
    $player $event->getPlayer();
           
            if(
    $event->getItem()->getId() === 373) {
               
                
    $damage $event->getItem()->getDamage();
                
    $hand $player->getInventory()->getItemInHand();
               
                switch(
    $damage) {
                   
                    case 
    100:
                   
                    
    $item Item::get(Item::POTION1001);
                   
                    
    $player->getInventory()->removeItem($item);
                   
                    
    $item->setCustomName(TF::RESET TF::RED TF::BOLD "Raiding Elixir" PHP_EOL PHP_EOL .
                                        
    TF::RESET TF::DARK_GRAY " * " TF::GREEN "Speed I" TF::GRAY " (6:00)" PHP_EOL .
                                        
    TF::DARK_GRAY " * " TF::GREEN "Haste II" TF::GRAY " (6:00)" PHP_EOL .
                                        
    TF::DARK_GRAY " * " TF::GREEN "Night Vision" TF::GRAY " (3:00)");
                   
                    
    $player->getInventory()->addItem($item);
                    return 
    true;
                   
                    break;
                   
                    case 
    101:
                   
                    
    $item Item::get(Item::POTION1011);
                   
                    
    $player->getInventory()->removeItem($item);
                   
                    
    $item->setCustomName(TF::RESET TF::DARK_RED TF::BOLD "PvP Elixir" PHP_EOL PHP_EOL .
                                        
    TF::RESET TF::DARK_GRAY " * " TF::GREEN "Jump Boost III" TF::GRAY " (3:00)" PHP_EOL .
                                        
    TF::RESET TF::DARK_GRAY " * " TF::GREEN "Speed II" TF::GRAY " (6:00)" PHP_EOL .
                                        
    TF::DARK_GRAY " * " TF::GREEN "Strength V" TF::GRAY " (5:30)" PHP_EOL .
                                        
    TF::DARK_GRAY " * " TF::GREEN "Fire Resistance" TF::GRAY " (6:00)");
                                         
                    
    $player->getInventory()->addItem($item);
                    return 
    true;
                    break;
           
                }
               
                if(
    $hand->hasCustomName()) {
                   
                    
    $event->setCancelled();
                   
                }
            }
        }
    }
    Someone please help me :) Thank you!
     
  2. Primus

    Primus Zombie Pigman

    Messages:
    408
    Construct an EffectInstance class and pass necessary arguments in the constructor, or set fluently as you wish.
     
  3. minijaham

    minijaham Witch

    Messages:
    68
    GitHub:
    minijaham
    I have tried effectinstance, but I'm not sure how to exactly set the duration. I've kept the numbers the same, but they all last shorter
     
  4. Primus

    Primus Zombie Pigman

    Messages:
    408
    You know that duration is meant in in-game ticks? 20 tick = 1 second.

    360 / 20 = 18 seconds.
     
  5. minijaham

    minijaham Witch

    Messages:
    68
    GitHub:
    minijaham
    OHHH xD
    Thank you so much!
     
    Primus likes this.

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.