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

[Help Needed] Save to config | unknown error

Discussion in 'Development' started by InspectorGadget, Mar 8, 2017.

  1. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Hi! I need help with my new plugin, the issue is that it doesn't want to save the lines which is from Args[3] and the lines should be saved like: - tell {player} Hi

    How do I use the command to save the line?

    /if {player} event join tell {player} that you've joined!

    It'd make a little sense once you've read the SRC..

    Loader.php

    PHP:
    <?php

    /* 
     * Copyright (C) 2017 RTG
     *
     * This program is free software: you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation, either version 3 of the License, or
     * (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     */

    namespace RTG\IFMCPE;

    /* Essentials */
    use pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\TextFormat as TF;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\event\player\PlayerJoinEvent;

    class 
    Loader extends PluginBase implements Listener {
       
        public function 
    onEnable() {
           
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->saveDefaultConfig();
           
            
    $cfg $this->getConfig()->getAll();
           
        }
       
        public function 
    onJoin(PlayerJoinEvent $e) {
           
            
    $p $e->getPlayer();
           
                foreach(
    $cfg["jcmd"] as $cmd) {
                   
                    
    $this->getServer()->dispatchCommand(new \pocketmine\command\ConsoleCommandSender(), str_replace("{player}"$e->getPlayer(), $cmd));
                   
                }
           
        }
       
        public function 
    onCommand(CommandSender $senderCommand $command$label, array $args) {
           
            switch (
    strtolower($command->getName())) {
               
                case 
    "if":
                   
                    if(!
    $sender->hasPermission("ifmcpe.command")) {
                        
    $sender->sendMessage(TF::RED "You have no permission to use this command!");
                    }
                   
                    if(!isset(
    $args[1])) {
                        
    $sender->sendMessage($this->onHelp($sender));
                    }
                   
                    if(
    $args[1] === "{player}") {
                       
                        if(
    $args[2] === "event") {
                           
                            if(
    $args[3] === "join") {
                               
                                
    $line $args[3];
                               
                                
    $this->getConfig()->set("jcmd"$line);
                                
    $this->getConfig()->save();
                               
                            }
                            else {
                                
    $this->onHelp($sender);
                            }

                        }
                        else {
                            
    $this->onHelp($sender);
                        }
                       
                    }
                    else {
                        
    $this->onHelp($sender);
                    }
                   
            }
           
        }
       
        public function 
    onHelp($p) {
           
            
    $p->sendMessage(TF::YELLOW "-- HELP --");
           
        }
       
        public function 
    onDisable() {
            
    $this->getConfig()->save();
        }
       
    }
    Config:

    Code:
    ---
    # Author: RTGDaCoder
    version: 1.0.0
    jcmd:
        - tell {player} you are awesome!
    ...
    
    GitHub: https://github.com/RTGDaCoder/IFMCPE
     
  2. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    I think there's something wrong in the config, can someone check my command execution and config save method? If it's not correct
    Thanks..
     
  3. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    You save $args[3] in the config, but you should array split the array at $args[3] and then implode it with a space.
     
  4. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Would this work?

    PHP:

        
    public function onCommand(CommandSender $senderCommand $command$label, array $args) {
           
            switch (
    strtolower($command->getName())) {
               
                case 
    "if":
                   
                    if(!
    $sender->hasPermission("ifmcpe.command")) {
                        
    $sender->sendMessage(TF::RED "You have no permission to use this command!");
                    }
                   
                    if(!isset(
    $args[1])) {
                        
    $sender->sendMessage($this->onHelp($sender));
                    }
                   
                    if(
    $args[1] === "{player}") {
                       
                        if(
    $args[2] === "event") {
                           
                            if(
    $args[3] === "join") {
                               
                                
    $line implode(" "$args[3]);
                               
                                
    $this->getConfig()->set("jcmd"$line);
                                
    $this->getConfig()->save();
                               
                            }
                            else {
                                
    $this->onHelp($sender);
                            }

                        }
                        else {
                            
    $this->onHelp($sender);
                        }
                       
                    }
                    else {
                        
    $this->onHelp($sender);
                    }
                   
            }
           
        }
     
  5. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    What @Sandertv meant is
    PHP:
    $line implode(' ',array_slice($args,3));
    and the first argument is $args[0] not $args[1], second is $args[1] not $args[2] and so on
     
    Marabou, corytortoise and Sandertv like this.
  6. 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.