Search in sources :

Example 61 with NPCTag

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

the class PauseCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    DurationTag duration = scriptEntry.getObjectTag("duration");
    ElementTag pauseTypeElement = scriptEntry.getElement("pause_type");
    PauseType pauseType = PauseType.valueOf(pauseTypeElement.asString().toUpperCase());
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), duration, pauseTypeElement);
    }
    NPCTag npc = null;
    if (Utilities.getEntryNPC(scriptEntry) != null) {
        npc = Utilities.getEntryNPC(scriptEntry);
    }
    pause(npc, pauseType, !scriptEntry.getCommandName().equalsIgnoreCase("RESUME"));
    if (duration != null) {
        if (durations.containsKey(npc.getCitizen().getId() + pauseType.name())) {
            try {
                Denizen.getInstance().getServer().getScheduler().cancelTask(durations.get(npc.getCitizen().getId() + pauseType.name()));
            } catch (Exception e) {
                Debug.echoError(scriptEntry, "There was an error pausing that!");
                Debug.echoError(scriptEntry, e);
            }
        }
        Debug.echoDebug(scriptEntry, "Running delayed task: Unpause " + pauseType.toString());
        final NPCTag theNpc = npc;
        final ScriptEntry se = scriptEntry;
        durations.put(npc.getId() + pauseType.name(), Denizen.getInstance().getServer().getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> {
            Debug.echoDebug(se, "Running delayed task: Pausing " + pauseType.toString());
            pause(theNpc, pauseType, false);
        }, duration.getTicks()));
    }
}
Also used : NPCTag(com.denizenscript.denizen.objects.NPCTag) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException)

Example 62 with NPCTag

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

the class SleepCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    LocationTag location = scriptEntry.getObjectTag("location");
    NPCTag npc = Utilities.getEntryNPC(scriptEntry);
    if (npc.getEntityType() != EntityType.PLAYER && !(npc.getEntity() instanceof Villager)) {
        Debug.echoError("Only Player or villager type NPCs can sit!");
        return;
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), npc, location);
    }
    SleepingTrait trait = npc.getCitizen().getOrAddTrait(SleepingTrait.class);
    if (location != null) {
        trait.toSleep(location);
    } else {
        trait.toSleep();
    }
    if (!trait.isSleeping()) {
        npc.getCitizen().removeTrait(SleepingTrait.class);
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) SleepingTrait(com.denizenscript.denizen.npc.traits.SleepingTrait) NPCTag(com.denizenscript.denizen.objects.NPCTag) Villager(org.bukkit.entity.Villager)

Example 63 with NPCTag

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

the class TraitCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag toggle = scriptEntry.getElement("state");
    ElementTag traitName = scriptEntry.getElement("trait");
    List<NPCTag> npcs = (List<NPCTag>) scriptEntry.getObject("npcs");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), traitName, toggle, db("npc", npcs));
    }
    Class<? extends Trait> trait = CitizensAPI.getTraitFactory().getTraitClass(traitName.asString());
    if (trait == null) {
        Debug.echoError(scriptEntry, "Trait not found: " + traitName.asString());
        return;
    }
    for (NPCTag npcTag : npcs) {
        NPC npc = npcTag.getCitizen();
        switch(Toggle.valueOf(toggle.asString())) {
            case TRUE:
            case ON:
                if (npc.hasTrait(trait)) {
                    Debug.echoError(scriptEntry, "NPC already has trait '" + traitName.asString() + "'");
                } else {
                    npc.addTrait(trait);
                }
                break;
            case FALSE:
            case OFF:
                if (!npc.hasTrait(trait)) {
                    Debug.echoError(scriptEntry, "NPC does not have trait '" + traitName.asString() + "'");
                } else {
                    npc.removeTrait(trait);
                }
                break;
            case TOGGLE:
                if (npc.hasTrait(trait)) {
                    npc.removeTrait(trait);
                } else {
                    npc.addTrait(trait);
                }
                break;
        }
    }
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) NPCTag(com.denizenscript.denizen.objects.NPCTag) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 64 with NPCTag

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

the class WalkCommandCitizensEvents method checkHeld.

public void checkHeld(NavigationEvent e) {
    if (e.getNPC() == null) {
        return;
    }
    for (int i = 0; i < WalkCommand.held.size(); i++) {
        ScriptEntry entry = WalkCommand.held.get(i);
        List<NPCTag> tally = (List<NPCTag>) entry.getObject("tally");
        if (tally == null) {
            WalkCommand.held.remove(i--);
            continue;
        }
        for (int x = 0; x < tally.size(); x++) {
            if (!tally.get(x).isSpawned()) {
                tally.remove(x--);
            }
        }
        tally.remove(new NPCTag(e.getNPC()));
        if (tally.isEmpty()) {
            Bukkit.getScheduler().runTaskLater(Denizen.getInstance(), () -> entry.setFinished(true), 1);
            WalkCommand.held.remove(i--);
        }
    }
}
Also used : NPCTag(com.denizenscript.denizen.objects.NPCTag) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) List(java.util.List)

Example 65 with NPCTag

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

the class SitCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    LocationTag location = scriptEntry.getObjectTag("location");
    NPCTag npc = Utilities.getEntryNPC(scriptEntry);
    if (!(npc.getEntity() instanceof Player || npc.getEntity() instanceof Sittable)) {
        Debug.echoError("Entities of type " + npc.getEntityType().getName() + " cannot sit.");
        return;
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), npc, location);
    }
    Entity entity = npc.getEntity();
    if (entity instanceof Sittable) {
        ((Sittable) entity).setSitting(true);
    } else {
        SittingTrait trait = npc.getCitizen().getOrAddTrait(SittingTrait.class);
        if (location != null) {
            trait.sit(location);
        } else {
            trait.sit();
        }
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) SittingTrait(com.denizenscript.denizen.npc.traits.SittingTrait) NPCTag(com.denizenscript.denizen.objects.NPCTag)

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