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

More instanceof pattern matching

This commit is contained in:
Jonathan Coates 2021-11-28 15:58:30 +00:00
parent 095101831c
commit 2418cfb87b
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
18 changed files with 29 additions and 34 deletions

View File

@ -209,7 +209,7 @@ public abstract class ComputerScreenBase<T extends ContainerComputerBase> extend
private void continueUpload()
{
if( minecraft.screen instanceof OptionScreen ) ((OptionScreen) minecraft.screen).disable();
if( minecraft.screen instanceof OptionScreen screen ) screen.disable();
NetworkHandler.sendToServer( new ContinueUploadMessage( computer.getInstanceID(), true ) );
}

View File

@ -100,7 +100,7 @@ public class BinaryWritableHandle extends HandleGeneric
try
{
// Technically this is not needed
if( writer instanceof FileChannel ) ((FileChannel) writer).force( false );
if( writer instanceof FileChannel channel ) channel.force( false );
}
catch( IOException ignored )
{

View File

@ -153,7 +153,7 @@ public class WebsocketHandle implements Closeable
return MethodResult.of();
}
else if( event.length >= 2 && timeoutId != -1 && Objects.equal( event[0], TIMER_EVENT )
&& event[1] instanceof Number && ((Number) event[1]).intValue() == timeoutId )
&& event[1] instanceof Number id && id.intValue() == timeoutId )
{
// If we received a matching timer event then abort.
return MethodResult.of();

View File

@ -87,7 +87,7 @@ public final class CommonHooks
public static void onServerStarting( FMLServerStartingEvent event )
{
MinecraftServer server = event.getServer();
if( server instanceof DedicatedServer && ((DedicatedServer) server).getProperties().enableJmxMonitoring )
if( server instanceof DedicatedServer dediServer && dediServer.getProperties().enableJmxMonitoring )
{
ComputerMBean.register();
}

View File

@ -384,7 +384,7 @@ public final class Config
{
// Ensure file configs are reloaded. Forge should probably do this, so worth checking in the future.
CommentedConfig config = event.getConfig().getConfigData();
if( config instanceof CommentedFileConfig ) ((CommentedFileConfig) config).load();
if( config instanceof CommentedFileConfig loadable ) loadable.load();
sync();
}

View File

@ -67,6 +67,6 @@ public enum UserLevel implements Predicate<CommandSourceStack>
Entity sender = source.getEntity();
return server.isDedicatedServer()
? source.getEntity() == null && source.hasPermission( 4 ) && source.getTextName().equals( "Server" )
: sender instanceof Player && ((Player) sender).getGameProfile().getName().equalsIgnoreCase( server.getServerModName() );
: sender instanceof Player player && player.getGameProfile().getName().equalsIgnoreCase( server.getServerModName() );
}
}

View File

@ -44,7 +44,7 @@ public abstract class BlockGeneric extends BaseEntityBlock
BlockEntity tile = world.getBlockEntity( pos );
super.onRemove( block, world, pos, replace, bool );
world.removeBlockEntity( pos );
if( tile instanceof TileGeneric ) ((TileGeneric) tile).destroy();
if( tile instanceof TileGeneric generic ) generic.destroy();
}
@Nonnull
@ -53,7 +53,7 @@ public abstract class BlockGeneric extends BaseEntityBlock
public final InteractionResult use( @Nonnull BlockState state, Level world, @Nonnull BlockPos pos, @Nonnull Player player, @Nonnull InteractionHand hand, @Nonnull BlockHitResult hit )
{
BlockEntity tile = world.getBlockEntity( pos );
return tile instanceof TileGeneric ? ((TileGeneric) tile).onActivate( player, hand, hit ) : InteractionResult.PASS;
return tile instanceof TileGeneric generic ? generic.onActivate( player, hand, hit ) : InteractionResult.PASS;
}
@Override
@ -61,14 +61,14 @@ public abstract class BlockGeneric extends BaseEntityBlock
public final void neighborChanged( @Nonnull BlockState state, Level world, @Nonnull BlockPos pos, @Nonnull Block neighbourBlock, @Nonnull BlockPos neighbourPos, boolean isMoving )
{
BlockEntity tile = world.getBlockEntity( pos );
if( tile instanceof TileGeneric ) ((TileGeneric) tile).onNeighbourChange( neighbourPos );
if( tile instanceof TileGeneric generic ) generic.onNeighbourChange( neighbourPos );
}
@Override
public final void onNeighborChange( BlockState state, LevelReader world, BlockPos pos, BlockPos neighbour )
{
BlockEntity tile = world.getBlockEntity( pos );
if( tile instanceof TileGeneric ) ((TileGeneric) tile).onNeighbourTileEntityChange( neighbour );
if( tile instanceof TileGeneric generic ) generic.onNeighbourTileEntityChange( neighbour );
}
@Override
@ -76,7 +76,7 @@ public abstract class BlockGeneric extends BaseEntityBlock
public void tick( @Nonnull BlockState state, ServerLevel world, @Nonnull BlockPos pos, @Nonnull Random rand )
{
BlockEntity te = world.getBlockEntity( pos );
if( te instanceof TileGeneric ) ((TileGeneric) te).blockTick();
if( te instanceof TileGeneric generic ) generic.blockTick();
}
@Nullable

