From fb4719187abe82136643906ccf947ffd5c9e3a67 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. --- src/client/resources/assets/molehunt/lang/en_us.json | 15 +++++++++++++++ src/main/java/world/anhgelus/molehunt/Game.java | 18 +++++++++--------- src/main/java/world/anhgelus/molehunt/Molehunt.java | 16 +++++++++------- 3 files changed, 33 insertions(+), 16 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 9c3910b..c680a8f 100644 --- a/src/main/java/world/anhgelus/molehunt/Game.java +++ b/src/main/java/world/anhgelus/molehunt/Game.java @@ -53,7 +53,7 @@ public class Game { gamerules.get(GameRules.DO_IMMEDIATE_RESPAWN).set(true, server); gamerules.get(GameRules.DO_ENTITY_DROPS).set(false, server); - final var title = new TitleS2CPacket(Text.of("You are...")); + final var title = new TitleS2CPacket(Text.translatable("molehunt.game.start.suspense")); playerManager.getPlayerList().forEach(p -> { p.kill(); p.networkHandler.sendPacket(timing); @@ -69,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("The Mole!"))); - p.networkHandler.sendPacket(new SubtitleS2CPacket(Text.of("Get 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("Not the Mole!"))); + p.networkHandler.sendPacket(new TitleS2CPacket(Text.translatable("molehunt.game.start.survivor"))); } // reset health and food level p.setHealth(p.getMaxHealth()); @@ -110,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(); } @@ -119,7 +119,7 @@ public class Game { timer = new Timer(); started = false; final var pm = server.getPlayerManager(); - final var winnerSuspense = new TitleS2CPacket(Text.of("And 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); @@ -130,11 +130,11 @@ public class Game { public void run() { TitleS2CPacket winner; if (gameWonByMoles()) { - winner = new TitleS2CPacket(Text.of("The Moles!")); + winner = new TitleS2CPacket(Text.translatable("molehunt.game.end.winners.moles.title")); } else { - winner = new TitleS2CPacket(Text.of("The survivors!")); + winner = new TitleS2CPacket(Text.translatable("molehunt.game.end.winners.survivors.title")); } - pm.sendToAll(new SubtitleS2CPacket(Text.of("The moles were " + getMolesAsString()))); + pm.sendToAll(new SubtitleS2CPacket(Text.translatable("molehunt.game.end.winners.subtitle").append(getMolesAsString()))); pm.sendToAll(winner); pm.sendToAll(timing); } 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