Solved Spawning an Entity

Discussion in 'Development' started by dbcooper, Aug 13, 2018.

  1. dbcooper

    dbcooper Spider Jockey

    Messages:
    32
    GitHub:
    dbcooper
    Hello, I am wondering how to create an entity and by that I mean how to spawn sheep, cows, pigs, and chickens at a certain coordinate.
    They do not need to move.
    As long as they can be killed and loot drops.
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit

    Have you attempted to do it yourself?
     
  3. dbcooper

    dbcooper Spider Jockey

    Messages:
    32
    GitHub:
    dbcooper
    Yes I have had no success. I have had other devs help me aswell.
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Here's an example for a Cow entity.

    You start off by creating an Animal entity class and specify it's network id. Network IDs alter how the entity looks like.
    P.S. A list of entity IDs can be found here.
    You will need to specify the entity's width and height too, you can get those values from the Minecraft wiki. For our case, it's 0.9 and 1.4 for a cow. Widths and heights alter the bounding box and the hitbox of the entity.
    It's also mandatory to specify a getName() method that returns a string.

    Also, you might want to use the Monster entity class instead of Animal class for hostile mobs like zombies. If you are unsure, use the Creature class — that's what I do, I may be doing it wrong but for now, it doesn't make much of a difference.
    PHP:
    use pocketmine\entity\Animal;
    class 
    Cow extends Animal{
        const 
    NETWORK_ID self::COW//if you change this to SHEEP, it will look like sheep

        
    public $width 0.9;
        public 
    $height 1.4;

        public function 
    getName() : string{
            return 
    "Cow";
        }
    }
    Now you will need to register the entity when you plugin gets enabled, using
    PHP:
    Entity::registerEntity(Cow::class);
    Once that has been done, you can spawn the cow using a spawn egg. If you want to spawn it through code, read the SpawnEgg source code to figure out how pocketmine does it.

    Kinda offtopic but do read the README, there are consequences for abusing font sizes AFAIK.
     
    Last edited: Aug 14, 2018
    corytortoise likes this.
  5. dbcooper

    dbcooper Spider Jockey

    Messages:
    32
    GitHub:
    dbcooper
    There's an error on class Cow extends animal{ which says some error about pocketmine/entity/Living::getName();
     
  6. Destroyer57

    Destroyer57 Zombie

    Messages:
    275
    Use PureEntitiesX since there is a bug that Mobs wont move XD.
     
    SkySeven likes this.
  7. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    I think it's the best solution :

    PHP:
    $nbt Entity::createBaseNBT($posnull$yaw$pitch);
    $entity Entity::createEntity("Cow"$level$nbt);
    $entity->spawnToAll();
     
    Last edited: Aug 13, 2018
  8. dbcooper

    dbcooper Spider Jockey

    Messages:
    32
    GitHub:
    dbcooper
    The entities do no need to move, thanks for helping.
     
  9. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    With my code entities don't move, just when you hit it :)
     
  10. dbcooper

    dbcooper Spider Jockey

    Messages:
    32
    GitHub:
    dbcooper
    Thanks, but that doesn't get rid of the crash.
     
  11. Destroyer57

    Destroyer57 Zombie

    Messages:
    275
    Use pureentities
     
  12. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    no crash with me, Do you use Vector3 for $pos ?
     
  13. dbcooper

    dbcooper Spider Jockey

    Messages:
    32
    GitHub:
    dbcooper
    SkySeven can you show me your code?
     
  14. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    I use that :

    PHP:
    $nbt Entity::createBaseNBT($posnull$yaw$pitch);
    $entity Entity::createEntity("Cow"$level$nbt);
    $entity->spawnToAll();
     
  15. dbcooper

    dbcooper Spider Jockey

    Messages:
    32
    GitHub:
    dbcooper
    you just put that on your OnEnable
     
  16. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Updated code. Thanks.
    Also DON'T use PureEntities as it's destroying half the purpose of your thread.
     
    SkySeven likes this.
  17. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    Yes or in a command for example.
     
  18. dbcooper

    dbcooper Spider Jockey

    Messages:
    32
    GitHub:
    dbcooper
    THANK YOU MUQSIT
     
    Muqsit likes this.
  19. dbcooper

    dbcooper Spider Jockey

    Messages:
    32
    GitHub:
    dbcooper
    How would I be able to dmg an entity such as cow, sheep, pig, or chicken. But only those 4. I have my server to block any entity dmg in the main world. What could I add into the function to make it allow only the 4 entities to be damaged?
     
  20. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    EntityDamageEvent instance it to EntityDamageByEntityEvent and get damager
     

Share This Page

  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.