use of net.citizensnpcs.api.npc.NPCRegistry in project Denizen-For-Bukkit by DenizenScript.
the class CreateCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) {
ElementTag name = scriptEntry.getElement("name");
EntityTag type = scriptEntry.getObjectTag("entity_type");
LocationTag loc = scriptEntry.getObjectTag("spawn_location");
ListTag traits = scriptEntry.getObjectTag("traits");
ElementTag registry = scriptEntry.getElement("registry");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), name, type, loc, traits, registry);
}
NPCTag created;
if (!type.isGeneric() && type.isCitizensNPC()) {
created = new NPCTag(type.getDenizenNPC().getCitizen().clone());
created.getCitizen().setName(name.asString());
} else {
NPCRegistry actualRegistry = CitizensAPI.getNPCRegistry();
if (registry != null) {
actualRegistry = NPCTag.getRegistryByName(registry.asString());
if (actualRegistry == null) {
actualRegistry = CitizensAPI.createNamedNPCRegistry(registry.asString(), new MemoryNPCDataStore());
}
}
created = new NPCTag(actualRegistry.createNPC(type.getBukkitEntityType(), name.asString()));
}
// Add the created NPC into the script entry so it can be utilized if need be.
scriptEntry.addObject("created_npc", created);
if (created.isSpawned()) {
if (loc != null) {
created.getCitizen().teleport(loc, PlayerTeleportEvent.TeleportCause.PLUGIN);
} else {
created.getCitizen().despawn();
}
} else {
if (loc != null) {
created.getCitizen().spawn(loc);
}
}
if (traits != null) {
for (String trait_name : traits) {
Trait trait = CitizensAPI.getTraitFactory().getTrait(trait_name);
if (trait != null) {
created.getCitizen().addTrait(trait);
} else {
Debug.echoError(scriptEntry, "Could not add trait to NPC: " + trait_name);
}
}
}
for (Mechanism mechanism : type.getWaitingMechanisms()) {
created.safeAdjust(mechanism);
}
}
Aggregations