Fix CLI Mess (#1811)

* Fix CLI awfulness

* Fix indentation

* Licenses
This commit is contained in:
Octavia Togami 2021-07-01 09:44:44 -07:00 committed by GitHub
parent fbb047a0c1
commit 0790e6e5d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 119 additions and 12 deletions

View File

@ -9,6 +9,9 @@
addJarManifest(WorldEditKind.Standalone("com.sk89q.worldedit.cli.CLIWorldEdit"))
dependencies {
"compileOnly"(project(":worldedit-libs:core:ap"))
"annotationProcessor"(project(":worldedit-libs:core:ap"))
"annotationProcessor"("com.google.guava:guava:${Versions.GUAVA}")
"api"(project(":worldedit-core"))
"implementation"(platform("org.apache.logging.log4j:log4j-bom:2.14.1") {
because("We control Log4J on this platform")

View File

@ -0,0 +1,62 @@
/*
* 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 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 <https://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.cli;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.regions.selector.CuboidRegionSelector;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.task.Task;
import com.sk89q.worldedit.world.World;
import org.enginehub.piston.annotation.Command;
import org.enginehub.piston.annotation.CommandContainer;
import java.util.concurrent.ExecutionException;
@CommandContainer
public class CLIExtraCommands {
@Command(
name = "selectworld",
desc = "Select the entire world"
)
public void selectWorld(Actor actor, World world, LocalSession session) {
session.setRegionSelector(world, new CuboidRegionSelector(
world, world.getMinimumPoint(), world.getMaximumPoint()
));
actor.printInfo(TextComponent.of("Selected the entire world."));
}
@Command(
name = "await",
desc = "Await all pending tasks"
)
public void await() {
for (Task<?> task : WorldEdit.getInstance().getSupervisor().getTasks()) {
try {
task.get();
} catch (InterruptedException e) {
WorldEdit.logger.warn("Interrupted awaiting task", e);
} catch (ExecutionException e) {
WorldEdit.logger.warn("Error awaiting task", e);
}
}
}
}

View File

@ -19,18 +19,23 @@
package com.sk89q.worldedit.cli;
import com.google.common.collect.ImmutableList;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.cli.data.FileRegistries;
import com.sk89q.worldedit.cli.schematic.ClipboardWorld;
import com.sk89q.worldedit.event.platform.CommandEvent;
import com.sk89q.worldedit.event.platform.PlatformReadyEvent;
import com.sk89q.worldedit.event.platform.PlatformsRegisteredEvent;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.extension.platform.PlatformCommandManager;
import com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
import com.sk89q.worldedit.internal.Constants;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
@ -87,10 +92,29 @@ public CLIWorldEdit() {
private void setupPlatform() {
WorldEdit.getInstance().getPlatformManager().register(platform);
registerCommands();
config = new CLIConfiguration(this);
// There's no other platforms, so fire this immediately
WorldEdit.getInstance().getEventBus().post(new PlatformsRegisteredEvent());
this.fileRegistries = new FileRegistries(this);
this.fileRegistries.loadDataFiles();
}
private void registerCommands() {
PlatformCommandManager pcm = WorldEdit.getInstance().getPlatformManager()
.getPlatformCommandManager();
pcm.registerSubCommands(
"cli",
ImmutableList.of(),
"CLI-specific commands",
CLIExtraCommandsRegistration.builder(),
new CLIExtraCommands()
);
}
public void setupRegistries() {
// Blocks
for (Map.Entry<String, FileRegistries.BlockManifest> manifestEntry : fileRegistries.getDataFile().blocks.entrySet()) {
@ -172,7 +196,6 @@ public void onStarted() {
setupRegistries();
WorldEdit.getInstance().loadMappings();
config = new CLIConfiguration(this);
config.load();
WorldEdit.getInstance().getEventBus().post(new PlatformReadyEvent(platform));
@ -235,8 +258,15 @@ public void saveAllWorlds(boolean force) {
public void run(InputStream inputStream) {
try (Scanner scanner = new Scanner(inputStream)) {
while (scanner.hasNextLine()) {
while (true) {
System.err.print("> ");
if (!scanner.hasNextLine()) {
break;
}
String line = scanner.nextLine();
if (line.isEmpty()) {
continue;
}
if (line.equals("stop")) {
commandSender.printInfo(TranslatableComponent.of("worldedit.cli.stopping"));
break;
@ -280,31 +310,40 @@ public static void main(String[] args) {
if (file == null) {
throw new IllegalArgumentException("A file must be provided!");
}
LOGGER.info(() -> "Loading '" + file + "'...");
if (file.getName().endsWith("level.dat")) {
throw new IllegalArgumentException("level.dat file support is unfinished.");
} else {
ClipboardFormat format = ClipboardFormats.findByFile(file);
if (format != null) {
ClipboardReader dataVersionReader = format
.getReader(Files.newInputStream(file.toPath(), StandardOpenOption.READ));
int dataVersion = dataVersionReader.getDataVersion()
int dataVersion;
if (format != BuiltInClipboardFormat.MCEDIT_SCHEMATIC) {
try (ClipboardReader dataVersionReader = format.getReader(
Files.newInputStream(file.toPath(), StandardOpenOption.READ)
)) {
dataVersion = dataVersionReader.getDataVersion()
.orElseThrow(() -> new IllegalArgumentException("Failed to obtain data version from schematic."));
dataVersionReader.close();
}
} else {
dataVersion = Constants.DATA_VERSION_MC_1_13_2;
}
app.platform.setDataVersion(dataVersion);
app.onStarted();
ClipboardWorld world;
try (ClipboardReader clipboardReader = format.getReader(Files.newInputStream(file.toPath(), StandardOpenOption.READ))) {
ClipboardWorld world = new ClipboardWorld(
world = new ClipboardWorld(
file,
clipboardReader.read(),
file.getName()
);
}
app.platform.addWorld(world);
WorldEdit.getInstance().getSessionManager().get(app.commandSender).setWorldOverride(world);
}
} else {
throw new IllegalArgumentException("Unknown file provided!");
}
}
LOGGER.info(() -> "Loaded '" + file + "'");
String scriptFile = cmd.getOptionValue('s');
if (scriptFile != null) {

View File

@ -273,7 +273,10 @@ private void registerAlwaysInjectedValues() {
});
}
private <CI> void registerSubCommands(String name, List<String> aliases, String desc,
/**
* Internal use only.
*/
public <CI> void registerSubCommands(String name, List<String> aliases, String desc,
CommandRegistration<CI> registration, CI instance) {
registerSubCommands(name, aliases, desc, registration, instance, m -> {
});