From 6f6b0b03ae447906574b9b30b957a5e25650315a Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 19 Jan 2021 18:28:33 +0000 Subject: Fix tab complete on Forge/Fabric --- .../fabric/plugin/FabricClientSparkPlugin.java | 21 ++++++++++++------- .../fabric/plugin/FabricServerSparkPlugin.java | 24 +++++++++++----------- 2 files changed, 26 insertions(+), 19 deletions(-) (limited to 'spark-fabric/src/main/java') diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java index 5b63904..1bc6707 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java @@ -41,6 +41,7 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayNetworkHandler; import net.minecraft.command.CommandSource; import net.minecraft.server.command.CommandOutput; +import net.minecraft.server.command.ServerCommandSource; import java.util.Arrays; import java.util.Optional; @@ -98,12 +99,11 @@ public class FabricClientSparkPlugin extends FabricSparkPlugin implements Sugges } public boolean onClientChat(String chat) { - String[] split = chat.split(" "); - if (split.length == 0 || (!split[0].equals("/sparkc") && !split[0].equals("/sparkclient"))) { + String[] args = processArgs(chat, false); + if (args == null) { return false; } - String[] args = Arrays.copyOfRange(split, 1, split.length); this.platform.executeCommand(new FabricCommandSender(this.minecraft.player, this), args); this.minecraft.inGameHud.getChatHud().addToMessageHistory(chat); return true; @@ -111,13 +111,11 @@ public class FabricClientSparkPlugin extends FabricSparkPlugin implements Sugges @Override public CompletableFuture getSuggestions(CommandContext context, SuggestionsBuilder builder) throws CommandSyntaxException { - String[] split = context.getInput().split(" "); - if (split.length == 0 || (!split[0].equals("/sparkc") && !split[0].equals("/sparkclient"))) { + String[] args = processArgs(context.getInput(), true); + if (args == null) { return Suggestions.empty(); } - String[] args = Arrays.copyOfRange(split, 1, split.length); - return CompletableFuture.supplyAsync(() -> { for (String suggestion : this.platform.tabCompleteCommand(new FabricCommandSender(this.minecraft.player, this), args)) { builder.suggest(suggestion); @@ -126,6 +124,15 @@ public class FabricClientSparkPlugin extends FabricSparkPlugin implements Sugges }); } + private static String[] processArgs(String input, boolean tabComplete) { + String[] split = input.split(" ", tabComplete ? -1 : 0); + if (split.length == 0 || !split[0].equals("/sparkc") && !split[0].equals("/sparkclient")) { + return null; + } + + return Arrays.copyOfRange(split, 1, split.length); + } + @Override public boolean hasPermission(CommandOutput sender, String permission) { return true; diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java index 7ef0643..08edc82 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java @@ -73,18 +73,9 @@ public class FabricServerSparkPlugin extends FabricSparkPlugin implements Comman this.server = server; } - private static String /*Nullable*/ [] processArgs(CommandContext context) { - String[] split = context.getInput().split(" "); - if (split.length == 0 || !split[0].equals("/spark") && !split[0].equals("spark")) { - return null; - } - - return Arrays.copyOfRange(split, 1, split.length); - } - @Override public int run(CommandContext context) throws CommandSyntaxException { - String[] args = processArgs(context); + String[] args = processArgs(context, false); if (args == null) { return 0; } @@ -96,12 +87,12 @@ public class FabricServerSparkPlugin extends FabricSparkPlugin implements Comman @Override public CompletableFuture getSuggestions(CommandContext context, SuggestionsBuilder builder) throws CommandSyntaxException { - String[] args = processArgs(context); + String[] args = processArgs(context, true); if (args == null) { return Suggestions.empty(); } - ServerPlayerEntity player = context.getSource().getPlayer(); + ServerPlayerEntity player = context.getSource().getPlayer(); return CompletableFuture.supplyAsync(() -> { for (String suggestion : this.platform.tabCompleteCommand(new FabricCommandSender(player, this), args)) { builder.suggest(suggestion); @@ -110,6 +101,15 @@ public class FabricServerSparkPlugin extends FabricSparkPlugin implements Comman }); } + private static String[] processArgs(CommandContext context, boolean tabComplete) { + String[] split = context.getInput().split(" ", tabComplete ? -1 : 0); + if (split.length == 0 || !split[0].equals("/spark") && !split[0].equals("spark")) { + return null; + } + + return Arrays.copyOfRange(split, 1, split.length); + } + @Override public boolean hasPermission(CommandOutput sender, String permission) { if (sender instanceof PlayerEntity) { -- cgit