mirror of
https://github.com/PaperMC/Paper.git
synced 2025-08-14 19:55:52 -07:00
[Bleeding] Added Help API. Addresses BUKKIT-863
By: rmichela <deltahat@gmail.com>
This commit is contained in:
@@ -83,7 +83,7 @@ public abstract class Command {
|
||||
* @return true if they can use it, otherwise false
|
||||
*/
|
||||
public boolean testPermission(CommandSender target) {
|
||||
if ((permission == null) || (permission.length() == 0) || (target.hasPermission(permission))) {
|
||||
if (testPermissionSilent(target)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -98,6 +98,28 @@ public abstract class Command {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the given {@link CommandSender} to see if they can perform this command.
|
||||
* <p />
|
||||
* No error is sent to the sender.
|
||||
*
|
||||
* @param target User to test
|
||||
* @return true if they can use it, otherwise false
|
||||
*/
|
||||
public boolean testPermissionSilent(CommandSender target) {
|
||||
if ((permission == null) || (permission.length() == 0)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (String p : permission.split(";")) {
|
||||
if (target.hasPermission(p)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current lable for this command
|
||||
*
|
||||
|
@@ -12,6 +12,13 @@ public interface CommandSender extends Permissible {
|
||||
*/
|
||||
public void sendMessage(String message);
|
||||
|
||||
/**
|
||||
* Sends this sender multiple messages
|
||||
*
|
||||
* @param messages An array of messages to be displayed
|
||||
*/
|
||||
public void sendMessage(String[] messages);
|
||||
|
||||
/**
|
||||
* Returns the server instance that this command is running on
|
||||
*
|
||||
|
@@ -10,6 +10,10 @@ public class MultipleCommandAlias extends Command {
|
||||
super(name);
|
||||
this.commands = commands;
|
||||
}
|
||||
|
||||
public Command[] getCommands() {
|
||||
return commands;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
|
||||
|
@@ -1,13 +1,9 @@
|
||||
package org.bukkit.command;
|
||||
|
||||
import org.bukkit.command.defaults.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.Iterator;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.bukkit.Server;
|
||||
import static org.bukkit.util.Java15Compat.Arrays_copyOfRange;
|
||||
|
||||
@@ -143,6 +139,10 @@ public class SimpleCommandMap implements CommandMap {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Set<VanillaCommand> getFallbackCommands() {
|
||||
return Collections.unmodifiableSet(fallbackCommands);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
@@ -156,9 +156,7 @@ public class SimpleCommandMap implements CommandMap {
|
||||
|
||||
String sentCommandLabel = args[0].toLowerCase();
|
||||
Command target = getCommand(sentCommandLabel);
|
||||
if (target == null) {
|
||||
target = getFallback(commandLine.toLowerCase());
|
||||
}
|
||||
|
||||
if (target == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -186,7 +184,15 @@ public class SimpleCommandMap implements CommandMap {
|
||||
}
|
||||
|
||||
public Command getCommand(String name) {
|
||||
return knownCommands.get(name.toLowerCase());
|
||||
Command target = knownCommands.get(name.toLowerCase());
|
||||
if (target == null) {
|
||||
target = getFallback(name);
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
public Collection<Command> getCommands() {
|
||||
return knownCommands.values();
|
||||
}
|
||||
|
||||
public void registerServerAliases() {
|
||||
|
@@ -1,12 +1,23 @@
|
||||
package org.bukkit.command.defaults;
|
||||
|
||||
import org.apache.commons.lang.ArrayUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang.math.NumberUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.help.HelpMap;
|
||||
import org.bukkit.help.HelpTopic;
|
||||
import org.bukkit.util.ChatPaginator;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class HelpCommand extends VanillaCommand {
|
||||
public HelpCommand() {
|
||||
super("help");
|
||||
this.description = "Shows the help menu";
|
||||
this.usageMessage = "/help";
|
||||
this.usageMessage = "/help <pageNumber>\n/help <topic>\n/help <topic> <pageNumber>";
|
||||
this.setPermission("bukkit.command.help");
|
||||
}
|
||||
|
||||
@@ -14,25 +25,62 @@ public class HelpCommand extends VanillaCommand {
|
||||
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
|
||||
if (!testPermission(sender)) return true;
|
||||
|
||||
sender.sendMessage("help or ? shows this message");
|
||||
sender.sendMessage("kick <player> removes a player from the server");
|
||||
sender.sendMessage("ban <player> bans a player from the server");
|
||||
sender.sendMessage("pardon <player> pardons a banned player so that they can connect again");
|
||||
sender.sendMessage("ban-ip <ip> bans an IP address from the server");
|
||||
sender.sendMessage("pardon-ip <ip> pardons a banned IP address so that they can connect again");
|
||||
sender.sendMessage("op <player> turns a player into an op");
|
||||
sender.sendMessage("deop <player> removes op status from a player");
|
||||
sender.sendMessage("tp <player1> <player2> moves one player to the same location as another player");
|
||||
sender.sendMessage("give <player> <id> [num] gives a player a resource");
|
||||
sender.sendMessage("tell <player> <message> sends a private message to a player");
|
||||
sender.sendMessage("stop gracefully stops the server");
|
||||
sender.sendMessage("save-all forces a server-wide level save");
|
||||
sender.sendMessage("save-off disables terrain saving (useful for backup scripts)");
|
||||
sender.sendMessage("save-on re-enables terrain saving");
|
||||
sender.sendMessage("list lists all currently connected players");
|
||||
sender.sendMessage("say <message> broadcasts a message to all players");
|
||||
sender.sendMessage("time <add|set> <amount> adds to or sets the world time (0-24000)");
|
||||
sender.sendMessage("gamemode <player> <mode> sets player\'s game mode (0 or 1)");
|
||||
String command;
|
||||
int pageNumber;
|
||||
int pageHeight;
|
||||
int pageWidth;
|
||||
|
||||
if (args.length == 0) {
|
||||
command = "";
|
||||
pageNumber = 1;
|
||||
} else if (NumberUtils.isDigits(args[args.length - 1])) {
|
||||
command = StringUtils.join(ArrayUtils.subarray(args, 0, args.length - 1), " ");
|
||||
pageNumber = NumberUtils.createInteger(args[args.length - 1]);
|
||||
} else {
|
||||
command = StringUtils.join(args, " ");
|
||||
pageNumber = 1;
|
||||
}
|
||||
|
||||
if (sender instanceof ConsoleCommandSender) {
|
||||
pageHeight = ChatPaginator.UNBOUNDED_PAGE_HEIGHT;
|
||||
pageWidth = ChatPaginator.UNBOUNDED_PAGE_WIDTH;
|
||||
} else {
|
||||
pageHeight = ChatPaginator.CLOSED_CHAT_PAGE_HEIGHT - 1;
|
||||
pageWidth = ChatPaginator.AVERAGE_CHAT_PAGE_WIDTH;
|
||||
}
|
||||
|
||||
HelpMap helpMap = Bukkit.getServer().getHelpMap();
|
||||
HelpTopic topic = helpMap.getHelpTopic(command);
|
||||
|
||||
if (topic == null) {
|
||||
topic = helpMap.getHelpTopic("/" + command);
|
||||
}
|
||||
|
||||
if (topic == null || !topic.canSee(sender)) {
|
||||
sender.sendMessage(ChatColor.RED + "No help for " + command);
|
||||
return true;
|
||||
}
|
||||
|
||||
ChatPaginator.ChatPage page = ChatPaginator.paginate(topic.getFullText(sender), pageNumber, pageWidth, pageHeight);
|
||||
|
||||
StringBuilder header = new StringBuilder();
|
||||
header.append(ChatColor.GREEN);
|
||||
header.append("===== Help: ");
|
||||
header.append(topic.getName());
|
||||
header.append(" ");
|
||||
if (page.getTotalPages() > 1) {
|
||||
header.append("(");
|
||||
header.append(page.getPageNumber());
|
||||
header.append(" of ");
|
||||
header.append(page.getTotalPages());
|
||||
header.append(") ");
|
||||
}
|
||||
for (int i = header.length(); i < ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH; i++) {
|
||||
header.append("=");
|
||||
}
|
||||
sender.sendMessage(header.toString());
|
||||
|
||||
sender.sendMessage(page.getLines());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@@ -11,6 +11,7 @@ public class TimeCommand extends VanillaCommand {
|
||||
super("time");
|
||||
this.description = "Changes the time on each world";
|
||||
this.usageMessage = "/time set <value>\n/time add <value>";
|
||||
this.setPermission("bukkit.command.time.add;bukkit.command.time.set");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -11,6 +11,7 @@ public class WhitelistCommand extends VanillaCommand {
|
||||
super("whitelist");
|
||||
this.description = "Prevents the specified player from using this server";
|
||||
this.usageMessage = "/whitelist (add|remove) <player>\n/whitelist (on|off|list|reload)";
|
||||
this.setPermission("bukkit.command.whitelist.reload;bukkit.command.whitelist.enable;bukkit.command.whitelist.disable;bukkit.command.whitelist.list;bukkit.command.whitelist.add;bukkit.command.whitelist.remove");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user