Search in sources :

Example 6 with NPCRegistry

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);
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) MemoryNPCDataStore(net.citizensnpcs.api.npc.MemoryNPCDataStore) NPCTag(com.denizenscript.denizen.objects.NPCTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) Trait(net.citizensnpcs.api.trait.Trait) ListTag(com.denizenscript.denizencore.objects.core.ListTag) NPCRegistry(net.citizensnpcs.api.npc.NPCRegistry)

Aggregations

NPCRegistry (net.citizensnpcs.api.npc.NPCRegistry)6 NPC (net.citizensnpcs.api.npc.NPC)4 LocationTag (com.denizenscript.denizen.objects.LocationTag)2 EntityType (org.bukkit.entity.EntityType)2 Player (org.bukkit.entity.Player)2 Denizen (com.denizenscript.denizen.Denizen)1 BukkitScriptEvent (com.denizenscript.denizen.events.BukkitScriptEvent)1 NMSHandler (com.denizenscript.denizen.nms.NMSHandler)1 AssignmentTrait (com.denizenscript.denizen.npc.traits.AssignmentTrait)1 com.denizenscript.denizen.objects (com.denizenscript.denizen.objects)1 ChunkTag (com.denizenscript.denizen.objects.ChunkTag)1 EntityTag (com.denizenscript.denizen.objects.EntityTag)1 NPCTag (com.denizenscript.denizen.objects.NPCTag)1 BossBarCommand (com.denizenscript.denizen.scripts.commands.server.BossBarCommand)1 AssignmentScriptContainer (com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer)1 CommandScriptHelper (com.denizenscript.denizen.scripts.containers.core.CommandScriptHelper)1 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)1 ScoreboardHelper (com.denizenscript.denizen.utilities.ScoreboardHelper)1 Settings (com.denizenscript.denizen.utilities.Settings)1 Utilities (com.denizenscript.denizen.utilities.Utilities)1