From a91b5ac1f0408b5a6525eee392e25e6af89cbf8a Mon Sep 17 00:00:00 2001 From: Chip <65827213+ChipmunkMC@users.noreply.github.com> Date: Sat, 28 Jan 2023 19:01:04 -0500 Subject: [PATCH] Command suggestions Closes #1 --- .../mixin/ChatInputSuggestorMixin.java | 50 +++++++++++++++++++ src/main/resources/chipmunkmod.mixins.json | 3 +- src/main/resources/fabric.mod.json | 2 +- 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/main/java/land/chipmunk/chipmunkmod/mixin/ChatInputSuggestorMixin.java diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatInputSuggestorMixin.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatInputSuggestorMixin.java new file mode 100644 index 0000000..3d66baf --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatInputSuggestorMixin.java @@ -0,0 +1,50 @@ +package land.chipmunk.chipmunkmod.mixin; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import java.util.concurrent.CompletableFuture; +import com.mojang.brigadier.suggestion.Suggestions; +import net.minecraft.client.gui.widget.TextFieldWidget; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.client.MinecraftClient; +import land.chipmunk.chipmunkmod.command.CommandManager; + +@Mixin(net.minecraft.client.gui.screen.ChatInputSuggestor.class) +public class ChatInputSuggestorMixin { + @Shadow + CompletableFuture pendingSuggestions; + + @Shadow + public void show (boolean narrateFirstSuggestion) {} + + @Shadow + final TextFieldWidget textField; + + public ChatInputSuggestorMixin () { + textField = null; + } + + @Inject(at = @At("TAIL"), method = "refresh()V") + public void onRefresh (CallbackInfo ci) { + final String text = this.textField.getText(); + final int cursor = this.textField.getCursor(); + + if (cursor < CommandManager.prefix.length() || !text.startsWith(CommandManager.prefix)) return; + + final StringReader reader = new StringReader(text); + reader.setCursor(CommandManager.prefix.length()); // Skip the prefix + + final CommandDispatcher dispatcher = CommandManager.dispatcher; + final MinecraftClient client = MinecraftClient.getInstance(); + final FabricClientCommandSource commandSource = (FabricClientCommandSource) client.getNetworkHandler().getCommandSource(); + + pendingSuggestions = dispatcher.getCompletionSuggestions(dispatcher.parse(reader, commandSource), cursor); + show(true); + } +} diff --git a/src/main/resources/chipmunkmod.mixins.json b/src/main/resources/chipmunkmod.mixins.json index d7e0255..53f3e39 100644 --- a/src/main/resources/chipmunkmod.mixins.json +++ b/src/main/resources/chipmunkmod.mixins.json @@ -6,7 +6,8 @@ "mixins": [ ], "client": [ - "ChatScreenMixin" + "ChatScreenMixin", + "ChatInputSuggestorMixin" ], "injectors": { "defaultRequire": 1 diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 7ccf789..5f0aecf 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -23,7 +23,7 @@ ] }, "mixins": [ - "modid.mixins.json" + "chipmunkmod.mixins.json" ], "depends": {