Merge commit '2af8c8dbf2d5eb16d7492fd12b4b786d6205d0f9' as 'lib'

This commit is contained in:
Charlie
2014-07-31 11:37:54 -04:00
46 changed files with 3570 additions and 0 deletions

58
lib/bukkit/pom.xml Normal file
View File

@@ -0,0 +1,58 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sk89q</groupId>
<artifactId>command-framework-parent</artifactId>
<version>0.5-SNAPSHOT</version>
</parent>
<artifactId>command-framework-bukkit</artifactId>
<packaging>jar</packaging>
<version>0.5-SNAPSHOT</version>
<name>Sk89q Command Framework for Bukkit</name>
<description>Supporting classes for running the command framework on Bukkit servers.</description>
<repositories>
<repository>
<id>bukkit-repo</id>
<url>http://repo.bukkit.org/content/groups/public</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.sk89q</groupId>
<artifactId>command-framework-core</artifactId>
<version>0.5-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.4.7-R1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<configuration>
<artifactSet>
<includes>
<include>com.sk89q:command-framework-core</include>
</includes>
</artifactSet>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,12 @@
package com.sk89q.bukkit.util;
import org.bukkit.command.CommandSender;
import com.sk89q.minecraft.util.commands.CommandsManager;
public class BukkitCommandsManager extends CommandsManager<CommandSender> {
@Override
public boolean hasPermission(CommandSender player, String perm) {
return player.hasPermission(perm);
}
}

View File

@@ -0,0 +1,54 @@
package com.sk89q.bukkit.util;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import com.sk89q.minecraft.util.commands.WrappedCommandSender;
public class BukkitWrappedCommandSender implements WrappedCommandSender {
public BukkitWrappedCommandSender(CommandSender wrapped) {
this.wrapped = wrapped;
}
@Override
public String getName() {
return this.getName();
}
@Override
public void sendMessage(String message) {
this.wrapped.sendMessage(message);
}
@Override
public void sendMessage(String[] messages) {
this.wrapped.sendMessage(messages);
}
@Override
public boolean hasPermission(String permission) {
return this.wrapped.hasPermission(permission);
}
@Override
public Type getType() {
if (this.wrapped instanceof ConsoleCommandSender) {
return Type.CONSOLE;
} else if (this.wrapped instanceof Player) {
return Type.PLAYER;
} else if (this.wrapped instanceof BlockCommandSender) {
return Type.BLOCK;
} else {
return Type.UNKNOWN;
}
}
@Override
public Object getCommandSender() {
return this.wrapped;
}
private final CommandSender wrapped;
}

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

View File

@@ -0,0 +1,112 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2011 sk89q <http://www.sk89q.com> and contributors
*
* 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 java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.sk89q.util.ReflectionUtil;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandMap;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.plugin.Plugin;
/**
* @author zml2008
*/
public class CommandRegistration {
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) {
this(plugin, plugin);
}
public CommandRegistration(Plugin plugin, CommandExecutor executor) {
this.plugin = plugin;
this.executor = executor;
}
public boolean register(List<CommandInfo> registered) {
CommandMap commandMap = getCommandMap();
if (registered == null || commandMap == null) {
return false;
}
for (CommandInfo command : registered) {
DynamicPluginCommand cmd = new DynamicPluginCommand(command.getAliases(),
command.getDesc(), "/" + command.getAliases()[0] + " " + command.getUsage(), executor, command.getRegisteredWith(), plugin);
cmd.setPermissions(command.getPermissions());
commandMap.register(plugin.getDescription().getName(), cmd);
}
return true;
}
public CommandMap getCommandMap() {
CommandMap commandMap = ReflectionUtil.getField(plugin.getServer().getPluginManager(), "commandMap");
if (commandMap == null) {
if (fallbackCommands != null) {
commandMap = fallbackCommands;
} else {
Bukkit.getServer().getLogger().severe(plugin.getDescription().getName() +
": Could not retrieve server CommandMap, using fallback instead! Please report to http://redmine.sk89q.com");
fallbackCommands = commandMap = new SimpleCommandMap(Bukkit.getServer());
Bukkit.getServer().getPluginManager().registerEvents(new FallbackRegistrationListener(fallbackCommands), plugin);
}
}
return commandMap;
}
public boolean unregisterCommands() {
CommandMap commandMap = getCommandMap();
List<String> toRemove = new ArrayList<String>();
Map<String, org.bukkit.command.Command> knownCommands = ReflectionUtil.getField(commandMap, "knownCommands");
Set<String> aliases = ReflectionUtil.getField(commandMap, "aliases");
if (knownCommands == null || aliases == null) {
return false;
}
for (Iterator<org.bukkit.command.Command> i = knownCommands.values().iterator(); i.hasNext();) {
org.bukkit.command.Command cmd = i.next();
if (cmd instanceof DynamicPluginCommand && ((DynamicPluginCommand) cmd).getOwner().equals(executor)) {
i.remove();
for (String alias : cmd.getAliases()) {
org.bukkit.command.Command aliasCmd = knownCommands.get(alias);
if (cmd.equals(aliasCmd)) {
aliases.remove(alias);
toRemove.add(alias);
}
}
}
}
for (String string : toRemove) {
knownCommands.remove(string);
}
return true;
}
}

