From 33ece210f588d865ed228bb4b1012ecf20170f1a Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 20 Mar 2023 23:28:27 +0100 Subject: [PATCH] chore: initial work on a better chat patch --- .../java/ftbsc/bscv/modules/hud/Terminal.java | 21 ++++++++++ .../java/ftbsc/bscv/patches/ChatPatch.java | 42 +++++++++++++++++-- 2 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 src/main/java/ftbsc/bscv/modules/hud/Terminal.java diff --git a/src/main/java/ftbsc/bscv/modules/hud/Terminal.java b/src/main/java/ftbsc/bscv/modules/hud/Terminal.java new file mode 100644 index 0000000..432f78b --- /dev/null +++ b/src/main/java/ftbsc/bscv/modules/hud/Terminal.java @@ -0,0 +1,21 @@ +package ftbsc.bscv.modules.hud; + +import ftbsc.bscv.modules.AbstractModule; +import ftbsc.bscv.tools.Setting; +import net.minecraftforge.common.ForgeConfigSpec; + +public class Terminal extends AbstractModule { + + public final ForgeConfigSpec.ConfigValue msg_count; + + public Terminal() { + super(); + + this.msg_count = Setting.Number.builder() + .min(-1) + .fallback(100) + .name("msg-count") + .comment("How many messages to keep in chat, set to 0 for unlimited") + .build(this); + } +} diff --git a/src/main/java/ftbsc/bscv/patches/ChatPatch.java b/src/main/java/ftbsc/bscv/patches/ChatPatch.java index 0441c28..7ec733d 100644 --- a/src/main/java/ftbsc/bscv/patches/ChatPatch.java +++ b/src/main/java/ftbsc/bscv/patches/ChatPatch.java @@ -1,12 +1,21 @@ package ftbsc.bscv.patches; +import ftbsc.bscv.modules.hud.Terminal; +import ftbsc.lll.processor.annotations.Find; import ftbsc.lll.processor.annotations.Injector; import ftbsc.lll.processor.annotations.Patch; import ftbsc.lll.processor.annotations.Target; +import ftbsc.lll.proxies.FieldProxy; import ftbsc.lll.tools.InsnSequence; import ftbsc.lll.tools.PatternMatcher; +import ftbsc.lll.tools.debug.BytecodePrinter; import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.NewChatGui; import net.minecraft.client.gui.screen.Screen; +import net.minecraft.util.text.ITextComponent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.eventbus.api.Event; +import net.minecraftforge.eventbus.api.Cancelable; import org.objectweb.asm.Opcodes; import org.objectweb.asm.tree.*; @@ -14,6 +23,13 @@ import org.objectweb.asm.tree.*; public class ChatPatch { + @Cancelable + public static class ClearChatEvent extends Event {} + + public static boolean shouldPreventChatClearing() { + return MinecraftForge.EVENT_BUS.post(new ClearChatEvent()); + } + @Patch(value = Minecraft.class, reason = "add hook to prevent chat from being cleared") public abstract static class ChatClearInterceptor implements Opcodes { @@ -23,21 +39,39 @@ public class ChatPatch { abstract void setScreen(Screen screen); @Injector - public void inject(ClassNode clazz, MethodNode main) { + public void inject(ClassNode clazz, MethodNode method) { InsnSequence match = PatternMatcher.builder() .opcodes(ALOAD, GETFIELD, INVOKEVIRTUAL, ICONST_1, INVOKEVIRTUAL) .ignoreLineNumbers() .ignoreLabels() .ignoreFrames() .build() - .find(main); + .find(method); LabelNode skip = new LabelNode(); + JumpInsnNode jump = new JumpInsnNode(GOTO, skip); - main.instructions.insertBefore(match.getFirst(), jump); - main.instructions.insert(match.getLast(), skip); + method.instructions.insertBefore(match.getFirst(), jump); + method.instructions.insert(match.getLast(), skip); } } + @Patch(value = NewChatGui.class, reason = "make max number of messages in chat configurable (instead of just 100)") + public abstract static class ChatHistoryRemoveInterceptor implements Opcodes { + + @Target + abstract void addMessage(ITextComponent txt, int idk1, int idk2, boolean idk3); + + @Find(parent = Terminal.class) + abstract FieldProxy msg_count(); + + @Injector + public void inject(ClassNode clazz, MethodNode method) { + InsnSequence match = PatternMatcher.builder() + .opcodes(ALOAD, ICONST_2, INVOKESPECIAL, INVOKEINTERFACE, GOTO) + .build() + .find(method); + } + } }