View File

@ -59,7 +59,7 @@ public abstract class BlockComputerBase<T extends TileComputerBase> extends Bloc
super.onPlace( state, world, pos, oldState, isMoving );
BlockEntity tile = world.getBlockEntity( pos );
if( tile instanceof TileComputerBase ) ((TileComputerBase) tile).updateInput();
if( tile instanceof TileComputerBase computer ) computer.updateInput();
}
@Override

View File

@ -34,7 +34,7 @@ public final class BlockNamedEntityLootCondition implements LootItemCondition
public boolean test( LootContext lootContext )
{
BlockEntity tile = lootContext.getParamOrNull( LootContextParams.BLOCK_ENTITY );
return tile instanceof Nameable && ((Nameable) tile).hasCustomName();
return tile instanceof Nameable nameable && nameable.hasCustomName();
}
@Nonnull

View File

@ -34,7 +34,7 @@ public final class HasComputerIdLootCondition implements LootItemCondition
public boolean test( LootContext lootContext )
{
BlockEntity tile = lootContext.getParamOrNull( LootContextParams.BLOCK_ENTITY );
return tile instanceof IComputerTile && ((IComputerTile) tile).getComputerID() >= 0;
return tile instanceof IComputerTile computer && computer.getComputerID() >= 0;
}
@Nonnull

View File

@ -34,7 +34,7 @@ public final class PlayerCreativeLootCondition implements LootItemCondition
public boolean test( LootContext lootContext )
{
Entity entity = lootContext.getParamOrNull( LootContextParams.THIS_ENTITY );
return entity instanceof Player && ((Player) entity).getAbilities().instabuild;
return entity instanceof Player player && player.getAbilities().instabuild;
}
@Nonnull

View File

