From d89d380f3229ab22a47e43e6cef50c604629bda1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Kosman?= Date: Fri, 23 Aug 2024 14:27:22 +0200 Subject: style: improve code as suggested by @anhgelus --- src/main/java/world/anhgelus/molehunt/Game.java | 11 ++++++----- src/main/java/world/anhgelus/molehunt/Molehunt.java | 5 +---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/java/world/anhgelus/molehunt/Game.java b/src/main/java/world/anhgelus/molehunt/Game.java index 32dea83..54e5c3a 100644 --- a/src/main/java/world/anhgelus/molehunt/Game.java +++ b/src/main/java/world/anhgelus/molehunt/Game.java @@ -44,10 +44,7 @@ public class Game { final var playerManager = server.getPlayerManager(); final var players = new ArrayList<>(playerManager.getPlayerList()); - for (int i = 0; i < n; i++) { - // Can happen if the mole count is greater than the player count - if (players.isEmpty()) break; - + for (int i = 0; i < n && !players.isEmpty(); i++) { final var r = ThreadLocalRandom.current().nextInt(0, players.size()); final var mole = players.get(r); if (mole == null) throw new IllegalStateException("Mole is null!"); @@ -160,7 +157,11 @@ public class Game { } public String getMolesAsString() { - return moles.stream().map(ServerPlayerEntity::getDisplayName).filter(Objects::nonNull).map(Text::toString).collect(Collectors.joining(", ")); + return moles.stream() + .map(ServerPlayerEntity::getDisplayName) + .filter(Objects::nonNull) + .map(Text::toString) + .collect(Collectors.joining(", ")); } public boolean isAMole(ServerPlayerEntity player) { diff --git a/src/main/java/world/anhgelus/molehunt/Molehunt.java b/src/main/java/world/anhgelus/molehunt/Molehunt.java index 1752d26..5df63b1 100644 --- a/src/main/java/world/anhgelus/molehunt/Molehunt.java +++ b/src/main/java/world/anhgelus/molehunt/Molehunt.java @@ -34,7 +34,6 @@ public class Molehunt implements ModInitializer { @Override public void onInitialize() { LOGGER.info("Initializing Molehunt"); - LOGGER.info(String.valueOf(CONFIG.GAME_DURATION)); final var command = literal("molehunt"); command.then(literal("start").requires(source -> source.hasPermissionLevel(1)).executes(context -> { @@ -71,9 +70,7 @@ public class Molehunt implements ModInitializer { })); command.then(literal("stop").requires(source -> source.hasPermissionLevel(1)).executes(context -> { if (game == null || !game.hasStarted()) { - var e = new SimpleCommandExceptionType(Text.translatable("commands.molehunt.stop.failed")); - - throw e.create(); + throw (new SimpleCommandExceptionType(Text.translatable("commands.molehunt.stop.failed"))).create(); } game.stop(); -- cgit v1.2.3