diff --git a/build.gradle b/build.gradle index 7d6a89d48..29a5327dc 100644 --- a/build.gradle +++ b/build.gradle @@ -153,7 +153,7 @@ configurations { } dependencies { - checkstyle "com.puppycrawl.tools:checkstyle:8.25" + checkstyle "com.puppycrawl.tools:checkstyle:8.45" minecraft "net.minecraftforge:forge:${mc_version}-${forge_version}" diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml index aaafdfd01..b01b63204 100644 --- a/config/checkstyle/checkstyle.xml +++ b/config/checkstyle/checkstyle.xml @@ -58,13 +58,20 @@ - + + + + - + + + diff --git a/src/main/java/dan200/computercraft/client/render/MonitorHighlightRenderer.java b/src/main/java/dan200/computercraft/client/render/MonitorHighlightRenderer.java index 7dd5725b2..45aeb5435 100644 --- a/src/main/java/dan200/computercraft/client/render/MonitorHighlightRenderer.java +++ b/src/main/java/dan200/computercraft/client/render/MonitorHighlightRenderer.java @@ -47,9 +47,8 @@ public final class MonitorHighlightRenderer BlockPos pos = event.getTarget().getBlockPos(); BlockEntity tile = world.getBlockEntity( pos ); - if( !(tile instanceof TileMonitor) ) return; + if( !(tile instanceof TileMonitor monitor) ) return; - TileMonitor monitor = (TileMonitor) tile; event.setCanceled( true ); // Determine which sides are part of the external faces of the monitor, and so which need to be rendered. diff --git a/src/main/java/dan200/computercraft/client/render/TurtleSmartItemModel.java b/src/main/java/dan200/computercraft/client/render/TurtleSmartItemModel.java index c4b2e0747..528c0a223 100644 --- a/src/main/java/dan200/computercraft/client/render/TurtleSmartItemModel.java +++ b/src/main/java/dan200/computercraft/client/render/TurtleSmartItemModel.java @@ -73,9 +73,8 @@ public class TurtleSmartItemModel implements BakedModel public boolean equals( Object other ) { if( other == this ) return true; - if( !(other instanceof TurtleModelCombination) ) return false; + if( !(other instanceof TurtleModelCombination otherCombo) ) return false; - TurtleModelCombination otherCombo = (TurtleModelCombination) other; return otherCombo.colour == colour && otherCombo.leftUpgrade == leftUpgrade && otherCombo.rightUpgrade == rightUpgrade && diff --git a/src/main/java/dan200/computercraft/core/apis/handles/HandleGeneric.java b/src/main/java/dan200/computercraft/core/apis/handles/HandleGeneric.java index fc1954354..e799c37cd 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/HandleGeneric.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/HandleGeneric.java @@ -96,9 +96,8 @@ public abstract class HandleGeneric protected static SeekableByteChannel asSeekable( Channel channel ) { - if( !(channel instanceof SeekableByteChannel) ) return null; + if( !(channel instanceof SeekableByteChannel seekable) ) return null; - SeekableByteChannel seekable = (SeekableByteChannel) channel; try { seekable.position( seekable.position() ); diff --git a/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequestHandler.java b/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequestHandler.java index 4ef42ff44..003b3da5f 100644 --- a/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequestHandler.java +++ b/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequestHandler.java @@ -100,9 +100,8 @@ public final class HttpRequestHandler extends SimpleChannelInboundHandler 0 ) { @@ -137,9 +136,8 @@ public final class HttpRequestHandler extends SimpleChannelInboundHandler return; } - if( msg instanceof FullHttpResponse ) + if( msg instanceof FullHttpResponse response ) { - FullHttpResponse response = (FullHttpResponse) msg; throw new IllegalStateException( "Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString( CharsetUtil.UTF_8 ) + ')' ); } @@ -76,9 +75,8 @@ public class WebsocketHandler extends SimpleChannelInboundHandler websocket.environment().addTrackingChange( TrackingField.WEBSOCKET_INCOMING, converted.length ); websocket.environment().queueEvent( MESSAGE_EVENT, websocket.address(), converted, true ); } - else if( frame instanceof CloseWebSocketFrame ) + else if( frame instanceof CloseWebSocketFrame closeFrame ) { - CloseWebSocketFrame closeFrame = (CloseWebSocketFrame) frame; websocket.close( closeFrame.statusCode(), closeFrame.reasonText() ); } else if( frame instanceof PingWebSocketFrame ) diff --git a/src/main/java/dan200/computercraft/core/asm/Reflect.java b/src/main/java/dan200/computercraft/core/asm/Reflect.java index 4517fecc3..57a624683 100644 --- a/src/main/java/dan200/computercraft/core/asm/Reflect.java +++ b/src/main/java/dan200/computercraft/core/asm/Reflect.java @@ -52,9 +52,8 @@ final class Reflect { if( underlying instanceof Class ) return (Class) underlying; - if( underlying instanceof ParameterizedType ) + if( underlying instanceof ParameterizedType type ) { - ParameterizedType type = (ParameterizedType) underlying; if( !allowParameter ) { for( java.lang.reflect.Type arg : type.getActualTypeArguments() ) diff --git a/src/main/java/dan200/computercraft/core/filesystem/MountWrapper.java b/src/main/java/dan200/computercraft/core/filesystem/MountWrapper.java index 96b11f24f..0df456cc1 100644 --- a/src/main/java/dan200/computercraft/core/filesystem/MountWrapper.java +++ b/src/main/java/dan200/computercraft/core/filesystem/MountWrapper.java @@ -293,9 +293,8 @@ class MountWrapper private FileSystemException localExceptionOf( @Nullable String localPath, @Nonnull IOException e ) { - if( !location.isEmpty() && e instanceof FileOperationException ) + if( !location.isEmpty() && e instanceof FileOperationException ex ) { - FileOperationException ex = (FileOperationException) e; if( ex.getFilename() != null ) return localExceptionOf( ex.getFilename(), ex.getMessage() ); } diff --git a/src/main/java/dan200/computercraft/core/lua/CobaltLuaMachine.java b/src/main/java/dan200/computercraft/core/lua/CobaltLuaMachine.java index c6b2932e7..f444729e2 100644 --- a/src/main/java/dan200/computercraft/core/lua/CobaltLuaMachine.java +++ b/src/main/java/dan200/computercraft/core/lua/CobaltLuaMachine.java @@ -260,14 +260,12 @@ public class CobaltLuaMachine implements ILuaMachine if( object instanceof Number ) return valueOf( ((Number) object).doubleValue() ); if( object instanceof Boolean ) return valueOf( (Boolean) object ); if( object instanceof String ) return valueOf( object.toString() ); - if( object instanceof byte[] ) + if( object instanceof byte[] b ) { - byte[] b = (byte[]) object; return valueOf( Arrays.copyOf( b, b.length ) ); } - if( object instanceof ByteBuffer ) + if( object instanceof ByteBuffer b ) { - ByteBuffer b = (ByteBuffer) object; byte[] bytes = new byte[b.remaining()]; b.get( bytes ); return valueOf( bytes ); @@ -304,9 +302,8 @@ public class CobaltLuaMachine implements ILuaMachine return table; } - if( object instanceof Collection ) + if( object instanceof Collection objects ) { - Collection objects = (Collection) object; LuaTable table = new LuaTable( objects.size(), 0 ); values.put( object, table ); int i = 0; @@ -314,9 +311,8 @@ public class CobaltLuaMachine implements ILuaMachine return table; } - if( object instanceof Object[] ) + if( object instanceof Object[] objects ) { - Object[] objects = (Object[]) object; LuaTable table = new LuaTable( objects.length, 0 ); values.put( object, table ); for( int i = 0; i < objects.length; i++ ) table.rawset( i + 1, toValue( objects[i], values ) ); diff --git a/src/main/java/dan200/computercraft/shared/command/CommandComputerCraft.java b/src/main/java/dan200/computercraft/shared/command/CommandComputerCraft.java index 7acee80cb..97042910e 100644 --- a/src/main/java/dan200/computercraft/shared/command/CommandComputerCraft.java +++ b/src/main/java/dan200/computercraft/shared/command/CommandComputerCraft.java @@ -177,9 +177,8 @@ public final class CommandComputerCraft if( world == null || pos == null ) throw TP_NOT_THERE.create(); Entity entity = context.getSource().getEntityOrException(); - if( !(entity instanceof ServerPlayer) ) throw TP_NOT_PLAYER.create(); + if( !(entity instanceof ServerPlayer player) ) throw TP_NOT_PLAYER.create(); - ServerPlayer player = (ServerPlayer) entity; if( player.getCommandSenderWorld() == world ) { player.connection.teleport( diff --git a/src/main/java/dan200/computercraft/shared/common/DefaultBundledRedstoneProvider.java b/src/main/java/dan200/computercraft/shared/common/DefaultBundledRedstoneProvider.java index 3c85eaa7f..07c49934d 100644 --- a/src/main/java/dan200/computercraft/shared/common/DefaultBundledRedstoneProvider.java +++ b/src/main/java/dan200/computercraft/shared/common/DefaultBundledRedstoneProvider.java @@ -24,9 +24,8 @@ public class DefaultBundledRedstoneProvider implements IBundledRedstoneProvider public static int getDefaultBundledRedstoneOutput( Level world, BlockPos pos, Direction side ) { Block block = world.getBlockState( pos ).getBlock(); - if( block instanceof IBundledRedstoneBlock ) + if( block instanceof IBundledRedstoneBlock generic ) { - IBundledRedstoneBlock generic = (IBundledRedstoneBlock) block; if( generic.getBundledRedstoneConnectivity( world, pos, side ) ) { return generic.getBundledRedstoneOutput( world, pos, side ); diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/BlockComputerBase.java b/src/main/java/dan200/computercraft/shared/computer/blocks/BlockComputerBase.java index 70e6cff96..167cd53cc 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/BlockComputerBase.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/BlockComputerBase.java @@ -74,9 +74,8 @@ public abstract class BlockComputerBase extends Bloc public int getDirectSignal( @Nonnull BlockState state, BlockGetter world, @Nonnull BlockPos pos, @Nonnull Direction incomingSide ) { BlockEntity entity = world.getBlockEntity( pos ); - if( !(entity instanceof TileComputerBase) ) return 0; + if( !(entity instanceof TileComputerBase computerEntity) ) return 0; - TileComputerBase computerEntity = (TileComputerBase) entity; ServerComputer computer = computerEntity.getServerComputer(); if( computer == null ) return 0; @@ -109,9 +108,8 @@ public abstract class BlockComputerBase extends Bloc public int getBundledRedstoneOutput( Level world, BlockPos pos, Direction side ) { BlockEntity entity = world.getBlockEntity( pos ); - if( !(entity instanceof TileComputerBase) ) return 0; + if( !(entity instanceof TileComputerBase computerEntity) ) return 0; - TileComputerBase computerEntity = (TileComputerBase) entity; ServerComputer computer = computerEntity.getServerComputer(); if( computer == null ) return 0; @@ -144,16 +142,14 @@ public abstract class BlockComputerBase extends Bloc @Override public void playerWillDestroy( @Nonnull Level world, @Nonnull BlockPos pos, @Nonnull BlockState state, @Nonnull Player player ) { - if( !(world instanceof ServerLevel) ) return; - ServerLevel serverWorld = (ServerLevel) world; + if( !(world instanceof ServerLevel serverWorld) ) return; // We drop the item here instead of doing it in the harvest method, as we should // drop computers for creative players too. BlockEntity tile = world.getBlockEntity( pos ); - if( tile instanceof TileComputerBase ) + if( tile instanceof TileComputerBase computer ) { - TileComputerBase computer = (TileComputerBase) tile; LootContext.Builder context = new LootContext.Builder( serverWorld ) .withRandom( world.random ) .withParameter( LootContextParams.ORIGIN, Vec3.atCenterOf( pos ) ) @@ -176,10 +172,8 @@ public abstract class BlockComputerBase extends Bloc super.setPlacedBy( world, pos, state, placer, stack ); BlockEntity tile = world.getBlockEntity( pos ); - if( !world.isClientSide && tile instanceof IComputerTile && stack.getItem() instanceof IComputerItem ) + if( !world.isClientSide && tile instanceof IComputerTile computer && stack.getItem() instanceof IComputerItem item ) { - IComputerTile computer = (IComputerTile) tile; - IComputerItem item = (IComputerItem) stack.getItem(); int id = item.getComputerID( stack ); if( id != -1 ) computer.setComputerID( id ); diff --git a/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java b/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java index 1925b194a..4afdcb6e6 100644 --- a/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java +++ b/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java @@ -362,9 +362,8 @@ public class ServerComputer extends ServerTerminal implements IComputer, IComput if( player == null ) return null; AbstractContainerMenu container = player.containerMenu; - if( !(container instanceof IContainerComputer) ) return null; + if( !(container instanceof IContainerComputer computerContainer) ) return null; - IContainerComputer computerContainer = (IContainerComputer) container; return computerContainer.getComputer() != this ? null : computerContainer; } diff --git a/src/main/java/dan200/computercraft/shared/integration/jei/JEIComputerCraft.java b/src/main/java/dan200/computercraft/shared/integration/jei/JEIComputerCraft.java index 86beee63b..17913e067 100644 --- a/src/main/java/dan200/computercraft/shared/integration/jei/JEIComputerCraft.java +++ b/src/main/java/dan200/computercraft/shared/integration/jei/JEIComputerCraft.java @@ -114,9 +114,8 @@ public class JEIComputerCraft implements IModPlugin */ private static final IIngredientSubtypeInterpreter turtleSubtype = ( stack, ctx ) -> { Item item = stack.getItem(); - if( !(item instanceof ITurtleItem) ) return IIngredientSubtypeInterpreter.NONE; + if( !(item instanceof ITurtleItem turtle) ) return IIngredientSubtypeInterpreter.NONE; - ITurtleItem turtle = (ITurtleItem) item; StringBuilder name = new StringBuilder( "turtle:" ); // Add left and right upgrades to the identifier @@ -150,9 +149,7 @@ public class JEIComputerCraft implements IModPlugin */ private static final IIngredientSubtypeInterpreter diskSubtype = ( stack, ctx ) -> { Item item = stack.getItem(); - if( !(item instanceof ItemDisk) ) return IIngredientSubtypeInterpreter.NONE; - - ItemDisk disk = (ItemDisk) item; + if( !(item instanceof ItemDisk disk) ) return IIngredientSubtypeInterpreter.NONE; int colour = disk.getColour( stack ); return colour == -1 ? IIngredientSubtypeInterpreter.NONE : String.format( "%06x", colour ); diff --git a/src/main/java/dan200/computercraft/shared/integration/jei/RecipeResolver.java b/src/main/java/dan200/computercraft/shared/integration/jei/RecipeResolver.java index 54a942468..33facb6ef 100644 --- a/src/main/java/dan200/computercraft/shared/integration/jei/RecipeResolver.java +++ b/src/main/java/dan200/computercraft/shared/integration/jei/RecipeResolver.java @@ -96,9 +96,8 @@ class RecipeResolver implements IRecipeManagerPlugin public List getRecipeCategoryUids( @Nonnull IFocus focus ) { V value = focus.getValue(); - if( !(value instanceof ItemStack) ) return Collections.emptyList(); + if( !(value instanceof ItemStack stack) ) return Collections.emptyList(); - ItemStack stack = (ItemStack) value; switch( focus.getMode() ) { case INPUT: @@ -119,12 +118,11 @@ class RecipeResolver implements IRecipeManagerPlugin @Override public List getRecipes( @Nonnull IRecipeCategory recipeCategory, @Nonnull IFocus focus ) { - if( !(focus.getValue() instanceof ItemStack) || !recipeCategory.getUid().equals( VanillaRecipeCategoryUid.CRAFTING ) ) + if( !(focus.getValue() instanceof ItemStack stack) || !recipeCategory.getUid().equals( VanillaRecipeCategoryUid.CRAFTING ) ) { return Collections.emptyList(); } - ItemStack stack = (ItemStack) focus.getValue(); switch( focus.getMode() ) { case INPUT: @@ -148,10 +146,9 @@ class RecipeResolver implements IRecipeManagerPlugin { setupCache(); - if( stack.getItem() instanceof ITurtleItem ) + if( stack.getItem() instanceof ITurtleItem item ) { // Suggest possible upgrades which can be applied to this turtle - ITurtleItem item = (ITurtleItem) stack.getItem(); ITurtleUpgrade left = item.getUpgrade( stack, TurtleSide.LEFT ); ITurtleUpgrade right = item.getUpgrade( stack, TurtleSide.RIGHT ); if( left != null && right != null ) return Collections.emptyList(); @@ -227,9 +224,8 @@ class RecipeResolver implements IRecipeManagerPlugin private static List findRecipesWithOutput( @Nonnull ItemStack stack ) { // Find which upgrade this item currently has, an so how we could build it. - if( stack.getItem() instanceof ITurtleItem ) + if( stack.getItem() instanceof ITurtleItem item ) { - ITurtleItem item = (ITurtleItem) stack.getItem(); List recipes = new ArrayList<>( 0 ); ITurtleUpgrade left = item.getUpgrade( stack, TurtleSide.LEFT ); diff --git a/src/main/java/dan200/computercraft/shared/integration/morered/MoreRedIntegration.java b/src/main/java/dan200/computercraft/shared/integration/morered/MoreRedIntegration.java index ba83a633a..c4aaa01d4 100644 --- a/src/main/java/dan200/computercraft/shared/integration/morered/MoreRedIntegration.java +++ b/src/main/java/dan200/computercraft/shared/integration/morered/MoreRedIntegration.java @@ -68,9 +68,8 @@ public class MoreRedIntegration BlockPos pos = wirePos.relative( wireFace ); BlockState state = world.getBlockState( pos ); - if( !(state.getBlock() instanceof IBundledRedstoneBlock) ) return 0; + if( !(state.getBlock() instanceof IBundledRedstoneBlock block) ) return 0; - IBundledRedstoneBlock block = (IBundledRedstoneBlock) state.getBlock(); return (block.getBundledRedstoneOutput( world, pos, wireFace.getOpposite() ) & (1 << channel)) != 0 ? 31 : 0; } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheral.java index 1e038e977..75dff0c2b 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheral.java @@ -67,9 +67,8 @@ class GenericPeripheral implements IDynamicPeripheral public boolean equals( @Nullable IPeripheral other ) { if( other == this ) return true; - if( !(other instanceof GenericPeripheral) ) return false; + if( !(other instanceof GenericPeripheral generic) ) return false; - GenericPeripheral generic = (GenericPeripheral) other; return tile == generic.tile && methods.equals( generic.methods ); } } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/generic/SaturatedMethod.java b/src/main/java/dan200/computercraft/shared/peripheral/generic/SaturatedMethod.java index eca38e63f..406d62466 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/generic/SaturatedMethod.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/generic/SaturatedMethod.java @@ -44,9 +44,8 @@ final class SaturatedMethod public boolean equals( Object obj ) { if( obj == this ) return true; - if( !(obj instanceof SaturatedMethod) ) return false; + if( !(obj instanceof SaturatedMethod other) ) return false; - SaturatedMethod other = (SaturatedMethod) obj; return method == other.method && target.equals( other.target ); } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockCable.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockCable.java index 349faeffa..537f382a6 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockCable.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockCable.java @@ -107,9 +107,8 @@ public class BlockCable extends BlockGeneric implements SimpleWaterloggedBlock if( hit.getType() == HitResult.Type.BLOCK ) { BlockEntity tile = world.getBlockEntity( pos ); - if( tile instanceof TileCable && tile.hasLevel() ) + if( tile instanceof TileCable cable && tile.hasLevel() ) { - TileCable cable = (TileCable) tile; ItemStack item; BlockState newState; @@ -164,9 +163,8 @@ public class BlockCable extends BlockGeneric implements SimpleWaterloggedBlock public void setPlacedBy( Level world, @Nonnull BlockPos pos, @Nonnull BlockState state, LivingEntity placer, @Nonnull ItemStack stack ) { BlockEntity tile = world.getBlockEntity( pos ); - if( tile instanceof TileCable ) + if( tile instanceof TileCable cable ) { - TileCable cable = (TileCable) tile; if( cable.hasCable() ) cable.connectionsChanged(); } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/ItemBlockCable.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/ItemBlockCable.java index 3d3a827b6..7a97a7e80 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/ItemBlockCable.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/ItemBlockCable.java @@ -46,9 +46,8 @@ public abstract class ItemBlockCable extends BlockItem world.playSound( null, pos, soundType.getPlaceSound(), SoundSource.BLOCKS, (soundType.getVolume() + 1.0F) / 2.0F, soundType.getPitch() * 0.8F ); BlockEntity tile = world.getBlockEntity( pos ); - if( tile instanceof TileCable ) + if( tile instanceof TileCable cable ) { - TileCable cable = (TileCable) tile; cable.modemChanged(); cable.connectionsChanged(); } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemPeripheral.java index 75def1aa6..45d470132 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemPeripheral.java @@ -236,9 +236,8 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW @Override public boolean equals( IPeripheral other ) { - if( other instanceof WiredModemPeripheral ) + if( other instanceof WiredModemPeripheral otherModem ) { - WiredModemPeripheral otherModem = (WiredModemPeripheral) other; return otherModem.modem == modem; } return false; diff --git a/src/main/java/dan200/computercraft/shared/peripheral/monitor/BlockMonitor.java b/src/main/java/dan200/computercraft/shared/peripheral/monitor/BlockMonitor.java index e1e8a6121..a546e62b3 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/monitor/BlockMonitor.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/monitor/BlockMonitor.java @@ -83,9 +83,8 @@ public class BlockMonitor extends BlockGeneric super.setPlacedBy( world, pos, blockState, livingEntity, itemStack ); BlockEntity entity = world.getBlockEntity( pos ); - if( entity instanceof TileMonitor && !world.isClientSide ) + if( entity instanceof TileMonitor monitor && !world.isClientSide ) { - TileMonitor monitor = (TileMonitor) entity; // Defer the block update if we're being placed by another TE. See #691 if( livingEntity == null || livingEntity instanceof FakePlayer ) { diff --git a/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorWatcher.java b/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorWatcher.java index ccdd67607..4e85952c2 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorWatcher.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorWatcher.java @@ -54,9 +54,8 @@ public final class MonitorWatcher for( BlockEntity te : chunk.getBlockEntities().values() ) { // Find all origin monitors who are not already on the queue. - if( !(te instanceof TileMonitor) ) continue; + if( !(te instanceof TileMonitor monitor) ) continue; - TileMonitor monitor = (TileMonitor) te; ServerMonitor serverMonitor = getMonitor( monitor ); if( serverMonitor == null || monitor.enqueued ) continue; diff --git a/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java b/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java index 87bd30f96..7e45d65e3 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java @@ -392,9 +392,8 @@ public class TileMonitor extends TileGeneric if( world == null || !world.isAreaLoaded( pos, 0 ) ) return MonitorState.UNLOADED; BlockEntity tile = world.getBlockEntity( pos ); - if( !(tile instanceof TileMonitor) ) return MonitorState.MISSING; + if( !(tile instanceof TileMonitor monitor) ) return MonitorState.MISSING; - TileMonitor monitor = (TileMonitor) tile; return !monitor.visiting && !monitor.destroyed && advanced == monitor.advanced && getDirection() == monitor.getDirection() && getOrientation() == monitor.getOrientation() ? MonitorState.present( monitor ) : MonitorState.MISSING; diff --git a/src/main/java/dan200/computercraft/shared/pocket/apis/PocketAPI.java b/src/main/java/dan200/computercraft/shared/pocket/apis/PocketAPI.java index 85cfa96f3..c25f97ae8 100644 --- a/src/main/java/dan200/computercraft/shared/pocket/apis/PocketAPI.java +++ b/src/main/java/dan200/computercraft/shared/pocket/apis/PocketAPI.java @@ -63,8 +63,7 @@ public class PocketAPI implements ILuaAPI public final Object[] equipBack() { Entity entity = computer.getEntity(); - if( !(entity instanceof Player) ) return new Object[] { false, "Cannot find player" }; - Player player = (Player) entity; + if( !(entity instanceof Player player) ) return new Object[] { false, "Cannot find player" }; Inventory inventory = player.getInventory(); IPocketUpgrade previousUpgrade = computer.getUpgrade(); @@ -108,8 +107,7 @@ public class PocketAPI implements ILuaAPI public final Object[] unequipBack() { Entity entity = computer.getEntity(); - if( !(entity instanceof Player) ) return new Object[] { false, "Cannot find player" }; - Player player = (Player) entity; + if( !(entity instanceof Player player) ) return new Object[] { false, "Cannot find player" }; Inventory inventory = player.getInventory(); IPocketUpgrade previousUpgrade = computer.getUpgrade(); diff --git a/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java b/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java index af0debdc4..55025a084 100644 --- a/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java +++ b/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java @@ -56,9 +56,8 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces Inventory inventory = ((Player) entity).getInventory(); return inventory.items.contains( stack ) || inventory.offhand.contains( stack ) ? entity : null; } - else if( entity instanceof LivingEntity ) + else if( entity instanceof LivingEntity living ) { - LivingEntity living = (LivingEntity) entity; return living.getMainHandItem() == stack || living.getOffhandItem() == stack ? entity : null; } else @@ -184,10 +183,9 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces { super.broadcastState( force ); - if( (hasTerminalChanged() || force) && entity instanceof ServerPlayer ) + if( (hasTerminalChanged() || force) && entity instanceof ServerPlayer player ) { // Broadcast the state to the current entity if they're not already interacting with it. - ServerPlayer player = (ServerPlayer) entity; if( player.connection != null && !isInteracting( player ) ) { NetworkHandler.sendToPlayer( player, createTerminalPacket() ); diff --git a/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketModem.java b/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketModem.java index a743ea337..b465a3fda 100644 --- a/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketModem.java +++ b/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketModem.java @@ -41,12 +41,10 @@ public class PocketModem extends AbstractPocketUpgrade @Override public void update( @Nonnull IPocketAccess access, @Nullable IPeripheral peripheral ) { - if( !(peripheral instanceof PocketModemPeripheral) ) return; + if( !(peripheral instanceof PocketModemPeripheral modem) ) return; Entity entity = access.getEntity(); - PocketModemPeripheral modem = (PocketModemPeripheral) peripheral; - if( entity != null ) modem.setLocation( entity.getCommandSenderWorld(), entity.getEyePosition( 1 ) ); ModemState state = modem.getModemState(); diff --git a/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketSpeaker.java b/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketSpeaker.java index 6b1d8d208..14b815f0d 100644 --- a/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketSpeaker.java +++ b/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketSpeaker.java @@ -32,9 +32,7 @@ public class PocketSpeaker extends AbstractPocketUpgrade @Override public void update( @Nonnull IPocketAccess access, @Nullable IPeripheral peripheral ) { - if( !(peripheral instanceof PocketSpeakerPeripheral) ) return; - - PocketSpeakerPeripheral speaker = (PocketSpeakerPeripheral) peripheral; + if( !(peripheral instanceof PocketSpeakerPeripheral speaker) ) return; Entity entity = access.getEntity(); if( entity != null ) diff --git a/src/main/java/dan200/computercraft/shared/turtle/blocks/BlockTurtle.java b/src/main/java/dan200/computercraft/shared/turtle/blocks/BlockTurtle.java index d9d4e82fc..8e618a7f7 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/blocks/BlockTurtle.java +++ b/src/main/java/dan200/computercraft/shared/turtle/blocks/BlockTurtle.java @@ -88,7 +88,7 @@ public class BlockTurtle extends BlockComputerBase implements Simple public VoxelShape getShape( @Nonnull BlockState state, BlockGetter world, @Nonnull BlockPos pos, @Nonnull CollisionContext context ) { BlockEntity tile = world.getBlockEntity( pos ); - Vec3 offset = tile instanceof TileTurtle ? ((TileTurtle) tile).getRenderOffset( 1.0f ) : Vec3.ZERO; + Vec3 offset = tile instanceof TileTurtle turtle ? turtle.getRenderOffset( 1.0f ) : Vec3.ZERO; return offset.equals( Vec3.ZERO ) ? DEFAULT_SHAPE : DEFAULT_SHAPE.move( offset.x, offset.y, offset.z ); } @@ -119,24 +119,17 @@ public class BlockTurtle extends BlockComputerBase implements Simple } @Override - public void setPlacedBy( @Nonnull Level world, @Nonnull BlockPos pos, @Nonnull BlockState state, @Nullable LivingEntity player, @Nonnull ItemStack stack ) + public void setPlacedBy( @Nonnull Level world, @Nonnull BlockPos pos, @Nonnull BlockState state, @Nullable LivingEntity entity, @Nonnull ItemStack stack ) { - super.setPlacedBy( world, pos, state, player, stack ); + super.setPlacedBy( world, pos, state, entity, stack ); BlockEntity tile = world.getBlockEntity( pos ); - if( !world.isClientSide && tile instanceof TileTurtle ) + if( !world.isClientSide && tile instanceof TileTurtle turtle ) { - TileTurtle turtle = (TileTurtle) tile; + if( entity instanceof Player player ) turtle.setOwningPlayer( player.getGameProfile() ); - if( player instanceof Player ) + if( stack.getItem() instanceof ITurtleItem item ) { - ((TileTurtle) tile).setOwningPlayer( ((Player) player).getGameProfile() ); - } - - if( stack.getItem() instanceof ITurtleItem ) - { - ITurtleItem item = (ITurtleItem) stack.getItem(); - // Set Upgrades for( TurtleSide side : TurtleSide.values() ) { @@ -172,7 +165,7 @@ public class BlockTurtle extends BlockComputerBase implements Simple @Override protected ItemStack getItem( TileComputerBase tile ) { - return tile instanceof TileTurtle ? TurtleItemFactory.create( (TileTurtle) tile ) : ItemStack.EMPTY; + return tile instanceof TileTurtle turtle ? TurtleItemFactory.create( turtle ) : ItemStack.EMPTY; } @Override diff --git a/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java b/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java index 5b4f39b0e..bdb1063b3 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java +++ b/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java @@ -162,12 +162,12 @@ public class TileTurtle extends TileComputerBase implements ITurtleTile, Default ItemStack currentItem = player.getItemInHand( hand ); if( !currentItem.isEmpty() ) { - if( currentItem.getItem() instanceof DyeItem ) + if( currentItem.getItem() instanceof DyeItem dyeItem ) { // Dye to change turtle colour if( !getLevel().isClientSide ) { - DyeColor dye = ((DyeItem) currentItem.getItem()).getDyeColor(); + DyeColor dye = dyeItem.getDyeColor(); if( brain.getDyeColour() != dye ) { brain.setDyeColour( dye ); diff --git a/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java b/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java index 342f95d54..f64123fbc 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java +++ b/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java @@ -327,10 +327,9 @@ public class TurtleBrain implements ITurtleAccess if( block == oldBlock.getBlock() ) { BlockEntity newTile = world.getBlockEntity( pos ); - if( newTile instanceof TileTurtle ) + if( newTile instanceof TileTurtle newTurtle ) { // Copy the old turtle state into the new turtle - TileTurtle newTurtle = (TileTurtle) newTile; newTurtle.setLevel( world ); newTurtle.transferStateFrom( oldOwner ); newTurtle.createServerComputer().setLevel( world ); @@ -956,12 +955,12 @@ public class TurtleBrain implements ITurtleAccess @Override public MethodResult resume( Object[] response ) { - if( response.length < 3 || !(response[1] instanceof Number) || !(response[2] instanceof Boolean) ) + if( response.length < 3 || !(response[1] instanceof Number id) || !(response[2] instanceof Boolean) ) { return pull; } - if( ((Number) response[1]).intValue() != command ) return pull; + if( id.intValue() != command ) return pull; return MethodResult.of( Arrays.copyOfRange( response, 2, response.length ) ); } diff --git a/src/main/java/dan200/computercraft/shared/turtle/core/TurtlePlaceCommand.java b/src/main/java/dan200/computercraft/shared/turtle/core/TurtlePlaceCommand.java index 189323040..6c3c8b45c 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/core/TurtlePlaceCommand.java +++ b/src/main/java/dan200/computercraft/shared/turtle/core/TurtlePlaceCommand.java @@ -165,9 +165,9 @@ public class TurtlePlaceCommand implements ITurtleCommand if( interact != null ) return interact.consumesAction(); if( hitEntity.interact( turtlePlayer, InteractionHand.MAIN_HAND ).consumesAction() ) return true; - if( hitEntity instanceof LivingEntity ) + if( hitEntity instanceof LivingEntity hitLiving ) { - return stack.interactLivingEntity( turtlePlayer, (LivingEntity) hitEntity, InteractionHand.MAIN_HAND ).consumesAction(); + return stack.interactLivingEntity( turtlePlayer, hitLiving, InteractionHand.MAIN_HAND ).consumesAction(); } return false; @@ -236,7 +236,7 @@ public class TurtlePlaceCommand implements ITurtleCommand boolean placed = doDeployOnBlock( stack, turtlePlayer, position, context, hit ).consumesAction(); // Set text on signs - if( placed && item instanceof SignItem && extraArguments != null && extraArguments.length >= 1 && extraArguments[0] instanceof String ) + if( placed && item instanceof SignItem && extraArguments != null && extraArguments.length >= 1 && extraArguments[0] instanceof String message ) { Level world = turtle.getLevel(); BlockEntity tile = world.getBlockEntity( position ); @@ -245,7 +245,7 @@ public class TurtlePlaceCommand implements ITurtleCommand tile = world.getBlockEntity( position.relative( side ) ); } - if( tile instanceof SignBlockEntity ) setSignText( world, tile, (String) extraArguments[0] ); + if( tile instanceof SignBlockEntity ) setSignText( world, tile, message ); } return placed; diff --git a/src/main/java/dan200/computercraft/shared/turtle/core/TurtlePlayer.java b/src/main/java/dan200/computercraft/shared/turtle/core/TurtlePlayer.java index faa0e418b..7bbea3a95 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/core/TurtlePlayer.java +++ b/src/main/java/dan200/computercraft/shared/turtle/core/TurtlePlayer.java @@ -74,9 +74,8 @@ public final class TurtlePlayer extends FakePlayer public static TurtlePlayer get( ITurtleAccess access ) { - if( !(access instanceof TurtleBrain) ) return create( access ); + if( !(access instanceof TurtleBrain brain) ) return create( access ); - TurtleBrain brain = (TurtleBrain) access; TurtlePlayer player = brain.cachedPlayer; if( player == null || player.getGameProfile() != getProfile( access.getOwningPlayer() ) || player.getCommandSenderWorld() != access.getLevel() ) diff --git a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleModem.java b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleModem.java index e6c7bf29b..0546dad9d 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleModem.java +++ b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleModem.java @@ -57,7 +57,7 @@ public class TurtleModem extends AbstractTurtleUpgrade @Override public boolean equals( IPeripheral other ) { - return this == other || (other instanceof Peripheral && ((Peripheral) other).turtle == turtle); + return this == other || (other instanceof Peripheral modem && modem.turtle == turtle); } } @@ -131,9 +131,9 @@ public class TurtleModem extends AbstractTurtleUpgrade if( !turtle.getLevel().isClientSide ) { IPeripheral peripheral = turtle.getPeripheral( side ); - if( peripheral instanceof Peripheral ) + if( peripheral instanceof Peripheral modem ) { - ModemState state = ((Peripheral) peripheral).getModemState(); + ModemState state = modem.getModemState(); if( state.pollChanged() ) { turtle.getUpgradeNBTData( side ).putBoolean( "active", state.isOpen() ); diff --git a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleSpeaker.java b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleSpeaker.java index 982d4ee99..406e48c59 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleSpeaker.java +++ b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleSpeaker.java @@ -53,7 +53,7 @@ public class TurtleSpeaker extends AbstractTurtleUpgrade @Override public boolean equals( IPeripheral other ) { - return this == other || (other instanceof Peripheral && turtle == ((Peripheral) other).turtle); + return this == other || (other instanceof Peripheral speaker && turtle == speaker.turtle); } } @@ -79,7 +79,7 @@ public class TurtleSpeaker extends AbstractTurtleUpgrade @Override public void update( @Nonnull ITurtleAccess turtle, @Nonnull TurtleSide turtleSide ) { - IPeripheral turtlePeripheral = turtle.getPeripheral( turtleSide ); - if( turtlePeripheral instanceof Peripheral ) ((Peripheral) turtlePeripheral).update(); + IPeripheral peripheral = turtle.getPeripheral( turtleSide ); + if( peripheral instanceof Peripheral speaker ) speaker.update(); } } diff --git a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleTool.java b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleTool.java index d53d6c581..4109cf067 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleTool.java +++ b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleTool.java @@ -165,18 +165,12 @@ public class TurtleTool extends AbstractTurtleUpgrade { // Special case for armor stands: attack twice to guarantee destroy hitEntity.hurt( source, damage ); - if( hitEntity.isAlive() ) - { - hitEntity.hurt( source, damage ); - } + if( hitEntity.isAlive() ) hitEntity.hurt( source, damage ); attacked = true; } else { - if( hitEntity.hurt( source, damage ) ) - { - attacked = true; - } + if( hitEntity.hurt( source, damage ) ) attacked = true; } } } diff --git a/src/main/java/dan200/computercraft/shared/util/NBTUtil.java b/src/main/java/dan200/computercraft/shared/util/NBTUtil.java index 41ce9f229..aa5a09d20 100644 --- a/src/main/java/dan200/computercraft/shared/util/NBTUtil.java +++ b/src/main/java/dan200/computercraft/shared/util/NBTUtil.java @@ -33,9 +33,8 @@ public final class NBTUtil if( object instanceof Boolean ) return ByteTag.valueOf( (byte) ((boolean) (Boolean) object ? 1 : 0) ); if( object instanceof Number ) return DoubleTag.valueOf( ((Number) object).doubleValue() ); if( object instanceof String ) return StringTag.valueOf( object.toString() ); - if( object instanceof Map ) + if( object instanceof Map m ) { - Map m = (Map) object; CompoundTag nbt = new CompoundTag(); int i = 0; for( Map.Entry entry : m.entrySet() ) diff --git a/src/test/java/dan200/computercraft/core/ComputerTestDelegate.java b/src/test/java/dan200/computercraft/core/ComputerTestDelegate.java index d7ca48d69..40f0b0143 100644 --- a/src/test/java/dan200/computercraft/core/ComputerTestDelegate.java +++ b/src/test/java/dan200/computercraft/core/ComputerTestDelegate.java @@ -340,9 +340,8 @@ public class ComputerTestDelegate DynamicNodeBuilder root = new DynamicNodeBuilder( "" ); for( Object key : tests.keySet() ) { - if( !(key instanceof String) ) throw new LuaException( "Non-key string " + getType( key ) ); + if( !(key instanceof String name) ) throw new LuaException( "Non-key string " + getType( key ) ); - String name = (String) key; String[] parts = name.split( "\0" ); DynamicNodeBuilder builder = root; for( int i = 0; i < parts.length - 1; i++ ) builder = builder.get( parts[i] ); diff --git a/src/test/java/dan200/computercraft/core/apis/ObjectWrapper.java b/src/test/java/dan200/computercraft/core/apis/ObjectWrapper.java index 3950e813b..6dcc74a34 100644 --- a/src/test/java/dan200/computercraft/core/apis/ObjectWrapper.java +++ b/src/test/java/dan200/computercraft/core/apis/ObjectWrapper.java @@ -24,8 +24,8 @@ public class ObjectWrapper implements ILuaContext public ObjectWrapper( Object object ) { this.object = object; - String[] dynamicMethods = object instanceof IDynamicLuaObject - ? Objects.requireNonNull( ((IDynamicLuaObject) object).getMethodNames(), "Methods cannot be null" ) + String[] dynamicMethods = object instanceof IDynamicLuaObject dynamic + ? Objects.requireNonNull( dynamic.getMethodNames(), "Methods cannot be null" ) : LuaMethod.EMPTY_METHODS; List> methods = LuaMethod.GENERATOR.getMethods( object.getClass() );