From 0790e6e5d8b1f9f58afc1e1549f404362716c80c Mon Sep 17 00:00:00 2001 From: Octavia Togami Date: Thu, 1 Jul 2021 09:44:44 -0700 Subject: [PATCH] Fix CLI Mess (#1811) * Fix CLI awfulness * Fix indentation * Licenses --- worldedit-cli/build.gradle.kts | 3 + .../sk89q/worldedit/cli/CLIExtraCommands.java | 62 +++++++++++++++++++ .../com/sk89q/worldedit/cli/CLIWorldEdit.java | 59 +++++++++++++++--- .../platform/PlatformCommandManager.java | 7 ++- 4 files changed, 119 insertions(+), 12 deletions(-) create mode 100644 worldedit-cli/src/main/java/com/sk89q/worldedit/cli/CLIExtraCommands.java diff --git a/worldedit-cli/build.gradle.kts b/worldedit-cli/build.gradle.kts index 306b58963..1f8811d95 100644 --- a/worldedit-cli/build.gradle.kts +++ b/worldedit-cli/build.gradle.kts @@ -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") diff --git a/worldedit-cli/src/main/java/com/sk89q/worldedit/cli/CLIExtraCommands.java b/worldedit-cli/src/main/java/com/sk89q/worldedit/cli/CLIExtraCommands.java new file mode 100644 index 000000000..c3f74f2dc --- /dev/null +++ b/worldedit-cli/src/main/java/com/sk89q/worldedit/cli/CLIExtraCommands.java @@ -0,0 +1,62 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * 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 . + */ + +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); + } + } + } +} diff --git a/worldedit-cli/src/main/java/com/sk89q/worldedit/cli/CLIWorldEdit.java b/worldedit-cli/src/main/java/com/sk89q/worldedit/cli/CLIWorldEdit.java index 8f906b6a2..d52779015 100644 --- a/worldedit-cli/src/main/java/com/sk89q/worldedit/cli/CLIWorldEdit.java +++ b/worldedit-cli/src/main/java/com/sk89q/worldedit/cli/CLIWorldEdit.java @@ -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 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() - .orElseThrow(() -> new IllegalArgumentException("Failed to obtain data version from schematic.")); - dataVersionReader.close(); + 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.")); + } + } 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); } + 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) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java index abb6ba69f..79a358dc3 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java @@ -273,8 +273,11 @@ private void registerAlwaysInjectedValues() { }); } - private void registerSubCommands(String name, List aliases, String desc, - CommandRegistration registration, CI instance) { + /** + * Internal use only. + */ + public void registerSubCommands(String name, List aliases, String desc, + CommandRegistration registration, CI instance) { registerSubCommands(name, aliases, desc, registration, instance, m -> { }); }