Search in sources :

Example 6 with AssignmentScriptContainer

use of com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer in project Denizen-For-Bukkit by DenizenScript.

the class ConstantsTrait method rebuildAssignmentConstants.

public void rebuildAssignmentConstants() {
    assignmentConstants.clear();
    if (!npc.hasTrait(AssignmentTrait.class)) {
        npc.removeTrait(ConstantsTrait.class);
        return;
    }
    AssignmentTrait trait = npc.getOrAddTrait(AssignmentTrait.class);
    for (AssignmentScriptContainer container : trait.containerCache) {
        if (container != null) {
            if (container.contains("default constants", Map.class)) {
                for (StringHolder constant : container.getConfigurationSection("default constants").getKeys(false)) {
                    assignmentConstants.put(CoreUtilities.toLowerCase(constant.str), container.getString("default constants." + constant.str.toUpperCase(), ""));
                }
            }
        }
    }
}
Also used : StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) AssignmentScriptContainer(com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer)

Example 7 with AssignmentScriptContainer

use of com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer 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 8 with AssignmentScriptContainer

use of com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer in project Denizen-For-Bukkit by DenizenScript.

the class NPCCommandHandler method assignment.

@Command(aliases = { "npc" }, usage = "assignment ((--set|--remove|--add) assignment_name) (-c)", desc = "Controls the assignment for an NPC.", flags = "rc", modifiers = { "assignment", "assign" }, min = 1, max = 3, permission = "denizen.npc.assign")
@Requirements(selected = true, ownership = true)
public void assignment(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    Player player = null;
    if (sender instanceof Player) {
        player = (Player) sender;
    }
    if (args.hasFlag('r') || args.hasFlag('c')) {
        if (!npc.hasTrait(AssignmentTrait.class)) {
            Messaging.sendError(sender, "That NPC has no assignments.");
            return;
        }
        npc.getOrAddTrait(AssignmentTrait.class).clearAssignments(PlayerTag.mirrorBukkitPlayer(player));
        npc.removeTrait(AssignmentTrait.class);
        Messaging.sendInfo(sender, npc.getName() + "<f>'s assignments have been cleared.");
        return;
    }
    if (args.hasValueFlag("set")) {
        String script = args.getFlag("set").replace("\"", "");
        ScriptContainer container = ScriptRegistry.getScriptContainer(script);
        if (container == null) {
            Messaging.sendError(sender, "Invalid assignment! Has the script successfully loaded, or has it been misspelled?");
        } else if (!(container instanceof AssignmentScriptContainer)) {
            Messaging.sendError(sender, "A script with that name exists, but it is not an assignment script!");
        } else {
            AssignmentTrait trait = npc.getOrAddTrait(AssignmentTrait.class);
            trait.clearAssignments(PlayerTag.mirrorBukkitPlayer(player));
            trait.addAssignmentScript((AssignmentScriptContainer) container, PlayerTag.mirrorBukkitPlayer(player));
            Messaging.sendInfo(sender, npc.getName() + "<f>'s assignment is now just: '" + container.getName() + "'.");
        }
        return;
    } else if (args.hasValueFlag("add")) {
        String script = args.getFlag("add").replace("\"", "");
        ScriptContainer container = ScriptRegistry.getScriptContainer(script);
        AssignmentTrait trait = npc.getOrAddTrait(AssignmentTrait.class);
        if (container == null) {
            Messaging.sendError(sender, "Invalid assignment! Has the script successfully loaded, or has it been misspelled?");
        } else if (!(container instanceof AssignmentScriptContainer)) {
            Messaging.sendError(sender, "A script with that name exists, but it is not an assignment script!");
        } else if (trait.addAssignmentScript((AssignmentScriptContainer) container, PlayerTag.mirrorBukkitPlayer(player))) {
            Messaging.sendInfo(sender, npc.getName() + "<f> is now assigned to '" + container.getName() + "'.");
        } else {
            Messaging.sendError(sender, "That NPC was already assigned that script.");
        }
        return;
    } else if (args.hasValueFlag("remove")) {
        String script = args.getFlag("remove").replace("\"", "");
        AssignmentTrait trait = npc.getOrAddTrait(AssignmentTrait.class);
        if (trait.removeAssignmentScript(script, PlayerTag.mirrorBukkitPlayer(player))) {
            trait.checkAutoRemove();
            if (npc.hasTrait(AssignmentTrait.class)) {
                Messaging.sendInfo(sender, npc.getName() + "<f> is no longer assigned to '" + script + "'.");
            } else {
                Messaging.sendInfo(sender, npc.getName() + "<f> no longer has any assignment.");
            }
        } else {
            Messaging.sendError(sender, "That NPC was already not assigned that script.");
        }
        return;
    }
    Messaging.send(sender, "");
    Messaging.send(sender, "<f>Use '--set name' to set a single assignment script to this NPC.");
    Messaging.send(sender, "<b>Example: /npc assignment --set \"Magic Shop\"");
    Messaging.send(sender, "<f>Use '--add name' to add an assignment, or '--remove name' to remove one assignment.");
    Messaging.send(sender, "<f>Clear all assignments with '-c'.");
    Messaging.send(sender, "<f>Note: Assigning a script will fire an 'On Assignment:' action.");
    Messaging.send(sender, "");
}
Also used : AssignmentScriptContainer(com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer) ScriptContainer(com.denizenscript.denizencore.scripts.containers.ScriptContainer) Player(org.bukkit.entity.Player) AssignmentScriptContainer(com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Aggregations

AssignmentScriptContainer (com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer)8 AssignmentTrait (com.denizenscript.denizen.npc.traits.AssignmentTrait)3 NPCTag (com.denizenscript.denizen.objects.NPCTag)3 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)3 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)2 ScriptContainer (com.denizenscript.denizencore.scripts.containers.ScriptContainer)2 NPC (net.citizensnpcs.api.npc.NPC)2 Denizen (com.denizenscript.denizen.Denizen)1 BukkitScriptEvent (com.denizenscript.denizen.events.BukkitScriptEvent)1 NMSHandler (com.denizenscript.denizen.nms.NMSHandler)1 com.denizenscript.denizen.objects (com.denizenscript.denizen.objects)1 BossBarCommand (com.denizenscript.denizen.scripts.commands.server.BossBarCommand)1 CommandScriptHelper (com.denizenscript.denizen.scripts.containers.core.CommandScriptHelper)1 InteractScriptContainer (com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer)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 VanillaTagHelper (com.denizenscript.denizen.utilities.VanillaTagHelper)1 Depends (com.denizenscript.denizen.utilities.depends.Depends)1