From 79b1872cabab391831b08fcd8dea696003e16b3c Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Fri, 8 Apr 2022 10:12:41 +0100 Subject: [PATCH] Fall back to the given side if the internal one isn't provided See #1061, closes #1064. Nobody ever seems to implement this correctly (though it's better than 1.12, at least we've not seen any crashes), and this isn't a fight I care enough about fighting any more. --- .../generic/GenericPeripheralProvider.java | 2 +- .../shared/util/CapabilityUtil.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) 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 ); + } }