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

time() error?

Comments in 'Plugin Development' started by PEMapModder, Mar 27, 2014.

  1. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I am trying to make a plugin for this, but it seems there are some errors from intented.

    Here is the code:
    PHP:
    <?php

    /*
    __PocketMine Plugin__
    class=JoinTimeTag
    name=JoinTime-Tagger
    author=PEMapModder
    apiversion=12
    version=alpha 0
    */

    class JoinTimeTag implements Plugin{
        private 
    $sessions=array(), $tags=array();
        public function 
    __construct(ServerAPI $a$s=0){
        }
        public function 
    __destruct(){}
        public function 
    init(){
            
    $server=ServerAPI::request();
            
    $this->dir=$server->api->plugin->configPath($this);
            @
    mkdir($this->dir."players/");
            
    $server->addHandler("player.spawn", array($this"onSpawn"));
            
    $server->addHandler("player.quit", array($this"onQuit"));
            
    $server->addHandler("player.chat", array($this"onChat"));
            
    $this->initConfig();
        }
        private function 
    initConfig(){
            
    $path=$this->dir."config.";
            
    $ext="yml";
            if(
    file_exists($path."txt"))
                
    $ext="txt";
            
    $config=new Config($path.$extCONFIG_YAML, array(
                    
    "base-tag"=>"Newly spawned",
                    
    "tags"=>array(
                            array(
    30"second""Player"),
                            array(
    30"minute""Adult player"), // just some simple examples
                            
    array(12"hour""Elder player"),
                            array(
    96"hour""Addict of the Server"),
                    ),
                    
    "format of chat"=>"(@[email protected]) <@[email protected]> @[email protected] @[email protected]: @[email protected]",
            ));
            
    $data=$config->getAll();
            
    $this->chat=$data["format of chat"];
            
    $tempTags=array(0=>$data["base-tag"]);
            foreach(
    $data["tags"] as $data){
                
    $time=$data[0]*(($u=strtolower($data[1]))==="second"?1:($u==="hour"?3600:60));
                
    $tempTags[$time]=$data[2];
            }
            
    ksort($tempTagsSORT_NUMERIC);
            
    $this->tags=$tempTags;
        }
        public function 
    onChat($data){
            
    $time=$this->updateSession($data["player"]);
            
    $t="";
            foreach(
    $this->tags as $secs=>$tag){
                if(
    $secs<=$time)$t=$tag;
                else break;
            }
            
    $chat=$this->chat;
            
    $server=ServerAPI::request();
            if(!
    is_string($rk=$server->dhandle("numrk.rank.get.string"$server->dhandle("numrk.player.get.int"$data["player"]))))
                
    $rk="";
            else 
    $rk="[$rk]";
            foreach(
    ServerAPI::request()->api->player->getAll() as $p){
                
    $p->sendChat(str_replace(array("@[email protected]""@[email protected]""@[email protected]""@[email protected]""@[email protected]"), array($data["message"], $data["player"]->username$t$data["player"]->entity->level->getName(), $rk), $chat));
            }
            
    console("[INFO] ".str_replace(array("@[email protected]""@[email protected]""@[email protected]""@[email protected]"), array($data["message"], $data["player"]->username$t$data["player"]->entity->level->getName()), $chat));
            return 
    false;
        }
        public function 
    onSpawn(Player $p){
            
    $this->sessions[$p->iusername]=time();
        }
        public function 
    onQuit(Player $p){
            
    $this->updateSession($p);
            unset(
    $this->sessions[$p->iusername]);
        }
        public function 
    updateSession(Player $p){
            
    $n=strtolower(trim("$p"));
            
    $path=$this->dir."players/$n.txt";
            
    $time[email protected]file_get_contents($path);
            if(
    $time===false)
                
    $time=0;
            
    $time=intval($time);
            if(!isset(
    $this->sessions[$p->iusername]))
                return 
    $time;
            
    $newTime=time()-$this->sessions[$p->iusername];
            
    $time+=($newTime);
            
    file_put_contents($path"$time");
            
    $this->sessions[$p->iusername]=0;
            
    console("[DEBUG] $p's session has been updated. $newTime seconds have been added into $time."truetrue2);
            return 
    $time;
        }
    }
    It ends up like this:
    Code:
    12:38:23 [INFO] Starting Minecraft PE server version v0.8.1 alpha
    12:38:23 [INFO] Loading properties...
    12:38:23 [INFO] Starting Minecraft PE server on *:19132
    12:38:23 [INFO] This server is running PocketMine-MP version Alpha_1.3.12 "甘い(Amai)ビートルート(Beetroot)" (MCPE: v0.8.1 alpha) (API 12)
    12:38:23 [INFO] PocketMine-MP is distributed under the LGPL License
    12:38:23 [INTERNAL] API console [ConsoleAPI] loaded
    12:38:23 [INTERNAL] API level [LevelAPI] loaded
    12:38:23 [INTERNAL] API block [BlockAPI] loaded
    12:38:23 [INTERNAL] API chat [ChatAPI] loaded
    12:38:23 [INTERNAL] API ban [BanAPI] loaded
    12:38:23 [INTERNAL] API entity [EntityAPI] loaded
    12:38:23 [INTERNAL] API tile [TileAPI] loaded
    12:38:23 [INTERNAL] API player [PlayerAPI] loaded
    12:38:23 [INTERNAL] API time [TimeAPI] loaded
    12:38:23 [INFO] Preparing level "world"
    12:38:23 [INTERNAL] New handler BanAPI::permissionsCheck to special event console.command (ID 1)
    12:38:23 [INTERNAL] New handler BanAPI::permissionsCheck to special event player.block.break (ID 2)
    12:38:23 [INTERNAL] New handler BanAPI::permissionsCheck to special event player.block.place (ID 3)
    12:38:23 [INTERNAL] New handler BanAPI::permissionsCheck to special event player.flying (ID 4)
    12:38:23 [INTERNAL] New handler PlayerAPI::handle to special event player.death (ID 5)
    12:38:23 [INTERNAL] API plugin [PluginAPI] loaded
    12:38:23 [INTERNAL] Attached PluginAPI::initAll to event server.start (ID 1)
    12:38:23 [INFO] Loading plugin "AsyncWorldEditor (Loader)" 0 by PEMapModder
    12:38:23 [INFO] Loading plugin "JoinTime-Tagger" alpha 0 by PEMapModder
    12:38:23 [DEBUG] Sending usage data...
    12:38:23 [INFO] Starting GS4 status listener
    12:38:23 [INFO] Setting query port to 19132
    12:38:23 [INFO] Query running on 0.0.0.0:19132
    12:38:23 [INFO] Default game type: SURVIVAL
    12:38:23 [INFO] Starting plugins...
    12:38:23 [INTERNAL] New handler JoinTimeTag::onSpawn to special event player.spawn (ID 6)
    12:38:23 [INTERNAL] New handler JoinTimeTag::onQuit to special event player.quit (ID 7)
    12:38:23 [INTERNAL] New handler JoinTimeTag::onChat to special event player.chat (ID 8)
    12:38:23 [INFO] Done (3.194s)! For help, type "help" or "?"
    12:39:25 [INTERNAL] Attached Player::close to event server.close (ID 2)
    12:39:25 [DEBUG] New Session started with xx.xxx.x.xxx:59768. MTU 1464, Client ID 18446744073121918281
    12:39:26 [NOTICE] Player data not found for "czacza", creating new profile
    12:39:26 [INTERNAL] Attached Player::eventHandler to event server.chat (ID 3)
    12:39:26 [INTERNAL] Attached Player::eventHandler to event entity.motion (ID 4)
    12:39:26 [INTERNAL] Attached Player::eventHandler to event entity.animate (ID 5)
    12:39:26 [INTERNAL] Attached Player::eventHandler to event entity.event (ID 6)
    12:39:26 [INTERNAL] Attached Player::eventHandler to event entity.metadata (ID 7)
    12:39:26 [INTERNAL] Attached Player::eventHandler to event player.equipment.change (ID 8)
    12:39:26 [INTERNAL] Attached Player::eventHandler to event player.armor (ID 9)
    12:39:26 [INTERNAL] Attached Player::eventHandler to event player.pickup (ID 10)
    12:39:26 [INTERNAL] Attached Player::eventHandler to event tile.container.slot (ID 11)
    12:39:26 [INTERNAL] Attached Player::eventHandler to event tile.update (ID 12)
    12:39:26 [INFO] czacza[/xx.xxx.x.xxx:59768] logged in with entity id 1 at (world, 106, 65, 88)
    12:39:40 [INTERNAL] Attached Player::close to event server.close (ID 13)
    12:39:40 [DEBUG] New Session started with 192.168.0.100:49604. MTU 1464, Client ID 18446744073186104205
    12:39:41 [INTERNAL] Attached Player::eventHandler to event server.chat (ID 14)
    12:39:41 [INTERNAL] Attached Player::eventHandler to event entity.motion (ID 15)
    12:39:41 [INTERNAL] Attached Player::eventHandler to event entity.animate (ID 16)
    12:39:41 [INTERNAL] Attached Player::eventHandler to event entity.event (ID 17)
    12:39:41 [INTERNAL] Attached Player::eventHandler to event entity.metadata (ID 18)
    12:39:41 [INTERNAL] Attached Player::eventHandler to event player.equipment.change (ID 19)
    12:39:41 [INTERNAL] Attached Player::eventHandler to event player.armor (ID 20)
    12:39:41 [INTERNAL] Attached Player::eventHandler to event player.pickup (ID 21)
    12:39:41 [INTERNAL] Attached Player::eventHandler to event tile.container.slot (ID 22)
    12:39:41 [INTERNAL] Attached Player::eventHandler to event tile.update (ID 23)
    12:39:41 [INFO] PEMapModder[/192.168.0.100:49604] logged in with entity id 2 at (world, 106, 65, 88)
    list
    12:39:50 [DEBUG] *console issued server command: /list
    12:39:50 [CMD] There are 2/20 players online:
    12:39:50 [CMD] czacza, PEMapModder
    say hi
    12:39:51 [DEBUG] *console issued server command: /say hi
    12:39:51 [INFO] [Console] hi
    say I am testing a plugin
    12:39:55 [DEBUG] *console issued server command: /say I am testing a plugin
    12:39:55 [INFO] [Console] I am testing a plugin
    12:40:00 [DEBUG] czacza's session has been updated. 12 seconds have been added into 12.
    12:40:00 [INFO] (Newly spawned) <world> @[email protected] czacza: hi
    12:40:00 [INFO] <czacza> hi
    12:40:16 [DEBUG] PEMapModder's session has been updated. 4 seconds have been added into 1395923692.
    12:40:16 [INFO] (Addict of the Server) <world> @[email protected] PEMapModder: hi
    12:40:16 [INFO] <PEMapModder> hi
    12:40:18 [DEBUG] PEMapModder's session has been updated. 1395924018 seconds have been added into 2791847710.
    12:40:18 [INFO] PEMapModder[/192.168.0.100:49604] logged out due to client disconnect
    12:40:18 [DEBUG] czacza's session has been updated. 1395924018 seconds have been added into 1395924030.
    12:40:18 [INFO] (Addict of the Server) <world> @[email protected] czacza: ja polnisch
    12:40:18 [INFO] <czacza> ja polnisch
    s12:40:44 [DEBUG] czacza issued server command: /spawn
    12:40:44 [WARNING] czacza moved too quickly!
    12:40:44 [WARNING] czacza moved too quickly!
    12:40:47 [DEBUG] czacza's session has been updated. 1395924047 seconds have been added into 2791848077.
    12:40:47 [INFO] czacza[/xx.xxx.x.xxx:59768] logged out due to client disconnect
  2. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    When onChat is run and $time=$this->updateSession($data["player"]); will set the session to 0, thus next time it is evaluated we get time()-0. So this: $this->sessions[$p->iusername]=0; should be: $this->sessions[$p->iusername]=time();
    PEMapModder likes this.
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Thanks I should have used time() instead of 0

Share This Page

Advertisement