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

Cooldown

Comments in 'Plugin Development' started by Coolestgamer117, Nov 3, 2016.

  1. Coolestgamer117
    Offline

    Coolestgamer117 New Member

    Joined:
    May 9, 2016
    Posts:
    4
    Minecraft User:
    coolestgamer117
    I'm making a kit plugin and I need to know how to setup the cooldown
    Last edited: Nov 6, 2016
  2. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Use the time() function in PHP. If you have not struggled to make it yet, then please give it a try before asking.
  3. Thunder33345
    Offline

    Thunder33345 Notable Member

    Joined:
    Apr 3, 2014
    Posts:
    755
    Minecraft User:
    Thunder33345
    since you post this in plugin development i will suspect that you want to make a kit plugin WITH time feature in state of setting cooldown into someone's kit plugin
    so there two routes:
    use a callback timer aka heartbeat task
    or use time() and compare them

    most prefer the easy route look here: (heartbeat task)
    https://github.com/ThunderDoesPlugins/DeathBanV1/blob/master/src/Thunder33345/DeathBan/Main.php#L120
    PHP:
        private function update()
        {
            foreach (
    $this->bans as $index => $ban) {
                if (
    $ban['time'] < 0) {
                    unset(
    $this->bans[$index]);
                } else {
                    
    $this->bans[$index]['time'] = $ban['time'] - 1;
                }
            }
        }
    (yeah i know it is not the best pratice)
    if you wonder what i mean by that
    it does work but it create unnessary strains on server for having a heartbeat task running every so often and sorting a bunch of arrays
    (in most cases you dont need heartbeat task i could have just use time() and check it with current time on login event)

    the second one would be to use time
    ex on kit use
    PHP:
    $this->cabinet[$name]->setTime(time()+$kitCooldownTime)
    and 
    to check would be to
    if(time() > $this->cabinet[$name]->getTime()){
    //means the cooldown have been over
    } else {
    $time $this->cabinet[$name]->getTime() -time();
    echo 
    "cooldown over in :".$time;
    //not over
    }
    about the $this->cabinet[$name] it was just my way of doing things without confusing arrays littered everywhere you can just use $this->data[$name]['time']
    it is just the matter of personal preferences
    it was consist of a class with array object implemented if you wish to take a look go here https://github.com/ThunderDoesPlugins/WorldEdit/tree/master/src/Thunder33345/WorldEdit/Utilities
    EDIT: changed the code tag to PHP
  4. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    It is very questionable to use time() in Minecraft at all. MCPC MCPE and MCPC Official server software nearly never use it. I'd almost always recommend using the ticks. Of course ticks can be inaccurate if the server is under high load, but as everything is slower than that is normally what you want.
    There are some scenarios where you would of course use time():
    - TimeBanning (server could be stopped too)
    - Server Uptime (for whatever reason)
    - Player Online time
    Primus likes this.
  5. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Main reason to not to use ticks are because they are not reliable and rely on server runtime. To be more accurate I always use microtime function
  6. Thunder33345
    Offline

    Thunder33345 Notable Member

    Joined:
    Apr 3, 2014
    Posts:
    755
    Minecraft User:
    Thunder33345
    well using time or heartbeat task is alwayls a case by case basis
    if you are just making a kit plugin it may just be best to use time
    if you are making something that is more dynamic [Insert example] it would actually be better to use heartbeat task
  7. Coolestgamer117
    Offline

    Coolestgamer117 New Member

    Joined:
    May 9, 2016
    Posts:
    4
    Minecraft User:
    coolestgamer117
    Sorry forgot to put in the code
    PHP:
    <?php

    namespace RoyalMCPE;

    use 
    pocketmine\plugin\PluginBase;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerInteractEvent;

    use 
    pocketmine\utils\TextFormat as c;

    use 
    pocketmine\command\{CommandSenderCommand};

    use 
    pocketmine\Player;
    use 
    pocketmine\Server;

    use 
    pocketmine\item\Item;
    use 
    pocketmine\item\enchantment\Enchantment;

    use 
    pocketmine\entity\Effect;

    class 
    Main extends PluginBase implements Listener{

      public function 
    onEnable(){
        
    $this->getServer()->getPluginManager()->registerEvents($this$this);
      }
     
      public function 
    onCommand(CommandSender $senderCommand $cmd$label, array $args){
        
    $prefix c::BOLD.c::DARK_GRAY."[".c::DARK_PURPLE."MCA".c::DARK_GRAY."] ".c::DARK_AQUA;
        
    $inv $sender->getInventory();
        if(
    strtolower($cmd->getName()) == "kit"){
          
    $sender->sendMessage($prefix."Select a kit");
          
    $inv->addItem(Item::get(Item::WOODEN_SWORD,0,1)->setCustomName(c::GRAY."Starter"));
        }
      }
     
      public function 
    onSelect(PlayerInteractEvent $e){
        
    $p $e->getPlayer();
        
    $i $e->getItem();
        
    $inv $p->getInventory();
        
    $prefix c::BOLD.c::DARK_GRAY."[".c::DARK_PURPLE."MCA".c::DARK_GRAY."] ".c::DARK_AQUA;
        if(
    $inv->getItemInHand()->getId() === 268 && $i->hasCustomName(c::GRAY."Starter")){
          
    $inv->removeItem($i);
          
    $prot Enchantment::getEnchantment(0)->setLevel(1);
          
    $sharp Enchantment::getEnchantment(9);
          
    $p->addEffect(Effect::getEffect(5)->setDuration(1200)->setAmplifier(2));
          
    $inv->addItem(Item::get(Item::LEATHER_CAP,0,1)->setCustomName(c::GRAY."Starter Helemt")->addEnchantment($prot));
          
    $inv->addItem(Item::get(Item::LEATHER_TUNIC,0,1)->setCustomName(c::GRAY."Starter Chestplate")->addEnchantment($prot));
          
    $inv->addItem(Item::get(Item::LEATHER_PANTS,0,1)->setCustomName(c::GRAY."Starter Leggings")->addEnchantment($prot));
          
    $inv->addItem(Item::get(Item::LEATHER_BOOTS,0,1)->setCustomName (c::GRAY."Starter Boots")->addEnchantment($prot));
          
    $inv->addItem(Item::get(Item::STONE_SWORD,0,1)->setCustomName(c::GRAY."Starter Sword")->addEnchantment($sharp));
          
    $inv->addItem(Item::get(Item::STONE_PICKAXE,0,1)->setCustomName(c::GRAY."Starter Pickaxe"));
          
    $inv->addItem(Item::get(Item::STONE_SHOVEL,0,1)->setCustomName(c::GRAY."Starter Shovel"));
          
    $inv->addItem(Item::get(Item::STONE_AXE,0,1)->setCustomName(c::GRAY."Starter Axe")->addEnchantment($sharp));
          
    $inv->addItem(Item::get(Item::STEAK,0,64));
          
    $inv->addItem(Item::get(Item::GOLDEN_APPLE,0,3)->setCustomName(c::RED."Med Kit"));
          
    $p->sendPopup("You have recived the starter kit");
        }
      }
    }
  8. Thunder33345
    Offline

    Thunder33345 Notable Member

    Joined:
    Apr 3, 2014
    Posts:
    755
    Minecraft User:
    Thunder33345
    i will give you idea on how to implement it but i doubt it that i will write you the code to do so for you to just copy and paste it
    but i am happy to teach you on how to implement it

Share This Page

Advertisement