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
Dieser Commit ist enthalten in:
zml2008 2012-03-09 23:11:51 -08:00
Ursprung 956b3dd02f
Commit 4328be282c
9 geänderte Dateien mit 310 neuen und 72 gelöschten Zeilen

Datei anzeigen

@ -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;
}
}

Datei anzeigen

@ -25,7 +25,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.util.ReflectionUtil; import com.sk89q.util.ReflectionUtil;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -37,8 +36,13 @@ import org.bukkit.plugin.Plugin;
* @author zml2008 * @author zml2008
*/ */
public class CommandRegistration { 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; private CommandMap fallbackCommands;
public CommandRegistration(Plugin plugin) { public CommandRegistration(Plugin plugin) {
@ -50,14 +54,16 @@ public class CommandRegistration {
this.executor = executor; this.executor = executor;
} }
public boolean registerAll(List<Command> registered) { public boolean register(List<CommandInfo> registered) {
CommandMap commandMap = getCommandMap(); CommandMap commandMap = getCommandMap();
if (registered == null || commandMap == null) { if (registered == null || commandMap == null) {
return false; return false;
} }
for (Command command : registered) { for (CommandInfo command : registered) {
commandMap.register(plugin.getDescription().getName(), DynamicPluginCommand cmd = new DynamicPluginCommand(command.getAliases(),
new DynamicPluginCommand(command.aliases(), command.desc(), "/" + command.aliases()[0] + " " + command.usage(), executor)); command.getDesc(), "/" + command.getAliases()[0] + " " + command.getUsage(), executor, command.getRegisteredWith());
cmd.setPermissions(command.getPermissions());
commandMap.register(plugin.getDescription().getName(), cmd);
} }
return true; return true;
} }

Datei anzeigen

@ -20,10 +20,14 @@
package com.sk89q.bukkit.util; package com.sk89q.bukkit.util;
import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.sk89q.minecraft.util.commands.CommandsManager; import com.sk89q.minecraft.util.commands.CommandsManager;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandMap;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@ -43,7 +47,21 @@ public class CommandsManagerRegistration extends CommandRegistration {
} }
public boolean register(Class<?> clazz) { public boolean register(Class<?> clazz) {
List<Command> registered = commands.registerAndReturn(clazz); return registerAll(commands.registerAndReturn(clazz));
return registerAll(registered); }
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);
} }
} }

Datei anzeigen

@ -19,6 +19,7 @@
package com.sk89q.bukkit.util; package com.sk89q.bukkit.util;
import com.sk89q.util.StringUtil;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import java.util.Arrays; import java.util.Arrays;
@ -29,10 +30,13 @@ import java.util.Arrays;
public class DynamicPluginCommand extends org.bukkit.command.Command { public class DynamicPluginCommand extends org.bukkit.command.Command {
protected final CommandExecutor owner; 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)); super(aliases[0], desc, usage, Arrays.asList(aliases));
this.owner = owner; this.owner = owner;
this.registeredWith = registeredWith;
} }
@Override @Override
@ -43,4 +47,19 @@ public class DynamicPluginCommand extends org.bukkit.command.Command {
public Object getOwner() { public Object getOwner() {
return owner; 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;
}
} }

Datei anzeigen

@ -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);
}
}
}

Datei anzeigen

@ -35,12 +35,8 @@ public class FallbackRegistrationListener implements Listener {
this.commandRegistration = commandRegistration; this.commandRegistration = commandRegistration;
} }
@EventHandler @EventHandler(ignoreCancelled = true)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
if (event.isCancelled()) {
return;
}
if (commandRegistration.dispatch(event.getPlayer(), event.getMessage())) { if (commandRegistration.dispatch(event.getPlayer(), event.getMessage())) {
event.setCancelled(true); event.setCancelled(true);
} }

Datei anzeigen

