mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-26 19:37:39 +00:00 
			
		
		
		
	Switch a few more places to use Java 17 features
New ErrorProne hint, and one which is actually pretty useful!
This commit is contained in:
		| @@ -437,8 +437,8 @@ final class WiredNetworkImpl implements WiredNetwork { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private static WiredNodeImpl checkNode(WiredNode node) { |     private static WiredNodeImpl checkNode(WiredNode node) { | ||||||
|         if (node instanceof WiredNodeImpl) { |         if (node instanceof WiredNodeImpl n) { | ||||||
|             return (WiredNodeImpl) node; |             return n; | ||||||
|         } else { |         } else { | ||||||
|             throw new IllegalArgumentException("Unknown implementation of IWiredNode: " + node); |             throw new IllegalArgumentException("Unknown implementation of IWiredNode: " + node); | ||||||
|         } |         } | ||||||
|   | |||||||
| @@ -66,8 +66,8 @@ public final class HelpingArgumentBuilder extends LiteralArgumentBuilder<Command | |||||||
|     public LiteralArgumentBuilder<CommandSourceStack> then(final ArgumentBuilder<CommandSourceStack, ?> argument) { |     public LiteralArgumentBuilder<CommandSourceStack> then(final ArgumentBuilder<CommandSourceStack, ?> argument) { | ||||||
|         if (getRedirect() != null) throw new IllegalStateException("Cannot add children to a redirected node"); |         if (getRedirect() != null) throw new IllegalStateException("Cannot add children to a redirected node"); | ||||||
| 
 | 
 | ||||||
|         if (argument instanceof HelpingArgumentBuilder) { |         if (argument instanceof HelpingArgumentBuilder child) { | ||||||
|             children.add((HelpingArgumentBuilder) argument); |             children.add(child); | ||||||
|         } else if (argument instanceof LiteralArgumentBuilder) { |         } else if (argument instanceof LiteralArgumentBuilder) { | ||||||
|             super.then(argument); |             super.then(argument); | ||||||
|         } else { |         } else { | ||||||
|   | |||||||
| @@ -38,7 +38,7 @@ public class WirelessModemBlockEntity extends BlockEntity { | |||||||
| 
 | 
 | ||||||
|         @Override |         @Override | ||||||
|         public boolean equals(@Nullable IPeripheral other) { |         public boolean equals(@Nullable IPeripheral other) { | ||||||
|             return this == other || (other instanceof Peripheral && entity == ((Peripheral) other).entity); |             return this == other || (other instanceof Peripheral o && entity == o.entity); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         @Override |         @Override | ||||||
|   | |||||||
| @@ -40,7 +40,6 @@ import org.jspecify.annotations.Nullable; | |||||||
|  * monitor.setCursorPos(1, 1) |  * monitor.setCursorPos(1, 1) | ||||||
|  * monitor.write("Hello, world!") |  * monitor.write("Hello, world!") | ||||||
|  * }</pre> |  * }</pre> | ||||||
|  * |  | ||||||
|  * @cc.see monitor_resize Queued when a monitor is resized. |  * @cc.see monitor_resize Queued when a monitor is resized. | ||||||
|  * @cc.see monitor_touch Queued when an advanced monitor is clicked. |  * @cc.see monitor_touch Queued when an advanced monitor is clicked. | ||||||
|  */ |  */ | ||||||
| @@ -95,7 +94,7 @@ public class MonitorPeripheral extends TermMethods implements IPeripheral { | |||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public boolean equals(@Nullable IPeripheral other) { |     public boolean equals(@Nullable IPeripheral other) { | ||||||
|         return other instanceof MonitorPeripheral && monitor == ((MonitorPeripheral) other).monitor; |         return other instanceof MonitorPeripheral o && monitor == o.monitor; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private ServerMonitor getMonitor() throws LuaException { |     private ServerMonitor getMonitor() throws LuaException { | ||||||
|   | |||||||
| @@ -57,11 +57,10 @@ public final class MonitorWatcher { | |||||||
|             if (monitor == null) continue; |             if (monitor == null) continue; | ||||||
| 
 | 
 | ||||||
|             var pos = tile.getBlockPos(); |             var pos = tile.getBlockPos(); | ||||||
|             var world = tile.getLevel(); |             if (!(tile.getLevel() instanceof ServerLevel level)) continue; | ||||||
|             if (!(world instanceof ServerLevel)) continue; |  | ||||||
| 
 | 
 | ||||||
|             var chunk = world.getChunkAt(pos); |             var chunk = level.getChunkAt(pos); | ||||||
|             if (((ServerLevel) world).getChunkSource().chunkMap.getPlayers(chunk.getPos(), false).isEmpty()) { |             if (level.getChunkSource().chunkMap.getPlayers(chunk.getPos(), false).isEmpty()) { | ||||||
|                 continue; |                 continue; | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|   | |||||||
| @@ -53,7 +53,7 @@ public class SpeakerBlockEntity extends BlockEntity { | |||||||
| 
 | 
 | ||||||
|         @Override |         @Override | ||||||
|         public boolean equals(@Nullable IPeripheral other) { |         public boolean equals(@Nullable IPeripheral other) { | ||||||
|             return this == other || (other instanceof Peripheral && speaker == ((Peripheral) other).speaker); |             return this == other || (other instanceof Peripheral o && speaker == o.speaker); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -25,7 +25,6 @@ public class PocketSpeaker extends AbstractPocketUpgrade { | |||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public void update(IPocketAccess access, @Nullable IPeripheral peripheral) { |     public void update(IPocketAccess access, @Nullable IPeripheral peripheral) { | ||||||
|         if (!(peripheral instanceof PocketSpeakerPeripheral)) return; |         if (peripheral instanceof PocketSpeakerPeripheral speaker) speaker.update(); | ||||||
|         ((PocketSpeakerPeripheral) peripheral).update(); |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -62,14 +62,13 @@ public class TurtleDropCommand implements TurtleCommand { | |||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         switch (transferred) { |         return switch (transferred) { | ||||||
|             case ContainerTransfer.NO_SPACE: |             case ContainerTransfer.NO_SPACE -> TurtleCommandResult.failure("No space for items"); | ||||||
|                 return TurtleCommandResult.failure("No space for items"); |             case ContainerTransfer.NO_ITEMS -> TurtleCommandResult.failure("No items to drop"); | ||||||
|             case ContainerTransfer.NO_ITEMS: |             default -> { | ||||||
|                 return TurtleCommandResult.failure("No items to drop"); |  | ||||||
|             default: |  | ||||||
|                 turtle.playAnimation(TurtleAnimation.WAIT); |                 turtle.playAnimation(TurtleAnimation.WAIT); | ||||||
|                 return TurtleCommandResult.success(); |                 yield TurtleCommandResult.success(); | ||||||
|         } |             } | ||||||
|  |         }; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -50,15 +50,14 @@ public class TurtleSuckCommand implements TurtleCommand { | |||||||
|         if (inventory != null) { |         if (inventory != null) { | ||||||
|             // Take from inventory of thing in front |             // Take from inventory of thing in front | ||||||
|             var transferred = inventory.moveTo(TurtleUtil.getOffsetInventory(turtle), quantity); |             var transferred = inventory.moveTo(TurtleUtil.getOffsetInventory(turtle), quantity); | ||||||
|             switch (transferred) { |             return switch (transferred) { | ||||||
|                 case ContainerTransfer.NO_SPACE: |                 case ContainerTransfer.NO_SPACE -> TurtleCommandResult.failure("No space for items"); | ||||||
|                     return TurtleCommandResult.failure("No space for items"); |                 case ContainerTransfer.NO_ITEMS -> TurtleCommandResult.failure("No items to take"); | ||||||
|                 case ContainerTransfer.NO_ITEMS: |                 default -> { | ||||||
|                     return TurtleCommandResult.failure("No items to take"); |  | ||||||
|                 default: |  | ||||||
|                     turtle.playAnimation(TurtleAnimation.WAIT); |                     turtle.playAnimation(TurtleAnimation.WAIT); | ||||||
|                     return TurtleCommandResult.success(); |                     yield TurtleCommandResult.success(); | ||||||
|             } |                 } | ||||||
|  |             }; | ||||||
|         } else { |         } else { | ||||||
|             // Suck up loose items off the ground |             // Suck up loose items off the ground | ||||||
|             var aabb = new AABB( |             var aabb = new AABB( | ||||||
|   | |||||||
| @@ -70,9 +70,9 @@ public final class DropConsumer { | |||||||
| 
 | 
 | ||||||
|     public static boolean onEntitySpawn(Entity entity) { |     public static boolean onEntitySpawn(Entity entity) { | ||||||
|         // Capture any nearby item spawns |         // Capture any nearby item spawns | ||||||
|         if (dropWorld == entity.level() && entity instanceof ItemEntity |         if (dropWorld == entity.level() && entity instanceof ItemEntity item | ||||||
|             && assertNonNull(dropBounds).contains(entity.position())) { |             && assertNonNull(dropBounds).contains(entity.position())) { | ||||||
|             handleDrops(((ItemEntity) entity).getItem()); |             handleDrops(item.getItem()); | ||||||
|             return true; |             return true; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|   | |||||||
| @@ -79,8 +79,8 @@ public class PrettyJsonWriter extends JsonWriter { | |||||||
| 
 | 
 | ||||||
|         // Otherwise we either need to push to our list or finish a record pair. |         // Otherwise we either need to push to our list or finish a record pair. | ||||||
|         var head = stack.getLast(); |         var head = stack.getLast(); | ||||||
|         if (head instanceof DocList) { |         if (head instanceof DocList headList) { | ||||||
|             ((DocList) head).add(object); |             headList.add(object); | ||||||
|         } else { |         } else { | ||||||
|             stack.removeLast(); |             stack.removeLast(); | ||||||
|             ((DocList) stack.getLast()).add(new Pair((String) head, object)); |             ((DocList) stack.getLast()).add(new Pair((String) head, object)); | ||||||
|   | |||||||
| @@ -83,7 +83,7 @@ object ClientTestHooks { | |||||||
| 
 | 
 | ||||||
|         if (minecraft.levelSource.levelExists(LEVEL_NAME)) { |         if (minecraft.levelSource.levelExists(LEVEL_NAME)) { | ||||||
|             LOG.info("World already exists, opening.") |             LOG.info("World already exists, opening.") | ||||||
|             minecraft.createWorldOpenFlows().loadLevel(minecraft.screen, LEVEL_NAME) |             minecraft.createWorldOpenFlows().loadLevel(minecraft.screen!!, LEVEL_NAME) | ||||||
|         } else { |         } else { | ||||||
|             LOG.info("World does not exist, creating it.") |             LOG.info("World does not exist, creating it.") | ||||||
|             val rules = GameRules() |             val rules = GameRules() | ||||||
|   | |||||||
| @@ -112,7 +112,7 @@ final class LuaDateTime { | |||||||
| 
 | 
 | ||||||
|     private static int getField(Map<?, ?> table, String field, int def) throws LuaException { |     private static int getField(Map<?, ?> table, String field, int def) throws LuaException { | ||||||
|         var value = table.get(field); |         var value = table.get(field); | ||||||
|         if (value instanceof Number) return ((Number) value).intValue(); |         if (value instanceof Number n) return n.intValue(); | ||||||
|         if (def < 0) throw new LuaException("field \"" + field + "\" missing in date table"); |         if (def < 0) throw new LuaException("field \"" + field + "\" missing in date table"); | ||||||
|         return def; |         return def; | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -110,14 +110,14 @@ public class OSAPI implements ILuaAPI { | |||||||
|         return time; |         return time; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private static int getDayForCalendar(Calendar c) { |     private static int getDayForCalendar(Calendar calendar) { | ||||||
|         var g = c instanceof GregorianCalendar ? (GregorianCalendar) c : new GregorianCalendar(); |         var g = calendar instanceof GregorianCalendar c ? c : new GregorianCalendar(); | ||||||
|         var year = c.get(Calendar.YEAR); |         var year = calendar.get(Calendar.YEAR); | ||||||
|         var day = 0; |         var day = 0; | ||||||
|         for (var y = 1970; y < year; y++) { |         for (var y = 1970; y < year; y++) { | ||||||
|             day += g.isLeapYear(y) ? 366 : 365; |             day += g.isLeapYear(y) ? 366 : 365; | ||||||
|         } |         } | ||||||
|         day += c.get(Calendar.DAY_OF_YEAR); |         day += calendar.get(Calendar.DAY_OF_YEAR); | ||||||
|         return day; |         return day; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @@ -133,7 +133,7 @@ public class OSAPI implements ILuaAPI { | |||||||
|      * @param args The parameters of the event. |      * @param args The parameters of the event. | ||||||
|      * @cc.tparam string name The name of the event to queue. |      * @cc.tparam string name The name of the event to queue. | ||||||
|      * @cc.param ... The parameters of the event. These can be any primitive type (boolean, number, string) as well as |      * @cc.param ... The parameters of the event. These can be any primitive type (boolean, number, string) as well as | ||||||
|     *                tables. Other types (like functions), as well as metatables, will not be preserved. |      *               tables. Other types (like functions), as well as metatables, will not be preserved. | ||||||
|      * @cc.see os.pullEvent To pull the event queued |      * @cc.see os.pullEvent To pull the event queued | ||||||
|      */ |      */ | ||||||
|     @LuaFunction |     @LuaFunction | ||||||
|   | |||||||
| @@ -265,8 +265,8 @@ public abstract class AbstractHandle { | |||||||
|         checkOpen(); |         checkOpen(); | ||||||
|         try { |         try { | ||||||
|             var arg = arguments.get(0); |             var arg = arguments.get(0); | ||||||
|             if (binary && arg instanceof Number) { |             if (binary && arg instanceof Number n) { | ||||||
|                 var number = ((Number) arg).intValue(); |                 var number = n.intValue(); | ||||||
|                 writeSingle((byte) number); |                 writeSingle((byte) number); | ||||||
|             } else { |             } else { | ||||||
|                 channel.write(arguments.getBytesCoerced(0)); |                 channel.write(arguments.getBytesCoerced(0)); | ||||||
|   | |||||||
| @@ -58,8 +58,8 @@ class TableImpl implements dan200.computercraft.api.lua.LuaTable<Object, Object> | |||||||
| 
 | 
 | ||||||
|     private LuaValue getImpl(Object o) { |     private LuaValue getImpl(Object o) { | ||||||
|         checkValid(); |         checkValid(); | ||||||
|         if (o instanceof String) return table.rawget((String) o); |         if (o instanceof String s) return table.rawget(s); | ||||||
|         if (o instanceof Integer) return table.rawget((Integer) o); |         if (o instanceof Integer i) return table.rawget(i); | ||||||
|         return Constants.NIL; |         return Constants.NIL; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|   | |||||||
| @@ -181,8 +181,8 @@ final class VarargArguments implements IArguments { | |||||||
|         if (isClosed()) throw new IllegalStateException("Cannot use getTableUnsafe after IArguments has been closed."); |         if (isClosed()) throw new IllegalStateException("Cannot use getTableUnsafe after IArguments has been closed."); | ||||||
| 
 | 
 | ||||||
|         var value = varargs.arg(index + 1); |         var value = varargs.arg(index + 1); | ||||||
|         if (!(value instanceof LuaTable)) throw LuaValues.badArgument(index, "table", value.typeName()); |         if (!(value instanceof LuaTable table)) throw LuaValues.badArgument(index, "table", value.typeName()); | ||||||
|         return new TableImpl(this, (LuaTable) value); |         return new TableImpl(this, table); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
| @@ -191,8 +191,8 @@ final class VarargArguments implements IArguments { | |||||||
| 
 | 
 | ||||||
|         var value = varargs.arg(index + 1); |         var value = varargs.arg(index + 1); | ||||||
|         if (value.isNil()) return Optional.empty(); |         if (value.isNil()) return Optional.empty(); | ||||||
|         if (!(value instanceof LuaTable)) throw LuaValues.badArgument(index, "table", value.typeName()); |         if (!(value instanceof LuaTable table)) throw LuaValues.badArgument(index, "table", value.typeName()); | ||||||
|         return Optional.of(new TableImpl(this, (LuaTable) value)); |         return Optional.of(new TableImpl(this, table)); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
| @@ -237,6 +237,7 @@ final class VarargArguments implements IArguments { | |||||||
|         return metatable != null && metatable.rawget(NAME) instanceof LuaString s ? s.toString() : null; |         return metatable != null && metatable.rawget(NAME) instanceof LuaString s ? s.toString() : null; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     @SuppressWarnings("ArrayRecordComponent") | ||||||
|     private record ArraySlice<T>(T[] array, int offset) { |     private record ArraySlice<T>(T[] array, int offset) { | ||||||
|         // FIXME: We should be able to remove the @Nullables if we update NullAway. |         // FIXME: We should be able to remove the @Nullables if we update NullAway. | ||||||
| 
 | 
 | ||||||
|   | |||||||
| @@ -48,9 +48,10 @@ public class MethodTest { | |||||||
|     @Test |     @Test | ||||||
|     public void testDynamicPeripheral() { |     public void testDynamicPeripheral() { | ||||||
|         ComputerBootstrap.run( |         ComputerBootstrap.run( | ||||||
|             "local dynamic = peripheral.wrap('top')\n" + |             """ | ||||||
|                 "assert(dynamic.foo() == 123, 'foo: ' .. tostring(dynamic.foo()))\n" + |                 local dynamic = peripheral.wrap('top') | ||||||
|                 "assert(dynamic.bar() == 321, 'bar: ' .. tostring(dynamic.bar()))", |                 assert(dynamic.foo() == 123, 'foo: ' .. tostring(dynamic.foo())) | ||||||
|  |                 assert(dynamic.bar() == 321, 'bar: ' .. tostring(dynamic.bar()))""", | ||||||
|             x -> x.getEnvironment().setPeripheral(ComputerSide.TOP, new Dynamic()), |             x -> x.getEnvironment().setPeripheral(ComputerSide.TOP, new Dynamic()), | ||||||
|             50 |             50 | ||||||
|         ); |         ); | ||||||
| @@ -66,9 +67,10 @@ public class MethodTest { | |||||||
|     @Test |     @Test | ||||||
|     public void testPeripheralThrow() { |     public void testPeripheralThrow() { | ||||||
|         ComputerBootstrap.run( |         ComputerBootstrap.run( | ||||||
|             "local throw = peripheral.wrap('top')\n" + |             """ | ||||||
|                 "local _, err = pcall(function() throw.thisThread() end) assert(err == '/test.lua:2: !', (\"thisThread: %q\"):format(err))\n" + |                 local throw = peripheral.wrap('top') | ||||||
|                 "local _, err = pcall(function() throw.mainThread() end) assert(err == '/test.lua:3: !', (\"mainThread: %q\"):format(err))\n", |                 local _, err = pcall(function() throw.thisThread() end) assert(err == '/test.lua:2: !', ("thisThread: %q"):format(err)) | ||||||
|  |                 local _, err = pcall(function() throw.mainThread() end) assert(err == '/test.lua:3: !', ("mainThread: %q"):format(err))""", | ||||||
|             x -> x.getEnvironment().setPeripheral(ComputerSide.TOP, new PeripheralThrow()), |             x -> x.getEnvironment().setPeripheral(ComputerSide.TOP, new PeripheralThrow()), | ||||||
|             50 |             50 | ||||||
|         ); |         ); | ||||||
| @@ -77,8 +79,9 @@ public class MethodTest { | |||||||
|     @Test |     @Test | ||||||
|     public void testMany() { |     public void testMany() { | ||||||
|         ComputerBootstrap.run( |         ComputerBootstrap.run( | ||||||
|             "assert(many.method_0)\n" + |             """ | ||||||
|                 "assert(many.method_39)", |                 assert(many.method_0) | ||||||
|  |                 assert(many.method_39)""", | ||||||
|             x -> x.addApi(new ManyMethods()), 50); |             x -> x.addApi(new ManyMethods()), 50); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @@ -94,8 +97,7 @@ public class MethodTest { | |||||||
|     public void testModule() { |     public void testModule() { | ||||||
|         ComputerBootstrap.run( |         ComputerBootstrap.run( | ||||||
|             """ |             """ | ||||||
|             assert(require "test.module".func() == 123) |                 assert(require "test.module".func() == 123)""", | ||||||
|             """, |  | ||||||
|             x -> x.addApi(new IsModule()), 50); |             x -> x.addApi(new IsModule()), 50); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|   | |||||||
| @@ -168,6 +168,7 @@ public class TransformingClassLoader extends ClassLoader { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     @SuppressWarnings("ArrayRecordComponent") | ||||||
|     private record TransformedClass(String name, byte[] contents) { |     private record TransformedClass(String name, byte[] contents) { | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jonathan Coates
					Jonathan Coates