From c95fc06b828fb6be09e501542e1dfe63b624d937 Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Tue, 23 Apr 2019 19:32:27 -0700 Subject: [PATCH] Port scripting commands --- .../worldedit/command/ScriptingCommands.java | 61 ++++++++++--------- .../platform/PlatformCommandMananger.java | 8 ++- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ScriptingCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ScriptingCommands.java index d09c76c39..e60a3db51 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ScriptingCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ScriptingCommands.java @@ -19,23 +19,28 @@ package com.sk89q.worldedit.command; -import static com.google.common.base.Preconditions.checkNotNull; -import static com.sk89q.worldedit.command.util.Logging.LogMode.ALL; - -import com.sk89q.minecraft.util.commands.Command; -import com.sk89q.minecraft.util.commands.CommandContext; -import com.sk89q.minecraft.util.commands.CommandPermissions; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.command.util.CommandPermissions; +import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator; import com.sk89q.worldedit.command.util.Logging; import com.sk89q.worldedit.entity.Player; +import org.enginehub.piston.annotation.Command; +import org.enginehub.piston.annotation.CommandContainer; +import org.enginehub.piston.annotation.param.Arg; import java.io.File; +import java.util.List; +import java.util.stream.Stream; + +import static com.google.common.base.Preconditions.checkNotNull; +import static com.sk89q.worldedit.command.util.Logging.LogMode.ALL; /** * Commands related to scripting. */ +@CommandContainer(superTypes = CommandPermissionsConditionGenerator.Registration.class) public class ScriptingCommands { private final WorldEdit worldEdit; @@ -51,43 +56,40 @@ public class ScriptingCommands { } @Command( - aliases = { "cs" }, - usage = " [args...]", - desc = "Execute a CraftScript", - min = 1, - max = -1 + name = "cs", + desc = "Execute a CraftScript" ) @CommandPermissions("worldedit.scripting.execute") @Logging(ALL) - public void execute(Player player, LocalSession session, CommandContext args) throws WorldEditException { - - String[] scriptArgs = args.getSlice(1); - String name = args.getString(0); - - if (!player.hasPermission("worldedit.scripting.execute." + name)) { + public void execute(Player player, LocalSession session, + @Arg(desc = "Filename of the CraftScript to load") + String filename, + @Arg(desc = "Arguments to the CraftScript", def = "", variable = true) + List args) throws WorldEditException { + if (!player.hasPermission("worldedit.scripting.execute." + filename)) { player.printError("You don't have permission to use that script."); return; } - session.setLastScript(name); + session.setLastScript(filename); File dir = worldEdit.getWorkingDirectoryFile(worldEdit.getConfiguration().scriptsDir); - File f = worldEdit.getSafeOpenFile(player, dir, name, "js", "js"); + File f = worldEdit.getSafeOpenFile(player, dir, filename, "js", "js"); - worldEdit.runScript(player, f, scriptArgs); + worldEdit.runScript(player, f, Stream.concat(Stream.of(filename), args.stream()) + .toArray(String[]::new)); } @Command( - aliases = { ".s" }, - usage = "[args...]", - desc = "Execute last CraftScript", - min = 0, - max = -1 + name = ".s", + desc = "Execute last CraftScript" ) @CommandPermissions("worldedit.scripting.execute") @Logging(ALL) - public void executeLast(Player player, LocalSession session, CommandContext args) throws WorldEditException { - + public void executeLast(Player player, LocalSession session, + @Arg(desc = "Arguments to the CraftScript", def = "", variable = true) + List args) throws WorldEditException { + String lastScript = session.getLastScript(); if (!player.hasPermission("worldedit.scripting.execute." + lastScript)) { @@ -100,11 +102,10 @@ public class ScriptingCommands { return; } - String[] scriptArgs = args.getSlice(0); - File dir = worldEdit.getWorkingDirectoryFile(worldEdit.getConfiguration().scriptsDir); File f = worldEdit.getSafeOpenFile(player, dir, lastScript, "js", "js"); - worldEdit.runScript(player, f, scriptArgs); + worldEdit.runScript(player, f, Stream.concat(Stream.of(lastScript), args.stream()) + .toArray(String[]::new)); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandMananger.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandMananger.java index 7dd59b9f2..a0a94591d 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandMananger.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandMananger.java @@ -46,6 +46,8 @@ import com.sk89q.worldedit.command.RegionCommands; import com.sk89q.worldedit.command.RegionCommandsRegistration; import com.sk89q.worldedit.command.SchematicCommands; import com.sk89q.worldedit.command.SchematicCommandsRegistration; +import com.sk89q.worldedit.command.ScriptingCommands; +import com.sk89q.worldedit.command.ScriptingCommandsRegistration; import com.sk89q.worldedit.command.argument.Arguments; import com.sk89q.worldedit.command.argument.CommaSeparatedValuesConverter; import com.sk89q.worldedit.command.argument.DirectionConverter; @@ -298,13 +300,17 @@ public final class PlatformCommandMananger { RegionCommandsRegistration.builder(), new RegionCommands() ); + register( + commandManager, + ScriptingCommandsRegistration.builder(), + new ScriptingCommands(worldEdit) + ); // Unported commands are below. Delete once they're added to the main manager above. /* dispatcher = new CommandGraph() .builder(builder) .commands() - .registerMethods(new ScriptingCommands(worldEdit)) .registerMethods(new SelectionCommands(worldEdit)) .registerMethods(new SnapshotUtilCommands(worldEdit)) .registerMethods(new ToolUtilCommands(worldEdit))