From f2283e8ad019a342eef8e8863a15a9d2b9842d55 Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Tue, 23 Apr 2019 16:14:21 -0700 Subject: [PATCH] Port navigation commands --- .../worldedit/command/NavigationCommands.java | 126 +++++++++--------- .../platform/PlatformCommandMananger.java | 9 +- 2 files changed, 67 insertions(+), 68 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/NavigationCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/NavigationCommands.java index b2fb9c4cb..0b70e0bd7 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/NavigationCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/NavigationCommands.java @@ -19,23 +19,26 @@ package com.sk89q.worldedit.command; -import static com.google.common.base.Preconditions.checkNotNull; -import static com.sk89q.worldedit.command.util.Logging.LogMode.POSITION; - -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.LocalConfiguration; 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 com.sk89q.worldedit.util.Location; -import com.sk89q.worldedit.util.command.parametric.Optional; +import org.enginehub.piston.annotation.Command; +import org.enginehub.piston.annotation.CommandContainer; +import org.enginehub.piston.annotation.param.Arg; +import org.enginehub.piston.annotation.param.Switch; + +import static com.google.common.base.Preconditions.checkNotNull; +import static com.sk89q.worldedit.command.util.Logging.LogMode.POSITION; /** * Commands for moving the player around. */ +@CommandContainer(superTypes = CommandPermissionsConditionGenerator.Registration.class) public class NavigationCommands { private final WorldEdit worldEdit; @@ -51,80 +54,78 @@ public class NavigationCommands { } @Command( - aliases = { "unstuck", "!" }, - usage = "", - desc = "Escape from being stuck inside a block", - min = 0, - max = 0 + name = "unstuck", + aliases = { "!" }, + desc = "Escape from being stuck inside a block" ) @CommandPermissions("worldedit.navigation.unstuck") public void unstuck(Player player) throws WorldEditException { - player.print("There you go!"); player.findFreePosition(); + player.print("There you go!"); } @Command( - aliases = { "ascend", "asc" }, - usage = "[# of levels]", - desc = "Go up a floor", - min = 0, - max = 1 + name = "ascend", + aliases = { "asc" }, + desc = "Go up a floor" ) @CommandPermissions("worldedit.navigation.ascend") - public void ascend(Player player, @Optional("1") int levelsToAscend) throws WorldEditException { + public void ascend(Player player, + @Arg(desc = "# of levels to ascend", def = "1") + int levels) throws WorldEditException { int ascentLevels = 0; while (player.ascendLevel()) { ++ascentLevels; - if (levelsToAscend == ascentLevels) { + if (levels == ascentLevels) { break; } } if (ascentLevels == 0) { player.printError("No free spot above you found."); } else { - player.print((ascentLevels != 1) ? "Ascended " + Integer.toString(ascentLevels) + " levels." : "Ascended a level."); + player.print((ascentLevels != 1) ? "Ascended " + ascentLevels + " levels." : "Ascended a level."); } } @Command( - aliases = { "descend", "desc" }, - usage = "[# of floors]", - desc = "Go down a floor", - min = 0, - max = 1 + name = "descend", + aliases = { "desc" }, + desc = "Go down a floor" ) @CommandPermissions("worldedit.navigation.descend") - public void descend(Player player, @Optional("1") int levelsToDescend) throws WorldEditException { + public void descend(Player player, + @Arg(desc = "# of levels to descend", def = "1") + int levels) throws WorldEditException { int descentLevels = 0; while (player.descendLevel()) { ++descentLevels; - if (levelsToDescend == descentLevels) { + if (levels == descentLevels) { break; } } if (descentLevels == 0) { player.printError("No free spot below you found."); } else { - player.print((descentLevels != 1) ? "Descended " + Integer.toString(descentLevels) + " levels." : "Descended a level."); + player.print((descentLevels != 1) ? "Descended " + descentLevels + " levels." : "Descended a level."); } } @Command( - aliases = { "ceil" }, - usage = "[clearance]", - desc = "Go to the celing", - flags = "fg", - min = 0, - max = 1 + name = "ceil", + desc = "Go to the ceiling" ) @CommandPermissions("worldedit.navigation.ceiling") @Logging(POSITION) - public void ceiling(Player player, CommandContext args) throws WorldEditException { + public void ceiling(Player player, + @Arg(desc = "# of blocks to leave above you", def = "0") + int clearance, + @Switch(name = 'f', desc = "Force using flight to keep you still") + boolean forceFlight, + @Switch(name = 'g', desc = "Force using glass to keep you still") + boolean forceGlass) throws WorldEditException { + clearance = Math.max(0, clearance); - final int clearance = args.argsLength() > 0 ? - Math.max(0, args.getInteger(0)) : 0; - - final boolean alwaysGlass = getAlwaysGlass(args); + boolean alwaysGlass = getAlwaysGlass(forceFlight, forceGlass); if (player.ascendToCeiling(clearance, alwaysGlass)) { player.print("Whoosh!"); } else { @@ -133,11 +134,8 @@ public class NavigationCommands { } @Command( - aliases = { "thru" }, - usage = "", - desc = "Passthrough walls", - min = 0, - max = 0 + name = "thru", + desc = "Pass through walls" ) @CommandPermissions("worldedit.navigation.thru.command") public void thru(Player player) throws WorldEditException { @@ -149,11 +147,9 @@ public class NavigationCommands { } @Command( - aliases = { "jumpto", "j" }, - usage = "", - desc = "Teleport to a location", - min = 0, - max = 0 + name = "jumpto", + aliases = { "j" }, + desc = "Teleport to a location" ) @CommandPermissions("worldedit.navigation.jumpto.command") public void jumpTo(Player player) throws WorldEditException { @@ -168,19 +164,19 @@ public class NavigationCommands { } @Command( - aliases = { "up" }, - usage = "", - desc = "Go upwards some distance", - flags = "fg", - min = 1, - max = 1 + name = "up", + desc = "Go upwards some distance" ) @CommandPermissions("worldedit.navigation.up") @Logging(POSITION) - public void up(Player player, CommandContext args) throws WorldEditException { - final int distance = args.getInteger(0); - - final boolean alwaysGlass = getAlwaysGlass(args); + public void up(Player player, + @Arg(desc = "Distance to go upwards") + int distance, + @Switch(name = 'f', desc = "Force using flight to keep you still") + boolean forceFlight, + @Switch(name = 'g', desc = "Force using glass to keep you still") + boolean forceGlass) throws WorldEditException { + boolean alwaysGlass = getAlwaysGlass(forceFlight, forceGlass); if (player.ascendUpwards(distance, alwaysGlass)) { player.print("Whoosh!"); } else { @@ -190,16 +186,14 @@ public class NavigationCommands { /** * Helper function for /up and /ceil. - * - * @param args The {@link CommandContext} to extract the flags from. + * + * @param forceFlight if flight should be used, rather than the default config option + * @param forceGlass if glass should always be placed, rather than the default config option * @return true, if glass should always be put under the player */ - private boolean getAlwaysGlass(CommandContext args) { + private boolean getAlwaysGlass(boolean forceFlight, boolean forceGlass) { final LocalConfiguration config = worldEdit.getConfiguration(); - final boolean forceFlight = args.hasFlag('f'); - final boolean forceGlass = args.hasFlag('g'); - return forceGlass || (config.navigationUseGlass && !forceFlight); } } 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 8f0e0a8d7..59013d552 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 @@ -40,6 +40,8 @@ import com.sk89q.worldedit.command.GenerationCommands; import com.sk89q.worldedit.command.GenerationCommandsRegistration; import com.sk89q.worldedit.command.HistoryCommands; import com.sk89q.worldedit.command.HistoryCommandsRegistration; +import com.sk89q.worldedit.command.NavigationCommands; +import com.sk89q.worldedit.command.NavigationCommandsRegistration; import com.sk89q.worldedit.command.SchematicCommands; import com.sk89q.worldedit.command.SchematicCommandsRegistration; import com.sk89q.worldedit.command.argument.Arguments; @@ -284,14 +286,17 @@ public final class PlatformCommandMananger { HistoryCommandsRegistration.builder(), new HistoryCommands(worldEdit) ); + register( + commandManager, + NavigationCommandsRegistration.builder(), + new NavigationCommands(worldEdit) + ); // Unported commands are below. Delete once they're added to the main manager above. /* dispatcher = new CommandGraph() .builder(builder) .commands() - .registerMethods(new HistoryCommands(worldEdit)) - .registerMethods(new NavigationCommands(worldEdit)) .registerMethods(new RegionCommands(worldEdit)) .registerMethods(new ScriptingCommands(worldEdit)) .registerMethods(new SelectionCommands(worldEdit))