1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

ChatLog(Config)

Discussion in 'Development' started by SkyArt4, Jan 2, 2017.

  1. SkyArt4

    SkyArt4 Silverfish

    Messages:
    16
    GitHub:
    nadaree
    Take a log when you chat Plugin I want work.
    But get an error...
    Code:
    0:23:34] critical > Could not pass event 'pocketmine\event\player\PlayerChatEvent' to 'ChatLog v1': Call to a member function set() on null on CONSOLE\ChatLog\Main
    [00:23:34] critical > Error: "Call to a member function set() on null" (EXCEPTION) in "/plugins/ChatLog/src/CONSOLE/ChatLog/Main" at line 27
    
    PHP:
    public function onChat(PlayerChatEvent $event)
    {
    $p $event->getPlayer();
    $ms $event->getMessage();
    $user $p->getName();
    $this->cl->set('$user''$ms');
    }
    I don't know how to write config.
    Could you tell me?...
     
  2. VentroxStudio

    VentroxStudio Witch

    Messages:
    71
    If you want to do it with a config u need to save it.
    PHP:
    public function onChat(PlayerChatEvent $event)
    {
    $p $event->getPlayer();
    $ms $event->getMessage();
    $user $p->getName();

    $conf = new Config($this->getDataFolder() . "config.yml"Config::YAML);

    $conf->set($user$ms);
    $conf->save();
    }
    * And your error:
    Pls show us your public $cl

    And u have a server.log file on your server :p
     
    Last edited: Jan 2, 2017
    SkyArt4 likes this.
  3. VentroxStudio

    VentroxStudio Witch

    Messages:
    71
    You could also create message.log file or something like that
     
    SkyArt4 likes this.
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    No, way too laggy to call config in player chat event. Use global variables. Also... There is a difference between (') and (") in PHP.
    PHP:
    $number 1020;
    $var 'The number is $number.';
    //OUTPUT: The number is $number.

    $var "The number is $number."
    //OUTPUT: The number is 1020.
    Assuming you are a beginner, stick with double quotes for now.

    For the chat logging...
    PHP:
    public $logs = [];

    public function 
    onChat(PlayerChatEvent $event) {
        
    $player $event->getPlayer()->getName();
        
    $message $event->getMessage();
        
    $this->logs[$player] = $message;
    }

    public function 
    onDisable() {
        
    $conf = new Config($this->getDataFolder() . "config.yml"Config::YAML);
        foreach (
    $this->logs as $player => $message) {
            
    $conf->set($player$message);
        }
        
    $conf->save();
    }
     
    applqpak, 0x15f and SkyArt4 like this.
  5. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    re ^:
    it dosent hurt having a periodic save task
    but i would say use just use fwrite,
     
    0x15f likes this.
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Oh, yeah! That SOFe way.
     
    applqpak and 0x15f like this.
  7. SkyArt4

    SkyArt4 Silverfish

    Messages:
    16
    GitHub:
    nadaree
    Much obliged!
    I did not notice how to make a log file at all...
     
    0x15f likes this.
  8. VentroxStudio

    VentroxStudio Witch

    Messages:
    71
  9. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    Yea, I would recommend appending this to a log file
    PHP:
    #opening
    $handle fopen("PATH/TO/FILE.log"'a');
    if(!
    is_resource($handle)){
        echo(
    "Error: cannot open/create file! Does this process have proper perms?");
    }

    #writing
    if(fwrite($handle$msg) === false){
        echo(
    "Failed to write \"".$msg."\" to file :(");
    }

    #closing
    if(fclose($handle) !== true){
         echo(
    "Huh?");
    }
     
    applqpak and Muqsit like this.
  10. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    For this situation, path to config file is...
    $this->getDataFolder()."config.yml"
     
    applqpak likes this.
  11. VentroxStudio

    VentroxStudio Witch

    Messages:
    71
    I already posted that :p
     
    applqpak likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.