separate pagination out

This commit is contained in:
aPunch 2012-02-29 05:39:08 -06:00
parent 700a7752ca
commit 5e45c748f7
4 changed files with 66 additions and 30 deletions

View File

@ -14,13 +14,10 @@ import net.citizensnpcs.command.CommandContext;
import net.citizensnpcs.command.CommandManager;
import net.citizensnpcs.command.Requirements;
import net.citizensnpcs.command.exception.CommandException;
import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.StringHelper;
import net.citizensnpcs.util.Paginator;
@Requirements
public class HelpCommands {
private static final int LINES_PER_PAGE = 9;
private final CommandManager cmdManager;
public HelpCommands(Citizens plugin) {
@ -38,7 +35,11 @@ public class HelpCommands {
@Requirements
public void citizensHelp(CommandContext args, Player player, NPC npc) throws CommandException {
int page = args.argsLength() == 2 ? args.getInteger(1) : 1;
if (!sendPage(player, args.getCommand(), page))
Paginator paginator = new Paginator();
for (String line : getLines(player, "citizens"))
paginator.addLine(line);
paginator.setHeaderText("Citizens Help");
if (!paginator.sendPage(player, page))
throw new CommandException("The page '" + page + "' does not exist.");
}
@ -53,30 +54,14 @@ public class HelpCommands {
@Requirements
public void npcHelp(CommandContext args, Player player, NPC npc) throws CommandException {
int page = args.argsLength() == 2 ? args.getInteger(1) : 1;
if (!sendPage(player, args.getCommand(), page))
Paginator paginator = new Paginator();
for (String line : getLines(player, "npc"))
paginator.addLine(line);
paginator.setHeaderText("NPC Help");
if (!paginator.sendPage(player, page))
throw new CommandException("The page '" + page + "' does not exist.");
}
private boolean sendPage(Player player, String baseCommand, int page) {
List<String> lines = getLines(player, baseCommand);
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(player, StringHelper.wrapHeader("<e>"
+ (baseCommand.equalsIgnoreCase("npc") ? "NPC" : StringHelper.capitalize(baseCommand.toLowerCase()))
+ " Help <f>" + page + "/" + pages));
if (lines.size() < endIndex)
endIndex = lines.size();
for (String line : lines.subList(startIndex, endIndex))
Messaging.send(player, line);
return true;
}
private List<String> getLines(Player player, String baseCommand) {
// Ensures that commands with multiple modifiers are only added once
Set<Command> cmds = new HashSet<Command>();

View File

@ -45,7 +45,7 @@ public class NPCCommands {
@Command(
aliases = { "npc" },
usage = "create [name] --type (type) --char (character)",
usage = "create [name] (--type (type) --char (char))",
desc = "Create a new NPC",
modifiers = { "create" },
min = 2,
@ -282,4 +282,17 @@ public class NPCCommands {
+ (trait.shouldLookClose() ? "now rotate" : "no longer rotate");
Messaging.send(player, msg += " when a player is nearby.");
}
@Command(
aliases = { "npc" },
usage = "list (--type (type) --char (char)",
desc = "List NPCs",
modifiers = { "list" },
min = 2,
max = 5,
permission = "npc.list")
@Requirements
public void list(CommandContext args, Player player, NPC npc) {
}
}

View File

@ -0,0 +1,38 @@
package net.citizensnpcs.util;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.entity.Player;
public class Paginator {
private static final int LINES_PER_PAGE = 9;
private final List<String> lines = new ArrayList<String>();
private String header;
public void addLine(String line) {
lines.add(line);
}
public void setHeaderText(String header) {
this.header = header;
}
public boolean sendPage(Player player, 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(player, StringHelper.wrapHeader("<e>" + header + " <f>" + page + "/" + pages));
if (lines.size() < endIndex)
endIndex = lines.size();
for (String line : lines.subList(startIndex, endIndex))
Messaging.send(player, line);
return true;
}
}

View File

@ -71,11 +71,11 @@ public class StringHelper {
public static String capitalize(Object string) {
String capitalize = string.toString();
return capitalize.replaceFirst(String.valueOf(capitalize.charAt(0)),
String.valueOf(Character.toUpperCase(capitalize.charAt(0))));
return capitalize.replaceFirst(String.valueOf(capitalize.charAt(0)), String.valueOf(Character
.toUpperCase(capitalize.charAt(0))));
}
public static String wrapHeader(Object string) {
return ChatColor.GREEN + "=====[ " + string.toString() + ChatColor.GREEN + " ]=====";
return "<a>=====[ " + string.toString() + "<a> ]=====";
}
}