Solved Create Mob Class by String

Discussion in 'Plugin Development' started by padili, Jun 10, 2014.

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

    padili

    Hey Guys,
    I'm currently trying to spawn a custom mob from a yml file.
    I want to dynamically create a mob class from a string, but I can't assign a mob class by it's class name.
    Here's my (not working) code:
    Code:java
    1. String mobType = "Skeleton";
    2. Class mobTypeClass = Class.forName(mobType);
    3. Object mob = Class.forName(mobType).newInstance();
    4. mobTypeClass mob = (mobTypeClass) p.getLocation().getWorld().spawn(p.getLocation(),mobTypeClass.class);

    I hope you can help me!
     
  2. Offline

    Garris0n

    Store the entity type as a string?
     
  3. Offline

    padili

    yes, the class name of the entity, should be the same as:
    Skeleton mob = (Skeleton) p.getLocation().getWorld().spawn(p.getLocation(),Skeleton.class);
     
  4. Offline

    Smerfa

    Skeleton mob = (Skeleton) p.getLocation().getWorld().spawnEntity(p.getLocation(),EntityType.fromName("Skeleton".toUpperCase()));


    EDIT:
    or without "toUpperCase" because I just look in to bukkit code, and bukkit is doing .toLowerCase so just
    p.getLocation().getWorld().spawnEntity(p.getLocation(),EntityType.fromName("Skeleton"));
     
    Garris0n likes this.
  5. Offline

    padili

    Right, but I want to make it work dynamically, so if the type would be Zombie it wouldn't work this way.
     
  6. Offline

    Smerfa

    Entity entity = p.getLocation().getWorld().spawnEntity(p.getLocation(),EntityType.fromName("Use entity name type here"));
    You can use string from config/command etc...
     
  7. Offline

    padili

    Doesn't work, error from eclipse:
    The method spawn(Location, Class<T>) in the type World is not applicable for the arguments (Location, EntityType) CustomMobs.java
     
  8. Offline

    Smerfa

    use spawnEntity method
    I will edit my posts :p
     
  9. Offline

    padili

    Thank you, but I'm still unable to access functions like setCustomName, because the class isn't the class of the mobType string
     
  10. Offline

    Garris0n

    Not all entities have that method. Only living entities.
     
  11. Offline

    padili

    Sure, but the mobs I want to load from my yaml file are all LivingEntities.
     
  12. Offline

    Garris0n

    Well, if you know it's a living entity, cast it.
     
    Smerfa likes this.
  13. Offline

    padili

    Thanks. Now it works!

    But I still don't know if there is any way to cast it from my mobType String

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

    padili

    No answer after half a day! Is it really impossible to cast a Mob from his Class saved in a string???
     
  15. Offline

    evilmidget38

    padili Your question makes no sense.
     
    Smerfa and (deleted member) like this.
  16. Offline

    padili

    Why, is my english too bad? (It's not my first language)
    I just want to create a mob class like
    Code:java
    1. Skeleton s = (Skeleton) player.getLocation().getWorld().spawn(player.getLocation(), Skeleton.class)
    , having "Skeleton" (or whatever mob) saved in a variable. If the mob would be a Creeper it should be:
    Code:java
    1. Creeper s = (Creeper) player.getLocation().getWorld().spawn(player.getLocation(), Creeper.class)
     
  17. Offline

    Smerfa

    ...
    So you want magically use Sekeleton or Creeper in code without knowing what mob that will be?

    Use something like that:
    Code:
    LivingEntity e = (LivingEntity)p.getLocation().getWorld().spawnEntity(p.getLocation(),EntityType.fromName(stringWithType));
    And then you can try cast it to other types using:
    if (e instanceof Creeper)
    or
    if (e.getType().equals(EntityType.CREEPER))
    Code:
    if (e.getType().equals(EntityType.CREEPER))
    {
        Creeper c = (Creeper) e;
    }
     
  18. Offline

    padili

    Yeah that's what I have right now, but isn't there any way to do it without checking for every mob type? Maybe using
    Class.forName(className)?
     
  19. Offline

    Smerfa

    But.... how you want to use that?
    To use methods from class you must know type, but you must write it...
    If you get class of mob, then you don't get anything...

    Write example how you want to use that? (impossible)
     
  20. Offline

    padili

    Thanks for your answer, I just wanted to write a class to create custom mobs.
    I wanted to use it to access methods like setEquipment().

    Thanks for your answers but sadly it seems to be impossible!
     
  21. Offline

    Smerfa

    most of methods you can use from class like LivingEntity, Creature, Monster so you don't must use "Creeper" class - only if you want set creeper to powered, this same with other mobs.
     
Thread Status:
Not open for further replies.

Share This Page