Spawn custom entities from mobspawners

Discussion in 'Plugin Development' started by sistem21, Jul 1, 2015.

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

    sistem21

    Hi guys,i'm not experencied with NMS,so i wanted to know..anyone knows how to spawn custom entities from mobspawners?
    I specially wanted to know how to spawn an adult and domestic horse! I'd like also know how to equipe it of an armor!Pls anyone can help me with coding it?
     
  2. Do you currently have your custom entity classes?

    If so, serialize all the locations to Strings, of the mob spawners that you want to spawn custom entities in a Map along with the mob type. Then save it to a config. In CreatureSpawnEvent, check if the cause is a spawner, if so, get the spawner (you'd have to loop through nearby blocks and find the spawner - though if you're using 1.8 (don't admit it - they'll move your thread to the Alternative Section where no one looks and receives little help) I'm going to file a request for getting the block) location and serialize it to a String, then get the mob type from the Map (if it contains the string). Cancel the event and spawn your new entity using:
    Code:
    CustomEntity customEntity = new CustomEntity(((CraftWorld) event.getEntity().getWorld()).getHandle()); // Create a new instance of the custom entity.
    customEntity.setLocation(event.getEntity().getLocation().getX(), event.getEntity().getLocation().getY(), event.getEntity().getLocation().getZ(), event.getEntity().getLocation().getYaw(), event.getEntity().getLocation().getPitch()); // Set the location of the entity.
    customEntity.world.addEntity(customEntity); // Something along the lines of this, maybe it's called worldServer, IDK. The method also may be add, not addEntity.
    
    And in onEnable(), load all the spawner locations from the configuration.

    Setting armour doesn't require any NMS, nor does adult and domestic.

    Domestication: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/Horse.html#setDomestication(int)
    Inventory: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/Horse.html#getInventory()
    Adult: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/Ageable.html#setAdult()

    How would you access these methods? You could use NMS code to set it (names are probably different), but I'd just use customEntity.getBukkitEntity() instead.

    Example of a very basic/simple spawner manager (using the serialize method (link) given at the top):
    Code:
    private Map<String, String> spawnerLocations = new HashMap<>(); // The key (String) is the location of the spawner and the value (String) is the mob type/name.
    
    public void addSpawnerLocation(Entity nmsEntity, Location spawnerLocation) {
        this.spawnerLocations.put(fromLocation(spawnerLocation), nmsEntity.getName());
    }
    
    public String getEntityType(Location spawnerLocation) {
        return this.spawnerLocations.get(fromLocation(spawnerLocation));
    }
    
    public void removeSpawnerLocation(Location spawnerLocation) {
        this.spawnerLocations.remove(fromLocation(spawnerLocation));
    }
    
    Edit: WesJD's method is preferred (easier unless you don't know how to handle reflection etc.)
     
    Last edited: Jul 1, 2015
  3. Offline

    WesJD

    @sistem21 If you would really like to do it through NMS, you will need to call the a method, using reflection, in EntityTypes to register your entity. Then, also using reflection, you will need to set mobName in MobSpawnerAbstract (get the mob spawner that was placed and edit it) to the name you put in EntityTypes. That's it.
     
    KingFaris11 likes this.
  4. Just realised how much more simple that is. Always knew there was something like this, I just thought that it wouldn't work to be honest.
     
Thread Status:
Not open for further replies.

Share This Page