use of net.citizensnpcs.api.command.exception.CommandException in project Denizen-For-Bukkit by DenizenScript.
the class TriggerTrait method describe.
public void describe(CommandSender sender, int page) throws CommandException {
Paginator paginator = new Paginator().header("Triggers");
paginator.addLine("<e>Key: <a>Name <b>Status <c>Cooldown <d>Cooldown Type <e>(Radius)");
for (Entry<String, Boolean> entry : enabled.entrySet()) {
String line = "<a> " + entry.getKey() + "<b> " + (entry.getValue() ? "Enabled" : "Disabled") + "<c> " + getCooldownDuration(entry.getKey()) + "<e> " + (getRadius(entry.getKey()) == -1 ? "" : getRadius(entry.getKey()));
paginator.addLine(line);
}
if (!paginator.sendPage(sender, page)) {
throw new CommandException(Messages.COMMAND_PAGE_MISSING, page);
}
}
use of net.citizensnpcs.api.command.exception.CommandException in project Denizen-For-Bukkit by DenizenScript.
the class AssignmentTrait method describe.
public void describe(CommandSender sender, int page) throws CommandException {
AssignmentScriptContainer assignmentScript = ScriptRegistry.getScriptContainer(assignment);
Paginator paginator = new Paginator().header("Assignment");
paginator.addLine("<e>Current assignment: " + (hasAssignment() ? this.assignment : "None.") + "");
paginator.addLine("");
if (!hasAssignment()) {
paginator.sendPage(sender, page);
return;
}
// Interact Scripts
boolean entriesPresent = false;
paginator.addLine(ChatColor.GRAY + "Interact Scripts:");
paginator.addLine("<e>Key: <a>Priority <b>Name");
if (assignmentScript.contains("INTERACT SCRIPTS")) {
entriesPresent = true;
for (String scriptEntry : assignmentScript.getStringList("INTERACT SCRIPTS")) {
String[] split = scriptEntry.split(" ", 2);
if (split.length == 2 && aH.matchesInteger(split[0])) {
paginator.addLine("<a>" + split[0] + "<b> " + split[1]);
} else {
paginator.addLine("<b>" + scriptEntry);
}
}
}
if (!entriesPresent) {
paginator.addLine("<c>No Interact Scripts assigned.");
}
paginator.addLine("");
if (!entriesPresent) {
if (!paginator.sendPage(sender, page)) {
throw new CommandException(Messages.COMMAND_PAGE_MISSING);
}
return;
}
// Scheduled Activities
entriesPresent = false;
paginator.addLine(ChatColor.GRAY + "Scheduled Scripts:");
paginator.addLine("<e>Key: <a>Time <b>Name");
if (assignmentScript.contains("SCHEDULED ACTIVITIES")) {
entriesPresent = true;
for (String scriptEntry : assignmentScript.getStringList("SCHEDULED ACTIVITIES")) {
paginator.addLine("<a>" + scriptEntry.split(" ")[0] + "<b> " + scriptEntry.split(" ", 2)[1]);
}
}
if (!entriesPresent) {
paginator.addLine("<c>No scheduled scripts activities.");
}
paginator.addLine("");
// Actions
entriesPresent = false;
paginator.addLine(ChatColor.GRAY + "Actions:");
paginator.addLine("<e>Key: <a>Action name <b>Script Size");
if (assignmentScript.contains("ACTIONS")) {
entriesPresent = true;
}
if (entriesPresent) {
for (StringHolder action : assignmentScript.getConfigurationSection("ACTIONS").getKeys(false)) {
paginator.addLine("<a>" + action.str + " <b>" + assignmentScript.getStringList("ACTIONS." + action.str).size());
}
} else {
paginator.addLine("<c>No actions defined in the assignment.");
}
paginator.addLine("");
if (!paginator.sendPage(sender, page)) {
throw new CommandException(Messages.COMMAND_PAGE_MISSING, page);
}
}
use of net.citizensnpcs.api.command.exception.CommandException in project Denizen-For-Bukkit by DenizenScript.
the class NPCCommandHandler method assignment.
/*
* ASSIGNMENT
*/
@Command(aliases = { "npc" }, usage = "assignment --set assignment_name (-r)", desc = "Controls the assignment for an NPC.", flags = "r", 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 {
if (!npc.hasTrait(AssignmentTrait.class)) {
npc.addTrait(AssignmentTrait.class);
}
Player player = null;
if (sender instanceof Player) {
player = (Player) sender;
}
AssignmentTrait trait = npc.getTrait(AssignmentTrait.class);
if (args.hasValueFlag("set")) {
String script = args.getFlag("set").replace("\"", "");
if (trait.setAssignment(script, dPlayer.mirrorBukkitPlayer(player))) {
if (trait.hasAssignment()) {
Messaging.sendInfo(sender, npc.getName() + "'s assignment is now: '" + trait.getAssignment().getName() + "'.");
} else {
Messaging.sendInfo(sender, npc.getName() + "'s assignment was not able to be set.");
}
} else if (ScriptRegistry.containsScript(script)) {
Messaging.sendError(sender, "A script with that name exists, but it is not an assignment script!");
} else {
Messaging.sendError(sender, "Invalid assignment! Has the script sucessfully loaded, or has it been mispelled?");
}
return;
} else if (args.hasFlag('r')) {
trait.removeAssignment(dPlayer.mirrorBukkitPlayer(player));
Messaging.sendInfo(sender, npc.getName() + "'s assignment has been removed.");
return;
} else if (args.length() > 2 && args.getInteger(1, 0) < 1) {
Messaging.send(sender, "");
Messaging.send(sender, "<f>Use '--set name' to set an assignment script to this NPC.");
Messaging.send(sender, "<b>Example: /npc assignment --set \"Magic Shop\"");
Messaging.send(sender, "<f>Remove an assignment with '-r'.");
Messaging.send(sender, "<f>Note: Assigning a script will fire an 'On Assignment:' action.");
Messaging.send(sender, "");
return;
}
try {
trait.describe(sender, args.getInteger(1, 1));
} catch (net.citizensnpcs.api.command.exception.CommandException e) {
throw new CommandException(e.getMessage());
}
}
use of net.citizensnpcs.api.command.exception.CommandException in project CitizensAPI by CitizensDev.
the class CommandContext method parseLocation.
public static Location parseLocation(Location currentLocation, String flag) throws CommandException {
boolean denizen = flag.startsWith("l@");
String[] parts = Iterables.toArray(LOCATION_SPLITTER.split(flag.replaceFirst("l@", "")), String.class);
if (parts.length > 0) {
String worldName = currentLocation != null ? currentLocation.getWorld().getName() : "";
double x = 0, y = 0, z = 0;
float yaw = 0F, pitch = 0F;
switch(parts.length) {
case 6:
if (denizen) {
worldName = parts[5].replaceFirst("w@", "");
} else
pitch = Float.parseFloat(parts[5]);
case 5:
if (denizen) {
pitch = Float.parseFloat(parts[4]);
} else
yaw = Float.parseFloat(parts[4]);
case 4:
if (denizen && parts.length > 4) {
yaw = Float.parseFloat(parts[3]);
} else
worldName = parts[3].replaceFirst("w@", "");
case 3:
x = Double.parseDouble(parts[0]);
y = Double.parseDouble(parts[1]);
z = Double.parseDouble(parts[2]);
break;
default:
throw new CommandException(CommandMessages.INVALID_SPAWN_LOCATION);
}
World world = Bukkit.getWorld(worldName);
if (world == null)
throw new CommandException(CommandMessages.INVALID_SPAWN_LOCATION);
return new Location(world, x, y, z, yaw, pitch);
} else {
Player search = Bukkit.getPlayerExact(flag);
if (search == null)
throw new CommandException(CommandMessages.PLAYER_NOT_FOUND_FOR_SPAWN);
return search.getLocation();
}
}
use of net.citizensnpcs.api.command.exception.CommandException in project CitizensAPI by CitizensDev.
the class CommandManager method executeMethod.
// Attempt to execute a command.
private void executeMethod(String[] args, CommandSender sender, Object[] methodArgs) throws CommandException {
String cmdName = args[0].toLowerCase();
String modifier = args.length > 1 ? args[1] : "";
boolean help = modifier.toLowerCase().equals("help");
Method method = commands.get(cmdName + " " + modifier.toLowerCase());
if (method == null && !help) {
method = commands.get(cmdName + " *");
}
if (method == null && help) {
executeHelp(args, sender);
return;
}
if (method == null)
throw new UnhandledCommandException();
if (!serverCommands.contains(method) && sender instanceof ConsoleCommandSender)
throw new ServerCommandException();
if (!hasPermission(method, sender))
throw new NoPermissionsException();
Command cmd = method.getAnnotation(Command.class);
CommandContext context = new CommandContext(sender, args);
if (cmd.requiresFlags() && !context.hasAnyFlags())
throw new CommandUsageException("", getUsage(args, cmd));
if (context.argsLength() < cmd.min())
throw new CommandUsageException(CommandMessages.TOO_FEW_ARGUMENTS, getUsage(args, cmd));
if (cmd.max() != -1 && context.argsLength() > cmd.max())
throw new CommandUsageException(CommandMessages.TOO_MANY_ARGUMENTS, getUsage(args, cmd));
if (!cmd.flags().contains("*")) {
for (char flag : context.getFlags()) if (cmd.flags().indexOf(String.valueOf(flag)) == -1)
throw new CommandUsageException("Unknown flag: " + flag, getUsage(args, cmd));
}
methodArgs[0] = context;
for (Annotation annotation : registeredAnnotations.get(method)) {
CommandAnnotationProcessor processor = annotationProcessors.get(annotation.annotationType());
processor.process(sender, context, annotation, methodArgs);
}
Object instance = instances.get(method);
try {
method.invoke(instance, methodArgs);
} catch (IllegalArgumentException e) {
logger.log(Level.SEVERE, "Failed to execute command", e);
} catch (IllegalAccessException e) {
logger.log(Level.SEVERE, "Failed to execute command", e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof CommandException) {
if (e.getCause() instanceof CommandUsageException && ((CommandUsageException) e.getCause()).getUsage() == null) {
((CommandUsageException) e.getCause()).setUsage(getUsage(args, cmd));
}
throw (CommandException) e.getCause();
}
throw new WrappedCommandException(e.getCause());
}
}
Aggregations