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 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);
					}
				}
			}
		}
	}
	
}

@@ -1669,32 +1669,108 @@
 ).get(%22modid%22);%0A
+%09%09%09final Boolean client = (Boolean) data.getAnnotationData().get(%22client%22);%0A
 %09%09%09final String 
@@ -2424,10 +2424,363 @@
 %09%7D%0A%09%7D%0A%09%0A
+%09private static boolean canBeCalled(String modid, AnnotationData data) %7B%0A%09%09final String annotationModid = (String) data.getAnnotationData().get(%22modid%22);%0A%09%09final Boolean client = (Boolean) data.getAnnotationData().get(%22client%22);%0A%09%09return modid.equals(annotationModid) && (client == null %7C%7C !client %7C%7C client && FMLEnvironment.dist == Dist.CLIENT);%0A%09%7D%0A%09%0A
 %7D%0A

Add method canBeCalled to determine if the class can be called with this annotation based on the values