package com.runesuite.client.inject;

import com.runesuite.client.game.raw.MethodEvent;
import com.runesuite.client.game.raw.MethodExecution;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.implementation.bytecode.assign.Assigner;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public interface MethodAdvice {

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.PARAMETER)
    @interface Execution { }

    @SuppressWarnings("unchecked")
    @Advice.OnMethodEnter
    static MethodEvent.Enter onMethodEnter(
            @Execution MethodExecution exec,
            @Advice.This(optional = true) Object instance,
            @Advice.AllArguments Object[] arguments
    ) throws Throwable {
        MethodExecution.Implementation execImpl = (MethodExecution.Implementation) exec;
        MethodEvent.Enter enterEvent = new MethodEvent.Enter(execImpl.counter.getAndIncrement(), instance, arguments);
        execImpl.getEnter().accept(enterEvent);
        return enterEvent;
    }

    @SuppressWarnings("unchecked")
    @Advice.OnMethodExit
    static void onMethodExit(
            @Execution MethodExecution exec,
            @Advice.This(optional = true) Object instance,
            @Advice.Return(typing = Assigner.Typing.DYNAMIC) Object returned,
            @Advice.Enter MethodEvent.Enter enterEvent
    ) throws Throwable {
        ((MethodExecution.Implementation) exec).getExit().accept(new MethodEvent.Exit(enterEvent.getIndex(), instance, enterEvent.getArguments(), returned));
    }
}
@@ -230,16 +230,17 @@
 signer;%0A
+%0A
 import j
@@ -906,16 +906,107 @@
 ) exec;%0A
+        if (execImpl.getEnter().hasObservers() %7C%7C execImpl.getExit().hasObservers()) %7B%0A    
         
@@ -1124,16 +1124,20 @@
         
+    
 execImpl
@@ -1176,16 +1176,20 @@
         
+    
 return e
@@ -1199,16 +1199,68 @@
 rEvent;%0A
+        %7D else %7B%0A            return null;%0A        %7D%0A
     %7D%0A%0A 
@@ -1602,32 +1602,70 @@
 ows Throwable %7B%0A
+        if (enterEvent != null) %7B%0A    
         ((Method
@@ -1806,15 +1806,25 @@
 rned));%0A
+        %7D%0A
     %7D%0A%7D

Make method events lazier