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

Writing lines into Config

Discussion in 'Development' started by InspectorGadget, Jan 27, 2017.

  1. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Hi again :D. I've been cracking my head on this plugin and i didn't get a way to solve this..

    What i need:
    1) The command i execute should write in the config.

    Not an error but this is happening:
    config.yml
    PHP:
    ---
    formatformat
    ...

    My Command Code:
    PHP:
    <?php

    namespace RTG\HUD\command;

    use 
    pocketmine\{ServerPlayercommand\Commandcommand\CommandSender};
    use 
    pocketmine\command\CommandExecutor;

    use 
    RTG\HUD\Loader;

    /**
     * CustomHUD v 1.0.0
     *
     * @author RTG
     */

    class HUDCommand implements CommandExecutor {
     
        public function 
    __construct(Loader $plugin) {
            
    $this->plugin $plugin;
        }
     
        public function 
    onCommand(CommandSender $senderCommand $cmd$label, array $param) {
         
            switch(
    strtolower($cmd->getName())) {
             
                case 
    "hud":
                 
                    if(isset(
    $param[0])) {
                        switch(
    $param[0]) {
                         
                            case 
    "toggle":
                 
                                if(isset(
    $this->plugin->enable[strtolower($sender->getName())])) {
                                    unset(
    $this->plugin->enable[strtolower($sender->getName())]);
                                    
    $sender->sendMessage("You have turned off CustomHUD!");
                                }
                                else {
                                    
    $this->plugin->enable[strtolower($sender->getName())] = $sender->getName();
                                    
    $sender->sendMessage("You have turned on CustomHUD!");
                                }
                         
                                return 
    true;
                            break;
                         
                            case 
    "format":
                             
                                if(isset(
    $param[1])) {
                                 
                                    
    $key strtolower(array_shift($param));
                                     
                                     
                                    
    $this->plugin->cfg->set(format$key);
                                    
    $this->plugin->cfg->save();
                                    
    $sender->sendMessage("Passed!");
                                     
                                }
                                else {
                                    
    $sender->sendMessage("Usage: /hud set {format}");
                                }
                             
                                return 
    true;
                            break;

                        }
                    }
                    else {
                        
    $sender->sendMessage("Usage: /hud toggle");
                    }
                 
                    return 
    true;
                break;
             
            }
           
        }
     
    }
    Main:
    PHP:
    <?php

    namespace RTG\HUD;

    /* Essentials */
    use pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\Server;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\Config;

    use 
    RTG\HUD\Task;
    use 
    RTG\HUD\command\HUDCommand;

    class 
    Loader extends PluginBase implements Listener {
      
        public 
    $enable;
        public 
    $cfg;
      
        public function 
    onEnable() {
            @
    mkdir($this->getDataFolder());
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getLogger()->warning("Starting...");
            
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new Task($this), 20);
            
    $this->enable = array();
          
            
    /* Config */
            
    $this->cfg = new Config($this->getDataFolder() . "config.yml"Config::YAML);
          
            
    $this->getCommand("hud")->setExecutor(new HUDCommand($this));
        }
      
        public function 
    onDisable() {
        }
      
    }
    Task:
    PHP:
    <?php

    namespace RTG\HUD;

    use 
    RTG\HUD\Loader;

    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\plugin\Plugin;

    class 
    Task extends PluginTask {
       
        protected 
    $myPlugin;


        public function 
    __construct(Loader $plugin) {
            
    parent::__construct($plugin);
            
    $this->myPlugin $plugin;
        }
       
        public function 
    onRun($tick) {
           
            
    $online $this->myPlugin->getServer()->getOnlinePlayers();
            
    $c $this->myPlugin->getConfig();
           
                foreach(
    $online as $p) {
                   
                    if(isset(
    $this->myPlugin->enable[strtolower($p->getName())])) {
                        
    $p->sendPopup("->".$c->get("hud"));
                    }
                   
                }
               
        }
       
    }
    Any help would be appreciated...
     
  2. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    In your HUDCommand class
    PHP:
    $key strtolower(array_shift($param));
    array_shift will remove the first value and return it, so it will remove "format" and return "format" so $key = "format", to fix you just add another array_shift($param); before it
    PHP:
    $this->plugin->cfg->set(format$key);
    i doubt the plugin will even work though it should be "format" if im not wrong
     
  3. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
  4. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    You've put the array_shift call where you're getting the text to save to the config when it should be in the switch statement.

    Also, when you're working with strings in PHP make sure you put single or double quotes (when you set the format in the config you don't use quotes).
     
  5. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Ok Thanks
     
  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.