mirror of
https://github.com/EngineHub/WorldEdit.git
synced 2024-12-27 05:00:08 +08:00
Register command permissions, integrate with the Bukkit help API
Help API support requires a fix in Bukkit to work fully Allow annotation-free registering of commands with other plugins
This commit is contained in:
parent
956b3dd02f
commit
4328be282c
65
src/main/java/com/sk89q/bukkit/util/CommandInfo.java
Normal file
65
src/main/java/com/sk89q/bukkit/util/CommandInfo.java
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* WorldEdit
|
||||
* Copyright (C) 2012 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.bukkit.util;
|
||||
|
||||
/**
|
||||
* @author zml2008
|
||||
*/
|
||||
public class CommandInfo {
|
||||
private final String[] aliases;
|
||||
private final Object registeredWith;
|
||||
private final String usage, desc;
|
||||
private final String[] permissions;
|
||||
|
||||
public CommandInfo(String usage, String desc, String[] aliases, Object registeredWith) {
|
||||
this(usage, desc, aliases, registeredWith, null);
|
||||
}
|
||||
|
||||
public CommandInfo(String usage, String desc, String[] aliases, Object registeredWith, String[] permissions) {
|
||||
this.usage = usage;
|
||||
this.desc = desc;
|
||||
this.aliases = aliases;
|
||||
this.permissions = permissions;
|
||||
this.registeredWith = registeredWith;
|
||||
}
|
||||
|
||||
public String[] getAliases() {
|
||||
return aliases;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return aliases[0];
|
||||
}
|
||||
|
||||
public String getUsage() {
|
||||
return usage;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
public String[] getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
public Object getRegisteredWith() {
|
||||
return registeredWith;
|
||||
}
|
||||
}
|
@ -25,7 +25,6 @@
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.sk89q.minecraft.util.commands.Command;
|
||||
import com.sk89q.util.ReflectionUtil;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
@ -37,8 +36,13 @@
|
||||
* @author zml2008
|
||||
*/
|
||||
public class CommandRegistration {
|
||||
private final Plugin plugin;
|
||||
private final CommandExecutor executor;
|
||||
|
||||
static {
|
||||
Bukkit.getServer().getHelpMap().registerHelpTopicFactory(DynamicPluginCommand.class, new DynamicPluginCommandHelpTopic.Factory());
|
||||
}
|
||||
|
||||
protected final Plugin plugin;
|
||||
protected final CommandExecutor executor;
|
||||
private CommandMap fallbackCommands;
|
||||
|
||||
public CommandRegistration(Plugin plugin) {
|
||||
@ -50,14 +54,16 @@ public CommandRegistration(Plugin plugin, CommandExecutor executor) {
|
||||
this.executor = executor;
|
||||
}
|
||||
|
||||
public boolean registerAll(List<Command> registered) {
|
||||
public boolean register(List<CommandInfo> registered) {
|
||||
CommandMap commandMap = getCommandMap();
|
||||
if (registered == null || commandMap == null) {
|
||||
return false;
|
||||
}
|
||||
for (Command command : registered) {
|
||||
commandMap.register(plugin.getDescription().getName(),
|
||||
new DynamicPluginCommand(command.aliases(), command.desc(), "/" + command.aliases()[0] + " " + command.usage(), executor));
|
||||
for (CommandInfo command : registered) {
|
||||
DynamicPluginCommand cmd = new DynamicPluginCommand(command.getAliases(),
|
||||
command.getDesc(), "/" + command.getAliases()[0] + " " + command.getUsage(), executor, command.getRegisteredWith());
|
||||
cmd.setPermissions(command.getPermissions());
|
||||
commandMap.register(plugin.getDescription().getName(), cmd);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -20,10 +20,14 @@
|
||||
package com.sk89q.bukkit.util;
|
||||
|
||||
import com.sk89q.minecraft.util.commands.Command;
|
||||
import com.sk89q.minecraft.util.commands.CommandPermissions;
|
||||
import com.sk89q.minecraft.util.commands.CommandsManager;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandMap;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -43,7 +47,21 @@ public CommandsManagerRegistration(Plugin plugin, CommandExecutor executor, Comm
|
||||
}
|
||||
|
||||
public boolean register(Class<?> clazz) {
|
||||
List<Command> registered = commands.registerAndReturn(clazz);
|
||||
return registerAll(registered);
|
||||
return registerAll(commands.registerAndReturn(clazz));
|
||||
}
|
||||
|
||||
public boolean registerAll(List<Command> registered) {
|
||||
List<CommandInfo> toRegister = new ArrayList<CommandInfo>();
|
||||
for (Command command : registered) {
|
||||
String[] permissions = null;
|
||||
Method cmdMethod = commands.getMethods().get(null).get(command.aliases()[0]);
|
||||
if (cmdMethod != null && cmdMethod.isAnnotationPresent(CommandPermissions.class)) {
|
||||
permissions = cmdMethod.getAnnotation(CommandPermissions.class).value();
|
||||
}
|
||||
|
||||
toRegister.add(new CommandInfo(command.usage(), command.desc(), command.aliases(), commands, permissions));
|
||||
}
|
||||
|
||||
return register(toRegister);
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
package com.sk89q.bukkit.util;
|
||||
|
||||
import com.sk89q.util.StringUtil;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import java.util.Arrays;
|
||||
@ -29,10 +30,13 @@
|
||||
public class DynamicPluginCommand extends org.bukkit.command.Command {
|
||||
|
||||
protected final CommandExecutor owner;
|
||||
protected final Object registeredWith;
|
||||
protected String[] permissions = new String[0];
|
||||
|
||||
public DynamicPluginCommand(String[] aliases, String desc, String usage, CommandExecutor owner) {
|
||||
public DynamicPluginCommand(String[] aliases, String desc, String usage, CommandExecutor owner, Object registeredWith) {
|
||||
super(aliases[0], desc, usage, Arrays.asList(aliases));
|
||||
this.owner = owner;
|
||||
this.registeredWith = registeredWith;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -43,4 +47,19 @@ public boolean execute(CommandSender sender, String label, String[] args) {
|
||||
public Object getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public Object getRegisteredWith() {
|
||||
return registeredWith;
|
||||
}
|
||||
|
||||
public void setPermissions(String[] permissions) {
|
||||
this.permissions = permissions;
|
||||
if (permissions != null) {
|
||||
super.setPermission(StringUtil.joinString(permissions, ";"));
|
||||
}
|
||||
}
|
||||
|
||||
public String[] getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* WorldEdit
|
||||
* Copyright (C) 2012 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.bukkit.util;
|
||||
|
||||
import com.sk89q.minecraft.util.commands.CommandsManager;
|
||||
import com.sk89q.wepif.PermissionsResolverManager;
|
||||
import com.sk89q.wepif.WEPIFRuntimeException;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.help.HelpTopic;
|
||||
import org.bukkit.help.HelpTopicFactory;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author zml2008
|
||||
*/
|
||||
public class DynamicPluginCommandHelpTopic extends HelpTopic {
|
||||
private final DynamicPluginCommand cmd;
|
||||
|
||||
public DynamicPluginCommandHelpTopic(DynamicPluginCommand cmd) {
|
||||
this.cmd = cmd;
|
||||
this.name = "/" + cmd.getName();
|
||||
|
||||
if (cmd.getRegisteredWith() instanceof CommandsManager) {
|
||||
Map<String, String> helpText = ((CommandsManager<?>) cmd.getRegisteredWith()).getHelpMessages();
|
||||
final String lookupName = cmd.getName().replaceAll("/", "");
|
||||
if (helpText.containsKey(lookupName)) {
|
||||
this.fullText = helpText.get(lookupName);
|
||||
}
|
||||
helpText = ((CommandsManager<?>) cmd.getRegisteredWith()).getCommands();
|
||||
if (helpText.containsKey(cmd.getName())) {
|
||||
final String shortText = helpText.get(cmd.getName());
|
||||
if (this.fullText == null) {
|
||||
this.fullText = this.name + " " + shortText;
|
||||
}
|
||||
this.shortText = shortText;
|
||||
}
|
||||
} else {
|
||||
this.shortText = cmd.getDescription();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean canSee(CommandSender player) {
|
||||
if (cmd.getPermissions() != null && cmd.getPermissions().length > 0) {
|
||||
if (cmd.getRegisteredWith() instanceof CommandsManager) {
|
||||
try {
|
||||
for (String perm : cmd.getPermissions()) {
|
||||
if (((CommandsManager<Object>) cmd.getRegisteredWith()).hasPermission(player, perm)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
// Doesn't take the CommandSender (Hooray for compile-time generics!), we have other methods at our disposal
|
||||
}
|
||||
}
|
||||
if (player instanceof OfflinePlayer) {
|
||||
try {
|
||||
for (String perm : cmd.getPermissions()) {
|
||||
if (PermissionsResolverManager.getInstance().hasPermission((OfflinePlayer) player, perm)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (WEPIFRuntimeException e) {
|
||||
// PermissionsResolverManager not initialized, eat it
|
||||
}
|
||||
}
|
||||
for (String perm : cmd.getPermissions()) {
|
||||
if (player.hasPermission(perm)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullText(CommandSender forWho) {
|
||||
if (this.fullText == null || this.fullText.length() == 0) {
|
||||
return getShortText();
|
||||
} else {
|
||||
return this.fullText;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Factory implements HelpTopicFactory<DynamicPluginCommand> {
|
||||
|
||||
@Override
|
||||
public HelpTopic createTopic(DynamicPluginCommand command) {
|
||||
return new DynamicPluginCommandHelpTopic(command);
|
||||
}
|
||||
}
|
||||
}
|
@ -35,12 +35,8 @@ public FallbackRegistrationListener(CommandMap commandRegistration) {
|
||||
this.commandRegistration = commandRegistration;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (commandRegistration.dispatch(event.getPlayer(), event.getMessage())) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
package com.sk89q.worldedit;
|
||||
|
||||
import com.sk89q.minecraft.util.commands.Command;
|
||||
import com.sk89q.minecraft.util.commands.CommandsManager;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@ -32,16 +33,16 @@ public abstract class ServerInterface {
|
||||
/**
|
||||
* Resolves an item name to its ID.
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
* @param name The name to look up
|
||||
* @return The id that corresponds to the name, or -1 if no such ID exists
|
||||
*/
|
||||
public abstract int resolveItem(String name);
|
||||
|
||||
/**
|
||||
* Checks if a mob type is valid.
|
||||
*
|
||||
* @param type
|
||||
* @return
|
||||
* @param type The mob type name to check
|
||||
* @return Whether the name is a valid mod bype
|
||||
*/
|
||||
public abstract boolean isValidMobType(String type);
|
||||
|
||||
@ -67,7 +68,12 @@ public List<LocalWorld> getWorlds() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void onCommandRegistration(List<Command> commands) {
|
||||
// Do nothing :)
|
||||
}
|
||||
|
||||
public void onCommandRegistration(List<Command> commands, CommandsManager<LocalPlayer> manager) {
|
||||
onCommandRegistration(commands);
|
||||
}
|
||||
}
|
||||
|
@ -185,19 +185,19 @@ public void invokeMethod(Method parent, String[] args,
|
||||
|
||||
commands.setInjector(new SimpleInjector(this));
|
||||
|
||||
server.onCommandRegistration(commands.registerAndReturn(ChunkCommands.class));
|
||||
server.onCommandRegistration(commands.registerAndReturn(ClipboardCommands.class));
|
||||
server.onCommandRegistration(commands.registerAndReturn(GeneralCommands.class));
|
||||
server.onCommandRegistration(commands.registerAndReturn(GenerationCommands.class));
|
||||
server.onCommandRegistration(commands.registerAndReturn(HistoryCommands.class));
|
||||
server.onCommandRegistration(commands.registerAndReturn(NavigationCommands.class));
|
||||
server.onCommandRegistration(commands.registerAndReturn(RegionCommands.class));
|
||||
server.onCommandRegistration(commands.registerAndReturn(ScriptingCommands.class));
|
||||
server.onCommandRegistration(commands.registerAndReturn(SelectionCommands.class));
|
||||
server.onCommandRegistration(commands.registerAndReturn(SnapshotUtilCommands.class));
|
||||
server.onCommandRegistration(commands.registerAndReturn(ToolUtilCommands.class));
|
||||
server.onCommandRegistration(commands.registerAndReturn(ToolCommands.class));
|
||||
server.onCommandRegistration(commands.registerAndReturn(UtilityCommands.class));
|
||||
server.onCommandRegistration(commands.registerAndReturn(ChunkCommands.class), commands);
|
||||
server.onCommandRegistration(commands.registerAndReturn(ClipboardCommands.class), commands);
|
||||
server.onCommandRegistration(commands.registerAndReturn(GeneralCommands.class), commands);
|
||||
server.onCommandRegistration(commands.registerAndReturn(GenerationCommands.class), commands);
|
||||
server.onCommandRegistration(commands.registerAndReturn(HistoryCommands.class), commands);
|
||||
server.onCommandRegistration(commands.registerAndReturn(NavigationCommands.class), commands);
|
||||
server.onCommandRegistration(commands.registerAndReturn(RegionCommands.class), commands);
|
||||
server.onCommandRegistration(commands.registerAndReturn(ScriptingCommands.class), commands);
|
||||
server.onCommandRegistration(commands.registerAndReturn(SelectionCommands.class), commands);
|
||||
server.onCommandRegistration(commands.registerAndReturn(SnapshotUtilCommands.class), commands);
|
||||
server.onCommandRegistration(commands.registerAndReturn(ToolUtilCommands.class), commands);
|
||||
server.onCommandRegistration(commands.registerAndReturn(ToolCommands.class), commands);
|
||||
server.onCommandRegistration(commands.registerAndReturn(UtilityCommands.class), commands);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -19,12 +19,17 @@
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.sk89q.bukkit.util.CommandInfo;
|
||||
import com.sk89q.bukkit.util.CommandRegistration;
|
||||
import com.sk89q.minecraft.util.commands.Command;
|
||||
|
||||
import com.sk89q.minecraft.util.commands.CommandPermissions;
|
||||
import com.sk89q.minecraft.util.commands.CommandsManager;
|
||||
import com.sk89q.worldedit.LocalPlayer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Server;
|
||||
@ -80,8 +85,19 @@ public List<LocalWorld> getWorlds() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommandRegistration(List<Command> commands) {
|
||||
dynamicCommands.registerAll(commands);
|
||||
public void onCommandRegistration(List<Command> commands, CommandsManager<LocalPlayer> manager) {
|
||||
List<CommandInfo> toRegister = new ArrayList<CommandInfo>();
|
||||
for (Command command : commands) {
|
||||
String[] permissions = null;
|
||||
Method cmdMethod = manager.getMethods().get(null).get(command.aliases()[0]);
|
||||
if (cmdMethod != null && cmdMethod.isAnnotationPresent(CommandPermissions.class)) {
|
||||
permissions = cmdMethod.getAnnotation(CommandPermissions.class).value();
|
||||
}
|
||||
|
||||
toRegister.add(new CommandInfo(command.usage(), command.desc(), command.aliases(), manager, permissions));
|
||||
}
|
||||
|
||||
dynamicCommands.register(toRegister);
|
||||
}
|
||||
|
||||
public void unregisterCommands() {
|
||||
|
Loading…
Reference in New Issue
Block a user