1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-14 04:00:30 +00:00

Make capability invalidation callbacks less strict

Forge!! *shakes fist*.
This commit is contained in:
Jonathan Coates 2021-11-28 12:47:08 +00:00
parent 306e06a79a
commit 9d44f1ca66
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
5 changed files with 18 additions and 17 deletions

View File

@ -38,13 +38,13 @@ public final class Peripherals
} }
@Nullable @Nullable
public static IPeripheral getPeripheral( World world, BlockPos pos, Direction side, NonNullConsumer<LazyOptional<IPeripheral>> invalidate ) public static IPeripheral getPeripheral( World world, BlockPos pos, Direction side, NonNullConsumer<Object> invalidate )
{ {
return World.isInWorldBounds( pos ) && !world.isClientSide ? getPeripheralAt( world, pos, side, invalidate ) : null; return World.isInWorldBounds( pos ) && !world.isClientSide ? getPeripheralAt( world, pos, side, invalidate ) : null;
} }
@Nullable @Nullable
private static IPeripheral getPeripheralAt( World world, BlockPos pos, Direction side, NonNullConsumer<LazyOptional<IPeripheral>> invalidate ) private static IPeripheral getPeripheralAt( World world, BlockPos pos, Direction side, NonNullConsumer<? super Object> invalidate )
{ {
TileEntity block = world.getBlockEntity( pos ); TileEntity block = world.getBlockEntity( pos );
if( block != null ) if( block != null )

View File

@ -39,7 +39,6 @@ import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent; import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent; import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.common.util.NonNullConsumer; import net.minecraftforge.common.util.NonNullConsumer;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -58,7 +57,7 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT
private boolean on = false; private boolean on = false;
boolean startOn = false; boolean startOn = false;
private boolean fresh = false; private boolean fresh = false;
private final NonNullConsumer<LazyOptional<IPeripheral>>[] invalidate; private final NonNullConsumer<Object>[] invalidate;
private final ComputerFamily family; private final ComputerFamily family;
@ -69,7 +68,7 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT
// We cache these so we can guarantee we only ever register one listener for adjacent capabilities. // We cache these so we can guarantee we only ever register one listener for adjacent capabilities.
@SuppressWarnings( { "unchecked", "rawtypes" } ) @SuppressWarnings( { "unchecked", "rawtypes" } )
NonNullConsumer<LazyOptional<IPeripheral>>[] invalidate = this.invalidate = new NonNullConsumer[6]; NonNullConsumer<Object>[] invalidate = this.invalidate = new NonNullConsumer[6];
for( Direction direction : Direction.values() ) for( Direction direction : Direction.values() )
{ {
invalidate[direction.ordinal()] = o -> updateInput( direction ); invalidate[direction.ordinal()] = o -> updateInput( direction );

View File

@ -9,6 +9,7 @@ import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.peripheral.PeripheralType; import dan200.computercraft.api.peripheral.PeripheralType;
import dan200.computercraft.core.asm.NamedMethod; import dan200.computercraft.core.asm.NamedMethod;
import dan200.computercraft.core.asm.PeripheralMethod; import dan200.computercraft.core.asm.PeripheralMethod;
import dan200.computercraft.shared.util.CapabilityUtil;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction; import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@ -34,7 +35,7 @@ public class GenericPeripheralProvider
} }
@Nullable @Nullable
public static IPeripheral getPeripheral( @Nonnull World world, @Nonnull BlockPos pos, @Nonnull Direction side, NonNullConsumer<LazyOptional<IPeripheral>> invalidate ) public static IPeripheral getPeripheral( @Nonnull World world, @Nonnull BlockPos pos, @Nonnull Direction side, NonNullConsumer<Object> invalidate )
{ {
TileEntity tile = world.getBlockEntity( pos ); TileEntity tile = world.getBlockEntity( pos );
if( tile == null ) return null; if( tile == null ) return null;
@ -52,7 +53,7 @@ public class GenericPeripheralProvider
if( capabilityMethods.isEmpty() ) return; if( capabilityMethods.isEmpty() ) return;
saturated.addMethods( contents, capabilityMethods ); saturated.addMethods( contents, capabilityMethods );
wrapper.addListener( cast( invalidate ) ); CapabilityUtil.addListener( wrapper, invalidate );
} ); } );
} }
@ -91,10 +92,4 @@ public class GenericPeripheralProvider
} }
} }
} }
@SuppressWarnings( { "unchecked", "rawtypes" } )
private static <T> NonNullConsumer<T> cast( NonNullConsumer<?> consumer )
{
return (NonNullConsumer) consumer;
}
} }

View File

@ -15,7 +15,6 @@ import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants; import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.common.util.NonNullConsumer; import net.minecraftforge.common.util.NonNullConsumer;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -38,7 +37,7 @@ public final class WiredModemLocalPeripheral
private String type; private String type;
private IPeripheral peripheral; private IPeripheral peripheral;
private final NonNullConsumer<LazyOptional<IPeripheral>> invalidate; private final NonNullConsumer<Object> invalidate;
public WiredModemLocalPeripheral( @Nonnull Runnable invalidate ) public WiredModemLocalPeripheral( @Nonnull Runnable invalidate )
{ {

View File

@ -35,12 +35,20 @@ public final class CapabilityUtil
} }
} }
public static <T> void addListener( LazyOptional<T> p, NonNullConsumer<? super LazyOptional<T>> invalidate )
{
// We can make this safe with invalidate::accept, but then we're allocating it's just kind of absurd.
@SuppressWarnings( "unchecked" )
NonNullConsumer<LazyOptional<T>> safeInvalidate = (NonNullConsumer<LazyOptional<T>>) invalidate;
p.addListener( safeInvalidate );
}
@Nullable @Nullable
public static <T> T unwrap( LazyOptional<T> p, NonNullConsumer<LazyOptional<T>> invalidate ) public static <T> T unwrap( LazyOptional<T> p, NonNullConsumer<? super LazyOptional<T>> invalidate )
{ {
if( !p.isPresent() ) return null; if( !p.isPresent() ) return null;
p.addListener( invalidate ); addListener( p, invalidate );
return p.orElseThrow( NullPointerException::new ); return p.orElseThrow( NullPointerException::new );
} }