mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-06-11 20:13:55 +00:00
Added support for dynamic command registration.
This commit is contained in:
96
src/main/java/com/sk89q/bukkit/util/CommandRegistration.java
Normal file
96
src/main/java/com/sk89q/bukkit/util/CommandRegistration.java
Normal file
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* WorldEdit
|
||||
* Copyright (C) 2011 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.Command;
|
||||
import com.sk89q.minecraft.util.commands.CommandsManager;
|
||||
import com.sk89q.util.ReflectionUtil;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandMap;
|
||||
import org.bukkit.command.SimpleCommandMap;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author zml2008
|
||||
*/
|
||||
public class CommandRegistration {
|
||||
private final Plugin plugin;
|
||||
private CommandMap fallbackCommands;
|
||||
|
||||
public CommandRegistration(Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public boolean registerAll(List<Command> registered) {
|
||||
CommandMap commandMap = getCommandMap();
|
||||
if (registered == null || commandMap == null) {
|
||||
return false;
|
||||
}
|
||||
for (Command command : registered) {
|
||||
commandMap.register(plugin.getDescription().getName(), new DynamicPluginCommand(command, plugin));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private CommandMap getCommandMap() {
|
||||
CommandMap commandMap = ReflectionUtil.getField(plugin.getServer().getPluginManager(), "commandMap");
|
||||
if (commandMap == null) {
|
||||
if (fallbackCommands != null) {
|
||||
commandMap = fallbackCommands;
|
||||
} else {
|
||||
Bukkit.getServer().getLogger().warning(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().registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS,
|
||||
new FallbackRegistrationListener(fallbackCommands), Event.Priority.Normal, 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).getPlugin().equals(plugin)) {
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* WorldEdit
|
||||
* Copyright (C) 2011 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.Command;
|
||||
import com.sk89q.minecraft.util.commands.CommandsManager;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
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 boolean register(Class<?> clazz) {
|
||||
List<Command> registered = commands.registerAndReturn(clazz);
|
||||
return registerAll(registered);
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* WorldEdit
|
||||
* Copyright (C) 2011 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.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author zml2008
|
||||
*/
|
||||
public class DynamicPluginCommand extends org.bukkit.command.Command {
|
||||
|
||||
protected final Plugin plugin;
|
||||
|
||||
public DynamicPluginCommand(Command command, Plugin plugin) {
|
||||
super(command.aliases()[0], command.desc(), command.usage(), Arrays.asList(command.aliases()));
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender sender, String label, String[] args) {
|
||||
return plugin.onCommand(sender, this, label, args);
|
||||
}
|
||||
|
||||
public Plugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
}
|
@ -1,3 +1,21 @@
|
||||
/*
|
||||
* WorldEdit
|
||||
* Copyright (C) 2011 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 org.bukkit.command.CommandMap;
|
||||
|
40
src/main/java/com/sk89q/util/ReflectionUtil.java
Normal file
40
src/main/java/com/sk89q/util/ReflectionUtil.java
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2011 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.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
/**
|
||||
* @author zml2008
|
||||
*/
|
||||
public class ReflectionUtil {
|
||||
public static <T> T getField(Object from, String name) {
|
||||
Class<?> checkClass = from.getClass();
|
||||
do {
|
||||
try {
|
||||
Field field = checkClass.getDeclaredField(name);
|
||||
field.setAccessible(true);
|
||||
return (T) field.get(from);
|
||||
} catch (NoSuchFieldException e) {
|
||||
} catch (IllegalAccessException e) {
|
||||
}
|
||||
} while (checkClass.getSuperclass() != Object.class && ((checkClass = checkClass.getSuperclass()) != null));
|
||||
return null;
|
||||
}
|
||||
}
|
@ -19,6 +19,8 @@
|
||||
|
||||
package com.sk89q.worldedit;
|
||||
|
||||
import com.sk89q.minecraft.util.commands.Command;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@ -64,4 +66,8 @@ public abstract class ServerInterface {
|
||||
public List<LocalWorld> getWorlds() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
public void onCommandRegistration(List<Command> commands) {
|
||||
// Do nothing :)
|
||||
}
|
||||
}
|
||||
|
@ -176,19 +176,19 @@ public class WorldEdit {
|
||||
|
||||
commands.setInjector(new SimpleInjector(this));
|
||||
|
||||
commands.register(ChunkCommands.class);
|
||||
commands.register(ClipboardCommands.class);
|
||||
commands.register(GeneralCommands.class);
|
||||
commands.register(GenerationCommands.class);
|
||||
commands.register(HistoryCommands.class);
|
||||
commands.register(NavigationCommands.class);
|
||||
commands.register(RegionCommands.class);
|
||||
commands.register(ScriptingCommands.class);
|
||||
commands.register(SelectionCommands.class);
|
||||
commands.register(SnapshotUtilCommands.class);
|
||||
commands.register(ToolUtilCommands.class);
|
||||
commands.register(ToolCommands.class);
|
||||
commands.register(UtilityCommands.class);
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1222,27 +1222,7 @@ public class WorldEdit {
|
||||
*/
|
||||
public boolean handleCommand(LocalPlayer player, String[] split) {
|
||||
try {
|
||||
split[0] = split[0].substring(1);
|
||||
|
||||
// Quick script shortcut
|
||||
if (split[0].matches("^[^/].*\\.js$")) {
|
||||
String[] newSplit = new String[split.length + 1];
|
||||
System.arraycopy(split, 0, newSplit, 1, split.length);
|
||||
newSplit[0] = "cs";
|
||||
newSplit[1] = newSplit[1];
|
||||
split = newSplit;
|
||||
}
|
||||
|
||||
String searchCmd = split[0].toLowerCase();
|
||||
|
||||
// Try to detect the command
|
||||
if (commands.hasCommand(searchCmd)) {
|
||||
} else if (config.noDoubleSlash && commands.hasCommand("/" + searchCmd)) {
|
||||
split[0] = "/" + split[0];
|
||||
} else if (split[0].length() >= 2 && split[0].charAt(0) == '/'
|
||||
&& commands.hasCommand(searchCmd.substring(1))) {
|
||||
split[0] = split[0].substring(1);
|
||||
}
|
||||
split = commandDetection(split);
|
||||
|
||||
// No command found!
|
||||
if (!commands.hasCommand(split[0])) {
|
||||
@ -1337,6 +1317,31 @@ public class WorldEdit {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public String[] commandDetection(String[] split) {
|
||||
split[0] = split[0].substring(1);
|
||||
|
||||
// Quick script shortcut
|
||||
if (split[0].matches("^[^/].*\\.js$")) {
|
||||
String[] newSplit = new String[split.length + 1];
|
||||
System.arraycopy(split, 0, newSplit, 1, split.length);
|
||||
newSplit[0] = "cs";
|
||||
newSplit[1] = newSplit[1];
|
||||
split = newSplit;
|
||||
}
|
||||
|
||||
String searchCmd = split[0].toLowerCase();
|
||||
|
||||
// Try to detect the command
|
||||
if (commands.hasCommand(searchCmd)) {
|
||||
} else if (config.noDoubleSlash && commands.hasCommand("/" + searchCmd)) {
|
||||
split[0] = "/" + split[0];
|
||||
} else if (split[0].length() >= 2 && split[0].charAt(0) == '/'
|
||||
&& commands.hasCommand(searchCmd.substring(1))) {
|
||||
split[0] = split[0].substring(1);
|
||||
}
|
||||
return split;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a WorldEdit script.
|
||||
|
@ -1,8 +1,6 @@
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@ -31,23 +29,30 @@ public class BukkitCommandSender extends LocalPlayer {
|
||||
|
||||
@Override
|
||||
public void printRaw(String msg) {
|
||||
System.out.println(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printDebug(String msg) {
|
||||
Bukkit.getLogger().log(Level.WARNING, msg);
|
||||
|
||||
for (String part : msg.split("\n")) {
|
||||
sender.sendMessage(part);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(String msg) {
|
||||
Bukkit.getLogger().info(msg);
|
||||
for (String part : msg.split("\n")) {
|
||||
sender.sendMessage("\u00A7d" + part);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printDebug(String msg) {
|
||||
for (String part : msg.split("\n")) {
|
||||
sender.sendMessage("\u00A77" + part);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printError(String msg) {
|
||||
Bukkit.getLogger().log(Level.SEVERE, msg);
|
||||
for (String part : msg.split("\n")) {
|
||||
sender.sendMessage("\u00A7c" + part);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -22,18 +22,30 @@ package com.sk89q.worldedit.bukkit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.sk89q.bukkit.util.CommandRegistration;
|
||||
import com.sk89q.bukkit.util.DynamicPluginCommand;
|
||||
import com.sk89q.bukkit.util.FallbackRegistrationListener;
|
||||
import com.sk89q.util.ReflectionUtil;
|
||||
import com.sk89q.minecraft.util.commands.Command;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.command.CommandMap;
|
||||
import org.bukkit.command.SimpleCommandMap;
|
||||
import org.bukkit.entity.CreatureType;
|
||||
import com.sk89q.worldedit.LocalWorld;
|
||||
import com.sk89q.worldedit.ServerInterface;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
import org.bukkit.event.player.PlayerListener;
|
||||
|
||||
public class BukkitServerInterface extends ServerInterface {
|
||||
public Server server;
|
||||
public WorldEditPlugin plugin;
|
||||
private CommandRegistration dynamicCommands;
|
||||
|
||||
public BukkitServerInterface(WorldEditPlugin plugin, Server server) {
|
||||
this.plugin = plugin;
|
||||
this.server = server;
|
||||
dynamicCommands = new CommandRegistration(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -68,4 +80,13 @@ public class BukkitServerInterface extends ServerInterface {
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommandRegistration(List<Command> commands) {
|
||||
dynamicCommands.registerAll(commands);
|
||||
}
|
||||
|
||||
public void unregisterCommands() {
|
||||
dynamicCommands.unregisterCommands();
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.util.StringUtil;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.event.Event;
|
||||
@ -57,7 +58,7 @@ public class WorldEditPlayerListener extends PlayerListener {
|
||||
|
||||
plugin.registerEvent("PLAYER_QUIT", this);
|
||||
plugin.registerEvent("PLAYER_INTERACT", this);
|
||||
plugin.registerEvent("PLAYER_COMMAND_PREPROCESS", this);
|
||||
plugin.registerEvent("PLAYER_COMMAND_PREPROCESS", this, Event.Priority.Low);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -83,9 +84,12 @@ public class WorldEditPlayerListener extends PlayerListener {
|
||||
|
||||
String[] split = event.getMessage().split(" ");
|
||||
|
||||
if (plugin.getWorldEdit().handleCommand(plugin.wrapPlayer(event.getPlayer()), split)) {
|
||||
event.setCancelled(true);
|
||||
if (split.length > 0) {
|
||||
split = plugin.getWorldEdit().commandDetection(split);
|
||||
split[0] += "/";
|
||||
}
|
||||
|
||||
event.setMessage(StringUtil.joinString(split, " "));
|
||||
}
|
||||
|
||||
private boolean ignoreLeftClickAir = false;
|
||||
|
@ -56,7 +56,7 @@ public class WorldEditPlugin extends JavaPlugin {
|
||||
/**
|
||||
* The server interface that all server-related API goes through.
|
||||
*/
|
||||
private ServerInterface server;
|
||||
private BukkitServerInterface server;
|
||||
/**
|
||||
* Main WorldEdit instance.
|
||||
*/
|
||||
@ -121,6 +121,7 @@ public class WorldEditPlugin extends JavaPlugin {
|
||||
}
|
||||
controller.clearSessions();
|
||||
config.unload();
|
||||
server.unregisterCommands();
|
||||
this.getServer().getScheduler().cancelTasks(this);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user