Command suggestions

Closes #1
This commit is contained in:
Chipmunk 2023-01-28 19:01:04 -05:00
parent 384a35fa9b
commit a91b5ac1f0
3 changed files with 53 additions and 2 deletions

View file

@ -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<Suggestions> 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<FabricClientCommandSource> 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);
}
}

View file

@ -6,7 +6,8 @@
"mixins": [
],
"client": [
"ChatScreenMixin"
"ChatScreenMixin",
"ChatInputSuggestorMixin"
],
"injectors": {
"defaultRequire": 1

View file

@ -23,7 +23,7 @@
]
},
"mixins": [
"modid.mixins.json"
"chipmunkmod.mixins.json"
],
"depends": {