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

Is this wrong?

Comments in 'Plugin Development' started by wolfdale, Mar 31, 2015.

  1. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    21:29:41 [CRITICAL] Could not execute task Callback#PlayerLog\Main::Timeadd: Undefined index: wei2
    21:29:41 [NOTICE] ArrayOutOfBoundsException: "Undefined index: wei2" (E_NOTICE) in "/PlayerLog_v1.0.0/src/PlayerLog/Main" at line 65

    at line 65: $this->time[$p->getName()]++;

    Am i doing something wrong? The plugin still works, just that this message appears
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    this means $this->time[$p->getName()] is not intiailized.
    wolfdale likes this.
  3. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    how to intiailized it?
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Maybe in PlayerJoinEvent? I can't help much if I don't have full code.
  5. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    Code:
    <?php
    
    
    namespace PlayerLog;
    
    use pocketmine\plugin\PluginBase;
    use pocketmine\event\Listener;
    use pocketmine\Server;
    use pocketmine\Player;
    use pocketmine\event\player\PlayerJoinEvent;
    use pocketmine\event\player\PlayerQuitEvent;
    use pocketmine\utils\Config;
    use pocketmine\scheduler\CallbackTask;
    
    class Main extends PluginBase implements Listener{
       
        private $api, $server, $path;
        public $time;
       
        public function onEnable(){
            $this->getServer()->getPluginManager()->registerEvents($this, $this);
            if(!is_file($this->getDataFolder() . "/log.txt")) @mkdir($this->getDataFolder());
            $this->getServer()->getScheduler()->scheduleRepeatingTask(new CallbackTask([$this,"Timeadd" ]), 20);
        }
       
        public function onJoin(PlayerJoinEvent $event) {
            $name = $event->getPlayer()->getName();
            //$this->getDataFolder() . "/log.txt"
            //fopen($logPath, 'a');
            fwrite(fopen($this->getDataFolder() . "/log.txt", 'a'), "<" . date("Ymd") . " " . date('H') . ":" . date('i') . ":" .  date('s') . "> ". $name . " in" . PHP_EOL);
            //file_put_contents($this->getDataFolder() . "/log.txt", date('H') . ":" . date('i') ." ". $name . " in");
            $this->time[$name] = "0";
        }
       
        public function onPlayerQuit(PlayerQuitEvent $event){
            $name = $event->getPlayer()->getName();
            if ($this->time[$name] > 60){
            $min = $this->time[$name]/60;
            var_dump($min);
            $mins = round($min, 0, PHP_ROUND_HALF_UP);
            if (($mins * 60) >= $this->time[$name]){
                $secs = ($mins * 60) - $this->time[$name];
            }else{
            $secs = $this->time[$name] - ($mins * 60);
            }
            var_dump($secs);
            }else{
                $mins = 0;
                $secs = $this->time[$name];
            }
            var_dump($this->time[$name]);
            fwrite(fopen($this->getDataFolder() . "/log.txt", 'a'), "<" . date("Ymd") . " " . date('H') . ":" . date('i') . ":" .  date('s') . "> ". $name . " out" . " ". $mins. "min" . $secs . "sec" . PHP_EOL);
            //file_put_contents($this->getDataFolder() . "/log.txt", date('H') . ":" . date('i') . " ". $name . " out" . " ". $mins. "min" . $secs . "sec");
        }
       
        public function Timeadd() {
            foreach($this->getServer()->getOnlinePlayers() as $p){
                $this->time[$p->getName()]++;
            }
        }
       
        public function onDisable(){
            $this->getLogger()->info("PlayerLog Disabled!");
            fwrite(fopen($this->getDataFolder() . "/log.txt", 'a'), "<" . date("Ymd") . " " . date('H') . ":" . date('i') . ":" .  date('s') . "> serverclosed" . PHP_EOL);
            foreach($this->getServer()->getOnlinePlayers() as $p){
                 unset ($this->time[$p->getName()]);
            }
        }
    }
    ?>
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    1. What? private $api?
    2. Numbers should be saved as numbers not strings. Why "0"?
    3. PlayerQuitEvent could be called before PlayerJoinEvent is called. For example player disconnects before he spawns.
    4. File streams opened with fopen() must be closed with fclose(), else have fun with the Too Many Open Files error.
    5. It is a better practice to use $player->getId() rather than $player->getName().
    Andrey Nazarchuk likes this.
  7. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    1. I always thought that it is required, no?
    2. ok, fixed that, but does not seem to solve the problem
    3. Sorry, i cannot understand what you mean
    4. ok, just put fclose() after the line with fopen() will do right?
    5. i use names so that there is no need to convert to name again when writing log
    Andrey Nazarchuk likes this.

Share This Page

Advertisement