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.
This commit is contained in:
Jonathan Coates 2022-04-08 10:12:41 +01:00
parent 2a92794da3
commit 79b1872cab
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
2 changed files with 22 additions and 1 deletions

View File

@ -45,7 +45,7 @@ public static IPeripheral getPeripheral( @Nonnull World world, @Nonnull BlockPos
for( Capability<?> capability : capabilities )
{
LazyOptional<?> wrapper = tile.getCapability( capability );
LazyOptional<?> wrapper = CapabilityUtil.getCapability( tile, capability, side );
wrapper.ifPresent( contents -> {
List<NamedMethod<PeripheralMethod>> capabilityMethods = PeripheralMethod.GENERATOR.getMethods( contents.getClass() );
if( capabilityMethods.isEmpty() ) return;

View File

@ -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 static <T> T unwrapUnsafe( LazyOptional<T> p )
{
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 <T> The type of the underlying capability.
* @return The extracted capability, if present.
*/
@Nonnull
public static <T> LazyOptional<T> getCapability( ICapabilityProvider provider, Capability<T> capability, Direction side )
{
LazyOptional<T> cap = provider.getCapability( capability );
return cap.isPresent() ? cap : provider.getCapability( capability, side );
}
}