de.florianmichael.asmfabricloader.api.EarlyRiser Maven / Gradle / Ivy
/*
* This file is part of AsmFabricLoader - https://github.com/FlorianMichael/AsmFabricLoader
* Copyright (C) 2023-2024 FlorianMichael/EnZaXD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.florianmichael.asmfabricloader.api;
import de.florianmichael.asmfabricloader.loader.classloading.AFLConstants;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.LanguageAdapterException;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.impl.metadata.EntrypointMetadata;
import net.fabricmc.loader.impl.metadata.LoaderModMetadata;
import net.fabricmc.loader.impl.util.DefaultLanguageAdapter;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
/**
* Utility to manually create entrypoints from mods before fabric loader is doing it.
* This can be useful if you want to invoke entrypoints in language adapter loading stage.
*/
public class EarlyRiser {
/**
* Invokes a given consumer for all early entrypoints with a specific name and type.
*
* @param name the entrypoint name
* @param type the entrypoint type
* @param consumer the consumer to invoke
*/
public static void invokeEntrypoints(final String name, final Class type, final Consumer consumer) {
getEarlyEntrypoints(name, type).forEach(consumer);
}
/**
* Get all early entrypoints for a specific name and type.
*
* @param name the entrypoint name
* @param type the entrypoint type
* @return the list of entrypoints
*/
public static List getEarlyEntrypoints(final String name, final Class type) {
final List entrypoints = new ArrayList<>();
for (ModContainer mod : FabricLoader.getInstance().getAllMods()) {
if (mod.getMetadata() instanceof LoaderModMetadata modMetadata) {
final List entrypointMetadata = modMetadata.getEntrypoints(name);
for (EntrypointMetadata metadata : entrypointMetadata) {
final T entrypoint = createEntrypoint(mod, metadata.getValue(), type);
if (entrypoint != null) {
entrypoints.add(entrypoint);
}
}
}
}
return entrypoints;
}
/**
* Create an entrypoint from a mod. Failure will result in a null return value and a log message.
*
* @param mod the mod container
* @param value the entrypoint value
* @param type the entrypoint type
* @return the entrypoint instance
*/
public static T createEntrypoint(final ModContainer mod, final String value, final Class type) {
try {
return DefaultLanguageAdapter.INSTANCE.create(mod, value, type);
} catch (LanguageAdapterException e) {
AFLConstants.LOGGER.error("Failed to load early entrypoint {} for mod {}", value, mod.getMetadata().getId(), e);
return null;
}
}
}