View File

@@ -0,0 +1,66 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2011 sk89q <http://www.sk89q.com> and contributors
*
* 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.Command;
import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.sk89q.minecraft.util.commands.CommandsManager;
import org.bukkit.command.CommandExecutor;
import org.bukkit.plugin.Plugin;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* @author zml2008
*/
public class CommandsManagerRegistration extends CommandRegistration {
protected CommandsManager<?> commands;
public CommandsManagerRegistration(Plugin plugin, CommandsManager<?> commands) {
super(plugin);
this.commands = commands;
}
public CommandsManagerRegistration(Plugin plugin, CommandExecutor executor, CommandsManager<?> commands) {
super(plugin, executor);
this.commands = commands;
}
public boolean register(Class<?> clazz) {
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);
}
}

View File

@@ -0,0 +1,98 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2011 sk89q <http://www.sk89q.com> and contributors
*
* 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.util.StringUtil;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.plugin.Plugin;
import java.util.Arrays;
/**
* @author zml2008
*/
public class DynamicPluginCommand extends org.bukkit.command.Command implements PluginIdentifiableCommand {
protected final CommandExecutor owner;
protected final Object registeredWith;
protected final Plugin owningPlugin;
protected String[] permissions = new String[0];
public DynamicPluginCommand(String[] aliases, String desc, String usage, CommandExecutor owner, Object registeredWith, Plugin plugin) {
super(aliases[0], desc, usage, Arrays.asList(aliases));
this.owner = owner;
this.owningPlugin = plugin;
this.registeredWith = registeredWith;
}
@Override
public boolean execute(CommandSender sender, String label, String[] args) {
return owner.onCommand(sender, this, label, 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;
}
@Override
public Plugin getPlugin() {
return owningPlugin;
}
@SuppressWarnings("unchecked")
@Override
public boolean testPermissionSilent(CommandSender sender) {
if (permissions == null || permissions.length == 0) {
return true;
}
if (registeredWith instanceof CommandsManager<?>) {
try {
for (String permission : permissions) {
if (((CommandsManager<CommandSender>) registeredWith).hasPermission(sender, permission)) {
return true;
}
}
return false;
} catch (Throwable ignore) {
}
}
return super.testPermissionSilent(sender);
}
}

View File

@@ -0,0 +1,126 @@
/*
* 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 org.bukkit.ChatColor;
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();
String fullTextTemp = null;
StringBuilder fullText = new StringBuilder();
if (cmd.getRegisteredWith() instanceof CommandsManager) {
Map<String, String> helpText = ((CommandsManager<?>) cmd.getRegisteredWith()).getHelpMessages();
final String lookupName = cmd.getName().replaceAll("/", "");
if (helpText.containsKey(lookupName)) { // We have full help text for this command
fullTextTemp = helpText.get(lookupName);
}
// No full help text, assemble help text from info
helpText = ((CommandsManager<?>) cmd.getRegisteredWith()).getCommands();
if (helpText.containsKey(cmd.getName())) {
final String shortText = helpText.get(cmd.getName());
if (fullTextTemp == null) {
fullTextTemp = this.name + " " + shortText;
}
this.shortText = shortText;
}
} else {
this.shortText = cmd.getDescription();
}
// Put the usage in the format: Usage string (newline) Aliases (newline) Help text
String[] split = fullTextTemp == null ? new String[2] : fullTextTemp.split("\n", 2);
fullText.append(ChatColor.BOLD).append(ChatColor.GOLD).append("Usage: ").append(ChatColor.WHITE);
fullText.append(split[0]).append("\n");
if (cmd.getAliases().size() > 0) {
fullText.append(ChatColor.BOLD).append(ChatColor.GOLD).append("Aliases: ").append(ChatColor.WHITE);
boolean first = true;
for (String alias : cmd.getAliases()) {
if (!first) {
fullText.append(", ");
}
fullText.append(alias);
first = false;
}
fullText.append("\n");
}
if (split.length > 1) {
fullText.append(split[1]);
}
this.fullText = fullText.toString();
}
@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
}
}
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);
}
}
}

View File

@@ -0,0 +1,44 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2011 sk89q <http://www.sk89q.com> and contributors
*
* 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 org.bukkit.command.CommandMap;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
/**
* @author zml2008
*/
public class FallbackRegistrationListener implements Listener {
private final CommandMap commandRegistration;
public FallbackRegistrationListener(CommandMap commandRegistration) {
this.commandRegistration = commandRegistration;
}
@EventHandler(ignoreCancelled = true)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
if (commandRegistration.dispatch(event.getPlayer(), event.getMessage())) {
event.setCancelled(true);
}
}
}