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

Help with $player undefined

Discussion in 'Development' started by Robertcc19, Jan 1, 2017.

  1. Robertcc19

    Robertcc19 Silverfish

    Messages:
    18
    Sorry to post so much but I have 10 family members asking for a few issues to be fixed so here goes.

    I am trying to create a plugin that spawns animals and so far here is what I have

    PHP:
    <?php

    namespace robertcc19\spawnanimals;
        use 
    pocketmine\level\format\FullChunk;
        use 
    pocketmine\plugin\PluginBase;
        use 
    pocketmine\command\Command;
        use 
    pocketmine\command\CommandSender;
        use 
    pocketmine\command\CommandExecutor;
        use 
    pocketmine\entity\Entity;
        use 
    pocketmine\level\Level;
        use 
    pocketmine\level\Position;
        use 
    pocketmine\math\Vector3;
        use 
    pocketmine\nbt\NBT;
        use 
    pocketmine\nbt\tag\ByteTag;
        use 
    pocketmine\nbt\tag\CompoundTag;
        use 
    pocketmine\nbt\tag\DoubleTag;
        use 
    pocketmine\nbt\tag\FloatTag;
        use 
    pocketmine\nbt\tag\IntTag;
        use 
    pocketmine\nbt\tag\ListTag;
        use 
    pocketmine\nbt\tag\ShortTag;
        use 
    pocketmine\nbt\tag\StringTag;
        use 
    pocketmine\entity\Villager;
        use 
    pocketmine\event\Listener;
        use 
    pocketmine\Player;
        use 
    pocketmine\Server;
        use 
    pocketmine\event\player\PlayerJoinEvent;





       class 
    AnimalSpawn extends PluginBase implements Listener
        
    {


            public function 
    onCommand(CommandSender $senderCommand $command$label, array $args)
            {


                if (
    strtolower($command->getName()) === "vc") {
                    
    $nbt = new CompoundTag ("", [
                        
    "Pos" => new ListTag"Pos", [
                            new 
    DoubleTag(""$player->x),
                            new 
    DoubleTag(""$player->),
                            new 
    DoubleTag(""$player->z)
                        ]),
                        
    "Motion" => new ListTag "Motion", [
                            new 
    DoubleTag(""0),
                            new 
    DoubleTag(""0),
                            new 
    DoubleTag(""0)
                        ]),
                        
    "Rotation" => new ListTag("Rotation", [
                            new 
    FloatTag(""90),
                            new 
    FloatTag(""90)
                        ])
                    ]);
                    
    $youranimal Entity::createEntity(Pig::NETWORK_ID$player->chunk$nbt);
                    
    $youranimal->spawnToAll();

            }
                return 
    true;


                return 
    false;
            }
        }

    ?>
    This came from a suggestion on a previous question, and the $player variable seems to be undefined even though I have included the player file from the pocketmine api.
     
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    You need to define the variable
    just because you include Foo class that dosent meant $foo will automatically be defined with class foo
    to do that, check of command sender is player then assign player as command sender, if it isnt instance of player: dont spawn(console and remote console can run commands too!!! and they dont have cords like players)
     
    Robertcc19, HimbeersaftLP and Awzaw like this.
  3. Robertcc19

    Robertcc19 Silverfish

    Messages:
    18
    Thanks for that, it worked
    I am not getting a pig to spawn after executing command, am I missing something? do I need to set a spawn command or is $youranimal->spawnToAll(); enough?
     
  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.