mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2025-03-02 11:31:03 +01:00
Added support for using help menu in console. This addresses CITIZENS-20.
This commit is contained in:
parent
0cbf0d8d74
commit
15c1ad24ad
@ -100,14 +100,16 @@ public class Citizens extends JavaPlugin {
|
||||
} catch (UnhandledCommandException ex) {
|
||||
return false;
|
||||
} catch (CommandException ex) {
|
||||
Messaging.sendError(player, ex.getMessage());
|
||||
Messaging.sendError(sender, ex.getMessage());
|
||||
}
|
||||
} catch (NumberFormatException ex) {
|
||||
Messaging.sendError(player, "That is not a valid number.");
|
||||
} catch (Throwable ex) {
|
||||
ex.printStackTrace();
|
||||
Messaging.sendError(player, "Please report this error: [See console]");
|
||||
Messaging.sendError(player, ex.getClass().getName() + ": " + ex.getMessage());
|
||||
if (sender instanceof Player) {
|
||||
Messaging.sendError(player, "Please report this error: [See console]");
|
||||
Messaging.sendError(player, ex.getClass().getName() + ": " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import net.citizensnpcs.Citizens;
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
@ -13,6 +13,7 @@ import net.citizensnpcs.command.Command;
|
||||
import net.citizensnpcs.command.CommandContext;
|
||||
import net.citizensnpcs.command.CommandManager;
|
||||
import net.citizensnpcs.command.Requirements;
|
||||
import net.citizensnpcs.command.ServerCommand;
|
||||
import net.citizensnpcs.command.exception.CommandException;
|
||||
import net.citizensnpcs.util.Paginator;
|
||||
|
||||
@ -33,34 +34,17 @@ public class HelpCommands {
|
||||
max = 2,
|
||||
permission = "help")
|
||||
@Requirements
|
||||
public void citizensHelp(CommandContext args, Player player, NPC npc) throws CommandException {
|
||||
@ServerCommand
|
||||
public void citizensHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
|
||||
int page = args.argsLength() == 2 ? args.getInteger(1) : 1;
|
||||
Paginator paginator = new Paginator();
|
||||
for (String line : getLines(player, "citizens"))
|
||||
for (String line : getLines(sender, "citizens"))
|
||||
paginator.addLine(line);
|
||||
paginator.setHeaderText("Citizens Help");
|
||||
if (!paginator.sendPage(player, page))
|
||||
if (!paginator.sendPage(sender, page))
|
||||
throw new CommandException("The page '" + page + "' does not exist.");
|
||||
}
|
||||
|
||||
private List<String> getLines(Player player, String baseCommand) {
|
||||
// Ensures that commands with multiple modifiers are only added once
|
||||
Set<Command> cmds = new HashSet<Command>();
|
||||
List<String> lines = new ArrayList<String>();
|
||||
for (Command cmd : cmdManager.getCommands(baseCommand)) {
|
||||
if (cmds.contains(cmd)
|
||||
|| (!player.hasPermission("citizens.admin") && !player
|
||||
.hasPermission("citizens." + cmd.permission())))
|
||||
continue;
|
||||
|
||||
lines.add("<7>/<c>" + cmd.aliases()[0] + (cmd.usage().isEmpty() ? "" : " " + cmd.usage()) + " <7>- <e>"
|
||||
+ cmd.desc());
|
||||
if (cmd.modifiers().length > 1)
|
||||
cmds.add(cmd);
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
@Command(
|
||||
aliases = { "npc" },
|
||||
usage = "help (page)",
|
||||
@ -70,13 +54,32 @@ public class HelpCommands {
|
||||
max = 2,
|
||||
permission = "npc.help")
|
||||
@Requirements
|
||||
public void npcHelp(CommandContext args, Player player, NPC npc) throws CommandException {
|
||||
@ServerCommand
|
||||
public void npcHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
|
||||
int page = args.argsLength() == 2 ? args.getInteger(1) : 1;
|
||||
Paginator paginator = new Paginator();
|
||||
for (String line : getLines(player, "npc"))
|
||||
for (String line : getLines(sender, "npc"))
|
||||
paginator.addLine(line);
|
||||
paginator.setHeaderText("NPC Help");
|
||||
if (!paginator.sendPage(player, page))
|
||||
if (!paginator.sendPage(sender, page))
|
||||
throw new CommandException("The page '" + page + "' does not exist.");
|
||||
}
|
||||
|
||||
private List<String> getLines(CommandSender sender, String baseCommand) {
|
||||
// Ensures that commands with multiple modifiers are only added once
|
||||
Set<Command> cmds = new HashSet<Command>();
|
||||
List<String> lines = new ArrayList<String>();
|
||||
for (Command cmd : cmdManager.getCommands(baseCommand)) {
|
||||
if (cmds.contains(cmd)
|
||||
|| (!sender.hasPermission("citizens.admin") && !sender
|
||||
.hasPermission("citizens." + cmd.permission())))
|
||||
continue;
|
||||
|
||||
lines.add("<7>/<c>" + cmd.aliases()[0] + (cmd.usage().isEmpty() ? "" : " " + cmd.usage()) + " <7>- <e>"
|
||||
+ cmd.desc());
|
||||
if (cmd.modifiers().length > 1)
|
||||
cmds.add(cmd);
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
}
|
@ -3,17 +3,19 @@ package net.citizensnpcs.util;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class Paginator {
|
||||
private String header;
|
||||
private static final int LINES_PER_PAGE = 9;
|
||||
|
||||
private String header;
|
||||
private final List<String> lines = new ArrayList<String>();
|
||||
|
||||
public void addLine(String line) {
|
||||
lines.add(line);
|
||||
}
|
||||
|
||||
public boolean sendPage(Player player, int page) {
|
||||
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;
|
||||
@ -21,18 +23,16 @@ public class Paginator {
|
||||
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));
|
||||
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(player, line);
|
||||
Messaging.send(sender, line);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void setHeaderText(String header) {
|
||||
this.header = header;
|
||||
}
|
||||
|
||||
private static final int LINES_PER_PAGE = 9;
|
||||
}
|
Loading…
Reference in New Issue
Block a user