How to spawn NPC/mobs natively ?

Discussion in 'Plugin Development' started by lololmaker, Jul 8, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    lololmaker

    Hello !
    I'm not really beginner to making plugins, but I still don't know how to natively spawn NPC/mob (Is there a diffrence in code ?) without any external library or anything.

    For example I would like to spawn a pig, and prevent it's movement, define a type for Ocelot/Cat etc.
    I believe I would have to get it's id, and cancel it's movement event.

    Can you please give me some examples, readings source from other plugins is most of the time too hard, because each class is linked to another, without really written code at one place.
    Thanks in advance !
     
  2. Offline

    ZeusAllMighty11

  3. Offline

    gjossep

    Not so sure about the freezing the mob but this is how you can spawn one:
    Code:
    World world = Bukkit.getWorld("world");
           
            EntityType pig = EntityType.PIG;
            String pigname = pig.toString();
            world.spawnCreature(world.getSpawnLocation(), pig);
    With the freezing thing check out this:
    http://forums.bukkit.org/threads/tutorial-how-to-customize-the-behaviour-of-a-mob-or-entity.54547/

    If you need more help PM me or do
    gjossep
     
  4. Offline

    lololmaker

    This is what I am avoiding, don't want to rely on someone other.

    Thanks, tried spawnCreature() already, but it didn't spawn anything, I will try again though.
    Stupid me, didn't do @EventHandler because I made it onPlayetChatEvent, for testing purposes.

    @gjossep
    How can I typecast EntityType to etc Ocelot, or Cow.
    Also how can I find npc in whole world by searching something as ID ?

    Bump, anyone ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  5. Offline

    gjossep

    I think you can do

    World.getEnitiybyId

    I'm not sure thou, you can also get all entity and then check its Id!

    I'm not sure because I am not at my comp!
     
  6. Offline

    lololmaker

    Ok, but currently my main questions are:

    1. How to type cast EntityType to Cow/Ocelot/Enderman or anything similar, so I can edit the NPCs preferences.
    2. How can I get ID when spawning a NPC, for later use :)

    I can't find a way, thanks anyway ;)
     
  7. Offline

    gjossep

    Al help you when I get home, just wait a bit

    One way you could maybe get a ID is like this
    Code:
      List<Integer> entityList = new ArrayList<Integer>();
              HashMap<Integer, EntityType> List3 = new HashMap<Integer, EntityType>();
             
            EntityType pig = EntityType.PIG;
            int size = entityList.size();
            entityList.add(size, size);
            List3.put(size, pig);
    Then if you have a id, you can get the EntityType
    Not sure tho

    And not sure about the Cow thing, as idk what your Casting to, there is no class called Cow or anything!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
    lololmaker likes this.
  8. You sure about that ? http://jd.bukkit.org/apidocs/org/bukkit/entity/Cow.html

    lololmaker
    Why do you want to *cast* pig to cow/enderman/anything else ? Because you can't because it's not that animal/monster, you'll have to spawn the animal/monster you need.
     
  9. Offline

    lololmaker

    Basically, I want to spawn an Ocelot, than be able to set his age, ability to breed etc (Which is only possible when type is Ocelot).
     
  10. So why not spawn an Ocelot directly ? Just use EntityType.OCELOT.

    Also, you don't need to make a variable for that, just type it directly inside the spawnCreature() method.
     
  11. Offline

    gjossep

    But if you use EntityType.OCELOT how do you set its age, breed and stuff?
     
  12. How are you doing it now ? Because I don't see you doing it with pig either :}
    Asign spawnCreature() to an entity and cast that to Ocelot, then you can set all those stuff :)
    Code:
    Ocelot cat = (Ocelot)spawnCreature(location, EntityType.OCELOT);
    cat.setWhatever();
     
    lololmaker likes this.
  13. Offline

    gjossep

    I see! Thanks!

    You can now do Entitiy.getUniqieId()

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  14. Offline

    lololmaker

    Wow, that's amazing Digi, I will try that soon. Would never thought of such way. We learn all the time ...

    It works like a charm, thanks ;)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  15. Offline

    Zarkopafilis

    This really works , but any way to spawn more than one ?(scheduler or another trick?)
     
Thread Status:
Not open for further replies.

Share This Page