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;
|
|
|
|
|
2019-04-04 23:16:04 +00:00
|
|
|
import com.google.common.collect.ImmutableList;
|
2019-04-23 17:11:54 +00:00
|
|
|
import com.google.common.reflect.TypeToken;
|
2014-06-27 23:03:29 +00:00
|
|
|
import com.sk89q.worldedit.EditSession;
|
2019-04-19 02:11:57 +00:00
|
|
|
import com.sk89q.worldedit.IncompleteRegionException;
|
2014-06-27 23:03:29 +00:00
|
|
|
import com.sk89q.worldedit.LocalConfiguration;
|
|
|
|
import com.sk89q.worldedit.LocalSession;
|
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
2018-07-30 13:26:06 +00:00
|
|
|
import com.sk89q.worldedit.command.BiomeCommands;
|
2019-04-15 08:21:15 +00:00
|
|
|
import com.sk89q.worldedit.command.BiomeCommandsRegistration;
|
2019-04-15 09:01:25 +00:00
|
|
|
import com.sk89q.worldedit.command.BrushCommands;
|
|
|
|
import com.sk89q.worldedit.command.BrushCommandsRegistration;
|
2019-04-17 06:02:23 +00:00
|
|
|
import com.sk89q.worldedit.command.ChunkCommands;
|
|
|
|
import com.sk89q.worldedit.command.ChunkCommandsRegistration;
|
2019-04-18 19:34:57 +00:00
|
|
|
import com.sk89q.worldedit.command.ClipboardCommands;
|
|
|
|
import com.sk89q.worldedit.command.ClipboardCommandsRegistration;
|
2019-04-21 20:10:20 +00:00
|
|
|
import com.sk89q.worldedit.command.GeneralCommands;
|
|
|
|
import com.sk89q.worldedit.command.GeneralCommandsRegistration;
|
2019-04-23 17:11:54 +00:00
|
|
|
import com.sk89q.worldedit.command.GenerationCommands;
|
|
|
|
import com.sk89q.worldedit.command.GenerationCommandsRegistration;
|
2019-04-23 22:35:05 +00:00
|
|
|
import com.sk89q.worldedit.command.HistoryCommands;
|
|
|
|
import com.sk89q.worldedit.command.HistoryCommandsRegistration;
|
2018-07-30 13:26:06 +00:00
|
|
|
import com.sk89q.worldedit.command.SchematicCommands;
|
2019-04-04 23:16:04 +00:00
|
|
|
import com.sk89q.worldedit.command.SchematicCommandsRegistration;
|
2019-04-15 08:21:15 +00:00
|
|
|
import com.sk89q.worldedit.command.argument.Arguments;
|
2019-04-23 17:11:54 +00:00
|
|
|
import com.sk89q.worldedit.command.argument.CommaSeparatedValuesConverter;
|
2019-04-19 02:11:57 +00:00
|
|
|
import com.sk89q.worldedit.command.argument.DirectionConverter;
|
|
|
|
import com.sk89q.worldedit.command.argument.MaskConverter;
|
2019-04-23 17:11:54 +00:00
|
|
|
import com.sk89q.worldedit.command.argument.PatternConverter;
|
2019-04-04 23:16:04 +00:00
|
|
|
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
|
2019-04-15 08:21:15 +00:00
|
|
|
import com.sk89q.worldedit.command.util.PermissionCondition;
|
2019-03-16 23:04:24 +00:00
|
|
|
import com.sk89q.worldedit.entity.Entity;
|
2019-04-15 08:21:15 +00:00
|
|
|
import com.sk89q.worldedit.entity.Player;
|
2014-04-05 03:53:58 +00:00
|
|
|
import com.sk89q.worldedit.event.platform.CommandEvent;
|
2014-06-28 23:30:02 +00:00
|
|
|
import com.sk89q.worldedit.event.platform.CommandSuggestionEvent;
|
2019-03-16 23:04:24 +00:00
|
|
|
import com.sk89q.worldedit.extent.Extent;
|
2019-04-19 02:11:57 +00:00
|
|
|
import com.sk89q.worldedit.internal.annotation.Selection;
|
2018-07-30 13:26:06 +00:00
|
|
|
import com.sk89q.worldedit.internal.command.ActorAuthorizer;
|
2019-04-17 06:02:23 +00:00
|
|
|
import com.sk89q.worldedit.internal.command.CommandLoggingHandler;
|
2018-07-30 13:26:06 +00:00
|
|
|
import com.sk89q.worldedit.internal.command.UserCommandCompleter;
|
|
|
|
import com.sk89q.worldedit.internal.command.WorldEditBinding;
|
|
|
|
import com.sk89q.worldedit.internal.command.WorldEditExceptionConverter;
|
2019-04-19 02:11:57 +00:00
|
|
|
import com.sk89q.worldedit.regions.Region;
|
2014-06-28 08:42:59 +00:00
|
|
|
import com.sk89q.worldedit.session.request.Request;
|
2015-10-27 06:14:30 +00:00
|
|
|
import com.sk89q.worldedit.util.command.parametric.ExceptionConverter;
|
2014-06-27 23:03:29 +00:00
|
|
|
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;
|
2019-03-16 23:04:24 +00:00
|
|
|
import com.sk89q.worldedit.world.World;
|
2019-04-15 08:21:15 +00:00
|
|
|
import org.enginehub.piston.Command;
|
|
|
|
import org.enginehub.piston.CommandManager;
|
2019-04-04 23:16:04 +00:00
|
|
|
import org.enginehub.piston.DefaultCommandManagerService;
|
2019-04-23 17:11:54 +00:00
|
|
|
import org.enginehub.piston.converter.ArgumentConverters;
|
2019-04-15 08:21:15 +00:00
|
|
|
import org.enginehub.piston.exception.CommandException;
|
|
|
|
import org.enginehub.piston.exception.CommandExecutionException;
|
|
|
|
import org.enginehub.piston.exception.ConditionFailedException;
|
|
|
|
import org.enginehub.piston.exception.UsageException;
|
2019-04-17 06:02:23 +00:00
|
|
|
import org.enginehub.piston.gen.CommandCallListener;
|
|
|
|
import org.enginehub.piston.gen.CommandRegistration;
|
|
|
|
import org.enginehub.piston.inject.InjectedValueStore;
|
2019-04-18 19:34:57 +00:00
|
|
|
import org.enginehub.piston.inject.Key;
|
2019-04-17 06:02:23 +00:00
|
|
|
import org.enginehub.piston.inject.MapBackedValueStore;
|
|
|
|
import org.enginehub.piston.inject.MemoizingValueAccess;
|
2019-04-19 02:59:53 +00:00
|
|
|
import org.enginehub.piston.inject.MergedValueAccess;
|
2019-04-15 08:21:15 +00:00
|
|
|
import org.enginehub.piston.part.SubCommandPart;
|
|
|
|
import org.enginehub.piston.util.ValueProvider;
|
2019-03-14 02:51:48 +00:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2014-04-05 03:53:58 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2019-04-17 06:02:23 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2019-04-15 08:21:15 +00:00
|
|
|
import java.util.Optional;
|
2014-06-27 23:03:29 +00:00
|
|
|
import java.util.logging.FileHandler;
|
|
|
|
import java.util.logging.Level;
|
2014-07-01 06:17:44 +00:00
|
|
|
import java.util.regex.Pattern;
|
2019-04-15 08:21:15 +00:00
|
|
|
import java.util.stream.Collectors;
|
2014-04-05 03:53:58 +00:00
|
|
|
|
2019-04-04 23:16:04 +00:00
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
|
|
|
|
2014-04-05 03:53:58 +00:00
|
|
|
/**
|
|
|
|
* Handles the registration and invocation of commands.
|
2014-07-26 07:29:12 +00:00
|
|
|
*
|
|
|
|
* <p>This class is primarily for internal usage.</p>
|
2014-04-05 03:53:58 +00:00
|
|
|
*/
|
2019-04-15 08:21:15 +00:00
|
|
|
public final class PlatformCommandMananger {
|
2014-04-05 03:53:58 +00:00
|
|
|
|
2014-07-01 06:17:44 +00:00
|
|
|
public static final Pattern COMMAND_CLEAN_PATTERN = Pattern.compile("^[/]+");
|
2019-04-15 08:21:15 +00:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(PlatformCommandMananger.class);
|
2019-03-14 02:51:48 +00:00
|
|
|
private static final java.util.logging.Logger commandLog =
|
2019-04-15 08:21:15 +00:00
|
|
|
java.util.logging.Logger.getLogger(PlatformCommandMananger.class.getCanonicalName() + ".CommandLog");
|
2015-10-27 06:14:30 +00:00
|
|
|
private static final Pattern numberFormatExceptionPattern = Pattern.compile("^For input string: \"(.*)\"$");
|
2019-04-17 06:02:23 +00:00
|
|
|
private static final CommandPermissionsConditionGenerator PERM_GEN = new CommandPermissionsConditionGenerator();
|
2014-04-05 03:53:58 +00:00
|
|
|
|
|
|
|
private final WorldEdit worldEdit;
|
2014-06-28 03:12:44 +00:00
|
|
|
private final PlatformManager platformManager;
|
2019-04-15 08:21:15 +00:00
|
|
|
private final CommandManager commandManager;
|
2019-04-19 02:59:53 +00:00
|
|
|
private final InjectedValueStore globalInjectedValues;
|
2014-04-05 03:53:58 +00:00
|
|
|
private final DynamicStreamHandler dynamicHandler = new DynamicStreamHandler();
|
2019-04-19 02:11:57 +00:00
|
|
|
private final WorldEditExceptionConverter exceptionConverter;
|
2019-04-17 06:02:23 +00:00
|
|
|
private final List<CommandCallListener> callListeners;
|
2014-04-05 03:53:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance.
|
|
|
|
*
|
|
|
|
* @param worldEdit the WorldEdit instance
|
|
|
|
*/
|
2019-04-15 08:21:15 +00:00
|
|
|
PlatformCommandMananger(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;
|
2015-10-27 06:14:30 +00:00
|
|
|
this.exceptionConverter = new WorldEditExceptionConverter(worldEdit);
|
2019-04-15 08:21:15 +00:00
|
|
|
this.commandManager = DefaultCommandManagerService.getInstance()
|
|
|
|
.newCommandManager();
|
2019-04-19 02:59:53 +00:00
|
|
|
this.globalInjectedValues = MapBackedValueStore.create();
|
2019-04-17 06:02:23 +00:00
|
|
|
this.callListeners = Collections.singletonList(
|
|
|
|
new CommandLoggingHandler(worldEdit, commandLog)
|
|
|
|
);
|
|
|
|
// setup separate from main constructor
|
|
|
|
// ensures that everything is definitely assigned
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
private <CI> void register(CommandManager manager, CommandRegistration<CI> registration, CI instance) {
|
|
|
|
registration.containerInstance(instance)
|
|
|
|
.commandManager(manager)
|
|
|
|
.listeners(callListeners);
|
|
|
|
if (registration instanceof CommandPermissionsConditionGenerator.Registration) {
|
|
|
|
((CommandPermissionsConditionGenerator.Registration) registration).commandPermissionsConditionGenerator(
|
|
|
|
PERM_GEN
|
|
|
|
);
|
|
|
|
}
|
|
|
|
registration.build();
|
|
|
|
}
|
2014-04-05 03:53:58 +00:00
|
|
|
|
2019-04-17 06:02:23 +00:00
|
|
|
private void initialize() {
|
2014-04-05 03:53:58 +00:00
|
|
|
// Register this instance for command events
|
|
|
|
worldEdit.getEventBus().register(this);
|
|
|
|
|
|
|
|
// Setup the logger
|
2014-07-15 05:50:45 +00:00
|
|
|
commandLog.addHandler(dynamicHandler);
|
2014-04-05 03:53:58 +00:00
|
|
|
|
|
|
|
// 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-07-07 00:22:49 +00:00
|
|
|
builder.setDefaultCompleter(new UserCommandCompleter(platformManager));
|
2014-06-27 23:03:29 +00:00
|
|
|
builder.addBinding(new WorldEditBinding(worldEdit));
|
2014-06-28 23:30:02 +00:00
|
|
|
builder.addInvokeListener(new LegacyCommandsHandler());
|
2014-06-27 23:03:29 +00:00
|
|
|
|
2019-04-19 02:11:57 +00:00
|
|
|
registerAlwaysInjectedValues();
|
|
|
|
registerArgumentConverters();
|
|
|
|
registerAllCommands();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void registerArgumentConverters() {
|
|
|
|
DirectionConverter.register(worldEdit, commandManager);
|
|
|
|
MaskConverter.register(worldEdit, commandManager);
|
2019-04-23 17:11:54 +00:00
|
|
|
PatternConverter.register(worldEdit, commandManager);
|
|
|
|
for (int count = 2; count <= 3; count++) {
|
|
|
|
commandManager.registerConverter(Key.of(double.class, Annotations.radii(count)),
|
|
|
|
CommaSeparatedValuesConverter.wrapAndLimit(ArgumentConverters.get(
|
|
|
|
TypeToken.of(double.class)
|
|
|
|
), count)
|
|
|
|
);
|
|
|
|
}
|
2019-04-19 02:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void registerAlwaysInjectedValues() {
|
2019-04-19 02:59:53 +00:00
|
|
|
globalInjectedValues.injectValue(Key.of(Region.class, Selection.class),
|
2019-04-19 02:11:57 +00:00
|
|
|
context -> {
|
|
|
|
LocalSession localSession = context.injectedValue(Key.of(LocalSession.class))
|
|
|
|
.orElseThrow(() -> new IllegalStateException("No LocalSession"));
|
|
|
|
return context.injectedValue(Key.of(Player.class))
|
|
|
|
.map(player -> {
|
|
|
|
try {
|
|
|
|
return localSession.getSelection(player.getWorld());
|
|
|
|
} catch (IncompleteRegionException e) {
|
|
|
|
exceptionConverter.convert(e);
|
|
|
|
throw new AssertionError("Should have thrown a new exception.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-04-19 02:59:53 +00:00
|
|
|
globalInjectedValues.injectValue(Key.of(EditSession.class),
|
2019-04-19 02:11:57 +00:00
|
|
|
context -> {
|
|
|
|
LocalSession localSession = context.injectedValue(Key.of(LocalSession.class))
|
|
|
|
.orElseThrow(() -> new IllegalStateException("No LocalSession"));
|
|
|
|
return context.injectedValue(Key.of(Player.class))
|
|
|
|
.map(player -> {
|
|
|
|
EditSession editSession = localSession.createEditSession(player);
|
|
|
|
editSession.enableStandardMode();
|
|
|
|
return editSession;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void registerAllCommands() {
|
2019-04-15 08:21:15 +00:00
|
|
|
commandManager.register("schematic", cmd -> {
|
|
|
|
cmd.aliases(ImmutableList.of("schem", "/schematic", "/schem"));
|
|
|
|
cmd.description("Schematic commands for saving/loading areas");
|
|
|
|
cmd.action(Command.Action.NULL_ACTION);
|
|
|
|
|
|
|
|
CommandManager manager = DefaultCommandManagerService.getInstance()
|
|
|
|
.newCommandManager();
|
2019-04-17 06:02:23 +00:00
|
|
|
register(
|
|
|
|
manager,
|
|
|
|
SchematicCommandsRegistration.builder(),
|
|
|
|
new SchematicCommands(worldEdit)
|
|
|
|
);
|
2019-04-15 08:21:15 +00:00
|
|
|
|
|
|
|
cmd.addPart(SubCommandPart.builder("action", "Sub-command to run.")
|
|
|
|
.withCommands(manager.getAllCommands().collect(Collectors.toList()))
|
|
|
|
.required()
|
2019-04-15 09:01:25 +00:00
|
|
|
.build());
|
|
|
|
});
|
|
|
|
commandManager.register("brush", cmd -> {
|
|
|
|
cmd.aliases(ImmutableList.of("br"));
|
|
|
|
cmd.description("Brushing commands");
|
|
|
|
cmd.action(Command.Action.NULL_ACTION);
|
|
|
|
|
|
|
|
CommandManager manager = DefaultCommandManagerService.getInstance()
|
|
|
|
.newCommandManager();
|
2019-04-17 06:02:23 +00:00
|
|
|
register(
|
|
|
|
manager,
|
|
|
|
BrushCommandsRegistration.builder(),
|
|
|
|
new BrushCommands(worldEdit)
|
|
|
|
);
|
2019-04-15 09:01:25 +00:00
|
|
|
|
|
|
|
cmd.addPart(SubCommandPart.builder("action", "Sub-command to run.")
|
|
|
|
.withCommands(manager.getAllCommands().collect(Collectors.toList()))
|
|
|
|
.required()
|
|
|
|
.build());
|
2019-04-15 08:21:15 +00:00
|
|
|
});
|
2019-04-17 06:02:23 +00:00
|
|
|
register(
|
|
|
|
commandManager,
|
|
|
|
BiomeCommandsRegistration.builder(),
|
|
|
|
new BiomeCommands()
|
|
|
|
);
|
|
|
|
register(
|
|
|
|
commandManager,
|
|
|
|
ChunkCommandsRegistration.builder(),
|
|
|
|
new ChunkCommands(worldEdit)
|
|
|
|
);
|
2019-04-18 19:34:57 +00:00
|
|
|
register(
|
|
|
|
commandManager,
|
|
|
|
ClipboardCommandsRegistration.builder(),
|
|
|
|
new ClipboardCommands()
|
|
|
|
);
|
2019-04-21 20:10:20 +00:00
|
|
|
register(
|
|
|
|
commandManager,
|
|
|
|
GeneralCommandsRegistration.builder(),
|
|
|
|
new GeneralCommands(worldEdit)
|
|
|
|
);
|
2019-04-23 17:11:54 +00:00
|
|
|
register(
|
|
|
|
commandManager,
|
|
|
|
GenerationCommandsRegistration.builder(),
|
|
|
|
new GenerationCommands(worldEdit)
|
|
|
|
);
|
2019-04-23 22:35:05 +00:00
|
|
|
register(
|
|
|
|
commandManager,
|
|
|
|
HistoryCommandsRegistration.builder(),
|
|
|
|
new HistoryCommands(worldEdit)
|
|
|
|
);
|
2019-04-15 08:21:15 +00:00
|
|
|
|
|
|
|
// Unported commands are below. Delete once they're added to the main manager above.
|
|
|
|
/*
|
2014-06-27 23:03:29 +00:00
|
|
|
dispatcher = new CommandGraph()
|
|
|
|
.builder(builder)
|
|
|
|
.commands()
|
2014-06-28 23:39:20 +00:00
|
|
|
.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))
|
2015-10-30 01:20:27 +00:00
|
|
|
.register(adapt(new SelectionCommand(new ApplyCommand(new ReplaceParser(), "Set all blocks within selection"), "worldedit.region.set")), "/set")
|
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("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")
|
2015-10-29 20:05:04 +00:00
|
|
|
.register(adapt(new ShapedBrushCommand(new DeformCommand(), "worldedit.brush.deform")), "deform")
|
|
|
|
.register(adapt(new ShapedBrushCommand(new ApplyCommand(new ReplaceParser(), "Set all blocks within region"), "worldedit.brush.set")), "set")
|
|
|
|
.register(adapt(new ShapedBrushCommand(new PaintCommand(), "worldedit.brush.paint")), "paint")
|
|
|
|
.register(adapt(new ShapedBrushCommand(new ApplyCommand(), "worldedit.brush.apply")), "apply")
|
2015-10-29 20:16:22 +00:00
|
|
|
.register(adapt(new ShapedBrushCommand(new PaintCommand(new TreeGeneratorParser("treeType")), "worldedit.brush.forest")), "forest")
|
2015-10-28 20:50:07 +00:00
|
|
|
.register(adapt(new ShapedBrushCommand(ProvidedValue.create(new Deform("y-=1", Mode.RAW_COORD), "Raise one block"), "worldedit.brush.raise")), "raise")
|
|
|
|
.register(adapt(new ShapedBrushCommand(ProvidedValue.create(new Deform("y+=1", Mode.RAW_COORD), "Lower one block"), "worldedit.brush.lower")), "lower")
|
2015-10-27 06:14:30 +00:00
|
|
|
.parent()
|
2014-06-27 23:03:29 +00:00
|
|
|
.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();
|
2019-04-15 08:21:15 +00:00
|
|
|
*/
|
2019-04-04 23:16:04 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 06:14:30 +00:00
|
|
|
public ExceptionConverter getExceptionConverter() {
|
|
|
|
return exceptionConverter;
|
|
|
|
}
|
|
|
|
|
2014-04-05 03:53:58 +00:00
|
|
|
void register(Platform platform) {
|
2019-03-14 02:51:48 +00:00
|
|
|
log.info("Registering commands with " + platform.getClass().getCanonicalName());
|
2014-04-05 03:53:58 +00:00
|
|
|
|
|
|
|
LocalConfiguration config = platform.getConfiguration();
|
|
|
|
boolean logging = config.logCommands;
|
|
|
|
String path = config.logFile;
|
|
|
|
|
|
|
|
// Register log
|
|
|
|
if (!logging || path.isEmpty()) {
|
|
|
|
dynamicHandler.setHandler(null);
|
2014-07-15 05:50:45 +00:00
|
|
|
commandLog.setLevel(Level.OFF);
|
2014-04-05 03:53:58 +00:00
|
|
|
} else {
|
|
|
|
File file = new File(config.getWorkingDirectory(), path);
|
2014-07-15 05:50:45 +00:00
|
|
|
commandLog.setLevel(Level.ALL);
|
2014-04-05 03:53:58 +00:00
|
|
|
|
2019-03-14 02:51:48 +00:00
|
|
|
log.info("Logging WorldEdit commands to " + file.getAbsolutePath());
|
2014-04-05 03:53:58 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
dynamicHandler.setHandler(new FileHandler(file.getAbsolutePath(), true));
|
|
|
|
} catch (IOException e) {
|
2019-03-14 02:51:48 +00:00
|
|
|
log.warn("Could not use command log file " + path + ": " + e.getMessage());
|
2014-04-05 03:53:58 +00:00
|
|
|
}
|
2018-05-19 15:47:45 +00:00
|
|
|
|
|
|
|
dynamicHandler.setFormatter(new LogFormat(config.logFormat));
|
2014-04-05 03:53:58 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 08:21:15 +00:00
|
|
|
platform.registerCommands(commandManager);
|
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
|
2019-04-15 08:21:15 +00:00
|
|
|
if (!commandManager.containsCommand(searchCmd)) {
|
|
|
|
if (worldEdit.getConfiguration().noDoubleSlash && commandManager.containsCommand("/" + searchCmd)) {
|
2014-06-28 23:30:02 +00:00
|
|
|
split[0] = "/" + split[0];
|
2019-04-15 08:21:15 +00:00
|
|
|
} else if (searchCmd.length() >= 2 && searchCmd.charAt(0) == '/' && commandManager.containsCommand(searchCmd.substring(1))) {
|
2014-06-28 23:30:02 +00:00
|
|
|
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-07-26 07:29:12 +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!
|
2019-04-15 08:21:15 +00:00
|
|
|
if (!commandManager.containsCommand(split[0])) {
|
2014-06-27 23:03:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-04-05 03:53:58 +00:00
|
|
|
|
2014-06-27 23:03:29 +00:00
|
|
|
LocalSession session = worldEdit.getSessionManager().get(actor);
|
2019-03-16 23:04:24 +00:00
|
|
|
Request.request().setSession(session);
|
|
|
|
if (actor instanceof Entity) {
|
|
|
|
Extent extent = ((Entity) actor).getExtent();
|
|
|
|
if (extent instanceof World) {
|
|
|
|
Request.request().setWorld(((World) extent));
|
|
|
|
}
|
|
|
|
}
|
2014-06-27 23:03:29 +00:00
|
|
|
LocalConfiguration config = worldEdit.getConfiguration();
|
2014-04-05 03:53:58 +00:00
|
|
|
|
2019-04-17 06:02:23 +00:00
|
|
|
InjectedValueStore store = MapBackedValueStore.create();
|
2019-04-18 19:34:57 +00:00
|
|
|
store.injectValue(Key.of(Actor.class), ValueProvider.constant(actor));
|
2019-04-17 06:02:23 +00:00
|
|
|
if (actor instanceof Player) {
|
2019-04-18 19:34:57 +00:00
|
|
|
store.injectValue(Key.of(Player.class), ValueProvider.constant((Player) actor));
|
2019-04-17 06:02:23 +00:00
|
|
|
}
|
2019-04-18 19:34:57 +00:00
|
|
|
store.injectValue(Key.of(Arguments.class), ValueProvider.constant(event::getArguments));
|
|
|
|
store.injectValue(Key.of(LocalSession.class),
|
2019-04-17 06:02:23 +00:00
|
|
|
context -> {
|
|
|
|
LocalSession localSession = worldEdit.getSessionManager().get(actor);
|
|
|
|
localSession.tellVersion(actor);
|
|
|
|
return Optional.of(localSession);
|
|
|
|
});
|
|
|
|
|
2019-04-19 02:59:53 +00:00
|
|
|
MemoizingValueAccess context = MemoizingValueAccess.wrap(
|
|
|
|
MergedValueAccess.of(store, globalInjectedValues)
|
|
|
|
);
|
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 {
|
2016-06-28 23:36:34 +00:00
|
|
|
// This is a bit of a hack, since the call method can only throw CommandExceptions
|
|
|
|
// everything needs to be wrapped at least once. Which means to handle all WorldEdit
|
|
|
|
// exceptions without writing a hook into every dispatcher, we need to unwrap these
|
|
|
|
// exceptions and rethrow their converted form, if their is one.
|
|
|
|
try {
|
2019-04-17 06:02:23 +00:00
|
|
|
commandManager.execute(context, ImmutableList.copyOf(split));
|
2016-06-28 23:36:34 +00:00
|
|
|
} catch (Throwable t) {
|
|
|
|
// Use the exception converter to convert the exception if any of its causes
|
|
|
|
// can be converted, otherwise throw the original exception
|
|
|
|
Throwable next = t;
|
|
|
|
do {
|
|
|
|
exceptionConverter.convert(next);
|
|
|
|
next = next.getCause();
|
|
|
|
} while (next != null);
|
|
|
|
|
|
|
|
throw t;
|
|
|
|
}
|
2019-04-15 08:21:15 +00:00
|
|
|
} catch (ConditionFailedException e) {
|
|
|
|
if (e.getCondition() instanceof PermissionCondition) {
|
|
|
|
actor.printError("You are not permitted to do that. Are you in the right mode?");
|
2014-07-01 05:48:04 +00:00
|
|
|
}
|
2019-04-15 08:21:15 +00:00
|
|
|
} catch (UsageException e) {
|
|
|
|
String message = e.getMessage();
|
|
|
|
actor.printError(message != null ? message : "The command was not used properly (no more help available).");
|
|
|
|
} catch (CommandExecutionException e) {
|
2014-06-27 23:03:29 +00:00
|
|
|
Throwable t = e.getCause();
|
|
|
|
actor.printError("Please report this error: [See console]");
|
|
|
|
actor.printRaw(t.getClass().getName() + ": " + t.getMessage());
|
2019-03-14 02:51:48 +00:00
|
|
|
log.error("An unexpected error while handling a WorldEdit command", t);
|
2014-06-27 23:03:29 +00:00
|
|
|
} catch (CommandException e) {
|
2014-07-10 06:05:40 +00:00
|
|
|
String message = e.getMessage();
|
|
|
|
if (message != null) {
|
|
|
|
actor.printError(e.getMessage());
|
2014-07-10 06:07:40 +00:00
|
|
|
} else {
|
2014-07-10 06:05:40 +00:00
|
|
|
actor.printError("An unknown error has occurred! Please see console.");
|
2019-03-14 02:51:48 +00:00
|
|
|
log.error("An unknown error occurred", e);
|
2014-07-10 06:05:40 +00:00
|
|
|
}
|
2014-06-27 23:03:29 +00:00
|
|
|
} finally {
|
2019-04-17 06:02:23 +00:00
|
|
|
Optional<EditSession> editSessionOpt =
|
2019-04-19 02:59:53 +00:00
|
|
|
context.snapshotMemory().injectedValue(Key.of(EditSession.class));
|
2014-06-27 23:03:29 +00:00
|
|
|
|
2019-04-15 08:21:15 +00:00
|
|
|
if (editSessionOpt.isPresent()) {
|
|
|
|
EditSession editSession = editSessionOpt.get();
|
2014-04-05 03:53:58 +00:00
|
|
|
session.remember(editSession);
|
2018-10-21 01:54:58 +00:00
|
|
|
editSession.flushSession();
|
2014-04-05 03:53:58 +00:00
|
|
|
|
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
|
|
|
}
|
2019-03-16 23:04:24 +00:00
|
|
|
Request.reset();
|
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 {
|
2019-04-19 02:59:53 +00:00
|
|
|
globalInjectedValues.injectValue(Key.of(Actor.class), ValueProvider.constant(event.getActor()));
|
|
|
|
globalInjectedValues.injectValue(Key.of(Arguments.class), ValueProvider.constant(event::getArguments));
|
2019-04-15 08:21:15 +00:00
|
|
|
// TODO suggestions
|
2014-06-28 23:30:02 +00:00
|
|
|
} catch (CommandException e) {
|
|
|
|
event.getActor().printError(e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 23:03:29 +00:00
|
|
|
/**
|
2019-04-15 08:21:15 +00:00
|
|
|
* Get the command manager instance.
|
2014-06-27 23:03:29 +00:00
|
|
|
*
|
2019-04-15 08:21:15 +00:00
|
|
|
* @return the command manager
|
2014-06-27 23:03:29 +00:00
|
|
|
*/
|
2019-04-15 08:21:15 +00:00
|
|
|
public CommandManager getCommandManager() {
|
|
|
|
return commandManager;
|
2014-04-05 03:53:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 02:51:48 +00:00
|
|
|
public static java.util.logging.Logger getLogger() {
|
2014-07-15 05:50:45 +00:00
|
|
|
return commandLog;
|
2014-04-05 03:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|