Skip to content

Commit

Permalink
duplicate a field with our own name for runnable
Browse files Browse the repository at this point in the history
  • Loading branch information
My-Name-Is-Jeff committed Nov 3, 2021
1 parent e2fe887 commit f00f22f
Showing 1 changed file with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,34 @@ import dev.falsehonesty.asmhelper.dsl.instructions.JumpCondition
import dev.falsehonesty.asmhelper.dsl.modify
import net.minecraft.network.INetHandler
import net.minecraft.network.Packet
import org.objectweb.asm.Opcodes
import skytils.skytilsmod.events.impl.MainReceivePacketEvent

fun insertReceivePacketEvent() = modify("net/minecraft/network/PacketThreadUtil$1") {
val netHandler = classNode.fields.find { it.desc == "Lnet/minecraft/network/INetHandler;" || it.desc == "Lep;" }
?: error("couldn't find INetHandler field")
val packet =
classNode.fields.find { it.desc == "Lnet/minecraft/network/Packet;" || it.desc == "Lff;" }
?: error("couldn't find Packet field")
classNode.visitField(
Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL,
"skytils\$handler",
"Lnet/minecraft/network/INetHandler;",
null,
null
).visitEnd()
classNode.visitField(
Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL,
"skytils\$packet",
"Lnet/minecraft/network/Packet;",
null,
null
).visitEnd()
classNode.methods.find { it.name == "<init>" }?.apply {
instructions.insert(InsnListBuilder(this).apply {
aload(0)
aload(1)
putField(classNode.name, "skytils\$packet", "Lnet/minecraft/network/Packet;")
aload(0)
aload(2)
putField(classNode.name, "skytils\$handler", "Lnet/minecraft/network/INetHandler;")
}.build())
}
findMethod("run", "()V").apply {
instructions.insert(InsnListBuilder(this).apply {
invokeStatic(
Expand All @@ -39,9 +59,17 @@ fun insertReceivePacketEvent() = modify("net/minecraft/network/PacketThreadUtil$
"(Lnet/minecraft/network/INetHandler;Lnet/minecraft/network/Packet;)Z"
) {
aload(0)
getField("net/minecraft/network/PacketThreadUtil$1", netHandler.name, netHandler.desc)
getField(
"net/minecraft/network/PacketThreadUtil$1",
"skytils\$handler",
"Lnet/minecraft/network/INetHandler;"
)
aload(0)
getField("net/minecraft/network/PacketThreadUtil$1", packet.name, packet.desc)
getField(
"net/minecraft/network/PacketThreadUtil$1",
"skytils\$packet",
"Lnet/minecraft/network/Packet;"
)
}
ifClause(JumpCondition.EQUAL) {
methodReturn()
Expand Down

0 comments on commit f00f22f

Please sign in to comment.