2014-04-05 03:53:58 +00:00
|
|
|
/*
|
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser 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 Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.sk89q.worldedit.extension.platform;
|
|
|
|
|
2014-06-28 08:42:59 +00:00
|
|
|
import com.google.common.base.Joiner;
|
2014-06-27 23:03:29 +00:00
|
|
|
import com.sk89q.minecraft.util.commands.CommandException;
|
|
|
|
import com.sk89q.minecraft.util.commands.CommandLocals;
|
|
|
|
import com.sk89q.minecraft.util.commands.CommandPermissionsException;
|
|
|
|
import com.sk89q.minecraft.util.commands.WrappedCommandException;
|
|
|
|
import com.sk89q.worldedit.EditSession;
|
|
|
|
import com.sk89q.worldedit.LocalConfiguration;
|
|
|
|
import com.sk89q.worldedit.LocalSession;
|
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
2014-04-05 03:53:58 +00:00
|
|
|
import com.sk89q.worldedit.command.*;
|
|
|
|
import com.sk89q.worldedit.event.platform.CommandEvent;
|
2014-06-28 23:30:02 +00:00
|
|
|
import com.sk89q.worldedit.event.platform.CommandSuggestionEvent;
|
|
|
|
import com.sk89q.worldedit.internal.command.ActorAuthorizer;
|
2014-06-28 01:29:11 +00:00
|
|
|
import com.sk89q.worldedit.internal.command.CommandLoggingHandler;
|
2014-06-28 00:59:23 +00:00
|
|
|
import com.sk89q.worldedit.internal.command.WorldEditBinding;
|
|
|
|
import com.sk89q.worldedit.internal.command.WorldEditExceptionConverter;
|
2014-06-28 08:42:59 +00:00
|
|
|
import com.sk89q.worldedit.session.request.Request;
|
2014-06-27 23:03:29 +00:00
|
|
|
import com.sk89q.worldedit.util.command.Dispatcher;
|
|
|
|
import com.sk89q.worldedit.util.command.InvalidUsageException;
|
|
|
|
import com.sk89q.worldedit.util.command.fluent.CommandGraph;
|
|
|
|
import com.sk89q.worldedit.util.command.parametric.LegacyCommandsHandler;
|
|
|
|
import com.sk89q.worldedit.util.command.parametric.ParametricBuilder;
|
2014-04-05 03:53:58 +00:00
|
|
|
import com.sk89q.worldedit.util.eventbus.Subscribe;
|
|
|
|
import com.sk89q.worldedit.util.logging.DynamicStreamHandler;
|
2014-06-27 23:03:29 +00:00
|
|
|
import com.sk89q.worldedit.util.logging.LogFormat;
|
2014-04-05 03:53:58 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2014-06-27 23:03:29 +00:00
|
|
|
import java.util.logging.FileHandler;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2014-04-05 03:53:58 +00:00
|
|
|
|
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the registration and invocation of commands.
|
|
|
|
* </p>
|
|
|
|
* This class is primarily for internal usage.
|
|
|
|
*/
|
|
|
|
public final class CommandManager {
|
|
|
|
|
|
|
|
private static final Logger logger = Logger.getLogger(CommandManager.class.getCanonicalName());
|
|
|
|
private static final java.util.regex.Pattern numberFormatExceptionPattern = java.util.regex.Pattern.compile("^For input string: \"(.*)\"$");
|
|
|
|
|
|
|
|
private final WorldEdit worldEdit;
|
2014-06-28 03:12:44 +00:00
|
|
|
private final PlatformManager platformManager;
|
2014-06-27 23:03:29 +00:00
|
|
|
private final Dispatcher dispatcher;
|
2014-04-05 03:53:58 +00:00
|
|
|
private final DynamicStreamHandler dynamicHandler = new DynamicStreamHandler();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance.
|
|
|
|
*
|
|
|
|
* @param worldEdit the WorldEdit instance
|
|
|
|
*/
|
2014-06-28 03:12:44 +00:00
|
|
|
CommandManager(final WorldEdit worldEdit, PlatformManager platformManager) {
|
2014-04-05 03:53:58 +00:00
|
|
|
checkNotNull(worldEdit);
|
2014-06-28 03:12:44 +00:00
|
|
|
checkNotNull(platformManager);
|
2014-04-05 03:53:58 +00:00
|
|
|
this.worldEdit = worldEdit;
|
2014-06-28 03:12:44 +00:00
|
|
|
this.platformManager = platformManager;
|
2014-04-05 03:53:58 +00:00
|
|
|
|
|
|
|
// Register this instance for command events
|
|
|
|
worldEdit.getEventBus().register(this);
|
|
|
|
|
|
|
|
// Setup the logger
|
|
|
|
logger.addHandler(dynamicHandler);
|
|
|
|
dynamicHandler.setFormatter(new LogFormat());
|
|
|
|
|
|
|
|
// Set up the commands manager
|
2014-06-27 23:03:29 +00:00
|
|
|
ParametricBuilder builder = new ParametricBuilder();
|
2014-06-28 23:30:02 +00:00
|
|
|
builder.setAuthorizer(new ActorAuthorizer());
|
2014-06-27 23:03:29 +00:00
|
|
|
builder.addBinding(new WorldEditBinding(worldEdit));
|
2014-06-28 23:30:02 +00:00
|
|
|
builder.addExceptionConverter(new WorldEditExceptionConverter(worldEdit));
|
|
|
|
builder.addInvokeListener(new LegacyCommandsHandler());
|
|
|
|
builder.addInvokeListener(new CommandLoggingHandler(worldEdit, logger));
|
2014-06-27 23:03:29 +00:00
|
|
|
|
|
|
|
dispatcher = new CommandGraph()
|
|
|
|
.builder(builder)
|
|
|
|
.commands()
|
2014-06-28 23:39:20 +00:00
|
|
|
.registerMethods(new BiomeCommands(worldEdit))
|
|
|
|
.registerMethods(new ChunkCommands(worldEdit))
|
|
|
|
.registerMethods(new ClipboardCommands(worldEdit))
|
|
|
|
.registerMethods(new GeneralCommands(worldEdit))
|
|
|
|
.registerMethods(new GenerationCommands(worldEdit))
|
|
|
|
.registerMethods(new HistoryCommands(worldEdit))
|
|
|
|
.registerMethods(new NavigationCommands(worldEdit))
|
|
|
|
.registerMethods(new RegionCommands(worldEdit))
|
|
|
|
.registerMethods(new ScriptingCommands(worldEdit))
|
|
|
|
.registerMethods(new SelectionCommands(worldEdit))
|
|
|
|
.registerMethods(new SnapshotUtilCommands(worldEdit))
|
|
|
|
.registerMethods(new ToolUtilCommands(worldEdit))
|
|
|
|
.registerMethods(new ToolCommands(worldEdit))
|
|
|
|
.registerMethods(new UtilityCommands(worldEdit))
|
2014-06-27 23:03:29 +00:00
|
|
|
.group("worldedit", "we")
|
2014-06-28 23:39:20 +00:00
|
|
|
.describeAs("WorldEdit commands")
|
|
|
|
.registerMethods(new WorldEditCommands(worldEdit))
|
2014-06-27 23:03:29 +00:00
|
|
|
.parent()
|
|
|
|
.group("schematic", "schem", "/schematic", "/schem")
|
2014-06-28 23:39:20 +00:00
|
|
|
.describeAs("Schematic commands for saving/loading areas")
|
|
|
|
.registerMethods(new SchematicCommands(worldEdit))
|
2014-06-27 23:03:29 +00:00
|
|
|
.parent()
|
|
|
|
.group("snapshot", "snap")
|
2014-06-28 23:39:20 +00:00
|
|
|
.describeAs("Schematic commands for saving/loading areas")
|
|
|
|
.registerMethods(new SnapshotCommands(worldEdit))
|
2014-06-27 23:03:29 +00:00
|
|
|
.parent()
|
|
|
|
.group("brush", "br")
|
2014-06-28 23:39:20 +00:00
|
|
|
.describeAs("Brushing commands")
|
|
|
|
.registerMethods(new BrushCommands(worldEdit))
|
2014-06-27 23:03:29 +00:00
|
|
|
.parent()
|
|
|
|
.group("superpickaxe", "pickaxe", "sp")
|
2014-06-28 23:39:20 +00:00
|
|
|
.describeAs("Super-pickaxe commands")
|
|
|
|
.registerMethods(new SuperPickaxeCommands(worldEdit))
|
2014-06-27 23:03:29 +00:00
|
|
|
.parent()
|
|
|
|
.group("tool")
|
2014-06-28 23:39:20 +00:00
|
|
|
.describeAs("Bind functions to held items")
|
|
|
|
.registerMethods(new ToolCommands(worldEdit))
|
2014-06-27 23:03:29 +00:00
|
|
|
.parent()
|
|
|
|
.graph()
|
|
|
|
.getDispatcher();
|
2014-04-05 03:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void register(Platform platform) {
|
|
|
|
logger.log(Level.FINE, "Registering commands with " + platform.getClass().getCanonicalName());
|
|
|
|
|
|
|
|
LocalConfiguration config = platform.getConfiguration();
|
|
|
|
boolean logging = config.logCommands;
|
|
|
|
String path = config.logFile;
|
|
|
|
|
|
|
|
// Register log
|
|
|
|
if (!logging || path.isEmpty()) {
|
|
|
|
dynamicHandler.setHandler(null);
|
2014-06-28 01:28:31 +00:00
|
|
|
logger.setLevel(Level.OFF);
|
2014-04-05 03:53:58 +00:00
|
|
|
} else {
|
|
|
|
File file = new File(config.getWorkingDirectory(), path);
|
|
|
|
|
2014-06-28 01:28:31 +00:00
|
|
|
logger.setLevel(Level.ALL);
|
|
|
|
|
2014-04-05 03:53:58 +00:00
|
|
|
logger.log(Level.INFO, "Logging WorldEdit commands to " + file.getAbsolutePath());
|
|
|
|
|
|
|
|
try {
|
|
|
|
dynamicHandler.setHandler(new FileHandler(file.getAbsolutePath(), true));
|
|
|
|
} catch (IOException e) {
|
|
|
|
logger.log(Level.WARNING, "Could not use command log file " + path + ": " + e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 23:03:29 +00:00
|
|
|
platform.registerCommands(dispatcher);
|
2014-04-05 03:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void unregister() {
|
|
|
|
dynamicHandler.setHandler(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String[] commandDetection(String[] split) {
|
|
|
|
// 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
|
2014-06-28 23:30:02 +00:00
|
|
|
if (!dispatcher.contains(searchCmd)) {
|
|
|
|
if (worldEdit.getConfiguration().noDoubleSlash && dispatcher.contains("/" + searchCmd)) {
|
|
|
|
split[0] = "/" + split[0];
|
|
|
|
} else if (searchCmd.length() >= 2 && searchCmd.charAt(0) == '/' && dispatcher.contains(searchCmd.substring(1))) {
|
|
|
|
split[0] = split[0].substring(1);
|
|
|
|
}
|
2014-04-05 03:53:58 +00:00
|
|
|
}
|
2014-06-28 23:30:02 +00:00
|
|
|
|
2014-04-05 03:53:58 +00:00
|
|
|
return split;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Subscribe
|
|
|
|
public void handleCommand(CommandEvent event) {
|
|
|
|
Request.reset();
|
|
|
|
|
2014-06-28 03:12:44 +00:00
|
|
|
Actor actor = platformManager.createProxyActor(event.getActor());
|
2014-06-28 23:30:02 +00:00
|
|
|
String split[] = commandDetection(event.getArguments().split(" "));
|
2014-04-05 03:53:58 +00:00
|
|
|
|
2014-06-27 23:03:29 +00:00
|
|
|
// No command found!
|
|
|
|
if (!dispatcher.contains(split[0])) {
|
|
|
|
return;
|
|
|
|
}
|
2014-04-05 03:53:58 +00:00
|
|
|
|
2014-06-27 23:03:29 +00:00
|
|
|
LocalSession session = worldEdit.getSessionManager().get(actor);
|
|
|
|
LocalConfiguration config = worldEdit.getConfiguration();
|
2014-04-05 03:53:58 +00:00
|
|
|
|
2014-06-27 23:03:29 +00:00
|
|
|
CommandLocals locals = new CommandLocals();
|
|
|
|
locals.put(Actor.class, actor);
|
2014-04-05 03:53:58 +00:00
|
|
|
|
2014-06-27 23:03:29 +00:00
|
|
|
long start = System.currentTimeMillis();
|
2014-04-05 03:53:58 +00:00
|
|
|
|
2014-06-27 23:03:29 +00:00
|
|
|
try {
|
2014-06-29 00:04:38 +00:00
|
|
|
dispatcher.call(Joiner.on(" ").join(split), locals, new String[0]);
|
2014-06-27 23:03:29 +00:00
|
|
|
} catch (CommandPermissionsException e) {
|
|
|
|
actor.printError("You don't have permission to do this.");
|
|
|
|
} catch (InvalidUsageException e) {
|
|
|
|
actor.printError(e.getMessage() + "\nUsage: " + e.getUsage("/"));
|
|
|
|
} catch (WrappedCommandException e) {
|
|
|
|
Throwable t = e.getCause();
|
|
|
|
actor.printError("Please report this error: [See console]");
|
|
|
|
actor.printRaw(t.getClass().getName() + ": " + t.getMessage());
|
|
|
|
t.printStackTrace();
|
|
|
|
} catch (CommandException e) {
|
|
|
|
actor.printError(e.getMessage());
|
|
|
|
} finally {
|
|
|
|
EditSession editSession = locals.get(EditSession.class);
|
|
|
|
|
|
|
|
if (editSession != null) {
|
2014-04-05 03:53:58 +00:00
|
|
|
session.remember(editSession);
|
|
|
|
editSession.flushQueue();
|
|
|
|
|
2014-06-27 23:03:29 +00:00
|
|
|
if (config.profile) {
|
2014-04-05 03:53:58 +00:00
|
|
|
long time = System.currentTimeMillis() - start;
|
|
|
|
int changed = editSession.getBlockChangeCount();
|
|
|
|
if (time > 0) {
|
|
|
|
double throughput = changed / (time / 1000.0);
|
2014-06-27 23:03:29 +00:00
|
|
|
actor.printDebug((time / 1000.0) + "s elapsed (history: "
|
2014-04-05 03:53:58 +00:00
|
|
|
+ changed + " changed; "
|
|
|
|
+ Math.round(throughput) + " blocks/sec).");
|
|
|
|
} else {
|
2014-06-27 23:03:29 +00:00
|
|
|
actor.printDebug((time / 1000.0) + "s elapsed.");
|
2014-04-05 03:53:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-28 00:55:39 +00:00
|
|
|
worldEdit.flushBlockBag(actor, editSession);
|
2014-04-05 03:53:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
|
2014-06-28 23:30:02 +00:00
|
|
|
@Subscribe
|
|
|
|
public void handleCommandSuggestion(CommandSuggestionEvent event) {
|
|
|
|
try {
|
2014-06-28 23:57:11 +00:00
|
|
|
CommandLocals locals = new CommandLocals();
|
|
|
|
locals.put(Actor.class, event.getActor());
|
|
|
|
event.setSuggestions(dispatcher.getSuggestions(event.getArguments(), locals));
|
2014-06-28 23:30:02 +00:00
|
|
|
} catch (CommandException e) {
|
|
|
|
event.getActor().printError(e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 23:03:29 +00:00
|
|
|
/**
|
|
|
|
* Get the command dispatcher instance.
|
|
|
|
*
|
|
|
|
* @return the command dispatcher
|
|
|
|
*/
|
|
|
|
public Dispatcher getDispatcher() {
|
|
|
|
return dispatcher;
|
2014-04-05 03:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Logger getLogger() {
|
|
|
|
return logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|