package info.u_team.u_team_core.util.annotation;

import org.apache.logging.log4j.*;
import org.objectweb.asm.Type;

import info.u_team.u_team_core.api.construct.*;
import info.u_team.u_team_core.api.integration.*;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.loading.FMLEnvironment;
import net.minecraftforge.forgespi.language.ModFileScanData.AnnotationData;

public class AnnotationManager {
	
	private static final Logger LOGGER = LogManager.getLogger("AnnotationManager");
	
	public static void callConstructs(String modid) {
		for (AnnotationData data : AnnotationUtil.getAnnotations(modid, Type.getType(Construct.class))) {
			final String annotationModid = (String) data.getAnnotationData().get("modid");
			final Boolean client = (Boolean) data.getAnnotationData().get("client");
			if (modid.equals(annotationModid)) {
				if (client == null || !client || client && FMLEnvironment.dist == Dist.CLIENT) {
					LOGGER.info("Try to load construct for mod " + modid);
					try {
						Class.forName(data.getMemberName()).asSubclass(IModConstruct.class).newInstance().construct();
					} catch (LinkageError | ClassNotFoundException | InstantiationException | IllegalAccessException | ClassCastException ex) {
						LOGGER.error("Failed to load and construct mod construct : {}", data.getMemberName(), ex);
						throw new RuntimeException(ex);
					}
				}
			}
		}
	}
	
	public static void callIntegrations(String modid) {
		for (AnnotationData data : AnnotationUtil.getAnnotations(modid, Type.getType(Integration.class))) {
			final String annotationModid = (String) data.getAnnotationData().get("modid");
			final Boolean client = (Boolean) data.getAnnotationData().get("client");
			final String integrationModid = (String) data.getAnnotationData().get("integration");
			if (modid.equals(annotationModid)) {
				if (ModList.get().isLoaded(integrationModid)) {
					LOGGER.info("Try to load " + integrationModid + " integration for mod " + modid);
					try {
						Class.forName(data.getMemberName()).asSubclass(IModIntegration.class).newInstance().construct();
					} catch (LinkageError | ClassNotFoundException | InstantiationException | IllegalAccessException | ClassCastException ex) {
						LOGGER.error("Failed to load and construct integration : {}", data.getMemberName(), ex);
						throw new RuntimeException(ex);
					}
				}
			}
		}
	}
	
	private static boolean canBeCalled(String modid, AnnotationData data) {
		final String annotationModid = (String) data.getAnnotationData().get("modid");
		final Boolean client = (Boolean) data.getAnnotationData().get("client");
		return modid.equals(annotationModid) && (client == null || !client || client && FMLEnvironment.dist == Dist.CLIENT);
	}
	
}

@@ -701,289 +701,40 @@
 %0A%09%09%09
-final String annotationModid = (String) data.getAnnotationData().get(%22modid%22);%0A%09%09%09final Boolean client = (Boolean) data.getAnnotationData().get(%22client%22);%0A%09%09%09if (modid.equals(annotationModid)) %7B%0A%09%09%09%09if (client == null %7C%7C !client %7C%7C client && FMLEnvironment.dist == Dist.CLIENT) %7B%0A%09
+if (canBeCalled(modid, data)) %7B%0A
 %09%09%09%09
@@ -780,33 +780,32 @@
 mod %22 + modid);%0A
-%09
 %09%09%09%09try %7B%0A%09%09%09%09%09%09
@@ -795,33 +795,32 @@
 %0A%09%09%09%09try %7B%0A%09%09%09%09%09
-%09
 Class.forName(da
@@ -890,33 +890,32 @@
 ().construct();%0A
-%09
 %09%09%09%09%7D catch (Lin
@@ -1023,33 +1023,32 @@
 tion ex) %7B%0A%09%09%09%09%09
-%09
 LOGGER.error(%22Fa
@@ -1114,33 +1114,32 @@
 berName(), ex);%0A
-%09
 %09%09%09%09%09throw new R
@@ -1151,39 +1151,32 @@
 eException(ex);%0A
-%09%09%09%09%09%7D%0A
 %09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09
@@ -1175,19 +1175,19 @@
 %09%7D%0A%09%09%7D%0A%09
+%7D%0A%09
-%7D%0A%09
 %0A%09public
@@ -1338,166 +1338,8 @@
 ) %7B%0A
-%09%09%09final String annotationModid = (String) data.getAnnotationData().get(%22modid%22);%0A%09%09%09final Boolean client = (Boolean) data.getAnnotationData().get(%22client%22);%0A
 %09%09%09f
@@ -1434,49 +1434,36 @@
 if (
-modid.equals(annotationModid)) %7B%0A%09%09%09%09if (
+canBeCalled(modid, data) && 
 ModL
@@ -1498,25 +1498,24 @@
 onModid)) %7B%0A
-%09
 %09%09%09%09LOGGER.i
@@ -1588,17 +1588,16 @@
 modid);%0A
-%09
 %09%09%09%09try 
@@ -1599,25 +1599,24 @@
 %09try %7B%0A%09%09%09%09%09
-%09
 Class.forNam
@@ -1700,17 +1700,16 @@
 ruct();%0A
-%09
 %09%09%09%09%7D ca
@@ -1829,25 +1829,24 @@
  ex) %7B%0A%09%09%09%09%09
-%09
 LOGGER.error
@@ -1918,25 +1918,24 @@
 ame(), ex);%0A
-%09
 %09%09%09%09%09throw n
@@ -1963,15 +1963,8 @@
 x);%0A
-%09%09%09%09%09%7D%0A
 %09%09%09%09

Replace the calls with the new method