From e31510ba5ce15922901f56754b7866c110f3d405 Mon Sep 17 00:00:00 2001 From: Vankka Date: Mon, 18 Dec 2023 19:48:09 +0200 Subject: [PATCH] Some translation stuff --- .../abstraction/CommandExecution.java | 6 ++ .../abstraction/DiscordCommandExecution.java | 28 +++++-- .../abstraction/GameCommandExecution.java | 11 +++ .../combined/commands/DebugCommand.java | 2 +- .../combined/commands/LinkInitCommand.java | 40 ++++++++-- .../combined/commands/LinkedCommand.java | 16 ++-- .../commands/DiscordSRVDiscordCommand.java | 5 +- .../common/command/util/CommandUtil.java | 26 +++++-- .../config/messages/MessagesConfig.java | 73 ++++++++++++++----- 9 files changed, 158 insertions(+), 49 deletions(-) diff --git a/common/src/main/java/com/discordsrv/common/command/combined/abstraction/CommandExecution.java b/common/src/main/java/com/discordsrv/common/command/combined/abstraction/CommandExecution.java index 1d119b32..f24c5e6b 100644 --- a/common/src/main/java/com/discordsrv/common/command/combined/abstraction/CommandExecution.java +++ b/common/src/main/java/com/discordsrv/common/command/combined/abstraction/CommandExecution.java @@ -1,5 +1,8 @@ package com.discordsrv.common.command.combined.abstraction; +import com.discordsrv.common.config.messages.MessagesConfig; +import net.kyori.adventure.text.Component; + import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -8,6 +11,7 @@ import java.util.Locale; public interface CommandExecution { Locale locale(); + MessagesConfig messages(); void setEphemeral(boolean ephemeral); @@ -23,5 +27,7 @@ public interface CommandExecution { void send(Collection texts, Collection extra); + void send(Component minecraft, String discord); + void runAsync(Runnable runnable); } diff --git a/common/src/main/java/com/discordsrv/common/command/combined/abstraction/DiscordCommandExecution.java b/common/src/main/java/com/discordsrv/common/command/combined/abstraction/DiscordCommandExecution.java index f5ca0f49..7a733131 100644 --- a/common/src/main/java/com/discordsrv/common/command/combined/abstraction/DiscordCommandExecution.java +++ b/common/src/main/java/com/discordsrv/common/command/combined/abstraction/DiscordCommandExecution.java @@ -3,12 +3,14 @@ package com.discordsrv.common.command.combined.abstraction; import com.discordsrv.api.discord.events.interaction.command.DiscordChatInputInteractionEvent; import com.discordsrv.api.discord.events.interaction.command.DiscordCommandAutoCompleteInteractionEvent; import com.discordsrv.common.DiscordSRV; +import com.discordsrv.common.config.messages.MessagesConfig; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent; import net.dv8tion.jda.api.interactions.InteractionHook; import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback; import net.dv8tion.jda.api.interactions.commands.CommandInteractionPayload; import net.dv8tion.jda.api.interactions.commands.OptionMapping; +import net.kyori.adventure.text.Component; import org.apache.commons.lang3.StringUtils; import java.util.Collection; @@ -47,6 +49,11 @@ public class DiscordCommandExecution implements CommandExecution { return createEvent.getUserLocale().toLocale(); } + @Override + public MessagesConfig messages() { + return discordSRV.messagesConfig(locale()); + } + @Override public void setEphemeral(boolean ephemeral) { isEphemeral.set(ephemeral); @@ -60,10 +67,6 @@ public class DiscordCommandExecution implements CommandExecution { @Override public void send(Collection texts, Collection extra) { - if (replyCallback == null) { - throw new IllegalStateException("May not be used on auto completions"); - } - StringBuilder builder = new StringBuilder(); EnumMap formats = new EnumMap<>(Text.Formatting.class); @@ -80,12 +83,25 @@ public class DiscordCommandExecution implements CommandExecution { verifyStyle(builder, formats, null); } + sendResponse(builder.toString()); + } + + @Override + public void send(Component minecraft, String discord) { + sendResponse(discord); + } + + private void sendResponse(String content) { + if (replyCallback == null) { + throw new IllegalStateException("May not be used on auto completions"); + } + InteractionHook interactionHook = hook.get(); boolean ephemeral = isEphemeral.get(); if (interactionHook != null) { - interactionHook.sendMessage(builder.toString()).setEphemeral(ephemeral).queue(); + interactionHook.sendMessage(content).setEphemeral(ephemeral).queue(); } else { - replyCallback.reply(builder.toString()).setEphemeral(ephemeral).queue(); + replyCallback.reply(content).setEphemeral(ephemeral).queue(); } } diff --git a/common/src/main/java/com/discordsrv/common/command/combined/abstraction/GameCommandExecution.java b/common/src/main/java/com/discordsrv/common/command/combined/abstraction/GameCommandExecution.java index faa05fcc..46fbc3cf 100644 --- a/common/src/main/java/com/discordsrv/common/command/combined/abstraction/GameCommandExecution.java +++ b/common/src/main/java/com/discordsrv/common/command/combined/abstraction/GameCommandExecution.java @@ -3,6 +3,7 @@ package com.discordsrv.common.command.combined.abstraction; import com.discordsrv.common.DiscordSRV; import com.discordsrv.common.command.game.abstraction.GameCommandArguments; import com.discordsrv.common.command.game.sender.ICommandSender; +import com.discordsrv.common.config.messages.MessagesConfig; import com.discordsrv.common.player.IPlayer; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; @@ -41,6 +42,11 @@ public class GameCommandExecution implements CommandExecution { return sender instanceof IPlayer ? ((IPlayer) sender).locale() : null; } + @Override + public MessagesConfig messages() { + return discordSRV.messagesConfig(locale()); + } + @Override public void setEphemeral(boolean ephemeral) { // NO-OP @@ -60,6 +66,11 @@ public class GameCommandExecution implements CommandExecution { sender.sendMessage(builder.build().replaceText(URL_REPLACEMENT)); } + @Override + public void send(Component minecraft, String discord) { + sender.sendMessage(minecraft); + } + private TextComponent.Builder render(Collection texts) { TextComponent.Builder builder = Component.text(); for (Text text : texts) { diff --git a/common/src/main/java/com/discordsrv/common/command/combined/commands/DebugCommand.java b/common/src/main/java/com/discordsrv/common/command/combined/commands/DebugCommand.java index 23cd08c0..d70a93e7 100644 --- a/common/src/main/java/com/discordsrv/common/command/combined/commands/DebugCommand.java +++ b/common/src/main/java/com/discordsrv/common/command/combined/commands/DebugCommand.java @@ -18,8 +18,8 @@ package com.discordsrv.common.command.combined.commands; -import com.discordsrv.api.discord.entity.interaction.command.DiscordCommand; import com.discordsrv.api.discord.entity.interaction.command.CommandOption; +import com.discordsrv.api.discord.entity.interaction.command.DiscordCommand; import com.discordsrv.api.discord.entity.interaction.component.ComponentIdentifier; import com.discordsrv.common.DiscordSRV; import com.discordsrv.common.command.combined.abstraction.CombinedCommand; diff --git a/common/src/main/java/com/discordsrv/common/command/combined/commands/LinkInitCommand.java b/common/src/main/java/com/discordsrv/common/command/combined/commands/LinkInitCommand.java index aed1ecd5..653555e0 100644 --- a/common/src/main/java/com/discordsrv/common/command/combined/commands/LinkInitCommand.java +++ b/common/src/main/java/com/discordsrv/common/command/combined/commands/LinkInitCommand.java @@ -7,6 +7,7 @@ import com.discordsrv.common.DiscordSRV; import com.discordsrv.common.command.combined.abstraction.CombinedCommand; import com.discordsrv.common.command.combined.abstraction.CommandExecution; import com.discordsrv.common.command.combined.abstraction.GameCommandExecution; +import com.discordsrv.common.command.combined.abstraction.Text; import com.discordsrv.common.command.game.abstraction.GameCommand; import com.discordsrv.common.command.game.sender.ICommandSender; import com.discordsrv.common.command.util.CommandUtil; @@ -16,6 +17,7 @@ import com.discordsrv.common.linking.LinkStore; import com.discordsrv.common.permission.util.Permission; import com.discordsrv.common.player.IPlayer; import com.github.benmanes.caffeine.cache.Cache; +import net.kyori.adventure.text.format.NamedTextColor; import org.apache.commons.lang3.StringUtils; import java.util.UUID; @@ -93,7 +95,7 @@ public class LinkInitCommand extends CombinedCommand { if (sender instanceof IPlayer) { startLinking((IPlayer) sender, ((GameCommandExecution) execution).getLabel()); } else { - // TODO: please specify player+user + sender.sendMessage(execution.messages().minecraft.pleaseSpecifyPlayerAndUserToLink.asComponent()); } return; } @@ -106,25 +108,44 @@ public class LinkInitCommand extends CombinedCommand { LinkProvider linkProvider = discordSRV.linkProvider(); if (!(linkProvider instanceof LinkStore)) { - // TODO: not allowed + execution.send(new Text("Cannot create links using this link provider").withGameColor(NamedTextColor.DARK_RED)); return; } UUID playerUUID = CommandUtil.lookupPlayer(discordSRV, execution, false, playerArgument, null); if (playerUUID == null) { - // TODO: player not found + execution.send( + execution.messages().minecraft.playerNotFound.asComponent(), + execution.messages().discord.playerNotFound + ); return; } Long userId = CommandUtil.lookupUser(discordSRV, execution, false, userArgument, null); if (userId == null) { - // TODO: user not found + execution.send( + execution.messages().minecraft.userNotFound.asComponent(), + execution.messages().discord.userNotFound + ); return; } linkProvider.queryUserId(playerUUID).thenCompose(opt -> { if (opt.isPresent()) { - // TODO: already linked + execution.send( + execution.messages().minecraft.playerAlreadyLinked3rd.asComponent(), + execution.messages().discord.playerAlreadyLinked3rd + ); + return null; + } + + return linkProvider.queryPlayerUUID(userId); + }).thenCompose(opt -> { + if (opt.isPresent()) { + execution.send( + execution.messages().minecraft.userAlreadyLinked3rd.asComponent(), + execution.messages().discord.userAlreadyLinked3rd + ); return null; } @@ -135,14 +156,17 @@ public class LinkInitCommand extends CombinedCommand { return; } - // TODO: it did work + execution.send( + execution.messages().minecraft.nowLinked3rd.asComponent(), + execution.messages().discord.nowLinked3rd + ); }); } private void startLinking(IPlayer player, String label) { LinkProvider linkProvider = discordSRV.linkProvider(); if (linkProvider.getCachedUserId(player.uniqueId()).isPresent()) { - player.sendMessage(discordSRV.messagesConfig(player).alreadyLinked.asComponent()); + player.sendMessage(discordSRV.messagesConfig(player).alreadyLinked1st.asComponent()); return; } @@ -159,7 +183,7 @@ public class LinkInitCommand extends CombinedCommand { return; } if (userId.isPresent()) { - player.sendMessage(discordSRV.messagesConfig(player).youAreNowLinked.asComponent()); + player.sendMessage(discordSRV.messagesConfig(player).nowLinked1st.asComponent()); return; } diff --git a/common/src/main/java/com/discordsrv/common/command/combined/commands/LinkedCommand.java b/common/src/main/java/com/discordsrv/common/command/combined/commands/LinkedCommand.java index bf361f06..323ba200 100644 --- a/common/src/main/java/com/discordsrv/common/command/combined/commands/LinkedCommand.java +++ b/common/src/main/java/com/discordsrv/common/command/combined/commands/LinkedCommand.java @@ -74,13 +74,17 @@ public class LinkedCommand extends CombinedCommand { } if (result.isPlayer()) { - execution.runAsync(() -> discordSRV.linkProvider().getUserId(result.getPlayerUUID()).whenComplete((userId, t) -> { - execution.send(new Text(userId.map(Long::toUnsignedString).orElse("Not linked"))); // TODO: username - })); + execution.runAsync(() -> { + discordSRV.linkProvider().getUserId(result.getPlayerUUID()).whenComplete((userId, t) -> { + execution.send(new Text(userId.map(Long::toUnsignedString).orElse("Not linked"))); // TODO: username + }); + }); } else { - execution.runAsync(() -> discordSRV.linkProvider().getPlayerUUID(result.getUserId()).whenComplete((playerUUID, t) -> { - execution.send(new Text(playerUUID.map(UUID::toString).orElse("Not linked"))); // TODO: player name - })); + execution.runAsync(() -> { + discordSRV.linkProvider().getPlayerUUID(result.getUserId()).whenComplete((playerUUID, t) -> { + execution.send(new Text(playerUUID.map(UUID::toString).orElse("Not linked"))); // TODO: player name + }); + }); } } } diff --git a/common/src/main/java/com/discordsrv/common/command/discord/commands/DiscordSRVDiscordCommand.java b/common/src/main/java/com/discordsrv/common/command/discord/commands/DiscordSRVDiscordCommand.java index 1105bf95..3979ee59 100644 --- a/common/src/main/java/com/discordsrv/common/command/discord/commands/DiscordSRVDiscordCommand.java +++ b/common/src/main/java/com/discordsrv/common/command/discord/commands/DiscordSRVDiscordCommand.java @@ -6,6 +6,7 @@ import com.discordsrv.common.DiscordSRV; import com.discordsrv.common.command.combined.commands.*; import com.discordsrv.common.command.discord.commands.subcommand.ExecuteCommand; import com.discordsrv.common.config.main.DiscordCommandConfig; +import com.discordsrv.common.linking.LinkStore; public class DiscordSRVDiscordCommand { @@ -21,12 +22,14 @@ public class DiscordSRVDiscordCommand { .addSubCommand(DebugCommand.getDiscord(discordSRV)) .addSubCommand(VersionCommand.getDiscord(discordSRV)) .addSubCommand(ResyncCommand.getDiscord(discordSRV)) - .addSubCommand(LinkInitCommand.getDiscord(discordSRV)) .addSubCommand(LinkedCommand.getDiscord(discordSRV)); if (config.execute.enabled) { builder = builder.addSubCommand(ExecuteCommand.get(discordSRV)); } + if (discordSRV.linkProvider() instanceof LinkStore) { + builder = builder.addSubCommand(LinkInitCommand.getDiscord(discordSRV)); + } INSTANCE = builder .setGuildOnly(false) diff --git a/common/src/main/java/com/discordsrv/common/command/util/CommandUtil.java b/common/src/main/java/com/discordsrv/common/command/util/CommandUtil.java index 140632f7..12a303b9 100644 --- a/common/src/main/java/com/discordsrv/common/command/util/CommandUtil.java +++ b/common/src/main/java/com/discordsrv/common/command/util/CommandUtil.java @@ -4,14 +4,13 @@ import com.discordsrv.common.DiscordSRV; import com.discordsrv.common.command.combined.abstraction.CommandExecution; import com.discordsrv.common.command.combined.abstraction.DiscordCommandExecution; import com.discordsrv.common.command.combined.abstraction.GameCommandExecution; -import com.discordsrv.common.command.combined.abstraction.Text; import com.discordsrv.common.command.game.sender.ICommandSender; +import com.discordsrv.common.config.messages.MessagesConfig; import com.discordsrv.common.permission.util.Permission; import com.discordsrv.common.player.IPlayer; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.utils.MiscUtil; -import net.kyori.adventure.text.format.NamedTextColor; import org.jetbrains.annotations.Nullable; import java.util.List; @@ -76,6 +75,8 @@ public final class CommandUtil { boolean lookupUser, @Nullable Permission otherPermission ) { + MessagesConfig messages = discordSRV.messagesConfig(execution.locale()); + if (execution instanceof GameCommandExecution) { ICommandSender sender = ((GameCommandExecution) execution).getSender(); if (target != null) { @@ -86,7 +87,10 @@ public final class CommandUtil { } else if (sender instanceof IPlayer && selfPermitted && lookupPlayer) { target = ((IPlayer) sender).uniqueId().toString(); } else { - execution.send(new Text(discordSRV.messagesConfig(execution.locale()).both.placeSpecifyTarget).withGameColor(NamedTextColor.RED)); + execution.send( + messages.minecraft.pleaseSpecifyPlayer.asComponent(), + messages.discord.pleaseSpecifyPlayer + ); return TargetLookupResult.INVALID; } } else if (execution instanceof DiscordCommandExecution) { @@ -94,7 +98,10 @@ public final class CommandUtil { if (selfPermitted && lookupUser) { target = Long.toUnsignedString(((DiscordCommandExecution) execution).getUser().getIdLong()); } else { - execution.send(new Text(discordSRV.messagesConfig(execution.locale()).both.placeSpecifyTarget).withGameColor(NamedTextColor.RED)); + execution.send( + messages.minecraft.pleaseSpecifyUser.asComponent(), + messages.discord.pleaseSpecifyUser + ); return TargetLookupResult.INVALID; } } @@ -109,8 +116,10 @@ public final class CommandUtil { try { id = MiscUtil.parseLong(target); } catch (IllegalArgumentException ignored) { - execution.send(new Text(discordSRV.messagesConfig(execution.locale()).both.invalidTarget) - .withGameColor(NamedTextColor.RED)); + execution.send( + messages.minecraft.userNotFound.asComponent(), + messages.discord.userNotFound + ); return TargetLookupResult.INVALID; } @@ -142,7 +151,10 @@ public final class CommandUtil { try { uuid = UUID.fromString(target); } catch (IllegalArgumentException ignored) { - execution.send(new Text(discordSRV.messagesConfig(execution.locale()).both.invalidTarget).withGameColor(NamedTextColor.RED)); + execution.send( + messages.minecraft.playerNotFound.asComponent(), + messages.discord.playerNotFound + ); return TargetLookupResult.INVALID; } } else { diff --git a/common/src/main/java/com/discordsrv/common/config/messages/MessagesConfig.java b/common/src/main/java/com/discordsrv/common/config/messages/MessagesConfig.java index c16cc3f4..01eb74a7 100644 --- a/common/src/main/java/com/discordsrv/common/config/messages/MessagesConfig.java +++ b/common/src/main/java/com/discordsrv/common/config/messages/MessagesConfig.java @@ -21,34 +21,54 @@ public class MessagesConfig implements Config { @ConfigSerializable public static class Minecraft { + private static final String ERROR_COLOR = "&c"; + private static final String SUCCESS_COLOR = "&a"; + private static final String NEUTRAL_COLOR = "&b"; + private MinecraftMessage make(String rawFormat) { return new MinecraftMessage(rawFormat); } @Comment("Generic") - @Constants("&c") + @Constants(ERROR_COLOR) public MinecraftMessage noPermission = make("%1Sorry, but you do not have permission to use that command"); + @Constants(ERROR_COLOR) + public MinecraftMessage pleaseSpecifyPlayer = make("%1Please specify the Minecraft player"); + @Constants(ERROR_COLOR) + public MinecraftMessage pleaseSpecifyUser = make("%1Please specify the Discord user"); + @Constants(ERROR_COLOR) + public MinecraftMessage playerNotFound = make("%1Minecraft player not found"); + @Constants(ERROR_COLOR) + public MinecraftMessage userNotFound = make("%1Discord user not found"); @Untranslated(Untranslated.Type.COMMENT) @Comment("/discord link") - @Constants("&c") + @Constants(ERROR_COLOR) public MinecraftMessage unableToCheckLinkingStatus = make("%1Unable to check linking status, please try again later"); - @Constants("&c") - public MinecraftMessage alreadyLinked = make("%1You are already linked"); - @Constants("&c") + @Constants(ERROR_COLOR) + public MinecraftMessage alreadyLinked1st = make("%1You are already linked"); + @Constants(ERROR_COLOR) + public MinecraftMessage pleaseSpecifyPlayerAndUserToLink = make("%1Please specify the Minecraft player and the Discord user to link"); + @Constants(ERROR_COLOR) + public MinecraftMessage playerAlreadyLinked3rd = make("%1That player is already linked"); + @Constants(ERROR_COLOR) + public MinecraftMessage userAlreadyLinked3rd = make("%1That player is already linked"); + @Constants(ERROR_COLOR) public MinecraftMessage pleaseWaitBeforeRunningThatCommandAgain = make("%1Please wait before running that command again"); - @Constants("&c") + @Constants(ERROR_COLOR) public MinecraftMessage unableToLinkAtThisTime = make("%1Unable to check linking status, please try again later"); - @Constants("&b") + @Constants(NEUTRAL_COLOR) public MinecraftMessage checkingLinkStatus = make("%1Checking linking status..."); - @Constants("&b") - public MinecraftMessage youAreNowLinked = make("%1You are now linked!"); + @Constants(SUCCESS_COLOR) + public MinecraftMessage nowLinked1st = make("%1You are now linked!"); + @Constants(SUCCESS_COLOR) + public MinecraftMessage nowLinked3rd = make("%1Link created successfully"); @Constants({ - "&b", - "&7[click:open_url:%minecraftauth_link%][hover:show_text:Click to open]%minecraftauth_link_simple%[click]&b", - "&7MinecraftAuth" + NEUTRAL_COLOR, + "&f[click:open_url:%minecraftauth_link%][hover:show_text:Click to open]%minecraftauth_link_simple%[click]" + NEUTRAL_COLOR, + "&fMinecraftAuth" }) - public MinecraftMessage minecraftAuthLinking = make("%1Please visit %2 to link your account through %3"); + public MinecraftMessage minecraftAuthLinking = make("%1Please visit %2 to link your account through %4"); } @@ -57,14 +77,27 @@ public class MessagesConfig implements Config { @ConfigSerializable public static class Discord { - } - - public Both both = new Both(); - - public static class Both { + private static final String SUCCESS_PREFIX = "✅ "; + private static final String INPUT_ERROR_PREFIX = "\uD83D\uDDD2️ "; + private static final String ERROR_PREFIX = "❌ "; @Comment("Generic") - public String invalidTarget = "Invalid target"; - public String placeSpecifyTarget = "Please specify the target"; + @Constants(INPUT_ERROR_PREFIX) + public String pleaseSpecifyPlayer = "%1Please specify the Minecraft player"; + @Constants(INPUT_ERROR_PREFIX) + public String pleaseSpecifyUser = "%1Please specify the Discord user"; + @Constants(ERROR_PREFIX) + public String playerNotFound = "%1Minecraft player not found"; + @Constants(ERROR_PREFIX) + public String userNotFound = "%1Discord user not found"; + + @Untranslated(Untranslated.Type.COMMENT) + @Comment("/discord link") + @Constants(ERROR_PREFIX) + public String playerAlreadyLinked3rd = "%1That Minecraft player is already linked"; + @Constants(ERROR_PREFIX) + public String userAlreadyLinked3rd = "%1That Discord user is already linked"; + @Constants(SUCCESS_PREFIX) + public String nowLinked3rd = "%1Link created successfully"; } }