Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help with yml

Comments in 'Plugin Development' started by Rspuds, Aug 18, 2015.

  1. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    ok so I have this plugin that I'm pretty sure is all working except the yml. This code for the yml was given to me a while ago and haven't really had time to work on this project since. As I said this code was giving to me so i have no clue really how it is suppose to work so I have two requests. One, please help me solve this error and get the plugin working. Two, Explain to me what exactly is going on with the yml so I can understand it and I don't have to ask for help in the future with different projects. I have looked around many other posts and every post someone says one way to do it and then someone else says its in correct or bad practice so I really just need a straight forward answer of how to use them.

    Here's my code
    PHP:
    <?php

    namespace AreaEffects;

    use 
    pocketmine\Player;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\Server;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\event\entity;
    use 
    pocketmine\entity\Effect;
    use 
    pocketmine\event\player\PlayerMoveEvent;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\level\Location;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\math\Vector3;

    class 
    Main extends PluginBase implements Listener{
       
        public 
    $areas;
        private 
    $pos1$pos2;

        public function 
    onLoad() {
            
    $this->getLogger()->info(TextFormat::GREEN."AreaEffects has been loaded!");
        }
       
        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getLogger()->info(TextFormat::GREEN."AreaEffects has been loaded!");
            
    $this->areas = (new Config($this->getDataFolder(). "areas.yml"Config::YAML))->getAll();
        }
       
        public function 
    onDisable(){
            
    $this->areas = new Config($this->getDataFolder()."areas.yml"$this->areas);
        }
       
        public function 
    onCommand(CommandSender $senderCommand $command$label, array $args) {$this->areas = new Config($this->getDataFolder()."area.yml"Config::YAML, array());
            if (
    $command == "ae") {   
                switch (
    $args[0]){
                    case 
    "pos1":
                        if(
    $sender instanceof Player){
                            
    $this->pos[$sender->getName()]['pos1'] = new Position($sender->getFloorX(), $sender->getFloorY(), $sender->getFloorZ(), $sender->getLevel());
                            
    $sender->sendMessage('First position set');
                            return 
    true;
                            break;
                            }
                   
                    case 
    "pos2":
                        if(
    $sender instanceof Player){
                            
    $this->pos[$sender->getName()]['pos2'] = new Position($sender->getFloorX(), $sender->getFloorY(), $sender->getFloorZ(), $sender->getLevel());
                            
    $sender->sendMessage('Second position set');
                            return 
    true;
                            break;
                            }
                           
                    case 
    "create":
                        if(
    $sender instanceof Player){
                            if(isset(
    $args[1], $args[2])){
                                if(isset(
    $this->pos[$sender->getName()]['pos1'] , $this->pos[$sender->getName()]['pos2'])){
                                if(
    $this->pos[$sender->getName()]['pos1']->getLevel() === $this->pos[$sender->getName()]['pos2']->getLevel()){
                                    
    $this->areas[$args[1]] = array('pos1' => array('x' => $this->pos[$sender->getName()]['pos1']->getX(),'y' => $this->pos[$sender->getName()]['pos1']->getY(),'z' => $this->pos[$sender->getName()]['pos1']->getZ()),'pos2' => array('x' => $this->pos[$sender->getName()]['pos2']->getX(),'y' => $this->pos[$sender->getName()]['pos2']->getY(),'z' => $this->pos[$sender->getName()]['pos1']->getZ()));
                                    
    $sender->sendMessage('[AreasEffect] '.$args[1].' created');
                                    unset (
    $this->pos[$sender->getName()]);
                                    return 
    true;
                                    }
                                }
                            }
                                    }else{
    $sender->sendMessage('Use this command in-game');
                                    return 
    true;
                                    }
                }
            }
        }
                                   
                                   
                                   
       
        public function 
    onMove(PlayerMoveEvent $event){
        if (isset(
    $this->areas)){   
            
    $player $event->getPlayer();
            if(empty(
    $this->areas)) {return;}
            foreach(
    $this->areas as $area){
                if(
    $this->isInArea($player$area)){$this->giveEffect($player ,$area);
                    }
                }
            }
        }
       
        public function 
    isInArea(Player $player$area){
           
            if(
    $player->getFloorX() >= $area['pos1'['x']] && $player->getFloorX() <= $area['pos2'['x']] && $player->getFloorY() >=$area['pos1'['y']] && $player->getFloorY() <= $area['pos2'['y']] && $player->getFloorZ() >= $area['pos1'['z']] && $player->getFloorZ() <= $area['pos2'['y']] && $area['level'] == $player->getLevel()) {return true;}
            else {return 
    false;
            }
        }
           
        public function 
    giveEffect($player$area){
            if(
    $player instanceof Player){
                
    $id $area['effect'['id']];
                
    $effect Effect::getEffect($id);
                
    $effect->setDuration($area['effect'['duration']]);
                
    $effect->setAmplifier($area['effect'['amplifier']]);
                
    $effect->setVisable($area['effect'['show']]);
                
    $player->addEffect($effect);
            }
        }
    }
    Thanks for any help!
  2. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    Also forgot here is the error i get when i run it

    [14:40:28] [Server thread/CRITICAL]: Could not save Config C:\Users\Ryan\Desktop\Pocket mine\PocketMine-MP Tester\plugins\AreaEffects/areas.yml: file_put_contents(C:\Users\Ryan\Desktop\Pocket mine\PocketMine-MP Tester\plugins\AreaEffects/areas.yml): failed to open stream: No such file or directory
  3. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Try to add this to your onEnable() function:
    PHP:
    @mkdir($this->getDataFolder());
  4. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    ok now the plugin will load but when I use the create command it gives me this error

    Fatal error: Cannot use object of type pocketmine\utils\Config as array in C:\Users\Ryan\Desktop\Pocket mine\PocketMine-MP Tester\plugins\AreaEffects0.9.1\src\AreaEffects\Main.php on line 61
  5. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Can you give me the .yml please?
  6. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    Its empty other then this:

    --- []
    ...

    I'm using it to store areas to apply effects in so when they create areas the areas will be added but nothing in there to begin with.
  7. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    add
    PHP:
    $this->saveResource("arenas.yml");
    onEnable(). This will actually create the file :p
    thebigsmileXD likes this.
  8. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    OK thanks I'll give it a try when I have time.
  9. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    Now I'm getting and error when I start up the server.

    [12:19:11] [Server thread/WARNING]: RuntimeException: "yaml_parse(): end of stream reached without finding document 0" (E_WARNING) in "/src/pocketmine/utils/Config" at line 143

    here's my onEnable code

    PHP:
        public function onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getLogger()->info(TextFormat::GREEN."AreaEffects has been loaded!");
            @
    mkdir($this->getDataFolder());
            
    $this->areas = (new Config($this->getDataFolder(). "areas.yml"Config::YAML))->getAll();
            
    $this->saveResource("areas.yml");
    not sure If I put it in the wrong spot or what the problem is.
  10. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Try to put the .yml files into the plugin folder (plugins->nameofyourplugin)
  11. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    PHP:
    public function onEnable() {
        
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        
    $this->getLogger()->info(TextFormat::GREEN."AreaEffects has been loaded!");
        
    $this->saveResource("areas.yml");
        
    $this->areas = (new Config($this->getDataFolder() . "areas.yml"Config::YAML))->getAll();
    }
    You have to save the file before you setup a connection to it :p
    thebigsmileXD likes this.
  12. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    I just wanted to write the exact same piece of code about 5 Minutes after you :D
    Like for that!
    CrazedMiner likes this.
  13. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    You're using Multi Dimensional Arrays wrong in fuction - isInArray()
    Wrong
    PHP:
    $area['pos1'['x']];
    Correct:
    PHP:
    $area['pos1']['x']
    It was me who gaved you code and making such silly mistake? Such shame.
    thebigsmileXD likes this.
  14. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    Fuction? :p
  15. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Wow, what a pointless comment :D you also do spelling mistakes.
    Whoaa.. I just was like: I HAVENT SEEN THAT?! Nice.
  16. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    Ya i thought that didn't seems right but wasn't to sure
    thebigsmileXD likes this.
  17. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    I thought I seen somewhere you have to close resource's or something like that after you use them. Is that true in this case or am I thinking of something else?
  18. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    now I'm getting this error

    Fatal error: Cannot use object of type pocketmine\utils\Config as array in C:\Users\Ryan\Desktop\Pocket mine\PocketMine-MP Tester\plugins\AreaEffects0.9.4\src\AreaEffects\Main.php on line 61

    I don't know if the call stack will help at all but here it is
    Call Stack:
    0.0312 410200 1. {main}() C:\Users\Ryan\Desktop\Pocket mine\PocketMine-MP Tester\PocketMine-MP.phar:0
    0.0312 405232 2. require('phar://C:/Users/Ryan/Desktop/Pocket mine/PocketMine-MP Tester/PocketMine-MP.phar/src/pocketmine/PocketMine.php') C:\Users\Ryan\Desktop\Pocket mine\PocketMine-MP Tester\PocketMine-MP.phar:1
    0.3432 544992 3. pocketmine\Server->__construct() phar://C:/Users/Ryan/Desktop/Pocket mine/PocketMine-MP Tester/PocketMine-MP.phar/src/pocketmine/PocketMine.php:464
    3.2604 12801632 4. pocketmine\Server->start() phar://C:/Users/Ryan/Desktop/Pocket mine/PocketMine-MP Tester/PocketMine-MP.phar/src/pocketmine/Server__32bit.php:1784
    3.3072 12899952 5. pocketmine\Server->tickProcessor() phar://C:/Users/Ryan/Desktop/Pocket mine/PocketMine-MP Tester/PocketMine-MP.phar/src/pocketmine/Server__32bit.php:2184
    64.4574 51652864 6. pocketmine\Server->tick() phar://C:/Users/Ryan/Desktop/Pocket mine/PocketMine-MP Tester/PocketMine-MP.phar/src/pocketmine/Server__32bit.php:2306
    64.4574 51652928 7. pocketmine\network\Network->processInterfaces() phar://C:/Users/Ryan/Desktop/Pocket mine/PocketMine-MP Tester/PocketMine-MP.phar/src/pocketmine/Server__32bit.php:2491
    64.4574 51652928 8. pocketmine\network\RakLibInterface->process() phar://C:/Users/Ryan/Desktop/Pocket mine/PocketMine-MP Tester/PocketMine-MP.phar/src/pocketmine/network/Network.php:146
    64.4574 51652960 9. raklib\server\ServerHandler->handlePacket() phar://C:/Users/Ryan/Desktop/Pocket mine/PocketMine-MP Tester/PocketMine-MP.phar/src/pocketmine/network/RakLibInterface.php:79
    64.4574 51653888 10. pocketmine\network\RakLibInterface->handleEncapsulated() phar://C:/Users/Ryan/Desktop/Pocket mine/PocketMine-MP Tester/PocketMine-MP.phar/src/raklib/server/ServerHandler__32bit.php:90
    64.4574 51654160 11. pocketmine\Player->handleDataPacket() phar://C:/Users/Ryan/Desktop/Pocket mine/PocketMine-MP Tester/PocketMine-MP.phar/src/pocketmine/network/RakLibInterface.php:140
    64.4574 51654600 12. pocketmine\Server->dispatchCommand() phar://C:/Users/Ryan/Desktop/Pocket mine/PocketMine-MP Tester/PocketMine-MP.phar/src/pocketmine/Player__32bit.php:2507
    64.4574 51654600 13. pocketmine\command\SimpleCommandMap->dispatch() phar://C:/Users/Ryan/Desktop/Pocket mine/PocketMine-MP Tester/PocketMine-MP.phar/src/pocketmine/Server__32bit.php:2021
    64.4574 51655056 14. pocketmine\command\PluginCommand->execute() phar://C:/Users/Ryan/Desktop/Pocket mine/PocketMine-MP Tester/PocketMine-MP.phar/src/pocketmine/command/SimpleCommandMap.php:193
    64.4574 51655056 15. AreaEffects\Main->onCommand() phar://C:/Users/Ryan/Desktop/Pocket mine/PocketMine-MP Tester/PocketMine-MP.phar/src/pocketmine/command/PluginCommand.php:57

    And here is my new code
    PHP:
    <?php

    namespace AreaEffects;

    use 
    pocketmine\Player;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\Server;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\event\entity;
    use 
    pocketmine\entity\Effect;
    use 
    pocketmine\event\player\PlayerMoveEvent;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\level\Location;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\math\Vector3;

    class 
    Main extends PluginBase implements Listener{

        public 
    $areas;
        private 
    $pos1$pos2;

        public function 
    onEnable() {
        
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        
    $this->getLogger()->info(TextFormat::GREEN."AreaEffects has been loaded!");
        
    $this->saveResource("areas.yml");
        
    $this->areas = (new Config($this->getDataFolder() . "areas.yml"Config::YAML))->getAll();
        }

        public function 
    onDisable(){
            
    $this->areas = new Config($this->getDataFolder()."areas.yml"$this->areas);
        }

        public function 
    onCommand(CommandSender $senderCommand $command$label, array $args) {$this->areas = new Config($this->getDataFolder()."area.yml"Config::YAML, array());
            if (
    $command == "ae") {
                switch (
    $args[0]){
                    case 
    "pos1":
                        if(
    $sender instanceof Player){
                            
    $this->pos[$sender->getName()]['pos1'] = new Position($sender->getFloorX(), $sender->getFloorY(), $sender->getFloorZ(), $sender->getLevel());
                            
    $sender->sendMessage('First position set');
                            return 
    true;
                            break;
                            }
           
                    case 
    "pos2":
                        if(
    $sender instanceof Player){
                            
    $this->pos[$sender->getName()]['pos2'] = new Position($sender->getFloorX(), $sender->getFloorY(), $sender->getFloorZ(), $sender->getLevel());
                            
    $sender->sendMessage('Second position set');
                            return 
    true;
                            break;
                            }
                   
                    case 
    "create":
                        if(
    $sender instanceof Player){
                            if(isset(
    $args[1], $args[2])){
                                if(isset(
    $this->pos[$sender->getName()]['pos1'] , $this->pos[$sender->getName()]['pos2'])){
                                if(
    $this->pos[$sender->getName()]['pos1']->getLevel() === $this->pos[$sender->getName()]['pos2']->getLevel()){
                                    
    $this->areas[$args[1]] = array('pos1' => array('x' => $this->pos[$sender->getName()]['pos1']->getX(),'y' => $this->pos[$sender->getName()]['pos1']->getY(),'z' => $this->pos[$sender->getName()]['pos1']->getZ()),'pos2' => array('x' => $this->pos[$sender->getName()]['pos2']->getX(),'y' => $this->pos[$sender->getName()]['pos2']->getY(),'z' => $this->pos[$sender->getName()]['pos1']->getZ()), 'effect'=> array('duration'=>10'amplifier'=>0'show'=> true));// line 61
                                    
    $sender->sendMessage('[AreasEffect] '.$args[1].' created');
                                    unset (
    $this->pos[$sender->getName()]);
                                    return 
    true;
                                    }
                                }
                            }
                                    }else{
    $sender->sendMessage('Use this command in-game');
                                    return 
    true;
                                    }
                }
            }
        }
                               



    public function 
    onMove(PlayerMoveEvent $event){
    if (isset(
    $this->areas)){ $player $event->getPlayer();
    if(empty(
    $this->areas)) {return;}
    foreach(
    $this->areas as $area){
    if(
    $this->isInArea($player$area)){$this->giveEffect($player ,$area);
    }
    }
    }
    }

    public function 
    isInArea(Player $player$area){

    if(
    $player->getFloorX() >= $area['pos1'['x']] && $player->getFloorX() <= $area['pos2'['x']] && $player->getFloorY() >=$area['pos1'['y']] && $player->getFloorY() <= $area['pos2'['y']] && $player->getFloorZ() >= $area['pos1'['z']] && $player->getFloorZ() <= $area['pos2'['y']] && $area['level'] == $player->getLevel()) {return true;}
    else {return 
    false;
    }
    }
     

        public function 
    onMove(PlayerMoveEvent $event){
    if (isset(
    $this->areas)){ $player $event->getPlayer();
    if(empty(
    $this->areas)) {return;}
    foreach(
    $this->areas as $area){
    if(
    $this->isInArea($player$area)){$this->giveEffect($player ,$area);
            }
        }

        public function 
    giveEffect($player$area){
            if(
    $player instanceof Player){
                
    $id $area['id'];
                
    $effect Effect::getEffect($id);
                
    $effect->setDuration($area['effect']['duration']);
                
    $effect->setAmplifier($area['effect']['amplifier']);
                
    $effect->setVisable($area['effect']['show']);
                
    $player->addEffect($effect);
            }
        }
    }
    here's line 61 so you don't have to count them all. It's in the create command part of the code
    PHP:
    $this->areas[$args[1]] = array('pos1' => array('x' => $this->pos[$sender->getName()]['pos1']->getX(),'y' => $this->pos[$sender->getName()]['pos1']->getY(),'z' => $this->pos[$sender->getName()]['pos1']->getZ()),'pos2' => array('x' => $this->pos[$sender->getName()]['pos2']->getX(),'y' => $this->pos[$sender->getName()]['pos2']->getY(),'z' => $this->pos[$sender->getName()]['pos1']->getZ()), 'effect'=> array('duration'=>10'amplifier'=>0'show'=> true));// line 61
    Last edited: Aug 21, 2015
  19. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Try $this->areas["areas"][$args[1]]
  20. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    And isn't it $args[0]?
    Legoboy0215 likes this.

Share This Page

Advertisement