13
0
geforkt von Mirrors/Paper

Commands now have the ability to set a permission required before execution

By: Dinnerbone <dinnerbone@dinnerbone.com>
Dieser Commit ist enthalten in:
Bukkit/Spigot 2011-09-02 19:20:54 +01:00
Ursprung 8c73a0ec1b
Commit e60edf31eb
9 geänderte Dateien mit 407 neuen und 144 gelöschten Zeilen

Datei anzeigen

@ -2,6 +2,8 @@ package org.bukkit.command;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.permissions.Permissible;
/**
* Represents a Command, which executes various tasks upon user input
@ -15,6 +17,7 @@ public abstract class Command {
private CommandMap commandMap = null;
protected String description = "";
protected String usageMessage;
private String permission;
protected Command(String name) {
this(name, "", "/" + name, new ArrayList<String>());
@ -49,6 +52,41 @@ public abstract class Command {
return name;
}
/**
* Gets the permission required by users to be able to perform this command
*
* @return Permission name, or null if none
*/
public String getPermission() {
return permission;
}
/**
* Sets the permission required by users to be able to perform this command
*
* @param permission Permission name or null
*/
public void setPermission(String permission) {
this.permission = permission;
}
/**
* Tests the given {@link CommandSender} to see if they can perform this command.
*
* If they do not have permission, they will be informed that they cannot do this.
*
* @param target User to test
* @return true if they can use it, otherwise false
*/
public boolean testPermission(CommandSender target) {
if ((permission == null) || (permission.isEmpty()) || (target.hasPermission(permission))) {
return true;
}
target.sendMessage(ChatColor.RED + "I'm sorry, Dave. I'm afraid I can't do that.");
return false;
}
/**
* Returns the current lable for this command
*

Datei anzeigen

@ -31,6 +31,10 @@ public final class PluginCommand extends Command {
return false;
}
if (!testPermission(sender)) {
return true;
}
try {
success = executor.onCommand(sender, this, commandLabel, args);
} catch (Throwable ex) {

Datei anzeigen

@ -26,6 +26,7 @@ public class PluginCommandYamlParser {
Object description = entry.getValue().get("description");
Object usage = entry.getValue().get("usage");
Object aliases = entry.getValue().get("aliases");
Object permission = entry.getValue().get("permission");
if (description != null) {
newCmd.setDescription(description.toString());
@ -49,6 +50,10 @@ public class PluginCommandYamlParser {
newCmd.setAliases(aliasList);
}
if (permission != null) {
newCmd.setPermission(permission.toString());
}
pluginCmds.add(newCmd);
}
}

Datei anzeigen

@ -1,5 +1,8 @@
package org.bukkit.command;
import org.bukkit.command.defaults.ReloadCommand;
import org.bukkit.command.defaults.PluginsCommand;
import org.bukkit.command.defaults.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@ -13,7 +16,6 @@ import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import static org.bukkit.util.Java15Compat.Arrays_copyOfRange;
public final class SimpleCommandMap implements CommandMap {
@ -27,9 +29,9 @@ public final class SimpleCommandMap implements CommandMap {
}
private void setDefaultCommands(final Server server) {
register("bukkit", new VersionCommand("version", server));
register("bukkit", new ReloadCommand("reload", server));
register("bukkit", new PluginsCommand("plugins", server));
register("bukkit", new VersionCommand("version"));
register("bukkit", new ReloadCommand("reload"));
register("bukkit", new PluginsCommand("plugins"));
}
/**
@ -182,144 +184,4 @@ public final class SimpleCommandMap implements CommandMap {
}
}
}
private static class VersionCommand extends Command {
private final Server server;
public VersionCommand(String name, Server server) {
super(name);
this.server = server;
this.description = "Gets the version of this server including any plugins in use";
this.usageMessage = "/version [plugin name]";
this.setAliases(Arrays.asList("ver", "about"));
}
@Override
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
if (args.length == 0) {
sender.sendMessage("This server is running " + ChatColor.GREEN + server.getName() + ChatColor.WHITE + " version " + ChatColor.GREEN + server.getVersion());
sender.sendMessage("This server is also sporting some funky dev build of Bukkit!");
} else {
StringBuilder name = new StringBuilder();
for (String arg : args) {
if (name.length() > 0) {
name.append(' ');
}
name.append(arg);
}
Plugin plugin = server.getPluginManager().getPlugin(name.toString());
if (plugin != null) {
PluginDescriptionFile desc = plugin.getDescription();
sender.sendMessage(ChatColor.GREEN + desc.getName() + ChatColor.WHITE + " version " + ChatColor.GREEN + desc.getVersion());
if (desc.getDescription() != null) {
sender.sendMessage(desc.getDescription());
}
if (desc.getWebsite() != null) {
sender.sendMessage("Website: " + ChatColor.GREEN + desc.getWebsite());
}
if (!desc.getAuthors().isEmpty()) {
if (desc.getAuthors().size() == 1) {
sender.sendMessage("Author: " + getAuthors(desc));
} else {
sender.sendMessage("Authors: " + getAuthors(desc));
}
}
} else {
sender.sendMessage("This server is not running any plugin by that name.");
sender.sendMessage("Use /plugins to get a list of plugins.");
}
}
return true;
}
private String getAuthors(final PluginDescriptionFile desc) {
StringBuilder result = new StringBuilder();
ArrayList<String> authors = desc.getAuthors();
for (int i = 0; i < authors.size(); i++) {
if (result.length() > 0) {
result.append(ChatColor.WHITE);
if (i < authors.size() - 1) {
result.append(", ");
} else {
result.append(" and ");
}
}
result.append(ChatColor.GREEN);
result.append(authors.get(i));
}
return result.toString();
}
}
private static class ReloadCommand extends Command {
private final Server server;
public ReloadCommand(String name, Server server) {
super(name);
this.server = server;
this.description = "Reloads the server configuration and plugins";
this.usageMessage = "/reload";
this.setAliases(Arrays.asList("rl"));
}
@Override
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
if (sender.isOp()) {
server.reload();
sender.sendMessage(ChatColor.GREEN + "Reload complete.");
} else {
sender.sendMessage(ChatColor.RED + "You do not have sufficient access to reload this server.");
}
return true;
}
}
private static class PluginsCommand extends Command {
private final Server server;
public PluginsCommand(String name, Server server) {
super(name);
this.server = server;
this.description = "Gets a list of plugins running on the server";
this.usageMessage = "/plugins";
this.setAliases(Arrays.asList("pl"));
}
@Override
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
sender.sendMessage("Plugins: " + getPluginList());
return true;
}
private String getPluginList() {
StringBuilder pluginList = new StringBuilder();
Plugin[] plugins = server.getPluginManager().getPlugins();
for (Plugin plugin : plugins) {
if (pluginList.length() > 0) {
pluginList.append(ChatColor.WHITE);
pluginList.append(", ");
}
pluginList.append(plugin.isEnabled() ? ChatColor.GREEN : ChatColor.RED);
pluginList.append(plugin.getDescription().getName());
}
return pluginList.toString();
}
}
}

Datei anzeigen

@ -0,0 +1,43 @@
package org.bukkit.command.defaults;
import java.util.Arrays;
import org.bukkit.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
public class PluginsCommand extends Command {
public PluginsCommand(String name) {
super(name);
this.description = "Gets a list of plugins running on the server";
this.usageMessage = "/plugins";
this.setPermission("bukkit.command.plugins");
this.setAliases(Arrays.asList("pl"));
}
@Override
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
if (!testPermission(sender)) return true;
sender.sendMessage("Plugins: " + getPluginList());
return true;
}
private String getPluginList() {
StringBuilder pluginList = new StringBuilder();
Plugin[] plugins = Bukkit.getPluginManager().getPlugins();
for (Plugin plugin : plugins) {
if (pluginList.length() > 0) {
pluginList.append(ChatColor.WHITE);
pluginList.append(", ");
}
pluginList.append(plugin.isEnabled() ? ChatColor.GREEN : ChatColor.RED);
pluginList.append(plugin.getDescription().getName());
}
return pluginList.toString();
}
}

Datei anzeigen

@ -0,0 +1,27 @@
package org.bukkit.command.defaults;
import java.util.Arrays;
import org.bukkit.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
public class ReloadCommand extends Command {
public ReloadCommand(String name) {
super(name);
this.description = "Reloads the server configuration and plugins";
this.usageMessage = "/reload";
this.setPermission("bukkit.command.reload");
this.setAliases(Arrays.asList("rl"));
}
@Override
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
if (!testPermission(sender)) return true;
Bukkit.reload();
sender.sendMessage(ChatColor.GREEN + "Reload complete.");
return true;
}
}

Datei anzeigen

@ -0,0 +1,90 @@
package org.bukkit.command.defaults;
import java.util.ArrayList;
import java.util.Arrays;
import org.bukkit.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
public class VersionCommand extends Command {
public VersionCommand(String name) {
super(name);
this.description = "Gets the version of this server including any plugins in use";
this.usageMessage = "/version [plugin name]";
this.setPermission("bukkit.command.version");
this.setAliases(Arrays.asList("ver", "about"));
}
@Override
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
if (!testPermission(sender)) return true;
if (args.length == 0) {
sender.sendMessage("This server is running " + ChatColor.GREEN + Bukkit.getName() + ChatColor.WHITE + " version " + ChatColor.GREEN + Bukkit.getVersion());
sender.sendMessage("This server is also sporting some funky dev build of Bukkit!");
} else {
StringBuilder name = new StringBuilder();
for (String arg : args) {
if (name.length() > 0) {
name.append(' ');
}
name.append(arg);
}
Plugin plugin = Bukkit.getPluginManager().getPlugin(name.toString());
if (plugin != null) {
PluginDescriptionFile desc = plugin.getDescription();
sender.sendMessage(ChatColor.GREEN + desc.getName() + ChatColor.WHITE + " version " + ChatColor.GREEN + desc.getVersion());
if (desc.getDescription() != null) {
sender.sendMessage(desc.getDescription());
}
if (desc.getWebsite() != null) {
sender.sendMessage("Website: " + ChatColor.GREEN + desc.getWebsite());
}
if (!desc.getAuthors().isEmpty()) {
if (desc.getAuthors().size() == 1) {
sender.sendMessage("Author: " + getAuthors(desc));
} else {
sender.sendMessage("Authors: " + getAuthors(desc));
}
}
} else {
sender.sendMessage("This server is not running any plugin by that name.");
sender.sendMessage("Use /plugins to get a list of plugins.");
}
}
return true;
}
private String getAuthors(final PluginDescriptionFile desc) {
StringBuilder result = new StringBuilder();
ArrayList<String> authors = desc.getAuthors();
for (int i = 0; i < authors.size(); i++) {
if (result.length() > 0) {
result.append(ChatColor.WHITE);
if (i < authors.size() - 1) {
result.append(", ");
} else {
result.append(" and ");
}
}
result.append(ChatColor.GREEN);
result.append(authors.get(i));
}
return result.toString();
}
}

Datei anzeigen

@ -0,0 +1,111 @@
package org.bukkit.util.permissions;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
public class CommandPermissions {
private static final String ROOT = "bukkit.command";
private static final String PREFIX = ROOT + ".";
private CommandPermissions() {}
private static Permission registerWhitelist(Permission parent) {
Permission whitelist = DefaultPermissions.registerPermission(PREFIX + "whitelist", "Allows the user to modify the server whitelist", PermissionDefault.OP, parent);
DefaultPermissions.registerPermission(PREFIX + "whitelist.add", "Allows the user to add a player to the server whitelist", whitelist);
DefaultPermissions.registerPermission(PREFIX + "whitelist.remove", "Allows the user to remove a player from the server whitelist", whitelist);
DefaultPermissions.registerPermission(PREFIX + "whitelist.reload", "Allows the user to reload the server whitelist", whitelist);
DefaultPermissions.registerPermission(PREFIX + "whitelist.enable", "Allows the user to enable the server whitelist", whitelist);
DefaultPermissions.registerPermission(PREFIX + "whitelist.disable", "Allows the user to disable the server whitelist", whitelist);
DefaultPermissions.registerPermission(PREFIX + "whitelist.list", "Allows the user to list all the users on the server whitelist", whitelist);
whitelist.recalculatePermissibles();
return whitelist;
}
private static Permission registerBan(Permission parent) {
Permission ban = DefaultPermissions.registerPermission(PREFIX + "ban", "Allows the user to ban people", PermissionDefault.OP, parent);
DefaultPermissions.registerPermission(PREFIX + "ban.player", "Allows the user to ban players", ban);
DefaultPermissions.registerPermission(PREFIX + "ban.ip", "Allows the user to ban IP addresses", ban);
ban.recalculatePermissibles();
return ban;
}
private static Permission registerUnban(Permission parent) {
Permission unban = DefaultPermissions.registerPermission(PREFIX + "unban", "Allows the user to unban people", PermissionDefault.OP, parent);
DefaultPermissions.registerPermission(PREFIX + "unban.player", "Allows the user to unban players", unban);
DefaultPermissions.registerPermission(PREFIX + "unban.ip", "Allows the user to unban IP addresses", unban);
unban.recalculatePermissibles();
return unban;
}
private static Permission registerOp(Permission parent) {
Permission op = DefaultPermissions.registerPermission(PREFIX + "op", "Allows the user to change operators", PermissionDefault.OP, parent);
DefaultPermissions.registerPermission(PREFIX + "op.give", "Allows the user to give a player operator status", op);
DefaultPermissions.registerPermission(PREFIX + "op.take", "Allows the user to take a players operator status", op);
op.recalculatePermissibles();
return op;
}
private static Permission registerSave(Permission parent) {
Permission save = DefaultPermissions.registerPermission(PREFIX + "save", "Allows the user to save the worlds", PermissionDefault.OP, parent);
DefaultPermissions.registerPermission(PREFIX + "save.enable", "Allows the user to enable automatic saving", save);
DefaultPermissions.registerPermission(PREFIX + "save.disable", "Allows the user to disable automatic saving", save);
DefaultPermissions.registerPermission(PREFIX + "save.perform", "Allows the user to perform a manual save", save);
save.recalculatePermissibles();
return save;
}
private static Permission registerTime(Permission parent) {
Permission time = DefaultPermissions.registerPermission(PREFIX + "time", "Allows the user to alter the time", PermissionDefault.OP, parent);
DefaultPermissions.registerPermission(PREFIX + "time.add", "Allows the user to fast-forward time", time);
DefaultPermissions.registerPermission(PREFIX + "time.set", "Allows the user to change the time", time);
time.recalculatePermissibles();
return time;
}
public static Permission registerPermissions(Permission parent) {
Permission commands = DefaultPermissions.registerPermission(ROOT, "Gives the user the ability to use all Craftbukkit commands", parent);
registerWhitelist(commands);
registerBan(commands);
registerUnban(commands);
registerOp(commands);
registerSave(commands);
registerTime(commands);
DefaultPermissions.registerPermission(PREFIX + "kill", "Allows the user to commit suicide", PermissionDefault.TRUE, commands);
DefaultPermissions.registerPermission(PREFIX + "me", "Allows the user to perform a chat action", PermissionDefault.TRUE, commands);
DefaultPermissions.registerPermission(PREFIX + "tell", "Allows the user to privately message another player", PermissionDefault.TRUE, commands);
DefaultPermissions.registerPermission(PREFIX + "say", "Allows the user to talk as the console", PermissionDefault.OP, commands);
DefaultPermissions.registerPermission(PREFIX + "give", "Allows the user to give items to players", PermissionDefault.OP, commands);
DefaultPermissions.registerPermission(PREFIX + "teleport", "Allows the user to teleport players", PermissionDefault.OP, commands);
DefaultPermissions.registerPermission(PREFIX + "kick", "Allows the user to kick players", PermissionDefault.OP, commands);
DefaultPermissions.registerPermission(PREFIX + "stop", "Allows the user to stop the server", PermissionDefault.OP, commands);
DefaultPermissions.registerPermission(PREFIX + "list", "Allows the user to list all online players", PermissionDefault.OP, commands);
DefaultPermissions.registerPermission(PREFIX + "help", "Allows the user to view the vanilla help menu", PermissionDefault.OP, commands);
DefaultPermissions.registerPermission(PREFIX + "plugins", "Allows the user to view the list of plugins running on this server", PermissionDefault.TRUE, commands);
DefaultPermissions.registerPermission(PREFIX + "reload", "Allows the user to reload the server settings", PermissionDefault.OP, commands);
DefaultPermissions.registerPermission(PREFIX + "version", "Allows the user to view the version of the server", PermissionDefault.TRUE, commands);
commands.recalculatePermissibles();
return commands;
}
}

Datei anzeigen

@ -0,0 +1,83 @@
package org.bukkit.util.permissions;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
public final class DefaultPermissions {
private static final String ROOT = "craftbukkit";
private static final String PREFIX = ROOT + ".";
private static final String LEGACY_PREFIX = "craft";
private DefaultPermissions() {}
public static Permission registerPermission(Permission perm) {
return registerPermission(perm, true);
}
public static Permission registerPermission(Permission perm, boolean withLegacy) {
Permission result = perm;
try {
Bukkit.getPluginManager().addPermission(perm);
} catch (IllegalArgumentException ex) {
result = Bukkit.getPluginManager().getPermission(perm.getName());
}
if (withLegacy) {
Permission legacy = new Permission(LEGACY_PREFIX + result.getName(), result.getDescription(), PermissionDefault.FALSE);
legacy.getChildren().put(result.getName(), true);
registerPermission(perm, false);
}
return result;
}
public static Permission registerPermission(Permission perm, Permission parent) {
parent.getChildren().put(perm.getName(), true);
return registerPermission(perm);
}
public static Permission registerPermission(String name, String desc) {
Permission perm = registerPermission(new Permission(name, desc));
return perm;
}
public static Permission registerPermission(String name, String desc, Permission parent) {
Permission perm = registerPermission(name, desc);
parent.getChildren().put(perm.getName(), true);
return perm;
}
public static Permission registerPermission(String name, String desc, PermissionDefault def) {
Permission perm = registerPermission(new Permission(name, desc, def));
return perm;
}
public static Permission registerPermission(String name, String desc, PermissionDefault def, Permission parent) {
Permission perm = registerPermission(name, desc, def);
parent.getChildren().put(perm.getName(), true);
return perm;
}
public static Permission registerPermission(String name, String desc, PermissionDefault def, Map<String, Boolean> children) {
Permission perm = registerPermission(new Permission(name, desc, def, children));
return perm;
}
public static Permission registerPermission(String name, String desc, PermissionDefault def, Map<String, Boolean> children, Permission parent) {
Permission perm = registerPermission(name, desc, def, children);
parent.getChildren().put(perm.getName(), true);
return perm;
}
public static void registerCorePermissions() {
Permission parent = registerPermission(ROOT, "Gives the user the ability to use all Craftbukkit utilities and commands");
CommandPermissions.registerPermissions(parent);
parent.recalculatePermissibles();
}
}