@ -20,6 +20,7 @@
package com.sk89q.worldedit; package com.sk89q.worldedit;
import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandsManager;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -32,16 +33,16 @@ public abstract class ServerInterface {
/** /**
* Resolves an item name to its ID. * Resolves an item name to its ID.
* *
* @param name * @param name The name to look up
* @return * @return The id that corresponds to the name, or -1 if no such ID exists
*/ */
public abstract int resolveItem(String name); public abstract int resolveItem(String name);
/** /**
* Checks if a mob type is valid. * Checks if a mob type is valid.
* *
* @param type * @param type The mob type name to check
* @return * @return Whether the name is a valid mod bype
*/ */
public abstract boolean isValidMobType(String type); public abstract boolean isValidMobType(String type);
@ -67,7 +68,12 @@ public abstract class ServerInterface {
return Collections.emptyList(); return Collections.emptyList();
} }
@Deprecated
public void onCommandRegistration(List<Command> commands) { public void onCommandRegistration(List<Command> commands) {
// Do nothing :) // Do nothing :)
} }
public void onCommandRegistration(List<Command> commands, CommandsManager<LocalPlayer> manager) {
onCommandRegistration(commands);
}
} }

Datei anzeigen

@ -185,19 +185,19 @@ public class WorldEdit {
commands.setInjector(new SimpleInjector(this)); commands.setInjector(new SimpleInjector(this));
server.onCommandRegistration(commands.registerAndReturn(ChunkCommands.class)); server.onCommandRegistration(commands.registerAndReturn(ChunkCommands.class), commands);
server.onCommandRegistration(commands.registerAndReturn(ClipboardCommands.class)); server.onCommandRegistration(commands.registerAndReturn(ClipboardCommands.class), commands);
server.onCommandRegistration(commands.registerAndReturn(GeneralCommands.class)); server.onCommandRegistration(commands.registerAndReturn(GeneralCommands.class), commands);
server.onCommandRegistration(commands.registerAndReturn(GenerationCommands.class)); server.onCommandRegistration(commands.registerAndReturn(GenerationCommands.class), commands);
server.onCommandRegistration(commands.registerAndReturn(HistoryCommands.class)); server.onCommandRegistration(commands.registerAndReturn(HistoryCommands.class), commands);
server.onCommandRegistration(commands.registerAndReturn(NavigationCommands.class)); server.onCommandRegistration(commands.registerAndReturn(NavigationCommands.class), commands);
server.onCommandRegistration(commands.registerAndReturn(RegionCommands.class)); server.onCommandRegistration(commands.registerAndReturn(RegionCommands.class), commands);
server.onCommandRegistration(commands.registerAndReturn(ScriptingCommands.class)); server.onCommandRegistration(commands.registerAndReturn(ScriptingCommands.class), commands);
server.onCommandRegistration(commands.registerAndReturn(SelectionCommands.class)); server.onCommandRegistration(commands.registerAndReturn(SelectionCommands.class), commands);
server.onCommandRegistration(commands.registerAndReturn(SnapshotUtilCommands.class)); server.onCommandRegistration(commands.registerAndReturn(SnapshotUtilCommands.class), commands);
server.onCommandRegistration(commands.registerAndReturn(ToolUtilCommands.class)); server.onCommandRegistration(commands.registerAndReturn(ToolUtilCommands.class), commands);
server.onCommandRegistration(commands.registerAndReturn(ToolCommands.class)); server.onCommandRegistration(commands.registerAndReturn(ToolCommands.class), commands);
server.onCommandRegistration(commands.registerAndReturn(UtilityCommands.class)); server.onCommandRegistration(commands.registerAndReturn(UtilityCommands.class), commands);
} }
/** /**

Datei anzeigen

@ -19,12 +19,17 @@
package com.sk89q.worldedit.bukkit; package com.sk89q.worldedit.bukkit;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.sk89q.bukkit.util.CommandInfo;
import com.sk89q.bukkit.util.CommandRegistration; import com.sk89q.bukkit.util.CommandRegistration;
import com.sk89q.minecraft.util.commands.Command; 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.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Server; import org.bukkit.Server;
@ -80,8 +85,19 @@ public class BukkitServerInterface extends ServerInterface {
} }
@Override @Override
public void onCommandRegistration(List<Command> commands) { public void onCommandRegistration(List<Command> commands, CommandsManager<LocalPlayer> manager) {
dynamicCommands.registerAll(commands); 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() { public void unregisterCommands() {