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

Solved undefined variable

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

  1. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    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;

    class 
    Main extends PluginBase implements Listener{
     
        public function 
    onLoad() {
            
    $this->getLogger()->info(TextFormat::GREEN."AreaEffects has been loaded!");
        }
     
        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            @
    mkdir($this->getDataFolder());
            
    $this->configFile = new Config($this->getDataFolder()."mines.yml"Config::YAML, array());
            
    $this->getLogger()->info(TextFormat::GREEN."AreaEffects has been loaded!");
        }
     
        public function 
    onCommand(CommandSender $senderCommand $command$label, array $args) {
            
    $this->configFile = new Config($this->getDataFolder()."mines.yml"Config::YAML, array());
            if (
    $command == "ae") { 
                switch (
    $args[0]){
                    case 
    "pos1":
                        if(
    $sender instanceof Player){
                            global 
    $pos1x$pos1y$pos1z;
                            
    $pos1x $sender->getFloorX();
                            
    $pos1y $sender->getFloorY();
                            
    $pos1z $sender->getFloorZ();
                            
    $sender->sendMessage(TextFormat::GREEN."[AreaEffects]Possition 1 set as x:".$pos1x." y:".$pos1y." z:".$pos1z);
                            break;
                            }
                 
                    case 
    "pos2":
                        if(
    $sender instanceof Player){
                            global 
    $pos2x$pos2y$pos2z;
                            
    $pos2x $sender->getFloorX();
                            
    $pos2y $sender->getFloorY();
                            
    $pos2z $sender->getFloorZ();
                            
    $sender->sendMessage(TextFormat::GREEN."[AreaEffects]Possition 2 set as x:".$pos2x." y:".$pos2y." z:".$pos2z);
                            break;
                            }
                         
                    case 
    "create":
                        if(
    $sender instanceof Player){
                            
    $this->configFile->set($args[1],$pos1x ,$pos1y ,$pos1z ,$pos2x ,$pos2y ,$pos2z );
                            
    $sender->sendMessage(TextFormat::GREEN."[AreaEffects]Area ".$args[2]." has been created");
                            
    $this->saveConfig();
                            break;
                            }

                
                }
            }
        }
    }
    I'm working on a plugin to apply effects by area. I have com across a problem, I define variables in using one command in another case .know that they are defined properly because I send a message with there values later in that case. The problem comes when I try to save them to configs in a different case. When I try to do so it gives me this:

    [00:08:07] [Server thread/CRITICAL]: Unhandled exception executing command 'ae create test' in ae: Undefined variable: pos1x
    [00:08:07] [Server thread/NOTICE]: UndefinedVariableException: "Undefined variable: pos1x" (E_NOTICE) in "/AreaEffects0.5/src/AreaEffects/Main" at line 59

    I'm sure it something simple as I just started coding a few days ago. Would also appreciate feed back on how I'm using configs. I looked around and found some code for configs and it seems to work but not sure if its right for my situation.
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Don't use global >.< use fields for the specific player.
    EvolSoft likes this.
  3. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    Thanks for the reply. Can you explain what you mean by fields. As I said just started coding a few days ago and do know all the terms
    Last edited: Aug 4, 2015
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Take a look at WorldEditArt.
    Rspuds likes this.
  5. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    here are the changes I made

    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 
    $pos1//*new*/
        
    public $pos2//*new*/

        
    public function onLoad() {
            
    $this->getLogger()->info(TextFormat::GREEN."AreaEffects has been loaded!");
        }
       
        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            @
    mkdir($this->getDataFolder());
            
    $this->configFile = new Config($this->getDataFolder()."mines.yml"Config::YAML, array());
            
    $this->getLogger()->info(TextFormat::GREEN."AreaEffects has been loaded!");
        }
       
        public function 
    onCommand(CommandSender $senderCommand $command$label, array $args) {
            
    $this->configFile = new Config($this->getDataFolder()."mines.yml"Config::YAML, array());
            if (
    $command == "ae") {   
                switch (
    $args[0]){
                    case 
    "pos1":
                        if(
    $sender instanceof Player){
                            
    $pos1x $sender->getFloorX();
                            
    $pos1y $sender->getFloorY();
                            
    $pos1z $sender->getFloorZ();
                            
    $this->pos1 = new \pocketmine\math\Vector3($pos1x$pos1y$pos1z);
                            
    $sender->sendMessage(TextFormat::GREEN."[AreaEffects]Possition 1 set as x:".$pos1x." y:".$pos1y." z:".$pos1z);
                            break;
                            }
                   
                    case 
    "pos2":
                        if(
    $sender instanceof Player){
                            
    $pos2x $sender->getFloorX();
                            
    $pos2y $sender->getFloorY();
                            
    $pos2z $sender->getFloorZ();
                            
    $this->pos2 = new \pocketmine\math\Vector3($pos2x$pos2y$pos2z);//*new*/
                            
    $sender->sendMessage(TextFormat::GREEN."[AreaEffects]Possition 2 set as x:".$pos2x." y:".$pos2y." z:".$pos2z);
                            break;
                            }
                           
                    case 
    "create":
                        if(
    $sender instanceof Player){
                            
    $this->configFile->set($args[1],  $this->pos1$this->pos2);//*new*/
                            
    $sender->sendMessage(TextFormat::GREEN."[AreaEffects]Area ".$args[1]." has been created");
                            
    $this->saveConfig();
                            break;
                            }

                  
                }
            }
        }
    }
    I think this is what you told me to do. It seemed to work because i stopped getting the error but now i' m getting a new one

    [Server thread/CRITICAL]: Unhandled exception executing command 'ae create test' in ae: yaml_parse(): end of stream reached without finding document 0
    [11:57:46] [Server thread/WARNING]: RuntimeException: "yaml_parse(): end of stream reached without finding document 0" (E_WARNING) in "/src/pocketmine/utils/Config" at line 143

    I know it has something to do with the configs but I don't know what.
  6. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    looked round some other plugins and figured it out.

    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 
    $pos1;
        public 
    $pos2;

        public function 
    onLoad() {
            
    $this->getLogger()->info(TextFormat::GREEN."AreaEffects has been loaded!");
        }
       
        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            @
    mkdir($this->getDataFolder());
            
    $this->configFile = new Config($this->getDataFolder()."mines.yml"Config::YAML, array());
            
    $this->getLogger()->info(TextFormat::GREEN."AreaEffects has been loaded!");
        }
       
        public function 
    onCommand(CommandSender $senderCommand $command$label, array $args) {
            
    $this->configFile = new Config($this->getDataFolder()."areas.yml"Config::YAML, array());
            if (
    $command == "ae") {   
                switch (
    $args[0]){
                    case 
    "pos1":
                        if(
    $sender instanceof Player){
                            
    $pos1x $sender->getFloorX();
                            
    $pos1y $sender->getFloorY();
                            
    $pos1z $sender->getFloorZ();
                            
    $this->pos1 = new \pocketmine\math\Vector3($pos1x$pos1y$pos1z);
                            
    $sender->sendMessage(TextFormat::GREEN."[AreaEffects]Possition 1 set as x:".$pos1x." y:".$pos1y." z:".$pos1z);
                            break;
                            }
                   
                    case 
    "pos2":
                        if(
    $sender instanceof Player){
                            
    $pos2x $sender->getFloorX();
                            
    $pos2y $sender->getFloorY();
                            
    $pos2z $sender->getFloorZ();
                            
    $this->pos2 = new \pocketmine\math\Vector3($pos2x$pos2y$pos2z);//*new*/
                            
    $sender->sendMessage(TextFormat::GREEN."[AreaEffects]Possition 2 set as x:".$pos2x." y:".$pos2y." z:".$pos2z);
                            break;
                            }
                           
                    case 
    "create":
                        if(
    $sender instanceof Player){
                            
    $areas = array();
                            foreach (
    $this->configFile as $area);
                            
    $areas[] = array($args[1], $this->pos1$this->pos2);
                            
    file_put_contents($this->getDataFolder(). "areas.yml"$areas);
                            
    $sender->sendMessage(TextFormat::GREEN."[AreaEffects]Area ".$args[1]." has been created");
                            break;
                            }

                  
                }
            }
        }
    }
    Now just one minor issue. The areas I save are overwriting each other.
  7. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    Try using arrays (love arrays xD)
    PHP:
    public $areas;
    private 
    $pos1$pos2;

    public function 
    onEnable(){
    $this->areas = (new \pocketmine\utils\Config($this->getDataFolder()."areas.yml"Config::YAML))->getAll();
    }

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



    public function 
    onCommand(CommandSender $senderCommand $command$label, array $args) {$this->configFile = new Config($this->getDataFolder()."areas.yml"Config::YAML, array());
    if (
    $command == "ae") {
    switch (
    $args[0]){
    case 
    "pos1":
    if(
    $sender instanceof Player){
    $pos1x $sender->getFloorX();
    $pos1y $sender->getFloorY();
    $pos1z $sender->getFloorZ();
    $this->pos1 = new \pocketmine\math\Vector3($pos1x$pos1y$pos1z);
    $sender->sendMessage(TextFormat::GREEN."[AreaEffects]Possition 1 set as x:".$pos1x." y:".$pos1y." z:".$pos1z);
    return 
    true;
    break;
    }

    case 
    "pos2":
    if(
    $sender instanceof Player){
    pos2x $sender->getFloorX();
    $pos2y $sender->getFloorY();
    $pos2z $sender->getFloorZ();
    $this->pos2 = new \pocketmine\math\Vector3($pos2x$pos2y$pos2z);
    $sender->sendMessage(TextFormat::GREEN."[AreaEffects]Possition 2 set as x:".$pos2x." y:".$pos2y." z:".$pos2z);
    return 
    true // Remember to return true/false, false = will show usage, true = nothing.
    break;
    }

    case 
    "create":
    if(
    $sender instanceof Player){
      if(isset(
    $args[1])){
       if(isset(
    $this->pos1 && $this->pos2)){
        
    $this->areas[$args[1]] = array(
    'pos1' => array(
    'x' => $this->pos1->x,
    'y' => $this->pos1->y,
    'z' => $this->pos1->z
    ),
    'pos2' => array(
    'x' => $this->pos2->x,
    'y' => $this->pos2->y,
    'z' => $this->pos2->z
    )
    );
    $sender->sendMessage('[AreasEffect] '.$args[1].' created');
    return 
    true;
      }
    }
    }else{
    $sender->sendMessage('Use this command in-game');
    return 
    true;
    }
    }
    }
    }
    Rspuds likes this.
  8. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    ya it works :) now I can get back to the fun stuff. thanks for help especially to return true. was wondering why it kept giving me the usage every time i ran the command.
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    But you are still mixing up the data of different players. Positions of different players get mixed up.
  10. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    How if $sender is one?
  11. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    Btw if you check is player in area make sure that player is in same world as area was created
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You are using $this->pos1. And since you are in the same object ($this), you are referring to the same datum whenever you mention it.

    You have to make it an array.

    BTW you could simply have used $player->floor() or $player->getPosition() instead of all that trouble.
  13. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    these commands are only for setting up the areas though so only one player will be using them. so in this case there won't be any other players positions to get mix in right?
  14. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    PHP:
    if($sender instanceof Player){
    $this->pos1 = new \pocketmine\level\Position($sender->getFloorX(), $sender->getFloorY(), $sender->getFloorZ(), $sender->getLevel());
    $sender->sendMessage(TextFormat::GREEN."[AreaEffects]Possition 1 set as x:".$this->pos1->x" y:".$this->pos1->y." z:".$this->pos1->z." level:".$this->pos1->level);
    return 
    true;
    break;
    }
    ?
  15. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    although I will need to do as your saying for finding players positions to determine if they are in the area so if you could give me and example or tell me where to find an example of how to keep them seperate
    Last edited: Aug 4, 2015
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    And what if another player used the position 1 command before the old player used the position 2 command?
  17. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    On PlayerMoveEvent:
    PHP:
    public function onMove(){
    if(empty(
    $this->areas)) return;
    foreach(
    $this->areas as $area){
    if(
    $this->isInArea($sender$area)){
    # Execute if player is in area.
    # break; If you dont want to check for other areas (Not recommended)
    }
    }
    }

    public function 
    isInArea(Player $sender$area){ // Idk why i used $sender, it was too late when i saw it :D
    $pos1 = new Vector3($area['pos1']['x'], $area['pos1']['y'], $area['pos1']['z']);
    $pos2 = new Vector3($area['pos2']['x'], $area['pos2']['y'], $area['pos2']['z']);



    if(
    $sender->getFloorX() >= $pos1->getX(), && $sender->getFloorX() <= $pos2->getX() && $sender->getFloorY() >=$pos1->getY() && $sender->getFloorY() <= $pos2->getY() && $sender->getFloorZ() >= $pos1->getZ() && $sender->getFloorZ() <= $pos2->getZ() && $area['level'] == $sender->getLevel()) return true;
    return 
    false;
    }
    on Timer:
    PHP:
    public function onRun(){
    $players $this->plugin->getServer()->getOnlinePlayers();
    if(
    $players == null) return;
    if(empty(
    $this->areas)) return;
    foreach(
    $players as $player){
    foreach(
    $this->areas as $area){
    if(
    $this->plugin->isInArea($player$area)){
    #Execute your code
    }
    }
    }
    }
    }
  18. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
  19. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    If he is making this plugin for private use it does'nt matter.
  20. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    my intent is to use it for a prison server so you can get effects as you rank up. so in other words not for private use.

Share This Page

Advertisement