Remove HelpCommand, Paginator

This commit is contained in:
fullwall 2013-07-31 13:38:32 +08:00
parent 53c975cce8
commit aa353d44e6
6 changed files with 3 additions and 219 deletions

View File

@ -37,7 +37,6 @@ import net.citizensnpcs.api.util.Translator;
import net.citizensnpcs.api.util.YamlStorage; import net.citizensnpcs.api.util.YamlStorage;
import net.citizensnpcs.commands.AdminCommands; import net.citizensnpcs.commands.AdminCommands;
import net.citizensnpcs.commands.EditorCommands; import net.citizensnpcs.commands.EditorCommands;
import net.citizensnpcs.commands.HelpCommands;
import net.citizensnpcs.commands.NPCCommands; import net.citizensnpcs.commands.NPCCommands;
import net.citizensnpcs.commands.TemplateCommands; import net.citizensnpcs.commands.TemplateCommands;
import net.citizensnpcs.commands.TraitCommands; import net.citizensnpcs.commands.TraitCommands;
@ -302,7 +301,6 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
// Register command classes // Register command classes
commands.register(AdminCommands.class); commands.register(AdminCommands.class);
commands.register(EditorCommands.class); commands.register(EditorCommands.class);
commands.register(HelpCommands.class);
commands.register(NPCCommands.class); commands.register(NPCCommands.class);
commands.register(TemplateCommands.class); commands.register(TemplateCommands.class);
commands.register(TraitCommands.class); commands.register(TraitCommands.class);

View File

@ -1,170 +0,0 @@
package net.citizensnpcs.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import net.citizensnpcs.Citizens;
import net.citizensnpcs.api.command.Command;
import net.citizensnpcs.api.command.CommandContext;
import net.citizensnpcs.api.command.CommandManager.CommandInfo;
import net.citizensnpcs.api.command.Requirements;
import net.citizensnpcs.api.command.exception.CommandException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Paginator;
import net.citizensnpcs.util.StringHelper;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import com.google.common.collect.Sets;
@Requirements
public class HelpCommands {
private final Citizens plugin;
public HelpCommands(Citizens plugin) {
this.plugin = plugin;
}
@Command(
aliases = { "citizens" },
usage = "help (page|command)",
desc = "Citizens help menu",
modifiers = { "help" },
min = 1,
max = 2,
permission = "citizens.citizens.help")
@Requirements
public void citizensHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
int page = 1;
try {
page = args.argsLength() == 2 ? args.getInteger(1) : page;
} catch (NumberFormatException e) {
sendSpecificHelp(sender, "citizens", args.getString(1));
}
sendHelp(sender, "citizens", page);
}
private List<String> getLines(CommandSender sender, String baseCommand) {
// Ensures that commands with multiple modifiers are only added once
Set<CommandInfo> processed = Sets.newHashSet();
List<String> lines = new ArrayList<String>();
for (CommandInfo info : plugin.getCommands(baseCommand)) {
Command command = info.getCommandAnnotation();
if (processed.contains(info)
|| (!sender.hasPermission("citizens.admin") && !sender.hasPermission("citizens."
+ command.permission())))
continue;
lines.add(format(command));
if (command.modifiers().length > 1)
processed.add(info);
}
return lines;
}
@Command(
aliases = { "npc" },
usage = "help (page|command)",
desc = "NPC help menu",
modifiers = { "help" },
min = 1,
max = 2,
permission = "citizens.npc.help")
@Requirements
public void npcHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
int page = 1;
try {
page = args.argsLength() == 2 ? args.getInteger(1) : page;
} catch (NumberFormatException e) {
sendSpecificHelp(sender, "npc", args.getString(1));
}
sendHelp(sender, "NPC", page);
}
@Command(
aliases = { "script" },
usage = "help (page|command)",
desc = "Script help menu",
modifiers = { "help" },
min = 1,
max = 2,
permission = "citizens.script.help")
@Requirements
public void scriptHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
int page = 1;
try {
page = args.argsLength() == 2 ? args.getInteger(1) : page;
} catch (NumberFormatException e) {
sendSpecificHelp(sender, "script", args.getString(1));
}
sendHelp(sender, "script", page);
}
private void sendHelp(CommandSender sender, String name, int page) throws CommandException {
Paginator paginator = new Paginator().header(StringHelper.capitalize(name)
+ Messaging.tr(Messages.COMMAND_HELP_HEADER));
for (String line : getLines(sender, name.toLowerCase()))
paginator.addLine(line);
if (!paginator.sendPage(sender, page))
throw new CommandException(Messages.COMMAND_PAGE_MISSING, page);
}
private void sendSpecificHelp(CommandSender sender, String rootCommand, String modifier) throws CommandException {
CommandInfo info = plugin.getCommandInfo(rootCommand, modifier);
if (info == null)
throw new CommandException(Messages.COMMAND_MISSING, rootCommand + " " + modifier);
Messaging.send(sender, format(info.getCommandAnnotation()));
String help = Messaging.tryTranslate(info.getCommandAnnotation().help());
if (help.isEmpty())
return;
Messaging.send(sender, ChatColor.AQUA + help);
}
@Command(
aliases = { "template", "tpl" },
usage = "help (page|command)",
desc = "Template help menu",
modifiers = { "help" },
min = 1,
max = 2,
permission = "citizens.templates.help")
@Requirements
public void templatesHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
int page = 1;
try {
page = args.argsLength() == 2 ? args.getInteger(1) : page;
} catch (NumberFormatException e) {
sendSpecificHelp(sender, "templates", args.getString(1));
}
sendHelp(sender, "templates", page);
}
@Command(
aliases = { "waypoint", "waypoint", "wp" },
usage = "help (page|command)",
desc = "Waypoints help menu",
modifiers = { "help" },
min = 1,
max = 2,
permission = "citizens.waypoints.help")
@Requirements
public void waypointsHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
int page = 1;
try {
page = args.argsLength() == 2 ? args.getInteger(1) : page;
} catch (NumberFormatException e) {
sendSpecificHelp(sender, "waypoints", args.getString(1));
}
sendHelp(sender, "waypoints", page);
}
private static final String COMMAND_FORMAT = "<7>/<c>%s%s <7>- <e>%s";
private static final String format(Command command) {
return String.format(COMMAND_FORMAT, command.aliases()[0],
(command.usage().isEmpty() ? "" : " " + command.usage()), Messaging.tryTranslate(command.desc()));
}
}