@ -62,12 +62,9 @@ public final class PrintoutRecipe extends CustomRecipe
ItemStack stack = inventory.getItem( x + y * inventory.getWidth() );
if( !stack.isEmpty() )
{
if( stack.getItem() instanceof ItemPrintout && ((ItemPrintout) stack.getItem()).getType() != ItemPrintout.Type.BOOK )
if( stack.getItem() instanceof ItemPrintout printout && printout.getType() != ItemPrintout.Type.BOOK )
{
if( printouts == null )
{
printouts = new ItemStack[9];
}
if( printouts == null ) printouts = new ItemStack[9];
printouts[numPrintouts] = stack;
numPages += ItemPrintout.getPageCount( stack );
numPrintouts++;

View File

@ -62,13 +62,13 @@ public class BlockDiskDrive extends BlockGeneric
@Override
public void playerDestroy( @Nonnull Level world, @Nonnull Player player, @Nonnull BlockPos pos, @Nonnull BlockState state, @Nullable BlockEntity te, @Nonnull ItemStack stack )
{
if( te instanceof Nameable && ((Nameable) te).hasCustomName() )
if( te instanceof Nameable nameable && nameable.hasCustomName() )
{
player.awardStat( Stats.BLOCK_MINED.get( this ) );
player.causeFoodExhaustion( 0.005F );
ItemStack result = new ItemStack( this );
result.setHoverName( ((Nameable) te).getCustomName() );
result.setHoverName( nameable.getCustomName() );
popResource( world, pos, result );
}
else
@ -80,10 +80,9 @@ public class BlockDiskDrive extends BlockGeneric
@Override
public void setPlacedBy( @Nonnull Level world, @Nonnull BlockPos pos, @Nonnull BlockState state, LivingEntity placer, ItemStack stack )
{
if( stack.hasCustomHoverName() )
if( stack.hasCustomHoverName() && world.getBlockEntity( pos ) instanceof TileDiskDrive drive )
{
BlockEntity tileentity = world.getBlockEntity( pos );
if( tileentity instanceof TileDiskDrive ) ((TileDiskDrive) tileentity).customName = stack.getHoverName();
drive.customName = stack.getHoverName();
}
}

View File

@ -210,7 +210,7 @@ public class DiskDrivePeripheral implements IPeripheral
@Override
public boolean equals( IPeripheral other )
{
return this == other || other instanceof DiskDrivePeripheral && ((DiskDrivePeripheral) other).diskDrive == diskDrive;
return this == other || other instanceof DiskDrivePeripheral drive && drive.diskDrive == diskDrive;
}
@Nonnull

View File

@ -58,13 +58,13 @@ public class BlockPrinter extends BlockGeneric
@Override
public void playerDestroy( @Nonnull Level world, @Nonnull Player player, @Nonnull BlockPos pos, @Nonnull BlockState state, @Nullable BlockEntity te, @Nonnull ItemStack stack )
{
if( te instanceof Nameable && ((Nameable) te).hasCustomName() )
if( te instanceof Nameable nameable && nameable.hasCustomName() )
{
player.awardStat( Stats.BLOCK_MINED.get( this ) );
player.causeFoodExhaustion( 0.005F );
ItemStack result = new ItemStack( this );
result.setHoverName( ((Nameable) te).getCustomName() );
result.setHoverName( nameable.getCustomName() );
popResource( world, pos, result );
}
else
@ -76,10 +76,9 @@ public class BlockPrinter extends BlockGeneric
@Override
public void setPlacedBy( @Nonnull Level world, @Nonnull BlockPos pos, @Nonnull BlockState state, LivingEntity placer, ItemStack stack )
{
if( stack.hasCustomHoverName() )
if( stack.hasCustomHoverName() && world.getBlockEntity( pos ) instanceof TilePrinter printer )
{
BlockEntity tileentity = world.getBlockEntity( pos );
if( tileentity instanceof TilePrinter ) ((TilePrinter) tileentity).customName = stack.getHoverName();
printer.customName = stack.getHoverName();
}
}
}

View File

@ -168,7 +168,7 @@ public class PrinterPeripheral implements IPeripheral
@Override
public boolean equals( IPeripheral other )
{
return other instanceof PrinterPeripheral && ((PrinterPeripheral) other).printer == printer;
return this == other || (other instanceof PrinterPeripheral otherPrinter && otherPrinter.printer == printer);
}
@Nonnull

View File

@ -313,7 +313,7 @@ public final class TilePrinter extends TileGeneric implements DefaultSidedInvent
{
Item item = stack.getItem();
return item == Items.PAPER
|| (item instanceof ItemPrintout && ((ItemPrintout) item).getType() == ItemPrintout.Type.PAGE);
|| (item instanceof ItemPrintout printout && printout.getType() == ItemPrintout.Type.PAGE);
}
private boolean canInputPage()

View File

@ -115,7 +115,7 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces
@Override
public void updateUpgradeNBTData()
{
if( entity instanceof Player ) ((Player) entity).getInventory().setChanged();
if( entity instanceof Player player ) player.getInventory().setChanged();
}
@Override