下面列出了怎么用net.minecraftforge.fml.common.discovery.ASMDataTable的API类实例代码及写法,或者点击链接到github查看源代码。
public static List<CustomStuffPlugin> getPluginInstances(ASMDataTable asmDataTable)
{
String annotationName = CS4Plugin.class.getCanonicalName();
Set<ASMDataTable.ASMData> asmDatas = asmDataTable.getAll(annotationName);
List<CustomStuffPlugin> instances = Lists.newArrayList();
for (ASMDataTable.ASMData asmData : asmDatas)
{
try
{
Class<?> asmClass = Class.forName(asmData.getClassName());
if (CustomStuffPlugin.class.isAssignableFrom(asmClass))
{
Class<? extends CustomStuffPlugin> instanceClass = asmClass.asSubclass(CustomStuffPlugin.class);
CustomStuffPlugin instance = instanceClass.newInstance();
instances.add(instance);
}
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e)
{
e.printStackTrace();
}
}
return instances;
}
public static void scrapeData(ASMDataTable dataTable) {
ImmutableList.Builder<IConfigureNEI> plugins = ImmutableList.builder();
for (ASMDataTable.ASMData data : dataTable.getAll(NEIPlugin.class.getName())) {
try {
Class<?> pluginClass = Class.forName(data.getClassName());
if (IConfigureNEI.class.isAssignableFrom(pluginClass)) {
IConfigureNEI pluginInstance = (IConfigureNEI) pluginClass.newInstance();
plugins.add(pluginInstance);
} else {
LogHelper.error("Found class with annotation @NEIPlugin but class does not implement IConfigureNEI.. Class: " + data.getClassName());
}
} catch (Exception e) {
LogHelper.fatalError("Fatal exception occurred whilst loading a plugin! Class: %s", e, data.getClassName());
}
}
NEIInitialization.plugins = plugins.build();
}
/**
* Loads classes with a specific annotation from an asm data table.
*
* Borrowed from JEI's source code, which is licensed under the MIT license.
*
* @param <T> The type of class to load.
* @param asm The asm data table to load classes from.
* @param anno The annotation marking classes of interest.
* @param type The class type to load, as to get around Type erasure.
* @return A list of the loaded classes, instantiated.
*/
@Nonnull
private static <T> List<T> getInstances(ASMDataTable asm, Class anno, Class<T> type) {
final List<T> instances = new ArrayList<>();
for (ASMDataTable.ASMData asmData : asm.getAll(anno.getCanonicalName())) {
try {
T instance = Class.forName(asmData.getClassName()).asSubclass(type).newInstance();
instances.add(instance);
} catch (ClassNotFoundException | NoClassDefFoundError | IllegalAccessException | InstantiationException e) {
AgriCore.getLogger("agricraft-plugins").debug(
"%nFailed to load AgriPlugin%n\tOf class: {0}!%n\tFor annotation: {1}!%n\tAs Instanceof: {2}!",
asmData.getClassName(),
anno.getCanonicalName(),
type.getCanonicalName()
);
}
}
return instances;
}
public void fillAllHolders(ASMDataTable data) {
final Map<Field, Class<?>> classTargetToSource = Maps.newHashMap();
final Map<Field, Class<?>> fieldTargetToSource = Maps.newHashMap();
for (ASMData target : data.getAll(TypeVariableHolder.class.getName()))
findTargets(target, classTargetToSource, fieldTargetToSource);
fillFields(classTargetToSource, fieldTargetToSource);
}
private static <A> void fillTargetFields(final ApiProviderRegistry<A> registry, ASMDataTable table, Class<? extends Annotation> fieldMarker, Class<A> interfaceMarker) {
final ClassInfoCache clsCache = new ClassInfoCache();
final Set<ASMData> targets = table.getAll(fieldMarker.getName());
for (ASMData data : targets)
fillTargetField(clsCache, registry, data, interfaceMarker);
}
public <A> ApiProviderRegistry<A> createApi(Class<? extends Annotation> fieldMarker, Class<A> interfaceMarker, ASMDataTable table, ApiProviderSetup<A> registrySetup) {
Preconditions.checkState(apis.add(fieldMarker), "Duplicate API registration on %s", fieldMarker);
final ApiProviderRegistry<A> registry = new ApiProviderRegistry<>(interfaceMarker);
registrySetup.setup(registry);
registry.freeze();
fillTargetFields(registry, table, fieldMarker, interfaceMarker);
return registry;
}
private void initPlugins(ASMDataTable asmDataTable)
{
plugins = PluginHelper.getPluginInstances(asmDataTable);
plugins.forEach(plugin -> plugin.registerContent(contentRegistry));
}
public <A> void createApi(Class<? extends Annotation> fieldMarker, Class<A> interfaceMarker, ASMDataTable table, ApiProviderRegistry<A> registry) {
Preconditions.checkState(apis.add(fieldMarker), "Duplicate API registration on %s", fieldMarker);
Preconditions.checkState(registry.isFrozen(), "Registry must be frozen");
fillTargetFields(registry, table, fieldMarker, interfaceMarker);
}
public ClassSourceCollector(ASMDataTable table) {
this.table = table;
}