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

Hellp with yaml stuff!

Discussion in 'Development' started by nikoskon, Aug 28, 2018.

  1. nikoskon

    nikoskon Silverfish

    Messages:
    20
    Hello, im making a warps plugin and i use yaml as database!
    when i add a warp (lets call it 'a') the yaml looks like this:

    ---
    - world: world
    x: 253
    "y": 4
    z: 264
    name: a
    ...

    When i add another warp (lets call it 'b') the yaml looks like this:

    ---
    - world: world
    x: 253
    "y": 4
    z: 264
    name: a
    - world: world
    x: 250
    "y": 4
    z: 270
    name: b
    ...

    By deleting 'b' everything will be fine! BUT if you delete 'a' this will happen to the yaml:

    ---
    1:
    world: world
    x: 250
    "y": 4
    z: 270
    name: b
    ...

    instead of having '- world ...' its now '1: world ...' and if i try to call it i get this error:
    An unknown error occurred while attempting to perform this command
    [19:08:04] [Server thread/CRITICAL]: Unhandled exception executing command 'warp' in warp: Undefined offset: 0
    [19:08:04] [Server thread/CRITICAL]: ErrorException: "Undefined offset: 0" (EXCEPTION) in "WarpsPro-master/src/WarpsPro/WarpsPro" at line 112

    here is my code:
    PHP:
    <?php

    namespace WarpsPro;

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

    use 
    pocketmine\level\Position;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\Player;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\Server;

    class 
    WarpsPro extends PluginBase implements CommandExecutor{

        
    /** @var Config */
        
    private $warps;
        
    /** @var string */
        
    public $world;
        
    /** @var string */
        
    public $warp_loc;
        
    /** @var Position[] */
        
    public $config;
        
    /** @var int[] */
        
    public $player_cords;
        
    /** @var bool */
        
    public $enable_wild;
        
        public function 
    WarpID($name){
            
    $data $this->warps->getAll();

            for(
    $i 0$i count($data); $i++){
                if(
    $data[$i]["name"] == $name){
                    return 
    $i;
                }
            }
            return -
    1;
        }

        public function 
    onCommand(CommandSender $senderCommand $cmdstring $label, array $args) : bool{
            switch(
    $cmd->getName()){
                case 
    'warp':
                    if (!
    $sender->hasPermission("warpspro.command.warp")) {
                        
    $sender->sendMessage("§c[WarpsPro] No permission.");
                        return 
    true;
                    }
                    if (
    $sender instanceof Player)
                    {
                        if (
    count($args) == 0)
                        {
                            
    $warp_list null;

                            
    $data $this->warps->getAll();

                            for(
    $i 0$i count($data); $i++)
                            {
                                
    $warp_list .= '§a[§f' $data[$i]["name"] . '§a]';
                            }

                            if(
    $warp_list != null){
                                
    $sender->sendMessage("Warps: " $warp_list);
                                return 
    true;
                            }else{
                                
    $sender->sendMessage("§cThis server has no warps.");
                                return 
    true;
                            }

                        }else{
                            
    $this->warp_loc $args[0];
                            
    $warp_id $this->WarpID($this->warp_loc);
                            
    $data $this->warps->getAll();

                            if(
    $warp_id <= -1){
                                
    $sender->sendMessage("§cThere is no warp by that name listed.");
                                return 
    true;
                            }

                            if(isset(
    $data[$warp_id])){
                                if(
    Server::getInstance()->loadLevel($data[$warp_id]["world"]) != false){
                                    
    $curr_world Server::getInstance()->getLevelByName($data[$warp_id]["world"]);
                                    
    $pos = new Position((int)$data[$warp_id]["x"],
                                                        (int)
    $data[$warp_id]["y"],
                                                        (int)
    $data[$warp_id]["z"], $curr_world);

                                    
    $sender->sendMessage("§aYou warped to:§f " $this->warp_loc);
                                    
    $sender->teleport($pos);
                                    return 
    true;
                                }else{
                                    
    $sender->sendMessage("§cCould not load chunk.§f It's not safe to teleport.");
                                    return 
    true;
                                }

                            }else{
                                
    $sender->sendMessage("§cThere is no warp by that name listed.");
                                return 
    true;
                            }
                        }
                    } else{
                        if (
    count($args) == 0)
                        {
                            
    $warp_list null;

                            
    $data $this->warps->getAll();
     
                            for(
    $i 0$i count($data); $i++)
                            {
                                
    $warp_list .= '§a[§f' $data[$i]["name"] . '§a]';
                            }

                            if(
    $warp_list != null){
                                
    $sender->sendMessage("Warps: " $warp_list);
                                return 
    true;
                            }else{
                                
    $sender->sendMessage("§cThis server has no warps.");
                                return 
    true;
                            }

                        }else{
                            
    $sender->sendMessage("§cThis command can only be used in the game.");
                            return 
    true;
                        }
                    }
                    break;
                case 
    'setwarp':
                    if (!
    $sender->hasPermission("warpspro.command.setwarp")) {
                        
    $sender->sendMessage("§c[WarpsPro] No permission.");
                        return 
    true;
                    }
                    if (
    $sender instanceof Player)
                    {
                        if((
    count($args) != 0) && (count($args) < 2))
                        {
                            
    $data $this->warps->getAll();
                            if(
    $this->WarpID($args[0]) > -1){
                                
    $sender->sendMessage("§cWarp already exists!");
                                return 
    true;
                            }

                            
    $this->player_cords = array('x' => (int) $sender->getX(),'y' => (int) $sender->getY(),'z' => (int) $sender->getZ());
                            
    $this->world $sender->getLevel()->getName();
                            
    $this->warp_loc $args[0];
                            
    $warp_id count($data);
                            
                            
    $data[$warp_id]["world"] = $this->world;
                            
    $data[$warp_id]["x"] = $this->player_cords["x"];
                            
    $data[$warp_id]["y"] = $this->player_cords["y"];
                            
    $data[$warp_id]["z"] = $this->player_cords["z"];
                            
    $data[$warp_id]["name"] = $this->warp_loc;

                            
    $this->warps->setAll($data);
                            
    $this->warps->save();

                            
    $sender->sendMessage("§aWarp set as:§r " $args[0]);
                            return 
    true;
                        }
                        else
                        {
                            
    $sender->sendMessage("§cINVALID USAGE:");
                            return 
    false;
                        }
                    }
                    else
                    {
                        
    $sender->sendMessage("§cThis command can only be used in the game.");
                        return 
    true;
                    }
                    break;
                case 
    'delwarp':
                    if (!
    $sender->hasPermission("warpspro.command.delwarp")) {
                        
    $sender->sendMessage("§c[WarpsPro] No permission.");
                        return 
    true;
                    }
                    if((
    count($args) != 0) && (count($args) < 2))
                    {
                        
    $data $this->warps->getAll();
                        
    $this->warp_loc $args[0];

                        
    $warp_id $this->WarpID($this->warp_loc);

                        if(
    $warp_id <= -1){
                            
    $sender->sendMessage("§cNo Warps matching that name for this server.");
                            return 
    true;
                        }
                        
                        if(isset(
    $data[$warp_id]))
                        {
                            unset(
    $data[$warp_id]);

                            
    $this->warps->setAll($data);
                            
    $this->warps->save();

                            
    $sender->sendMessage("§aWarp named:§f " $this->warp_loc " §r§a,has been deleted.");
                            return 
    true;
                        }
                        else
                        {
                            
    $sender->sendMessage("§cNo Warps matching that name for this server.");
                            return 
    true;
                        }
                    }
                    else
                    {
                        
    $sender->sendMessage("§cINVALID USAGE!");
                        return 
    false;
                    }
                    break;                
                default:
                    return 
    false;
                }
                return 
    false;
            }

        public function 
    check_config(){
            
    $this->saveDefaultConfig();
            
    $this->config = new Config($this->getDataFolder()."config.yml"Config::YAML, array());
            
    $this->config->set('plugin-name',"WarpsPro");
            
    $this->config->save();
        }

        public function 
    onEnable(){
            
    $this->getLogger()->info("§6WarpsPro is loading...");
            @
    mkdir($this->getDataFolder());

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

            
    $this->check_config();

            
    $this->getLogger()->info("§aWarpsPro has been loaded!");
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }

        public function 
    onDisable(){
            
    $this->warps->save();
            
    $this->getLogger()->info("WarpsPro Disabled");
        }
    }
    any help is welcome!
     
  2. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    please cease and desist... YAML is not a database format, don't abuse it, period.
     
    corytortoise likes this.
  3. nikoskon

    nikoskon Silverfish

    Messages:
    20
    When i said database, i mean a local storing way. Sorry my bad!
     
  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.