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. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    We are in the plugin development forum. Here, people come and search for answers. Every post you make is responsible for our common future. A simple mistake months ago, for example, is now the prevailing wrong code around. Since this is not a plugin request, we have to think about what we should do for public plugins.
  2. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    how would you separate different players positions then PEMapModder? you said something about using arrays. do you make an separate array for each players position?
  3. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Is it problem for you? If it is i can try to help xD
  4. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    Only one person need to be able to create the areas but I need to be able to detect multiple people and if they are in one of the areas. Your way of determining if people are in an area doesn't seem to use the pos1 and pos2 commands so I don't think it's a problem but I could be wrong.
  5. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    PHP:
    <?php
    namespace AreaEffects;

    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\utils\Vector3;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\event\player\PlayerMoveEvent;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\PluginBase;

    class 
    Main /*Or whatever your file is called */ extends PluginBase implements Listener{

    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;
    }
    }
    }

    public function 
    onMove(PlayerMoveEvent $event){
    $player $event->getPlayer();
    if(empty(
    $this->areas)) return;
    foreach(
    $this->areas as $area){
    if(
    $this->isInArea($player$area)){
    $this->giveEffect($player);
    # 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;
    }

    public function 
    giveEffect($player){
    # I suggest to manage effects here.
    # It will be easier to manage
    }
    }
    I know that there is many bugs here due to i dont have phone to check it on so have it a try and give a response :)
    No, it's not problem if the "main-goal" of this plugin is to give players a effect when entering certain areas
    Rspuds likes this.
  6. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    OK thanks for the help I'll write up the rest of the code later today.
  7. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    But this is not mistake! Just easier way to do it with some negative features. Ok i will make proper code.
  8. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Fixed bug, noticed by @PEMapModder
    PHP:
    <?php
    namespace AreaEffects;

    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\utils\Vector3;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\event\player\PlayerMoveEvent;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\PluginBase;

    class 
    Main /*Or whatever your file is called */ extends PluginBase implements Listener{

    public 
    $areas;
    private 
    $pos;

    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){
    $this->pos[$sender->getName()]['pos1'] = new Position($sender->getFloorX(), $sender->getFloorY(), $sender->getFloorZ(), $sender->getLevel());
    $sender->sendMessage('First position set '); // Change this message looks crappy.
    return true // Remember to return true/false, false = will show usage, true = nothing.
    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 '); // Change this message looks crappy.
    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->pos[$sender->getName()]['pos1'] && $this->pos[$sender->getName()]['pos2'])){
    if(
    $this->pos[$sender->getName()]['pos1']->getLevel() === $this->pos[$sender->getName()]['pos2']->getLevel()){ // Check if both positions were made in same level (btw almost useless)
    $this->areas[$args[1]] = array('pos1' => array('x' => $this->pos[$sender->getName()]['pos1']->getX(),'y' => $this->pos[$sender->getName()]['pos1']->getY(),'z' => $this->[$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){$player $event->getPlayer();
    if(empty(
    $this->areas)) return;
    foreach(
    $this->areas as $area){
    if(
    $this->isInArea($player$area)){$this->giveEffect($player);# 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;
    }

    public function 
    giveEffect($player){# I suggest to manage effects here.
    # It will be easier to manage}
    }
    Last edited: Aug 4, 2015
  9. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    ok i think I have it almost complete but i'm coming across another error (as usual). Here's what I have

    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);
            @
    mkdir($this->getDataFolder());
            
    $this->areas = (new \pocketmine\utils\Config($this->getDataFolder()."areas.yml"Config::YAML))->getAll();
            
    $this->getLogger()->info(TextFormat::GREEN."AreaEffects has been loaded!");
        }
       
        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->areas = (new \pocketmine\utils\Config($this->getDataFolder()."areas.yml"Config::YAML));
            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;
                            break;
                            }
                           
                    case 
    "create":
                        if(
    $sender instanceof Player){
                            if(isset(
    $args[1], $args[2])){
                                if(isset(
    $this->pos1 $this->pos2)){
                                    
    $this->areas->$args[1] = array(//error is coming from this line
                            
    '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
                                
    ),
                            
    'effect' => array(
                                
    'id' => $args[2],
                                
    'duration' => 10,//only editable in configs
                                
    'amplifier' => 0,//TODO editable by command
                                
    'show' => true,//   ^
                                
    )//TODO multipul effects per area
                                
    );
                            
    $sender->sendMessage(TextFormat::GREEN."[AreaEffects]Area ".$args[1]."created");
                            return 
    true;
                            break;
                            }

                    }
                }  else {
                    
    $sender->sendMessage(TextFormat::RED."this command must be used in-game");
               
                    }
                }
            }
        }
       
        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);
            }
        }
    }
    The error i'm getting is this:

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

    the error is coming from

    $this->areas->$args[1] = array(

    I know what the error means but don't know the proper way to set it up.
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Syntax error.
    isset($a && $b)
    That's wrong.
    Use isset($a) && isset($b) instead.
    You can also use isset($a, $b), which is a shorthand method for that.
    Rspuds likes this.
  11. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Can you please open repi on github? It's hard to edit code without any syntax highlights and etc.
  12. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53
    Ya ill do that as soon as I can get on my computer again
  13. Rspuds
    Offline

    Rspuds New Member

    Joined:
    Aug 4, 2015
    Posts:
    53

Share This Page

Advertisement