Search in sources :

Example 1 with AssignmentTrait

use of com.denizenscript.denizen.npc.traits.AssignmentTrait in project Denizen-For-Bukkit by DenizenScript.

the class BukkitScriptEvent method runAssignedCheck.

public static boolean runAssignedCheck(ScriptPath path, NPCTag npc) {
    String matcher = path.switches.get("assigned");
    if (matcher == null) {
        return true;
    }
    if (npc == null) {
        return false;
    }
    AssignmentTrait trait = npc.getCitizen().getTraitNullable(AssignmentTrait.class);
    if (trait == null) {
        return false;
    }
    for (String script : trait.assignments) {
        if (runGenericCheck(matcher, script)) {
            return true;
        }
    }
    return false;
}
Also used : AssignmentTrait(com.denizenscript.denizen.npc.traits.AssignmentTrait)

Example 2 with AssignmentTrait

use of com.denizenscript.denizen.npc.traits.AssignmentTrait in project Denizen-For-Bukkit by DenizenScript.

the class ZapCommand method parseArgs.

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    for (Argument arg : scriptEntry) {
        if (!scriptEntry.hasObject("script") && !scriptEntry.hasObject("step") && arg.hasPrefix() && arg.getPrefix().matchesArgumentType(ScriptTag.class)) {
            Deprecations.zapPrefix.warn(scriptEntry);
            scriptEntry.addObject("script", arg.getPrefix().asType(ScriptTag.class));
            scriptEntry.addObject("step", arg.asElement());
        } else if (!scriptEntry.hasObject("script") && arg.matchesArgumentType(ScriptTag.class) && arg.asType(ScriptTag.class).getContainer() instanceof InteractScriptContainer && arg.limitToOnlyPrefix("script")) {
            scriptEntry.addObject("script", arg.asType(ScriptTag.class));
        } else if (!scriptEntry.hasObject("step") && arg.limitToOnlyPrefix("step")) {
            scriptEntry.addObject("step", arg.asElement());
        } else if (!scriptEntry.hasObject("duration") && arg.matchesArgumentType(DurationTag.class) && arg.limitToOnlyPrefix("duration")) {
            scriptEntry.addObject("duration", arg.asType(DurationTag.class));
        } else {
            arg.reportUnhandled();
        }
    }
    PlayerTag player = Utilities.getEntryPlayer(scriptEntry);
    if (player == null || !player.isValid()) {
        throw new InvalidArgumentsException("Must have player context!");
    }
    if (!scriptEntry.hasObject("script")) {
        ScriptTag script = scriptEntry.getScript();
        if (script != null) {
            if (script.getContainer() instanceof InteractScriptContainer) {
                scriptEntry.addObject("script", script);
            } else if (script.getContainer() instanceof AssignmentScriptContainer) {
                InteractScriptContainer interact = ((AssignmentScriptContainer) script.getContainer()).interact;
                if (interact != null) {
                    scriptEntry.addObject("script", new ScriptTag(interact));
                }
            }
        }
        if (!scriptEntry.hasObject("script")) {
            NPCTag npc = Utilities.getEntryNPC(scriptEntry);
            if (npc != null && npc.getCitizen().hasTrait(AssignmentTrait.class)) {
                AssignmentTrait trait = npc.getCitizen().getOrAddTrait(AssignmentTrait.class);
                for (AssignmentScriptContainer container : trait.containerCache) {
                    if (container != null && container.getInteract() != null) {
                        scriptEntry.addObject("script", new ScriptTag(container.getInteract()));
                        break;
                    }
                }
            }
        }
        if (!scriptEntry.hasObject("script")) {
            throw new InvalidArgumentsException("No script to zap! Must be in an interact script, or have a linked NPC with an associated interact script.");
        }
    }
}
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) InteractScriptContainer(com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer) AssignmentScriptContainer(com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException)

Example 3 with AssignmentTrait

use of com.denizenscript.denizen.npc.traits.AssignmentTrait in project Denizen-For-Bukkit by DenizenScript.

the class InteractScriptHelper method getInteractScripts.

public static List<InteractScriptContainer> getInteractScripts(NPCTag npc) {
    if (npc == null) {
        return null;
    }
    AssignmentTrait trait = npc.getCitizen().getTraitNullable(AssignmentTrait.class);
    if (trait == null) {
        return null;
    }
    ArrayList<InteractScriptContainer> result = new ArrayList<>();
    for (AssignmentScriptContainer container : trait.containerCache) {
        if (container != null) {
            InteractScriptContainer interact = container.getInteract();
            if (interact != null) {
                result.add(interact);
            }
        }
    }
    return result.isEmpty() ? null : result;
}
Also used : AssignmentTrait(com.denizenscript.denizen.npc.traits.AssignmentTrait) ArrayList(java.util.ArrayList)

Example 4 with AssignmentTrait

use of com.denizenscript.denizen.npc.traits.AssignmentTrait 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)

Aggregations

AssignmentTrait (com.denizenscript.denizen.npc.traits.AssignmentTrait)4 NPCTag (com.denizenscript.denizen.objects.NPCTag)2 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)2 AssignmentScriptContainer (com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer)2 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)2 InteractScriptContainer (com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer)1 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)1 InvalidArgumentsRuntimeException (com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException)1 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1