From 90473bd83ba503775e54fbfe623208c6805c60b1 Mon Sep 17 00:00:00 2001 From: "Blue (Lukas Rieger)" Date: Mon, 11 May 2020 13:23:56 +0200 Subject: [PATCH] More command improvements --- .../commands/AbstractSuggestionProvider.java | 10 +- .../common/plugin/commands/Commands.java | 173 +++++++++++++++--- .../commands/MarkerIdSuggestionProvider.java | 91 +++++++++ 3 files changed, 245 insertions(+), 29 deletions(-) create mode 100644 BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/MarkerIdSuggestionProvider.java diff --git a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/AbstractSuggestionProvider.java b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/AbstractSuggestionProvider.java index 9e75364f..f717ecca 100644 --- a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/AbstractSuggestionProvider.java +++ b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/AbstractSuggestionProvider.java @@ -26,8 +26,8 @@ import java.util.Collection; import java.util.concurrent.CompletableFuture; -import java.util.regex.Pattern; +import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.SuggestionProvider; @@ -35,8 +35,6 @@ import com.mojang.brigadier.suggestion.SuggestionsBuilder; public abstract class AbstractSuggestionProvider implements SuggestionProvider { - - private static final Pattern ESCAPE_PATTERN = Pattern.compile("[^a-zA-Z0-9_-]"); @Override public CompletableFuture getSuggestions(CommandContext context, SuggestionsBuilder builder) throws CommandSyntaxException { @@ -45,12 +43,8 @@ public CompletableFuture getSuggestions(CommandContext context, String remaining = builder.getRemaining().toLowerCase(); for (String str : possibleValues) { - if (ESCAPE_PATTERN.matcher(str).find() && str.indexOf('"') == -1) { - str = "\"" + str + "\""; - } - if (str.toLowerCase().startsWith(remaining)) { - builder.suggest(str); + builder.suggest(str = StringArgumentType.escapeIfRequired(str)); } } diff --git a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/Commands.java b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/Commands.java index 18930a59..8fc439a5 100644 --- a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/Commands.java +++ b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/Commands.java @@ -45,6 +45,11 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.tree.LiteralCommandNode; +import de.bluecolored.bluemap.api.BlueMapAPI; +import de.bluecolored.bluemap.api.BlueMapMap; +import de.bluecolored.bluemap.api.marker.MarkerAPI; +import de.bluecolored.bluemap.api.marker.MarkerSet; +import de.bluecolored.bluemap.api.marker.POIMarker; import de.bluecolored.bluemap.common.MapType; import de.bluecolored.bluemap.common.RenderTask; import de.bluecolored.bluemap.common.plugin.Plugin; @@ -59,6 +64,8 @@ import de.bluecolored.bluemap.core.world.World; public class Commands { + + public static final String DEFAULT_MARKER_SET_ID = "markers"; private final Plugin plugin; private final CommandDispatcher dispatcher; @@ -99,11 +106,7 @@ public void init() { .then(argument("x", DoubleArgumentType.doubleArg()) .then(argument("y", DoubleArgumentType.doubleArg()) .then(argument("z", DoubleArgumentType.doubleArg()) - .executes(this::debugCommand) - ) - ) - ) - ) + .executes(this::debugCommand))))) .build(); LiteralCommandNode pauseCommand = @@ -124,16 +127,12 @@ public void init() { .executes(this::renderCommand) // /bluemap render .then(argument("radius", IntegerArgumentType.integer()) - .executes(this::renderCommand) // /bluemap render - ) + .executes(this::renderCommand)) // /bluemap render .then(argument("x", DoubleArgumentType.doubleArg()) .then(argument("z", DoubleArgumentType.doubleArg()) .then(argument("radius", IntegerArgumentType.integer()) - .executes(this::renderCommand) // /bluemap render - ) - ) - ) + .executes(this::renderCommand)))) // /bluemap render .then(argument("world|map", StringArgumentType.string()).suggests(new WorldOrMapSuggestionProvider<>(plugin)) .executes(this::renderCommand) // /bluemap render @@ -141,28 +140,21 @@ public void init() { .then(argument("x", DoubleArgumentType.doubleArg()) .then(argument("z", DoubleArgumentType.doubleArg()) .then(argument("radius", IntegerArgumentType.integer()) - .executes(this::renderCommand) // /bluemap render - ) - ) - ) - ) - + .executes(this::renderCommand))))) // /bluemap render .build(); LiteralCommandNode prioRenderCommand = literal("prioritize") .requires(requirements("bluemap.render")) .then(argument("uuid", StringArgumentType.string()) - .executes(this::prioritizeRenderTaskCommand) - ) + .executes(this::prioritizeRenderTaskCommand)) .build(); LiteralCommandNode cancelRenderCommand = literal("cancel") .requires(requirements("bluemap.render")) .then(argument("uuid", StringArgumentType.string()) - .executes(this::cancelRenderTaskCommand) - ) + .executes(this::cancelRenderTaskCommand)) .build(); LiteralCommandNode worldsCommand = @@ -177,6 +169,34 @@ public void init() { .executes(this::mapsCommand) .build(); + LiteralCommandNode markerCommand = + literal("marker") + .requires(requirements("bluemap.marker")) + .build(); + + LiteralCommandNode createMarkerCommand = + literal("create") + .requires(requirements("bluemap.marker")) + .then(argument("id", StringArgumentType.word()) + .then(argument("map", StringArgumentType.string()).suggests(new MapSuggestionProvider<>(plugin)) + + .then(argument("label", StringArgumentType.string()) + .executes(this::createMarkerCommand)) + + .then(argument("x", DoubleArgumentType.doubleArg()) + .then(argument("y", DoubleArgumentType.doubleArg()) + .then(argument("z", DoubleArgumentType.doubleArg()) + .then(argument("label", StringArgumentType.string()) + .executes(this::createMarkerCommand))))))) + .build(); + + LiteralCommandNode removeMarkerCommand = + literal("remove") + .requires(requirements("bluemap.marker")) + .then(argument("id", StringArgumentType.word()).suggests(MarkerIdSuggestionProvider.getInstance()) + .executes(this::removeMarkerCommand)) + .build(); + // command tree dispatcher.getRoot().addChild(baseCommand); baseCommand.addChild(reloadCommand); @@ -188,6 +208,9 @@ public void init() { renderCommand.addChild(cancelRenderCommand); baseCommand.addChild(worldsCommand); baseCommand.addChild(mapsCommand); + baseCommand.addChild(markerCommand); + markerCommand.addChild(createMarkerCommand); + markerCommand.addChild(removeMarkerCommand); } private Predicate requirements(String permission){ @@ -491,4 +514,112 @@ public int mapsCommand(CommandContext context) { return 1; } + public int createMarkerCommand(CommandContext context) { + CommandSource source = commandSourceInterface.apply(context.getSource()); + + String markerId = context.getArgument("id", String.class); + String markerLabel = context.getArgument("label", String.class) + .replace("<", "<") + .replace(">", ">"); //no html via commands + + // parse world/map argument + String mapString = context.getArgument("map", String.class); + MapType map = parseMap(mapString).orElse(null); + + if (map == null) { + source.sendMessage(Text.of(TextColor.RED, "There is no ", helper.mapHelperHover(), " with this name: ", TextColor.WHITE, mapString)); + return 0; + } + + // parse position + Optional x = getOptionalArgument(context, "x", Double.class); + Optional y = getOptionalArgument(context, "y", Double.class); + Optional z = getOptionalArgument(context, "z", Double.class); + + Vector3d position; + + if (x.isPresent() && y.isPresent() && z.isPresent()) { + position = new Vector3d(x.get(), y.get(), z.get()); + } else { + position = source.getPosition().orElse(null); + + if (position == null) { + source.sendMessage(Text.of(TextColor.RED, "Can't detect a position from this command-source, you'll have to define the x,y,z coordinates for the marker!").setHoverText(Text.of(TextColor.GRAY, "/bluemap marker create " + markerId + " " + "[world|map]