View File

@ -28,6 +28,7 @@ import net.citizensnpcs.api.trait.trait.Spawned;
import net.citizensnpcs.api.trait.trait.Speech; import net.citizensnpcs.api.trait.trait.Speech;
import net.citizensnpcs.api.util.Colorizer; import net.citizensnpcs.api.util.Colorizer;
import net.citizensnpcs.api.util.Messaging; import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.Paginator;
import net.citizensnpcs.npc.NPCSelector; import net.citizensnpcs.npc.NPCSelector;
import net.citizensnpcs.npc.Template; import net.citizensnpcs.npc.Template;
import net.citizensnpcs.trait.Age; import net.citizensnpcs.trait.Age;
@ -48,7 +49,6 @@ import net.citizensnpcs.trait.ZombieModifier;
import net.citizensnpcs.util.Anchor; import net.citizensnpcs.util.Anchor;
import net.citizensnpcs.util.Messages; import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.NMS; import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Paginator;
import net.citizensnpcs.util.StringHelper; import net.citizensnpcs.util.StringHelper;
import net.citizensnpcs.util.Util; import net.citizensnpcs.util.Util;

View File

@ -7,8 +7,8 @@ import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.Messaging; import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.Paginator;
import net.citizensnpcs.util.Messages; import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Paginator;
import net.citizensnpcs.util.Pose; import net.citizensnpcs.util.Pose;
import net.citizensnpcs.util.Util; import net.citizensnpcs.util.Util;
@ -63,7 +63,6 @@ public class Poses extends Trait {
throw new CommandException(Messages.COMMAND_PAGE_MISSING); throw new CommandException(Messages.COMMAND_PAGE_MISSING);
} }
public Pose getPose(String name) { public Pose getPose(String name) {
for (Pose pose : poses.values()) for (Pose pose : poses.values())
if (pose.getName().equalsIgnoreCase(name)) if (pose.getName().equalsIgnoreCase(name))
@ -71,7 +70,6 @@ public class Poses extends Trait {
return null; return null;
} }
public boolean hasPose(String pose) { public boolean hasPose(String pose) {
return poses.containsKey(pose.toLowerCase()); return poses.containsKey(pose.toLowerCase());
} }

View File

@ -16,10 +16,10 @@ import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.Messaging; import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.Paginator;
import net.citizensnpcs.editor.Editor; import net.citizensnpcs.editor.Editor;
import net.citizensnpcs.trait.Toggleable; import net.citizensnpcs.trait.Toggleable;
import net.citizensnpcs.util.Messages; import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Paginator;
import net.citizensnpcs.util.Util; import net.citizensnpcs.util.Util;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;

View File

@ -1,42 +0,0 @@
package net.citizensnpcs.util;
import java.util.ArrayList;
import java.util.List;
import net.citizensnpcs.api.util.Messaging;
import org.bukkit.command.CommandSender;
public class Paginator {
private String header;
private final List<String> lines = new ArrayList<String>();
public void addLine(String line) {
lines.add(line);
}
public Paginator header(String header) {
this.header = header;
return this;
}
public boolean sendPage(CommandSender sender, int page) {
int pages = (int) ((lines.size() / LINES_PER_PAGE == 0) ? 1 : Math.ceil((double) lines.size() / LINES_PER_PAGE));
if (page < 0 || page > pages)
return false;
int startIndex = LINES_PER_PAGE * page - LINES_PER_PAGE;
int endIndex = page * LINES_PER_PAGE;
Messaging.send(sender, StringHelper.wrapHeader("<e>" + header + " <f>" + page + "/" + pages));
if (lines.size() < endIndex)
endIndex = lines.size();
for (String line : lines.subList(startIndex, endIndex))
Messaging.send(sender, line);
return true;
}
private static final int LINES_PER_PAGE = 9;
}