Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Starting a bomb

Comments in 'Plugin Development' started by Irish, May 6, 2016.

  1. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    So I've been trying to get timers working when a death occurs and I have a few questions about my code.

    Main:
    PHP:
    <?php

    namespace TimeBomb;

    use 
    pocketmine\event\player\PlayerDeathEvent;
    use 
    pocketmine\plugin\PluginBase as P;
    use 
    pocketmine\event\Listener as L;
    use 
    pocketmine\utils\TextFormat as TF;
    use 
    pocketmine\event\player\PlayerDeathEvent as Death;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\Player;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\level\Explosion as E;

    class 
    Main extends implements {

        public 
    $pos = [];

        public function 
    onEnable()
        {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new Timer($this), 20);
            
    $this->getLogger()->info('TimeBombPE has been enabled!');
        }
        public function 
    onDisable()
        {
            
    $this->getLogger()->info('TimeBombPE has been disabled!');
        }

        public function 
    onDeath(Death $event){
            
    $entity $event->getEntity();
            
    $lvl $entity->getLevel();
            if(
    $entity instanceof Player){
                
    $this->pos[$entity->getPlayer()] = clone $entity->getPosition();
            }

        }

        public function 
    startBomb(){
           
            
    $center = new Vector3($this->pos);
            
    $explosion = new E($center3);
            
    $explosion->explodeA();
            if(isset(
    $this->pos)){
                unset(
    $this->pos);
            }
        }
    }

    Timer:
    PHP:
    <?php

    namespace TimeBomb;

    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\TextFormat as TF;

    class 
    Timer extends PluginTask {

        private 
    $plugin;
        public  
    $seconds 0;
        public  
    $time 20;



        public function 
    __construct($plugin)
        {
            
    $this->plugin $plugin;
            
    parent::__construct($plugin);
            
    $plugin->getServer()->getScheduler()->scheduleRepeatingTask($this20);
        }

        public function 
    onRun($tick){
            if(
    $this->seconds === $this->time){
                
    $this->plugin->startBomb();
            }
            
    $this->seconds++;
        }
    }
  2. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    Questions that I have.. One, did I do it right at all? If not, how could I fix it? Two, would this do it on every death? I have looked around at other timers, but still am not sure. I have not dealt with timers very often, so thanks in advance :D
  3. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Your code is to difficult, you should also construct the player too, and in the startBomb() function the player should be the parameter, so you would not need an array for that.


    Oh and for that you should use a scheduleDelayedTask not a scheduleRepeatingTask. Here is how my code looks for a cooldown:

    PHP:

    <?php
    namespace plugin;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\plugin\Plugin;
    use 
    pocketmine\Player;
    use 
    pocketmine\item\Item;
    class 
    Cooldown extends PluginTask{
        public 
    $player;
        public function 
    __construct(Plugin $ownerPlayer $player){
            
    parent::__construct($owner);
            
    $this->player $player;
           
    $this->plugin $owner;
        }

        public function 
    onRun($currentTick){
        

    if(
    $this->player->getLevel() !== $this->player->getServer()->getDefaultLevel()){
    $this->player->getInventory()->addItem(Item::get(4601));

    // here you could put the startGame function with the player as the parameter given

    }

        }
    }




    And how to start the task:

    PHP:

    $task 
    = new Cooldown($this$player);
                
    $this->getServer()->getScheduler()->scheduleDelayedTask($task30);

    30 is the time in ticks the task should run, 1 second has 20 ticks, so this task has 1.5 seconds. You can varify the time of course. When the time is over, onRun gets triggered in the task class.
    Last edited: May 6, 2016
    ImagicalGamer likes this.
  4. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    Wouldn't it be new task, instead of Cooldown?

    [EDIT] Ok, I understand it now. So it'd just be new Timer. Thanks for the help :)
  5. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    You can call a task however you want
  6. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    Well doesn't the task have to be whatever name you call the other php file?
  7. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    No, it has to be whatever you called the class.

Share This Page

Advertisement