From e84b975266f11cdc0f30c1c4a446bdd9e033febd Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+chomens@users.noreply.github.com> Date: Sun, 5 Jan 2025 14:42:58 +0700 Subject: [PATCH] feat: (re-)add infinite chat --- .../chipmunkmod/mixin/ChatScreenMixin.java | 50 ++++++++++++++++--- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatScreenMixin.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatScreenMixin.java index fe23281..f47d04d 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatScreenMixin.java +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatScreenMixin.java @@ -6,7 +6,10 @@ import land.chipmunk.chipmunkmod.data.ChomeNSBotCommand; import land.chipmunk.chipmunkmod.modules.ChomeNSBotCommandSuggestions; import land.chipmunk.chipmunkmod.util.BotValidationUtilities; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.ChatInputSuggestor; import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.widget.TextFieldWidget; +import net.minecraft.text.MutableText; import net.minecraft.text.Text; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -17,17 +20,50 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import javax.net.ssl.HttpsURLConnection; import java.io.IOException; import java.io.OutputStream; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.util.ArrayList; import java.util.List; @Mixin(value = net.minecraft.client.gui.screen.ChatScreen.class) -public class ChatScreenMixin extends Screen { +public abstract class ChatScreenMixin extends Screen { @Shadow private String originalChatText; - public ChatScreenMixin(String originalChatText) { - super(Text.translatable("chat_screen.title")); - this.originalChatText = originalChatText; + @Shadow private int messageHistoryIndex; + + @Shadow protected TextFieldWidget chatField; + + @Shadow ChatInputSuggestor chatInputSuggestor; + + @Shadow protected abstract void onChatFieldUpdate(String chatText); + + protected ChatScreenMixin(Text title) { + super(title); + } + + // infinite chat + @Inject(method = "init", at = @At("HEAD"), cancellable = true) + protected void init (CallbackInfo ci) { + final MinecraftClient client = MinecraftClient.getInstance(); + + this.messageHistoryIndex = client.inGameHud.getChatHud().getMessageHistory().size(); + this.chatField = new TextFieldWidget(client.advanceValidatingTextRenderer, 4, this.height - 12, this.width - 4, 12, Text.translatable("chat.editBox")) { + protected MutableText getNarrationMessage() { + return super.getNarrationMessage().append(ChatScreenMixin.this.chatInputSuggestor.getNarration()); + } + }; + this.chatField.setMaxLength(Integer.MAX_VALUE); + this.chatField.setDrawsBackground(false); + this.chatField.setText(this.originalChatText); + this.chatField.setChangedListener(this::onChatFieldUpdate); + this.chatField.setFocusUnlocked(false); + this.addSelectableChild(this.chatField); + this.chatInputSuggestor = new ChatInputSuggestor(this.client, this, this.chatField, this.textRenderer, false, false, 1, 10, true, -805306368); + this.chatInputSuggestor.setCanLeave(false); + this.chatInputSuggestor.refresh(); + + ci.cancel(); } @Inject(method = "sendMessage", at = @At("HEAD"), cancellable = true) @@ -41,7 +77,7 @@ public class ChatScreenMixin extends Screen { if (ChipmunkMod.CONFIG.bots.testbot.webhookUrl != null && chatText.startsWith(ChipmunkMod.CONFIG.bots.testbot.prefix)) { ChipmunkMod.executorService.submit(() -> { try { - final URL url = new URL(ChipmunkMod.CONFIG.bots.testbot.webhookUrl); + final URL url = new URI(ChipmunkMod.CONFIG.bots.testbot.webhookUrl).toURL(); final HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.addRequestProperty("Content-Type", "application/json"); @@ -61,7 +97,7 @@ public class ChatScreenMixin extends Screen { connection.getInputStream().close(); connection.disconnect(); - } catch (IOException e) { + } catch (IOException | URISyntaxException e) { e.printStackTrace(); } }); @@ -98,7 +134,7 @@ public class ChatScreenMixin extends Screen { } } - if (client == null) return; + if (client.player == null) return; if (chatText.startsWith("/")) { client.player.networkHandler.sendChatCommand(chatText.substring(1));