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

Config isn't working as expected | Error

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

  1. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Hi! I've executed several commands to confirm that my code is working perfectly..
    Executed Commands:
    PHP:
    [05:30:19] [Server thread/INFO]: Done (1.254s)! For helptype "help" or "?"
    review
    [05:30:21] [Server thread/INFO]: Usage: /review add {review}
    review add
    [05:30:24] [Server thread/INFO]: Usage: /review add [review]
    review add Hey Testing
    [05:30:44] [Server thread/INFO]: [ReviewYou review has been added!
    Main File:
    PHP:
    <?php

    namespace RTG\GitHub;

    /*
     * All rights reserved RTGNetworkkk
     */

    /* Essentials */
    use pocketmine\Player;
    use 
    pocketmine\Server;
    use 
    pocketmine\plugin\PluginBase;

    use 
    RTG\GitHub\Command\MyCommand;

    use 
    pocketmine\utils\Config;

    class 
    Review extends PluginBase {
      
        public function 
    onEnable() {
            @
    mkdir($this->getDataFolder());
            @
    mkdir($this->getDataFolder() . "players");
            
    $this->getLogger()->warning("[Review] DIR has been made!");
            
    $this->getLogger()->warning("Loaded!");
          
            
    /* Execution */
            
    $this->getCommand("review")->setExecutor(new MyCommand($this));
          
            
    $this->getLogger()->warning("[Review] Everything has been loaded!");
        }
      
        public function 
    onDisable() {
        }
      
    }
    My Command:
    PHP:
    <?php

    namespace RTG\GitHub\Command;

    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\CommandExecutor;

    use 
    pocketmine\utils\Config;

    use 
    RTG\GitHub\Review;

    use 
    pocketmine\utils\TextFormat as TF;

    class 
    MyCommand implements CommandExecutor {
      
        public function 
    __construct(Review $plugin) {
            
    $this->plugin $plugin;
        }
      
        public function 
    onCommand(CommandSender $senderCommand $cmd$label, array $param) {
            switch(
    $cmd->getName()) {
              
                case 
    "review":
                  
                    if(isset(
    $param[0])) {
                      
                        switch(
    $param[0]) {
                          
                            case 
    "add":
                              
                                if(
    $sender->hasPermission("review.add")) {
                  
                                    
    $n $sender->getName();
                                  
                                    if(isset(
    $param[1])) {
                                      
                                      
                                        if(
    count($param) < 1) {
                                            
    $sender->sendMessage("You args are incorrect!");
                                            return 
    false;
                                        }
                                        else {
                                            
    $msg trim(implode(" "$param));
                      
                                            
    $this->cfg = new Config($this->plugin->getDataFolder() . "players/" strtolower($sender->getName()) . ".txt"Config::ENUM);
                      
                                            
    $this->cfg->set($msg);
                                            
    $this->cfg->save();
                      
                                            
    $sender->sendMessage("[Review] You review has been added!");
                                        }
                  
                                    }
                                    else {
                                        
    $sender->sendMessage("Usage: /review add [review]");
                                    }
                  
                                }
                                else {
                                    
    $sender->sendMessage(TF::RED "You have no permission to use this command!");
                                } 
                              
                                return 
    true;
                            break;       
                          
                        }
                    }
                    else {
                        
    $sender->sendMessage("Usage: /review add {review}");
                    }
                  
                    return 
    true;
                break;
              
            }
          
        }
      
    }
    but it's logging weirdly on my Config: :/

    Config:
    PHP:
    add Hey Testing
     
    Last edited: Jan 26, 2017
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    le poor msg format.
     
  3. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    What do you mean?
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Thank god you edited the message, now its readable.
     
  5. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Lol
     
  6. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    You're imploding the entire argument array and saving it to your config, you need to get rid off the sub-command input before you implode the array. I'd personally just call array_shift($args) when I switch the sub-command.
    PHP:
    switch(strtolower(array_shift($args))){
        case 
    "add":
            
    $msg implode(" "$args);
            
    // your code here
            
    break;
    }
     
  7. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Ok, I'll try this!
     
  8. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    I really don't understand this http://php.net/manual/en/function.array-shift.php , can u explain more in depth?
     
  9. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    The function takes an array as an argument, it takes the first entry of that array and returns the value of the entry it removed. The array that you pass to the function is edited by the function so that the value it returns will be removed from the array.
    PHP:
    $myArray = [
        
    "first value",
        
    "second value",
        
    "third value"
    ];

    array_shift($myArray); //returns "first value"

    // $myArray now looks like this: ["second value", "third value"]

    array_shift($myArray); // returns "second value"

    // $myArray now looks like: ["third value"]
    Whilst this is not a PHP forum I'm still answering your question because you're trying to understand the code instead of the usual copy-paste response, props to you for that.
     
    Last edited: Jan 26, 2017
  10. InspectorGadget

    InspectorGadget Zombie Pigman

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