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

[SOLVED] How to add delayed task

Discussion in 'Development' started by TheDiamondYT, Mar 11, 2017.

  1. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    298
    GitHub:
    TheDiamondYT1
    So i want to execute a function, wait a few seconds then execute another function. How can i do this? Im assuming it involves a DelayedTask but idk.

    SpectateTask.php
    PHP:
    class SpectateTask extends PluginTask {

        private 
    $plugin;
     
        public function 
    __construct(Main $plugin) {
            
    parent::__construct($plugin);
            
    $this->plugin $plugin;
        }
     
        public function 
    onRun($currentTick) {
            
    $this->plugin->getServer()->broadcastMessage("Set c mode");
            if(
    $currentTick === 1000) {
                
    $this->plugin->getServer()->broadcastMessage("done s mode");
            }
        }
    }
    Inside Main.php
    PHP:
        public function onEntityDamage(EntityDamageEvent $ev) {
           
    $ent $ev->getEntity();
           if(
    $ent instanceof Player && $ent->getHealth() - $ev->getDamage() <= 0) {
               
    $ev->setCancelled(true);
               
    $this->getServer()->getScheduler()->scheduleDelayedTask(new SpectateTask($this), $this->cfg["ticks"]);
               if(
    $this->cfg["death-message"]["display"] === true) {
                   if(
    $ev instanceof EntityDamageByEntityEvent) {
                       
    $message str_replace("{victim}"$ent->getName(), $this->cfg["death-message"]["killed"]);
                       
    $message str_replace("{killer}"$ev->getDamager()->getName(), $this->cfg["death-message"]["killed"]);
                       
    $this->getServer()->broadcastMessage($message);
                       return;
                   }
                   
    $this->getServer()->broadcastMessage(str_replace("{victim}"$ent->getName(), $this->cfg["death-message"]["died"]));
               }
            }
        }
    I have litterally know what to do, please help.
     
    Last edited: Mar 11, 2017
    Primus likes this.
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Make an attempt first. Then you will be helped. and show your code!
     
    Primus likes this.
  3. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    298
    GitHub:
    TheDiamondYT1
    Done
     
    Primus likes this.
  4. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    PHP:
    public function __construct(Main $plugin) { //SpectataTask
            
    parent::__construct($plugin);
        }

        public function 
    onRun($currentTick) {
            
    $this->getOwner()->getServer()->broadcastMessage("done s mode");
        }
    PHP:
    public function onEntityDamage(EntityDamageEvent $ev) { //Main
            
    $ent $ev->getEntity();
            if(
    $ent instanceof Player && $ent->getHealth() - $ev->getDamage() <= 0) {
                
    $ev->setCancelled(true);
                
    $this->getServer()->broadcastMessage("Set c mode");
                
    $this->getServer()->getScheduler()->scheduleDelayedTask(new SpectateTask($this), $this->cfg["ticks"]);
                if(
    $this->cfg["death-message"]["display"] === true) {
                    if(
    $ev instanceof EntityDamageByEntityEvent) {
                        
    $damager $ev->getDamager();
                        if(
    $damager instanceof Player) {
                            
    $message str_replace("{victim}"$ent->getName(), $this->cfg["death-message"]["killed"]);
                            
    $message str_replace("{killer}"$damager->getName(), $this->cfg["death-message"]["killed"]);
                            
    $this->getServer()->broadcastMessage($message);
                            return;
                        }
                    }
                    
    $this->getServer()->broadcastMessage(str_replace("{victim}"$ent->getName(), $this->cfg["death-message"]["died"]));
                }
            }
        }
     
    Primus likes this.
  5. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    298
    GitHub:
    TheDiamondYT1
    Oh thats so simple. Thanks!
     
  6. Taco

    Taco Spider Jockey

    Messages:
    41
    GitHub:
    taconoburrito
  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.