chore: initial work on a better chat patch

This commit is contained in:
əlemi 2023-03-20 23:28:27 +01:00
parent ed0d7d9621
commit 33ece210f5
2 changed files with 59 additions and 4 deletions

View file

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

View file

@ -1,12 +1,21 @@
package ftbsc.bscv.patches; 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.Injector;
import ftbsc.lll.processor.annotations.Patch; import ftbsc.lll.processor.annotations.Patch;
import ftbsc.lll.processor.annotations.Target; import ftbsc.lll.processor.annotations.Target;
import ftbsc.lll.proxies.FieldProxy;
import ftbsc.lll.tools.InsnSequence; import ftbsc.lll.tools.InsnSequence;
import ftbsc.lll.tools.PatternMatcher; import ftbsc.lll.tools.PatternMatcher;
import ftbsc.lll.tools.debug.BytecodePrinter;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.NewChatGui;
import net.minecraft.client.gui.screen.Screen; 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.Opcodes;
import org.objectweb.asm.tree.*; import org.objectweb.asm.tree.*;
@ -14,6 +23,13 @@ import org.objectweb.asm.tree.*;
public class ChatPatch { 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") @Patch(value = Minecraft.class, reason = "add hook to prevent chat from being cleared")
public abstract static class ChatClearInterceptor implements Opcodes { public abstract static class ChatClearInterceptor implements Opcodes {
@ -23,21 +39,39 @@ public class ChatPatch {
abstract void setScreen(Screen screen); abstract void setScreen(Screen screen);
@Injector @Injector
public void inject(ClassNode clazz, MethodNode main) { public void inject(ClassNode clazz, MethodNode method) {
InsnSequence match = PatternMatcher.builder() InsnSequence match = PatternMatcher.builder()
.opcodes(ALOAD, GETFIELD, INVOKEVIRTUAL, ICONST_1, INVOKEVIRTUAL) .opcodes(ALOAD, GETFIELD, INVOKEVIRTUAL, ICONST_1, INVOKEVIRTUAL)
.ignoreLineNumbers() .ignoreLineNumbers()
.ignoreLabels() .ignoreLabels()
.ignoreFrames() .ignoreFrames()
.build() .build()
.find(main); .find(method);
LabelNode skip = new LabelNode(); LabelNode skip = new LabelNode();
JumpInsnNode jump = new JumpInsnNode(GOTO, skip); JumpInsnNode jump = new JumpInsnNode(GOTO, skip);
main.instructions.insertBefore(match.getFirst(), jump); method.instructions.insertBefore(match.getFirst(), jump);
main.instructions.insert(match.getLast(), skip); 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);
}
}
} }