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

How to save time played on server?

Comments in 'Plugin Development' started by Radix, Jul 16, 2015.

  1. Radix
    Offline

    Radix Active Member

    Joined:
    Jul 2, 2015
    Posts:
    211
    Minecraft User:
    Dicks123
    Title Says it all!
  2. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    Try:
    PHP:
    <?php
     
    namespace TeamCounter;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\event\player\PlayerQuitEvent;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\plugin\PluginBase;

    class 
    Main extends PluginBase implements Listener{
    public function 
    onEnable(){
    $this->getServer()->getPluginManager()->registerEvents($this$this);
    $this->saveDefaultConfig;
    $this->time = new Config($this->getDataFolder() . "time.yml"Config::YAML);
    //sorry if config is wrong, rewrite it ;)
    }
    public function 
    onJoin(PlayerJoinEvent $ev){
    //1 is number of seconds to check
    $this->getServer()->getScheduler()->scheduleRepeatingTask($t = new TimeTask($this$ev->getPlayer()), 60*20);
    $this->time->set($ev->getPlayer() . "-task" $task->getId());
    $this->time->set($ev->getPlayer() . "0");
    $this->time->save();
    }
    public function 
    onQuit(PlayerQuitEvent $ev){
    $this->getServer()->getScheduler()->cancelTask($this->time>get($ev->getPlayer() . "-task"));
    }
    public function 
    writeTime($time){
    $this->time->set($ev->getPlayer(), $time);
    }
    }
    TimeTask.php
    PHP:
    <?php
     
    namespace TeamCounter;

    use 
    pocketmine\scheduler\PluginTask;

    class 
    TimeTask extends PluginTask{

    public function 
    _construct($main$p){
    $this->main $main;
    $this->$p;
    }
    public function 
    onRun($t){
    $main->writeTime($main->time->get($p)+1);
    }
    }
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It is meaningless to count the seconds every 20 ticks. Just save the Player join time in an array property with microtime(true) and microtime(true) - $this->joinTime[$player->getId()] when the Player quits or when plugin is disabled.
  4. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    Yes, it's better. I'm writing my own ideas to 1) Practice coding 2) Learn how do it better
  5. Radix
    Offline

    Radix Active Member

    Joined:
    Jul 2, 2015
    Posts:
    211
    Minecraft User:
    Dicks123
    Will this cause lag since it update so much?
  6. xiaoq
    Offline

    xiaoq Active Member

    Joined:
    Dec 23, 2014
    Posts:
    232
    Minecraft User:
    xiaoq
    what is save time? count how long a player played on your server?
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder

Share This Page

Advertisement