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(), ""));
}
}
}
}
}
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;
}
}
}
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, "");
}
Aggregations