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

Run plugin when life is low

Discussion in 'Development' started by Leonardo Goulart, Apr 21, 2022.

  1. Leonardo Goulart

    Leonardo Goulart Creeper

    Messages:
    2
    GitHub:
    Gou
    I want to make a plugin when life is low, I get some effects like:
    Class Main extends PluginBase implements Listener {


    public function onEnable() {
    $this->getLogger()->info ("§aAtivado");
    $this->getServer()->getPluginManager()->registerEvents($this,$this);
    }

    public function onDisable() {

    $this->getLogger()->info('§aDesativado');

    }




    public function execute(Listener $listener, Event $event){
    $p = $ev->getPlayer();
    $health = getHealth();
    if ($health <= 7) {
    $p->sendTip('§dVocê usou o §aTOTEM');
    $ev->getPlayer()->getInvenTory()->removeItem(Item::Get(341,0,1));
    $p->addEffect(Effect::getEffect(10)->setAmplifier(3)->setDuration(10 * 3)->setVisible(true));
    $p->addEffect(Effect::getEffect(11)->setAmplifier(1)->setDuration(5 * 1)->setVisible(true));
    $p->addEffect(Effect::getEffect(12)->setAmplifier(1)->setDuration(5 * 1)->setVisible(true));
    }
    }

    }​
    but i don't know how to load even the public function
     
    Last edited: Apr 22, 2022
  2. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    In my opinion there are two ways of doing that, if you want to check every second if a player has a specific number of hearts just create a Task and get all players of the server and check if they have x amount of hearts and if they have, for example lower than 3 hearts then give the player the effect you want to.
     
  3. SPINCY

    SPINCY Creeper

    Messages:
    2
    to track when damage is dealt use EntityDamageEvent, and make a request for execute()

    function onDamage(EntityDamageEvent $event){
    $entity = $event->getEntity();
    if($event instanceof EntityDamageByEntityEvent){
    if(($entity->getHealth() - $event->getFinalDamage()) <= 7){
    $this->execute($entity);
    }
    }
    }

    function execute($p){
    $p->sendTip('§dVocê usou o §aTOTEM');
    $p->getInventory()->removeItem(Item::get(341, 0, 1));
    $p->addEffect(Effect::getEffect(10)->setAmplifier(3)->setDuration(10 * 3)->setVisible(true));
    $p->addEffect(Effect::getEffect(11)->setAmplifier(1)->setDuration(5 * 1)->setVisible(true));
    $p->addEffect(Effect::getEffect(12)->setAmplifier(1)->setDuration(5 * 1)->setVisible(true));
    }
     
  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.