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

Config

Discussion in 'Development' started by Besher, Oct 26, 2020.

  1. Besher

    Besher Witch

    Messages:
    51
    How do accses a file
    this is my code and i would like to know how to get players file and change it
    i would like to change only the players config for example he dies or gets a kill

    PHP:
    <?php

    declare(strict_types=1);

    namespace 
    besher\PlayerStats;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\event\player\PlayerDeathEvent;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\Player;

    class 
    Main extends PluginBase implements Listener{
        
        public function 
    onEnable(){
            @
    mkdir($this->getDataFolder() . "stats/");
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }
        
        public function 
    onCommand(CommandSender $senderCommand $cmdString $label, array $args) : bool{
            switch(
    $cmd->getName()){
                case 
    "stats":
                    global 
    $reciver;
                    
    $reciver $this->getServer()->getPlayer($args[0]);
                        
    $this->profile($sender);
            }
            return 
    true;
        }
        
        public function 
    onJoin(PlayerJoinEvent $e)
        {
            
    $player $e->getPlayer();
            
    $name $player->getName();
            
    $this->cfg = new Config($this->getDataFolder() . "stats/" strtolower($player->getName()), Config::YAML, array(
                
    "Name" => "$name",
                
    "Deaths" => 0,
                
    "Kills" => 0,
                
    "Jumps" => 0
            
    ));
            
    $this->cfg;
        }
        
        public function 
    onDeath(PlayerDeathEvent $e)
        {
            
    $player $e->getPlayer();
            
    $this->cfg->set("Deaths"$this->cfg->get("Deaths") + 1);
            
    $this->cfg->save();
        }
        
        public function 
    onKill(PlayerDeathEvent $e){
            
    $cause $e->getEntity()->getLastDamageCause();
                    if(
    $cause instanceof EntityDamageEntityEvent){
                    
    $killer $cause->getDamager();
                    if(
    $killer instanceof Player){
                        
    $this->cfg->set("Kills"$this->cfg->get("Kills") + 1);
                        
    $this->cfg->save();
                   }
                }
        }
        
        public function 
    profile($player){
            
    $form $this->getServer()->getPluginManager()->getPlugin("FormAPI")->createSimpleForm(function (Player $playerint $data null){
                if(
    $data === null){
                    return 
    true;
                }
            });
            
    $form->setTitle("§l§2YOUR STATS");
            global 
    $reciver;
            
    $form->setContent("Player Stats: " $reciver->getName() . "\nDeaths: "
            
    $form->addButton("Exit");
            
    $form->sendToPlayer($player);
            return 
    $form;
        }

    }
     
  2. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Thats already the way, to store the config instances separately, store and retrieve using $this->cfg[$player->getId()]
     
  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.