Skip to content

Commit 31fd9a0

Browse files
committed
Added hook that inserts "Hello World" print into Minecraft#startGame
1 parent 848fb8f commit 31fd9a0

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/main/kotlin/com/example/mod/Entry.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package com.example.mod
22

33
import club.maxstats.weave.api.ModInitializer
44
import club.maxstats.weave.api.hook.HookManager
5+
import com.example.mod.hooks.MinecraftHook
56

67
class Entry: ModInitializer {
78
override fun init(hookManager: HookManager){
9+
hookManager.add(MinecraftHook())
810
println("Example Mod Initialized")
911
}
1012
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.example.mod.hooks
2+
3+
import club.maxstats.weave.api.hook.Hook
4+
import org.objectweb.asm.Opcodes
5+
import org.objectweb.asm.tree.ClassNode
6+
import org.objectweb.asm.tree.FieldInsnNode
7+
import org.objectweb.asm.tree.InsnList
8+
import org.objectweb.asm.tree.LdcInsnNode
9+
import org.objectweb.asm.tree.MethodInsnNode
10+
import org.objectweb.asm.tree.MethodNode
11+
12+
class MinecraftHook: Hook("net.minecraft.client.Minecraft") {
13+
override fun transform(cn: ClassNode) {
14+
for (method: MethodNode in cn.methods) {
15+
if (method.name.equals("startGame")) {
16+
this.startGameInject(method)
17+
}
18+
}
19+
}
20+
21+
private fun startGameInject(method: MethodNode) {
22+
val inject = InsnList()
23+
inject.add(FieldInsnNode(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"))
24+
inject.add(LdcInsnNode("Hello World from Minecraft#startGame"))
25+
inject.add(MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"))
26+
method.instructions.insert(inject)
27+
}
28+
}

0 commit comments

Comments
 (0)