Search in sources :

Example 1 with Spawned

use of net.citizensnpcs.api.trait.trait.Spawned in project Citizens2 by CitizensDev.

the class CitizensNPC method spawn.

@Override
public boolean spawn(Location at) {
    Preconditions.checkNotNull(at, "location cannot be null");
    if (isSpawned()) {
        Messaging.debug("Tried to spawn", getId(), "while already spawned.");
        return false;
    }
    data().get(NPC.DEFAULT_PROTECTED_METADATA, true);
    at = at.clone();
    getTrait(CurrentLocation.class).setLocation(at);
    entityController.spawn(at, this);
    getEntity().setMetadata(NPC_METADATA_MARKER, new FixedMetadataValue(CitizensAPI.getPlugin(), true));
    boolean couldSpawn = !Util.isLoaded(at) ? false : NMS.addEntityToWorld(getEntity(), SpawnReason.CUSTOM);
    // send skin packets, if applicable, before other NMS packets are sent
    if (couldSpawn) {
        SkinnableEntity skinnable = getEntity() instanceof SkinnableEntity ? ((SkinnableEntity) getEntity()) : null;
        if (skinnable != null) {
            skinnable.getSkinTracker().onSpawnNPC();
        }
    }
    getEntity().teleport(at);
    if (!couldSpawn) {
        Messaging.debug("Retrying spawn of", getId(), "later due to chunk being unloaded.", Util.isLoaded(at) ? "Util.isLoaded true" : "Util.isLoaded false");
        // we need to wait for a chunk load before trying to spawn
        entityController.remove();
        Bukkit.getPluginManager().callEvent(new NPCNeedsRespawnEvent(this, at));
        return false;
    }
    NMS.setHeadYaw(getEntity(), at.getYaw());
    // Set the spawned state
    getTrait(CurrentLocation.class).setLocation(at);
    getTrait(Spawned.class).setSpawned(true);
    NPCSpawnEvent spawnEvent = new NPCSpawnEvent(this, at);
    Bukkit.getPluginManager().callEvent(spawnEvent);
    if (spawnEvent.isCancelled()) {
        entityController.remove();
        Messaging.debug("Couldn't spawn", getId(), "due to event cancellation.");
        return false;
    }
    navigator.onSpawn();
    // Modify NPC using traits after the entity has been created
    Collection<Trait> onSpawn = traits.values();
    // work around traits modifying the map during this iteration.
    for (Trait trait : onSpawn.toArray(new Trait[onSpawn.size()])) {
        try {
            trait.onSpawn();
        } catch (Throwable ex) {
            Messaging.severeTr(Messages.TRAIT_ONSPAWN_FAILED, trait.getName(), getId());
            ex.printStackTrace();
        }
    }
    if (getEntity() instanceof LivingEntity) {
        LivingEntity entity = (LivingEntity) getEntity();
        entity.setRemoveWhenFarAway(false);
        if (NMS.getStepHeight(entity) < 1) {
            NMS.setStepHeight(entity, 1);
        }
        if (getEntity() instanceof Player) {
            NMS.replaceTrackerEntry((Player) getEntity());
        }
    }
    return true;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) SkinnableEntity(net.citizensnpcs.npc.skin.SkinnableEntity) NPCNeedsRespawnEvent(net.citizensnpcs.NPCNeedsRespawnEvent) Spawned(net.citizensnpcs.api.trait.trait.Spawned) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) NPCSpawnEvent(net.citizensnpcs.api.event.NPCSpawnEvent) Trait(net.citizensnpcs.api.trait.Trait) CurrentLocation(net.citizensnpcs.trait.CurrentLocation)

Aggregations

NPCNeedsRespawnEvent (net.citizensnpcs.NPCNeedsRespawnEvent)1 NPCSpawnEvent (net.citizensnpcs.api.event.NPCSpawnEvent)1 Trait (net.citizensnpcs.api.trait.Trait)1 Spawned (net.citizensnpcs.api.trait.trait.Spawned)1 SkinnableEntity (net.citizensnpcs.npc.skin.SkinnableEntity)1 CurrentLocation (net.citizensnpcs.trait.CurrentLocation)1 LivingEntity (org.bukkit.entity.LivingEntity)1 Player (org.bukkit.entity.Player)1 FixedMetadataValue (org.bukkit.metadata.FixedMetadataValue)1