1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-12-15 01:48:05 +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
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() 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 ) ); NetworkHandler.sendToServer( new ContinueUploadMessage( computer.getInstanceID(), true ) );
} }

View File

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

View File

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

View File

@@ -87,7 +87,7 @@ public final class CommonHooks
public static void onServerStarting( FMLServerStartingEvent event ) public static void onServerStarting( FMLServerStartingEvent event )
{ {
MinecraftServer server = event.getServer(); MinecraftServer server = event.getServer();
if( server instanceof DedicatedServer && ((DedicatedServer) server).getProperties().enableJmxMonitoring ) if( server instanceof DedicatedServer dediServer && dediServer.getProperties().enableJmxMonitoring )
{ {
ComputerMBean.register(); 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. // Ensure file configs are reloaded. Forge should probably do this, so worth checking in the future.
CommentedConfig config = event.getConfig().getConfigData(); CommentedConfig config = event.getConfig().getConfigData();
if( config instanceof CommentedFileConfig ) ((CommentedFileConfig) config).load(); if( config instanceof CommentedFileConfig loadable ) loadable.load();
sync(); sync();
} }

View File

@@ -67,6 +67,6 @@ public enum UserLevel implements Predicate<CommandSourceStack>
Entity sender = source.getEntity(); Entity sender = source.getEntity();
return server.isDedicatedServer() return server.isDedicatedServer()
? source.getEntity() == null && source.hasPermission( 4 ) && source.getTextName().equals( "Server" ) ? 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 ); BlockEntity tile = world.getBlockEntity( pos );
super.onRemove( block, world, pos, replace, bool ); super.onRemove( block, world, pos, replace, bool );
world.removeBlockEntity( pos ); world.removeBlockEntity( pos );
if( tile instanceof TileGeneric ) ((TileGeneric) tile).destroy(); if( tile instanceof TileGeneric generic ) generic.destroy();
} }
@Nonnull @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 ) 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 ); 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 @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 ) 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 ); BlockEntity tile = world.getBlockEntity( pos );
if( tile instanceof TileGeneric ) ((TileGeneric) tile).onNeighbourChange( neighbourPos ); if( tile instanceof TileGeneric generic ) generic.onNeighbourChange( neighbourPos );
} }
@Override @Override
public final void onNeighborChange( BlockState state, LevelReader world, BlockPos pos, BlockPos neighbour ) public final void onNeighborChange( BlockState state, LevelReader world, BlockPos pos, BlockPos neighbour )
{ {
BlockEntity tile = world.getBlockEntity( pos ); BlockEntity tile = world.getBlockEntity( pos );
if( tile instanceof TileGeneric ) ((TileGeneric) tile).onNeighbourTileEntityChange( neighbour ); if( tile instanceof TileGeneric generic ) generic.onNeighbourTileEntityChange( neighbour );
} }
@Override @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 ) public void tick( @Nonnull BlockState state, ServerLevel world, @Nonnull BlockPos pos, @Nonnull Random rand )
{ {
BlockEntity te = world.getBlockEntity( pos ); BlockEntity te = world.getBlockEntity( pos );
if( te instanceof TileGeneric ) ((TileGeneric) te).blockTick(); if( te instanceof TileGeneric generic ) generic.blockTick();
} }
@Nullable @Nullable

View File

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

View File

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

View File

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

View File

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

View File

@@ -62,12 +62,9 @@ public final class PrintoutRecipe extends CustomRecipe
ItemStack stack = inventory.getItem( x + y * inventory.getWidth() ); ItemStack stack = inventory.getItem( x + y * inventory.getWidth() );
if( !stack.isEmpty() ) 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 ) if( printouts == null ) printouts = new ItemStack[9];
{
printouts = new ItemStack[9];
}
printouts[numPrintouts] = stack; printouts[numPrintouts] = stack;
numPages += ItemPrintout.getPageCount( stack ); numPages += ItemPrintout.getPageCount( stack );
numPrintouts++; numPrintouts++;

View File

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

View File

@@ -210,7 +210,7 @@ public class DiskDrivePeripheral implements IPeripheral
@Override @Override
public boolean equals( IPeripheral other ) 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 @Nonnull

View File

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

View File

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

View File

@@ -313,7 +313,7 @@ public final class TilePrinter extends TileGeneric implements DefaultSidedInvent
{ {
Item item = stack.getItem(); Item item = stack.getItem();
return item == Items.PAPER 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() private boolean canInputPage()

View File

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