Add ability to Auto register capabilities via annotation (#8972)

This commit is contained in:
LexManos authored and GitHub committed 2022-08-17 15:06:35 -07:00
1 parent 4f2f5c02a0
commit c0aa8dd808
30 files changed
+134 -58

No files matched your search

@@ -226,7 +226,7 @@ public class ForgeMod
* Stock holder set type that represents any/all values in a registry. Can be used in a holderset object with {@code { "type": "forge:any" }}
*/
public static final RegistryObject<HolderSetType> ANY_HOLDER_SET = HOLDER_SET_TYPES.register("any", () -> AnyHolderSet::codec);
/**
* Stock holder set type that represents an intersection of other holdersets. Can be used in a holderset object with {@code { "type": "forge:and", "values": [list of holdersets] }}
*/
@@ -242,7 +242,7 @@ public class ForgeMod
* Can be used in a holderset object with {@code { "type": "forge:not", "value": holderset }}</p>
*/
public static final RegistryObject<HolderSetType> NOT_HOLDER_SET = HOLDER_SET_TYPES.register("not", () -> NotHolderSet::codec);
private static final DeferredRegister<FluidType> VANILLA_FLUID_TYPES = DeferredRegister.create(ForgeRegistries.Keys.FLUID_TYPES, "minecraft");
public static final RegistryObject<FluidType> EMPTY_TYPE = VANILLA_FLUID_TYPES.register("empty", () ->
@@ -437,7 +437,6 @@ public class ForgeMod
CrashReportCallables.registerCrashCallable("Forge", ()->ForgeVersion.getGroup()+":"+ForgeVersion.getVersion());
final IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
modEventBus.addListener(this::registerCapabilities);
modEventBus.addListener(this::preInit);
modEventBus.addListener(this::gatherData);
modEventBus.addListener(this::loadComplete);
@@ -470,13 +469,6 @@ public class ForgeMod
ForgeRegistries.ITEMS.tags().addOptionalTagDefaults(Tags.Items.ENCHANTING_FUELS, Set.of(ForgeRegistries.ITEMS.getDelegateOrThrow(Items.LAPIS_LAZULI)));
}
public void registerCapabilities(RegisterCapabilitiesEvent event)
{
CapabilityItemHandler.register(event);
CapabilityFluidHandler.register(event);
CapabilityEnergy.register(event);
}
public void preInit(FMLCommonSetupEvent evt)
{
VersionChecker.startVersionCheck();
@@ -0,0 +1,18 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.common.capabilities;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Marks a class to be automatically registered in Forge's {@link CapabilityManager}
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface AutoRegisterCapability {}
@@ -9,7 +9,9 @@ import net.minecraftforge.fml.ModLoader;
import net.minecraftforge.forgespi.language.ModFileScanData;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.objectweb.asm.Type;
import java.util.Comparator;
import java.util.IdentityHashMap;
import java.util.List;
@@ -35,7 +37,6 @@ public enum CapabilityManager
{
realName = realName.intern();
cap = (Capability<T>)providers.computeIfAbsent(realName, Capability::new);
}
@@ -59,9 +60,24 @@ public enum CapabilityManager
}
// INTERNAL
private static final Type AUTO_REGISTER = Type.getType(AutoRegisterCapability.class);
private final IdentityHashMap<String, Capability<?>> providers = new IdentityHashMap<>();
public void injectCapabilities(List<ModFileScanData> data)
{
var autos = data.stream()
.flatMap(e -> e.getAnnotations().stream())
.filter(a -> AUTO_REGISTER.equals(a.annotationType()))
.map(a -> a.clazz())
.distinct()
.sorted(Comparator.comparing(Type::toString))
.toList();
for (var auto : autos)
{
LOGGER.debug(CAPABILITIES, "Attempting to automatically register: " + auto);
get(auto.getInternalName(), true);
}
var event = new RegisterCapabilitiesEvent();
ModLoader.get().postEvent(event);
}
@@ -73,6 +73,7 @@ public abstract class CapabilityProvider<B extends ICapabilityProviderImpl<B>> i
this.initialized = true;
}
@SuppressWarnings("unchecked")
@NotNull
B getProvider()
{
@@ -0,0 +1,26 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.common.capabilities;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
import net.minecraftforge.items.IItemHandler;
import static net.minecraftforge.common.capabilities.CapabilityManager.get;
/*
* References to Forge's built in capabilities.
* Modders are recommended to use their own CapabilityTokens for 3rd party caps to maintain soft dependencies.
* However, since nobody has a soft dependency on Forge, we expose this as API.
*/
public class ForgeCapabilities
{
public static final Capability<IEnergyStorage> ENERGY = get(new CapabilityToken<>(){});
public static final Capability<IFluidHandler> FLUID_HANDLER = get(new CapabilityToken<>(){});
public static final Capability<IFluidHandlerItem> FLUID_HANDLER_ITEM = get(new CapabilityToken<>(){});
public static final Capability<IItemHandler> ITEM_HANDLER = get(new CapabilityToken<>(){});
}
@@ -5,8 +5,10 @@
package net.minecraftforge.common.capabilities;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
@ApiStatus.Internal // Modders should use ICapabilityProvider, this is for Forge
public interface ICapabilityProviderImpl<B extends ICapabilityProviderImpl<B>> extends ICapabilityProvider
{
boolean areCapsCompatible(CapabilityProvider<B> other);