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

It says "Call to a member function spawnTo() on null". How can I solve this problem?

Discussion in 'Help' started by Hoyee, Feb 6, 2020.

  1. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    $x = $player->x;
    $y = $player->y;
    $z = $player->z;


    $position = new Position($x, $y, $z, $player->getLevel());


    $nbtcr = Entity::createBaseNBT($position);

    $sc = Entity::createEntity(10, $level, $nbtcr, true);

    $sc->spawnTo($player);
     
  2. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    $sc is null, so probably Entity under type 10 isn't registered
     
    SkySeven and HimbeersaftLP like this.
  3. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    why is $sc null?

    $sc = Entity::createEntity(10, $level, $nbtcr, true);

    here is $sc
     
  4. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    createEntity returns null when the entity with the given network id is not registered yet.
     
  5. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    Then, How can I register id???
     
  6. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
  7. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    $x = $player->x;
    $y = $player->y;
    $z = $player->z;


    $position = new Position($x, $y, $z, $player->getLevel());


    Entity::registerEntity(Chicken::class, false, ['Chicken', 'minecraft:Chicken']);


    $nbtcr = Entity::createBaseNBT($position);
    $sc = Entity::createEntity(Chicken, $level, $nbtcr, true);

    $sc->spawnTo($player);



    I added Entity::registerEntity(Chicken::class, false, ['Chicken', 'minecraft:Chicken']); but it doesn't work and says ReflectionException: "Class ms\Chicken does not exist" (-1) in "src/pocketmine/entity/Entity" at line 391
     
  8. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Obviously, you will also have to write a chicken class (and import it with a "use" statement)
     
  9. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    I made a new folder(src->chicken->CChicken.php) and I wrote
    <?php

    declare(strict_types=1);

    namespace ms\chicken;

    class CChicken extends ms {

    const TYPE_ID = 10;
    const HEIGHT = 0.7;

    }

    and I added "use chicken\CChicken;" in MS.php(src->ms->MS.php)

    but nothing happened. no crash.
     
  10. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
  11. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    I see it, but I don't know what to do now.. I'm plugin beginner, please help me more detail. then what should I edit?
     
  12. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Your class will need to inherit "Entity" (in this case "Animal" is probably the best choice).

    You will need to have basic understanding of OOP if you want to make a complex PocketMine plugin.
     
  13. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    I edit MS.php like this(down)

    <?php

    namespace ms;

    use pocketmine\plugin\PluginBase;
    use pocketmine\event\Listener;
    use pocketmine\event\player\PlayerInteractEvent;

    use pocketmine\item\ItemBlock;

    use pocketmine\item\Item;

    use pocketmine\level\Particle;

    use pocketmine\level\Position;

    use pocketmine\level\particle\FlameParticle;

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

    use pocketmine\nbt\tag\CompoundTag;
    use pocketmine\nbt\tag\ListTag;
    use pocketmine\nbt\tag\ByteTag;
    use pocketmine\nbt\tag\DoubleTag;
    use pocketmine\nbt\tag\FloatTag;

    use pocketmine\entity\Entity;

    use pocketmine\level\Level;

    use pocketmine\Player;

    use pocketmine\math\Vector3;

    use ms\CChicken;




    class MS extends PluginBase implements Listener{

    const ENTITY_TYPES = "Chicken"

    public function onEnable(){
    foreach (Chicekn as $className){
    Entity::registerEntity($className, true);
    }
    $this->getServer()->getPluginManager()->registerEvents($this,$this);
    }

    public function Throwing(PlayerInteractEvent $event){
    $player = $event->getPlayer();
    $level = $player->getLevel();

    if($event->getItem()->getId() == 280){
    $nbt = new CompoundTag("", [
    "Pos" => new ListTag("Pos", [
    new DoubleTag("", $player->x),
    new DoubleTag("", $player->y + $player->getEyeHeight()),
    new DoubleTag("", $player->z)
    ]),
    "Motion" => new ListTag("Motion", [
    new DoubleTag("", -sin($player->yaw / 180 * M_PI) * cos($player->pitch / 180 * M_PI)),
    new DoubleTag("", -sin($player->pitch / 180 * M_PI)),
    new DoubleTag("", cos($player->yaw / 180 * M_PI) * cos($player->pitch / 180 * M_PI))
    ]),
    "Rotation" => new ListTag("Rotation", [
    new FloatTag("", $player->yaw),
    new FloatTag("", $player->pitch)
    ]),
    ]);


    $x = $player->x;
    $y = $player->y;
    $z = $player->z;


    $position = new Position($x, $y, $z, $player->getLevel());


    $nbtcr = Entity::createBaseNBT($position);
    $sc = Entity::createEntity(Chicken, $level, $nbtcr, true);

    $sc->spawnTo($player);


    and I edit CChicken.php like this(down)


    <?php

    declare(strict_types=1);

    namespace ms\chicken;

    class CChicken extends PluginBase {

    const TYPE_ID = 10;
    const HEIGHT = 0.7;

    }





    and There is no crash but still nothing happening, what's wrong??
     
  14. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Almost all of it
     
  15. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    Is there example plugin? I Iittle understanded about OOP, but still don't know how to solve this..
    I want to just make spawning one chicken to me when I touch with stick...


    I edit MS.php

    <?php

    namespace ms;

    use pocketmine\plugin\PluginBase;
    use pocketmine\event\Listener;
    use pocketmine\event\player\PlayerInteractEvent;

    use pocketmine\item\ItemBlock;

    use pocketmine\item\Item;

    use pocketmine\level\Particle;

    use pocketmine\level\Position;

    use pocketmine\level\particle\FlameParticle;

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

    use pocketmine\nbt\tag\CompoundTag;
    use pocketmine\nbt\tag\ListTag;
    use pocketmine\nbt\tag\ByteTag;
    use pocketmine\nbt\tag\DoubleTag;
    use pocketmine\nbt\tag\FloatTag;

    use pocketmine\entity\Entity;

    use pocketmine\level\Level;

    use pocketmine\Player;

    use pocketmine\math\Vector3;

    use chicken\CChicken;

    class MS extends PluginBase implements Listener{

    const ENTITY_TYPES = "Chicken";

    public function onEnable(){
    foreach (ENTITY_TYPES as $className){
    Entity::registerEntity($className, true);
    }
    $this->getServer()->getPluginManager()->registerEvents($this,$this);
    }

    public function Throwing(PlayerInteractEvent $event){
    $player = $event->getPlayer();
    $level = $player->getLevel();

    if($event->getItem()->getId() == 280){
    $nbt = new CompoundTag("", [
    "Pos" => new ListTag("Pos", [
    new DoubleTag("", $player->x),
    new DoubleTag("", $player->y + $player->getEyeHeight()),
    new DoubleTag("", $player->z)
    ]),
    "Motion" => new ListTag("Motion", [
    new DoubleTag("", -sin($player->yaw / 180 * M_PI) * cos($player->pitch / 180 * M_PI)),
    new DoubleTag("", -sin($player->pitch / 180 * M_PI)),
    new DoubleTag("", cos($player->yaw / 180 * M_PI) * cos($player->pitch / 180 * M_PI))
    ]),
    "Rotation" => new ListTag("Rotation", [
    new FloatTag("", $player->yaw),
    new FloatTag("", $player->pitch)
    ]),
    ]);


    $x = $player->x;
    $y = $player->y;
    $z = $player->z;


    $position = new Position($x, $y, $z, $player->getLevel());

    $nbtcr = Entity::createBaseNBT($position);
    $sc = Entity::createEntity(Chicken, $level, $nbtcr, true);

    $sc->spawnTo($player);


    and I edit CChicken.php

    <?php
    use pocketmine\entity\Animal;
    //declare(strict_types=1);

    namespace chicken;

    class CChicken extends Animal {

    const TYPE_ID = 10;
    const HEIGHT = 0.7;

    }


    It says
    "Use of undefined constant ENTITY_TYPES - assumed 'ENTITY_TYPES' (this will throw an Error in a future version of PHP)" (EXCEPTION) in "plugins/monster/src/ms/MS" at line 61
     
  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.