diff options
Diffstat (limited to 'featureClass/forgeEvents.js')
-rw-r--r-- | featureClass/forgeEvents.js | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/featureClass/forgeEvents.js b/featureClass/forgeEvents.js index e852c49..fb723b1 100644 --- a/featureClass/forgeEvents.js +++ b/featureClass/forgeEvents.js @@ -4,15 +4,20 @@ importPackage(org.objectweb.asm) importClass(java.lang.ClassLoader) importClass(org.apache.commons.lang3.RandomStringUtils) importClass(java.util.function.Consumer) +importClass(net.minecraftforge.fml.common.eventhandler.EventPriority) const L = s => `L${s};` - const LoadedInsts = [] -function defineClassBytes(name, bytes) { +function defineClassBytes(name, bytes) {//should support multymc? ty dawjaw https://canary.discord.com/channels/119493402902528000/688773480954855537/979959207124168744 const classLoader = Packages.com.chattriggers.ctjs.CTJS.class.getClassLoader() - const defClass = ClassLoader.class.getDeclaredMethods()[23] // defineClass() + let defClass; + ClassLoader.class.getDeclaredMethods().forEach(m => { + if (m.toString() === "protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int) throws java.lang.ClassFormatError") { + defClass = m; + } + }) defClass.setAccessible(true) @@ -22,7 +27,7 @@ function defineClassBytes(name, bytes) { return defClass.invoke(classLoader, n, bytes, o, s) } -const registerForge = (e, cb) => { +const registerForge = (e, priority = EventPriority.NORMAL, cb) => { const cw = new ClassWriter(0) const event = Type.getType(e.class).internalName @@ -35,7 +40,7 @@ const registerForge = (e, cb) => { const obj = Type.getType(java.lang.Object.class).internalName cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, name, null, obj, null) - // cw.visitInnerClass("net/minecraftforge/event/entity/player/PlayerEvent$BreakSpeed", "net/minecraftforge/event/entity/player/PlayerEvent", "BreakSpeed", ACC_PUBLIC + ACC_STATIC); + //cw.visitInnerClass("net/minecraftforge/event/entity/player/PlayerEvent$BreakSpeed","net/minecraftforge/event/entity/player/PlayerEvent","BreakSpeed",ACC_PUBLIC+ACC_STATIC); { cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "callback", L(consumer), L(consumer + "<" + L(event) + ">"), null).visitEnd() } @@ -44,7 +49,7 @@ const registerForge = (e, cb) => { con.visitCode() con.visitVarInsn(Opcodes.ALOAD, 0) con.visitMethodInsn(Opcodes.INVOKESPECIAL, obj, "<init>", "()V", false) - + con.visitVarInsn(Opcodes.ALOAD, 0) con.visitVarInsn(Opcodes.ALOAD, 1) con.visitFieldInsn(Opcodes.PUTFIELD, name, "callback", L(consumer)) @@ -59,6 +64,7 @@ const registerForge = (e, cb) => { const mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "on", "(" + L(event) + ")V", null, null) { const av = mv.visitAnnotation(L(subscribeEvent), true) + av.visitEnum("priority", "Lnet/minecraftforge/fml/common/eventhandler/EventPriority;", priority.name()); av.visitEnd() } mv.visitCode() @@ -75,7 +81,7 @@ const registerForge = (e, cb) => { const inst = defineClassBytes(name, cw.toByteArray()) .getDeclaredConstructor(Consumer.class) .newInstance(new java.util.function.Consumer({ - accept: function (t) { cb(t) } + accept: function (t) { cb(t) } })) LoadedInsts.push(inst) return inst; @@ -86,8 +92,8 @@ const unregisterForge = inst => { } register("gameUnload", () => { - LoadedInsts.forEach(unregisterForge) + LoadedInsts.forEach(unregisterForge) LoadedInsts.length = 0 }) -export { registerForge, unregisterForge } +export { registerForge, unregisterForge }
\ No newline at end of file |