package net.acomputerdog.BlazeLoader.transformers;

import com.mumfrey.liteloader.transformers.event.Event;
import com.mumfrey.liteloader.transformers.event.EventInjectionTransformer;
import com.mumfrey.liteloader.transformers.event.InjectionPoint;
import com.mumfrey.liteloader.transformers.event.MethodInfo;
import com.mumfrey.liteloader.transformers.event.inject.MethodHead;
import net.acomputerdog.BlazeLoader.util.obf.BLMethodInfo;
import net.acomputerdog.BlazeLoader.util.obf.BLOBF;

/**
 * Injects events into MC classes
 */
public class BLEventInjectionTransformer extends EventInjectionTransformer {
    /**
     * Subclasses should register events here
     */
    @Override
    protected void addEvents() {
        try {
            MethodInfo loadWorld = BLMethodInfo.create(BLOBF.getMethodMCP("net.minecraft.client.Minecraft.loadWorld (Lnet/minecraft/client/multiplayer/WorldClient;Ljava/lang/String;)V").getValue());
            MethodInfo startSection = BLMethodInfo.create(BLOBF.getMethodMCP("net.minecraft.profiler.Profiler.startSection (Ljava/lang/String;)V").getValue());
            MethodInfo endSection = BLMethodInfo.create(BLOBF.getMethodMCP("net.minecraft.profiler.Profiler.endSection ()V").getValue());

            InjectionPoint methodHead = new MethodHead();

            this.addEvent(Event.getOrCreate("BL.loadWorld", false), loadWorld, methodHead)
                    .addListener(new MethodInfo("net.acomputerdog.BlazeLoader.event.EventHandler", "eventLoadWorld"));
            this.addEvent(Event.getOrCreate("BL.profilerStart", false), startSection, methodHead)
                    .addListener(new MethodInfo("net.acomputerdog.BlazeLoader.event.EventHandler", "eventProfilerStart"));
            this.addEvent(Event.getOrCreate("BL.profilerEnd", false), endSection, methodHead)
                    .addListener(new MethodInfo("net.acomputerdog.BlazeLoader.event.EventHandler", "eventProfilerEnd"));
        } catch (Exception e) {
            System.err.println("A fatal exception occurred while injecting BlazeLoader!  BlazeLoader will not be able to run!");
            throw new RuntimeException("Exception injecting BlazeLoader!", e);
        }
    }
}

@@ -483,16 +483,49 @@
 BLOBF;%0A%0A
+import java.util.regex.Pattern;%0A%0A
 /**%0A * I
@@ -635,16 +635,179 @@
 ormer %7B%0A
+    private static final String EVENT_HANDLER = %22net.acomputerdog.BlazeLoader.event.EventHandler%22;%0A    private static final String PREFIX = Pattern.quote(%22BL.%22);%0A%0A
     /**%0A
@@ -1502,37 +1502,26 @@
 .add
+BL
 Event(
-Event.getOrCreate(%22BL.l
+%22BL.eventL
 oadW
@@ -1526,24 +1526,16 @@
 dWorld%22,
- false),
  loadWor
@@ -1545,32 +1545,26 @@
  methodHead)
+;
 %0A
-       
             
@@ -1567,157 +1567,34 @@
     
- .addListener(new MethodInfo(%22net.acomputerdog.BlazeLoader.event.EventHandler%22, %22eventLoadWorld%22));%0A            this.addEvent(Event.getOrCreate(%22BL.p
+this.addBLEvent(%22BL.eventP
 rofi
@@ -1603,24 +1603,16 @@
 rStart%22,
- false),
  startSe
@@ -1633,130 +1633,8 @@
 ead)
-%0A                    .addListener(new MethodInfo(%22net.acomputerdog.BlazeLoader.event.EventHandler%22, %22eventProfilerStart%22))
 ;%0A  
@@ -1655,37 +1655,26 @@
 .add
+BL
 Event(
-Event.getOrCreate(%22BL.p
+%22BL.eventP
 rofi
@@ -1685,16 +1685,8 @@
 nd%22,
- false),
  end
@@ -1709,128 +1709,8 @@
 ead)
-%0A                    .addListener(new MethodInfo(%22net.acomputerdog.BlazeLoader.event.EventHandler%22, %22eventProfilerEnd%22))
 ;%0A  
@@ -1962,11 +1962,414 @@
  %7D%0A    %7D
+%0A%0A    private void addBLEvent(String name, MethodInfo method, InjectionPoint injectionPoint) %7B%0A        this.addEvent(Event.getOrCreate(name, false), method, injectionPoint).addListener(new MethodInfo(EVENT_HANDLER, stripPrefix(name)));%0A    %7D%0A%0A    private String stripPrefix(String name) %7B%0A        if (name == null) %7B%0A            return null;%0A        %7D%0A        return name.replaceFirst(PREFIX, %22%22);%0A    %7D
 %0A%7D%0A

Refactor BLEventInjectionTransformer to simplify the creation of events