mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-07-01 00:44:50 +02:00
Added support for using help menu in console. This addresses CITIZENS-20.
This commit is contained in:
parent
a02521b5cf
commit
d1cb52cb0a
|
@ -100,14 +100,16 @@ public class Citizens extends JavaPlugin {
|
||||||
} catch (UnhandledCommandException ex) {
|
} catch (UnhandledCommandException ex) {
|
||||||
return false;
|
return false;
|
||||||
} catch (CommandException ex) {
|
} catch (CommandException ex) {
|
||||||
Messaging.sendError(player, ex.getMessage());
|
Messaging.sendError(sender, ex.getMessage());
|
||||||
}
|
}
|
||||||
} catch (NumberFormatException ex) {
|
} catch (NumberFormatException ex) {
|
||||||
Messaging.sendError(player, "That is not a valid number.");
|
Messaging.sendError(player, "That is not a valid number.");
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
Messaging.sendError(player, "Please report this error: [See console]");
|
if (sender instanceof Player) {
|
||||||
Messaging.sendError(player, ex.getClass().getName() + ": " + ex.getMessage());
|
Messaging.sendError(player, "Please report this error: [See console]");
|
||||||
|
Messaging.sendError(player, ex.getClass().getName() + ": " + ex.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
import net.citizensnpcs.Citizens;
|
import net.citizensnpcs.Citizens;
|
||||||
import net.citizensnpcs.api.npc.NPC;
|
import net.citizensnpcs.api.npc.NPC;
|
||||||
|
@ -13,6 +13,7 @@ import net.citizensnpcs.command.Command;
|
||||||
import net.citizensnpcs.command.CommandContext;
|
import net.citizensnpcs.command.CommandContext;
|
||||||
import net.citizensnpcs.command.CommandManager;
|
import net.citizensnpcs.command.CommandManager;
|
||||||
import net.citizensnpcs.command.Requirements;
|
import net.citizensnpcs.command.Requirements;
|
||||||
|
import net.citizensnpcs.command.ServerCommand;
|
||||||
import net.citizensnpcs.command.exception.CommandException;
|
import net.citizensnpcs.command.exception.CommandException;
|
||||||
import net.citizensnpcs.util.Paginator;
|
import net.citizensnpcs.util.Paginator;
|
||||||
|
|
||||||
|
@ -33,34 +34,17 @@ public class HelpCommands {
|
||||||
max = 2,
|
max = 2,
|
||||||
permission = "help")
|
permission = "help")
|
||||||
@Requirements
|
@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;
|
int page = args.argsLength() == 2 ? args.getInteger(1) : 1;
|
||||||
Paginator paginator = new Paginator();
|
Paginator paginator = new Paginator();
|
||||||
for (String line : getLines(player, "citizens"))
|
for (String line : getLines(sender, "citizens"))
|
||||||
paginator.addLine(line);
|
paginator.addLine(line);
|
||||||
paginator.setHeaderText("Citizens Help");
|
paginator.setHeaderText("Citizens Help");
|
||||||
if (!paginator.sendPage(player, page))
|
if (!paginator.sendPage(sender, page))
|
||||||
throw new CommandException("The page '" + page + "' does not exist.");
|
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(
|
@Command(
|
||||||
aliases = { "npc" },
|
aliases = { "npc" },
|
||||||
usage = "help (page)",
|
usage = "help (page)",
|
||||||
|
@ -70,13 +54,32 @@ public class HelpCommands {
|
||||||
max = 2,
|
max = 2,
|
||||||
permission = "npc.help")
|
permission = "npc.help")
|
||||||
@Requirements
|
@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;
|
int page = args.argsLength() == 2 ? args.getInteger(1) : 1;
|
||||||
Paginator paginator = new Paginator();
|
Paginator paginator = new Paginator();
|
||||||
for (String line : getLines(player, "npc"))
|
for (String line : getLines(sender, "npc"))
|
||||||
paginator.addLine(line);
|
paginator.addLine(line);
|
||||||
paginator.setHeaderText("NPC Help");
|
paginator.setHeaderText("NPC Help");
|
||||||
if (!paginator.sendPage(player, page))
|
if (!paginator.sendPage(sender, page))
|
||||||
throw new CommandException("The page '" + page + "' does not exist.");
|
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.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
public class Paginator {
|
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>();
|
private final List<String> lines = new ArrayList<String>();
|
||||||
|
|
||||||
public void addLine(String line) {
|
public void addLine(String line) {
|
||||||
lines.add(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));
|
int pages = (int) ((lines.size() / LINES_PER_PAGE == 0) ? 1 : Math.ceil((double) lines.size() / LINES_PER_PAGE));
|
||||||
if (page < 0 || page > pages)
|
if (page < 0 || page > pages)
|
||||||
return false;
|
return false;
|
||||||
|
@ -21,18 +23,16 @@ public class Paginator {
|
||||||
int startIndex = LINES_PER_PAGE * page - LINES_PER_PAGE;
|
int startIndex = LINES_PER_PAGE * page - LINES_PER_PAGE;
|
||||||
int endIndex = 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)
|
if (lines.size() < endIndex)
|
||||||
endIndex = lines.size();
|
endIndex = lines.size();
|
||||||
for (String line : lines.subList(startIndex, endIndex))
|
for (String line : lines.subList(startIndex, endIndex))
|
||||||
Messaging.send(player, line);
|
Messaging.send(sender, line);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHeaderText(String header) {
|
public void setHeaderText(String header) {
|
||||||
this.header = header;
|
this.header = header;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final int LINES_PER_PAGE = 9;
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user