diff --git a/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheralProvider.java b/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheralProvider.java index 35ff9e668..067bc13e0 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheralProvider.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheralProvider.java @@ -45,7 +45,7 @@ public class GenericPeripheralProvider for( Capability capability : capabilities ) { - LazyOptional wrapper = tile.getCapability( capability ); + LazyOptional wrapper = CapabilityUtil.getCapability( tile, capability, side ); wrapper.ifPresent( contents -> { List> capabilityMethods = PeripheralMethod.GENERATOR.getMethods( contents.getClass() ); if( capabilityMethods.isEmpty() ) return; diff --git a/src/main/java/dan200/computercraft/shared/util/CapabilityUtil.java b/src/main/java/dan200/computercraft/shared/util/CapabilityUtil.java index 482269cd2..1d14e55a6 100644 --- a/src/main/java/dan200/computercraft/shared/util/CapabilityUtil.java +++ b/src/main/java/dan200/computercraft/shared/util/CapabilityUtil.java @@ -5,9 +5,13 @@ */ package dan200.computercraft.shared.util; +import net.minecraft.util.Direction; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.ICapabilityProvider; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.common.util.NonNullConsumer; +import javax.annotation.Nonnull; import javax.annotation.Nullable; public final class CapabilityUtil @@ -57,4 +61,21 @@ public final class CapabilityUtil { return !p.isPresent() ? null : p.orElseThrow( NullPointerException::new ); } + + /** + * Find a capability, preferring the internal/null side but falling back to a given side if a mod doesn't support + * the internal one. + * + * @param provider The capability provider to get the capability from. + * @param capability The capability to get. + * @param side The side we'll fall back to. + * @param The type of the underlying capability. + * @return The extracted capability, if present. + */ + @Nonnull + public static LazyOptional getCapability( ICapabilityProvider provider, Capability capability, Direction side ) + { + LazyOptional cap = provider.getCapability( capability ); + return cap.isPresent() ? cap : provider.getCapability( capability, side ); + } }