From b7517376da3bb8725bfc487f1270e402b6354c9b Mon Sep 17 00:00:00 2001 From: Uriel Date: Wed, 27 Nov 2024 18:45:20 +0100 Subject: [PATCH] Add autoprefix command (#647) * try adding autoprefix * remove nix files * add working auto prefix command * remove more nix files * add comments on why shift by -2 * use `@ModifyReceiver` for mixin instead * fix review * i forgot the alphabet * fix bug and add custom parameter * dont include nix stuff in gitignore * Use a config for setting autoprefix instead * dont specify getter/setter --- .../earthcomputer/clientcommands/Configs.java | 3 +++ .../commands/generic/ChatScreenMixin.java | 20 +++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/earthcomputer/clientcommands/Configs.java b/src/main/java/net/earthcomputer/clientcommands/Configs.java index ddccaee72..63f3e13e0 100644 --- a/src/main/java/net/earthcomputer/clientcommands/Configs.java +++ b/src/main/java/net/earthcomputer/clientcommands/Configs.java @@ -143,6 +143,9 @@ public static void setMaxChorusItemThrows(int maxChorusItemThrows) { Configs.maxChorusItemThrows = Mth.clamp(maxChorusItemThrows, 0, 1000000); } + @Config(temporary = true) + public static String autoPrefix = ""; + @Config(temporary = true, condition = "conditionLessThan1_21") public static boolean infiniteTools = false; diff --git a/src/main/java/net/earthcomputer/clientcommands/mixin/commands/generic/ChatScreenMixin.java b/src/main/java/net/earthcomputer/clientcommands/mixin/commands/generic/ChatScreenMixin.java index 03751cbb8..0b433ffde 100644 --- a/src/main/java/net/earthcomputer/clientcommands/mixin/commands/generic/ChatScreenMixin.java +++ b/src/main/java/net/earthcomputer/clientcommands/mixin/commands/generic/ChatScreenMixin.java @@ -1,22 +1,34 @@ package net.earthcomputer.clientcommands.mixin.commands.generic; +import com.llamalad7.mixinextras.injector.ModifyReceiver; +import com.llamalad7.mixinextras.sugar.Local; +import com.llamalad7.mixinextras.sugar.ref.LocalRef; import net.earthcomputer.clientcommands.ClientCommands; +import net.earthcomputer.clientcommands.Configs; import net.earthcomputer.clientcommands.command.VarCommand; import net.minecraft.client.gui.screens.ChatScreen; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.ModifyVariable; @Mixin(ChatScreen.class) public class ChatScreenMixin { // replace the text before the Fabric Command API executes it, // but ensure the message is added to the history in its raw form. - @ModifyVariable(method = "handleChatInput", at = @At(value = "INVOKE", target = "Ljava/lang/String;startsWith(Ljava/lang/String;)Z", remap = false), argsOnly = true) - private String onHandleChatInput(String message) { - String command = VarCommand.replaceVariables(message); + @ModifyReceiver(method = "handleChatInput", at = @At(value = "INVOKE", target = "Ljava/lang/String;startsWith(Ljava/lang/String;)Z", remap = false)) + private String onHandleChatInput(String instance, String slash, @Local(argsOnly = true) LocalRef message) { + String prefix = Configs.autoPrefix; + if (prefix.isEmpty() || instance.startsWith("/")) { + prefix = ""; + } else { + prefix = prefix + " "; + } + + String command = VarCommand.replaceVariables(prefix + instance); if (command.startsWith("/")) { ClientCommands.sendCommandExecutionToServer(command.substring(1)); } + + message.set(command); return command; } }