From 06f970b1bdb370afd38d321ec6225f47b6cf3d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Kosman?= Date: Thu, 22 Aug 2024 22:45:00 +0200 Subject: feat: support translation to enable configuration of all mod strings. --- .../resources/assets/molehunt/lang/en_us.json | 15 +++++++++++++ src/main/java/world/anhgelus/molehunt/Game.java | 26 +++++++++------------- .../java/world/anhgelus/molehunt/Molehunt.java | 16 +++++++------ 3 files changed, 34 insertions(+), 23 deletions(-) create mode 100644 src/client/resources/assets/molehunt/lang/en_us.json (limited to 'src') diff --git a/src/client/resources/assets/molehunt/lang/en_us.json b/src/client/resources/assets/molehunt/lang/en_us.json new file mode 100644 index 0000000..d68fd0e --- /dev/null +++ b/src/client/resources/assets/molehunt/lang/en_us.json @@ -0,0 +1,15 @@ +{ + "commands.molehunt.stop.failed": "The Molehunt game has not been started yet.", + "commands.molehunt.timer.show": "Showing Molehunt timer.", + "commands.molehunt.timer.hide": "Hiding Molehunt timer.", + "commands.molehunt.moles.list": "List of moles:", + "commands.molehunt.stop.success": "The Molehunt game has been stopped.", + "molehunt.game.end.suspense.title": "And the winners are...", + "molehunt.game.end.winners.moles.title": "§cThe Moles!", + "molehunt.game.end.winners.survivors.title": "§aThe survivors!", + "molehunt.game.end.winners.subtitle": "The moles were", + "molehunt.game.start.suspense": "You are...", + "molehunt.game.start.mole.title": "§cThe Mole!", + "molehunt.game.start.mole.subtitle": "Get the list of moles with §6/molehunt moles", + "molehunt.game.start.survivor": "§aNot the Mole!" +} \ No newline at end of file diff --git a/src/main/java/world/anhgelus/molehunt/Game.java b/src/main/java/world/anhgelus/molehunt/Game.java index bf0f294..1091c0f 100644 --- a/src/main/java/world/anhgelus/molehunt/Game.java +++ b/src/main/java/world/anhgelus/molehunt/Game.java @@ -7,7 +7,6 @@ import net.minecraft.network.packet.s2c.play.TitleS2CPacket; import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; -import net.minecraft.text.TextContent; import net.minecraft.world.GameMode; import net.minecraft.world.GameRules; import world.anhgelus.molehunt.utils.TimeUtils; @@ -35,10 +34,9 @@ public class Game { } public void start() { - if (started) return; final int n = (server.getCurrentPlayerCount() - server.getCurrentPlayerCount() % 4)/4; final var playerManager = server.getPlayerManager(); - final var players = new ArrayList<>(playerManager.getPlayerList()); + final var players = playerManager.getPlayerList(); for (int i = 0; i < n; i++) { final var r = ThreadLocalRandom.current().nextInt(0, players.size()); final var mole = players.get(r); @@ -71,10 +69,10 @@ public class Game { playerManager.getPlayerList().forEach(p -> { p.networkHandler.sendPacket(timing); if (moles.contains(p)) { - p.networkHandler.sendPacket(new TitleS2CPacket(Text.of("§cThe Mole!"))); - p.networkHandler.sendPacket(new SubtitleS2CPacket(Text.of("§6get the list of moles with /molehunt moles"))); + p.networkHandler.sendPacket(new TitleS2CPacket(Text.translatable("molehunt.game.start.mole.title"))); + p.networkHandler.sendPacket(new SubtitleS2CPacket(Text.translatable("molehunt.game.start.mole.subtitle"))); } else { - p.networkHandler.sendPacket(new TitleS2CPacket(Text.of("§aNot the Mole!"))); + p.networkHandler.sendPacket(new TitleS2CPacket(Text.translatable("molehunt.game.start.survivor"))); } // reset health and food level p.setHealth(p.getMaxHealth()); @@ -112,7 +110,7 @@ public class Game { } public void stop() { - server.getPlayerManager().broadcast(Text.of("Game stopped"), false); + server.getPlayerManager().broadcast(Text.translatable("commands.molehunt.stop.success"), false); end(); } @@ -121,7 +119,7 @@ public class Game { timer = new Timer(); started = false; final var pm = server.getPlayerManager(); - final var winnerSuspense = new TitleS2CPacket(Text.of("§eAnd the winners are...")); + final var winnerSuspense = new TitleS2CPacket(Text.translatable("molehunt.game.end.suspense.title")); pm.getPlayerList().forEach(p -> { p.networkHandler.sendPacket(timing); p.networkHandler.sendPacket(winnerSuspense); @@ -132,11 +130,11 @@ public class Game { public void run() { TitleS2CPacket winner; if (gameWonByMoles()) { - winner = new TitleS2CPacket(Text.of("§cThe Moles!")); + winner = new TitleS2CPacket(Text.translatable("molehunt.game.end.winners.moles.title")); } else { - winner = new TitleS2CPacket(Text.of("§aNot the Mole!")); + winner = new TitleS2CPacket(Text.translatable("molehunt.game.end.winners.survivors.title")); } - pm.sendToAll(new SubtitleS2CPacket(Text.of("§6Moles were " + getMolesAsString()))); + pm.sendToAll(new SubtitleS2CPacket(Text.translatable("molehunt.game.end.winners.subtitle").append(getMolesAsString()))); pm.sendToAll(winner); pm.sendToAll(timing); } @@ -160,11 +158,7 @@ public class Game { } public String getMolesAsString() { - return moles.stream() - .map(ServerPlayerEntity::getDisplayName) - .filter(Objects::nonNull) - .map(Text::getString) - .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 49e775d..b2d3842 100644 --- a/src/main/java/world/anhgelus/molehunt/Molehunt.java +++ b/src/main/java/world/anhgelus/molehunt/Molehunt.java @@ -1,6 +1,7 @@ package world.anhgelus.molehunt; import com.mojang.brigadier.Command; +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents; @@ -9,7 +10,7 @@ import net.fabricmc.fabric.api.message.v1.ServerMessageEvents; import net.minecraft.network.packet.s2c.play.OverlayMessageS2CPacket; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.text.Text; +import net.minecraft.text.*; import net.minecraft.world.GameMode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,13 +45,13 @@ public class Molehunt implements ModInitializer { command.then(literal("timer").requires(ServerCommandSource::isExecutedByPlayer).then( literal("show").executes(context -> { timerVisibility.put(context.getSource().getPlayer(), true); - context.getSource().sendFeedback(() -> Text.of("Showing molehunt timer"), false); + context.getSource().sendFeedback(() -> Text.translatable("commands.molehunt.timer.show"), false); var player = context.getSource().getPlayer(); assert player != null; if (game == null || !game.hasStarted()) { - player.networkHandler.sendPacket(new OverlayMessageS2CPacket(Text.of("§cGame has not started yet"))); + player.networkHandler.sendPacket(new OverlayMessageS2CPacket(Text.translatable("commands.molehunt.stop.failed").setStyle(Style.EMPTY.withColor(16733525)))); } else { player.networkHandler.sendPacket(new OverlayMessageS2CPacket(Text.of(game.getShortRemainingText()))); } @@ -60,18 +61,19 @@ public class Molehunt implements ModInitializer { ).then( literal("hide").executes(context -> { timerVisibility.put(context.getSource().getPlayer(), false); - context.getSource().sendFeedback(() -> Text.of("Hiding molehunt timer"), false); + context.getSource().sendFeedback(() -> Text.translatable("commands.molehunt.timer.hide"), false); return Command.SINGLE_SUCCESS; }) )); command.then(literal("moles").requires(source -> (game != null) && game.hasStarted() && game.isAMole(source.getPlayer())).executes(context -> { - context.getSource().sendFeedback(() -> Text.literal("List of moles: " + game.getMolesAsString()),false); + context.getSource().sendFeedback(() -> Text.translatable("commands.molehunt.moles.list").append(" " + game.getMolesAsString()),false); return Command.SINGLE_SUCCESS; })); command.then(literal("stop").requires(source -> source.hasPermissionLevel(1)).executes(context -> { if (game == null || !game.hasStarted()) { - context.getSource().sendError(Text.of("Game has not started yet")); - return Command.SINGLE_SUCCESS; + var e = new SimpleCommandExceptionType(Text.translatable("commands.molehunt.stop.failed")); + + throw e.create(); } game.stop(); -- cgit v1.2.3