Search in sources :

Example 56 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class AnchorCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    Action action = (Action) scriptEntry.getObject("action");
    LocationTag location = scriptEntry.getObjectTag("location");
    ElementTag range = scriptEntry.getElement("range");
    ElementTag id = scriptEntry.getElement("id");
    NPCTag npc = Utilities.getEntryNPC(scriptEntry);
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), npc, db("action", action.name()), id, location, range);
    }
    Anchors anchors = npc.getCitizen().getOrAddTrait(Anchors.class);
    switch(action) {
        case ADD:
            {
                if (location == null) {
                    Debug.echoError("Must specify a location!");
                    return;
                }
                Anchor existing = anchors.getAnchor(id.asString());
                if (existing != null) {
                    anchors.removeAnchor(existing);
                }
                anchors.addAnchor(id.asString(), location);
                break;
            }
        case REMOVE:
            {
                Anchor n = anchors.getAnchor(id.asString());
                if (n == null) {
                    Debug.echoError(scriptEntry, "Invalid anchor name '" + id.asString() + "'");
                } else {
                    anchors.removeAnchor(n);
                }
                break;
            }
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) Anchors(net.citizensnpcs.trait.Anchors) Anchor(net.citizensnpcs.util.Anchor) NPCTag(com.denizenscript.denizen.objects.NPCTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 57 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class WalkCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    LocationTag loc = scriptEntry.getObjectTag("location");
    ElementTag speed = scriptEntry.getElement("speed");
    ElementTag auto_range = scriptEntry.getElement("auto_range");
    ElementTag radius = scriptEntry.getElement("radius");
    ElementTag stop = scriptEntry.getElement("stop");
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    final LocationTag lookat = scriptEntry.getObjectTag("lookat");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), loc, speed, auto_range, radius, lookat, stop, (db("entities", entities)));
    }
    boolean shouldStop = stop.asBoolean();
    List<NPCTag> npcs = new ArrayList<>();
    final List<EntityTag> waitForEntities = new ArrayList<>();
    for (final EntityTag entity : entities) {
        if (entity.isCitizensNPC()) {
            NPCTag npc = entity.getDenizenNPC();
            npcs.add(npc);
            if (!npc.isSpawned()) {
                Debug.echoError(scriptEntry, "NPC " + npc.identify() + " is not spawned!");
                continue;
            }
            if (shouldStop) {
                npc.getNavigator().cancelNavigation();
                continue;
            }
            if (auto_range != null && auto_range.asBoolean()) {
                double distance = npc.getLocation().distance(loc);
                if (npc.getNavigator().getLocalParameters().range() < distance + 10) {
                    npc.getNavigator().getLocalParameters().range((float) distance + 10);
                }
            }
            npc.getNavigator().setTarget(loc);
            if (lookat != null) {
                npc.getNavigator().getLocalParameters().lookAtFunction(nav -> lookat);
            }
            if (speed != null) {
                npc.getNavigator().getLocalParameters().speedModifier(speed.asFloat());
            }
            if (radius != null) {
                npc.getNavigator().getLocalParameters().addRunCallback(WalkCommandCitizensEvents.generateNewFlocker(npc.getCitizen(), radius.asDouble()));
            }
        } else if (shouldStop) {
            NMSHandler.getEntityHelper().stopWalking(entity.getBukkitEntity());
        } else {
            waitForEntities.add(entity);
            NMSHandler.getEntityHelper().walkTo(entity.getLivingEntity(), loc, speed != null ? speed.asDouble() : null, () -> checkHeld(entity));
        }
    }
    if (scriptEntry.shouldWaitFor()) {
        held.add(scriptEntry);
        if (!npcs.isEmpty()) {
            scriptEntry.addObject("tally", npcs);
        }
        if (!waitForEntities.isEmpty()) {
            scriptEntry.addObject("entities", waitForEntities);
        }
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) NPCTag(com.denizenscript.denizen.objects.NPCTag) ArrayList(java.util.ArrayList) EntityTag(com.denizenscript.denizen.objects.EntityTag) ArrayList(java.util.ArrayList) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 58 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class WalkCommand method checkHeld.

public void checkHeld(EntityTag entity) {
    for (int i = 0; i < held.size(); i++) {
        ScriptEntry entry = held.get(i);
        List<EntityTag> waitForEntities = (List<EntityTag>) entry.getObject("entities");
        if (waitForEntities == null) {
            continue;
        }
        waitForEntities.remove(entity);
        if (waitForEntities.isEmpty()) {
            if (!entry.hasObject("tally") || ((List<NPCTag>) entry.getObject("tally")).isEmpty()) {
                entry.setFinished(true);
                held.remove(i);
                i--;
            }
        }
    }
}
Also used : NPCTag(com.denizenscript.denizen.objects.NPCTag) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) EntityTag(com.denizenscript.denizen.objects.EntityTag) ArrayList(java.util.ArrayList) List(java.util.List)

Example 59 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class AssignmentCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ScriptTag script = scriptEntry.getObjectTag("script");
    Action action = (Action) scriptEntry.getObject("action");
    List<NPCTag> npcs = (List<NPCTag>) scriptEntry.getObject("npcs");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("action", action), script, db("npc", npcs));
    }
    PlayerTag player = Utilities.getEntryPlayer(scriptEntry);
    for (NPCTag npc : npcs) {
        switch(action) {
            case SET:
                {
                    if (script == null) {
                        throw new InvalidArgumentsRuntimeException("Missing script!");
                    }
                    AssignmentTrait assignment = npc.getCitizen().getOrAddTrait(AssignmentTrait.class);
                    assignment.clearAssignments(player);
                    assignment.addAssignmentScript((AssignmentScriptContainer) script.getContainer(), player);
                    break;
                }
            case ADD:
                if (script == null) {
                    throw new InvalidArgumentsRuntimeException("Missing script!");
                }
                npc.getCitizen().getOrAddTrait(AssignmentTrait.class).addAssignmentScript((AssignmentScriptContainer) script.getContainer(), player);
                break;
            case REMOVE:
                if (script == null) {
                    Deprecations.assignmentRemove.warn(scriptEntry);
                    if (npc.getCitizen().hasTrait(AssignmentTrait.class)) {
                        npc.getCitizen().getOrAddTrait(AssignmentTrait.class).clearAssignments(player);
                        npc.getCitizen().removeTrait(AssignmentTrait.class);
                    }
                } else {
                    if (npc.getCitizen().hasTrait(AssignmentTrait.class)) {
                        AssignmentTrait trait = npc.getCitizen().getOrAddTrait(AssignmentTrait.class);
                        trait.removeAssignmentScript(script.getName(), player);
                        trait.checkAutoRemove();
                    }
                }
                break;
            case CLEAR:
                if (npc.getCitizen().hasTrait(AssignmentTrait.class)) {
                    npc.getCitizen().getOrAddTrait(AssignmentTrait.class).clearAssignments(player);
                    npc.getCitizen().removeTrait(AssignmentTrait.class);
                }
                break;
        }
    }
}
Also used : AssignmentTrait(com.denizenscript.denizen.npc.traits.AssignmentTrait) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) NPCTag(com.denizenscript.denizen.objects.NPCTag) InvalidArgumentsRuntimeException(com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException) List(java.util.List) AssignmentScriptContainer(com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer)

Example 60 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag 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

NPCTag (com.denizenscript.denizen.objects.NPCTag)72 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)24 EventHandler (org.bukkit.event.EventHandler)22 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)21 Player (org.bukkit.entity.Player)15 LocationTag (com.denizenscript.denizen.objects.LocationTag)12 EntityTag (com.denizenscript.denizen.objects.EntityTag)10 ListTag (com.denizenscript.denizencore.objects.core.ListTag)9 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)8 HashMap (java.util.HashMap)8 NPC (net.citizensnpcs.api.npc.NPC)8 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)7 AssignmentTrait (com.denizenscript.denizen.npc.traits.AssignmentTrait)6 Location (org.bukkit.Location)6 TriggerTrait (com.denizenscript.denizen.npc.traits.TriggerTrait)5 BukkitScriptEntryData (com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData)5 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Entity (org.bukkit.entity.Entity)5