From 4602ca71db189fab6f8a6615dbae1e91eccbc011 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Tue, 1 Mar 2022 16:56:12 +0100 Subject: [PATCH] Translates the list command's messages --- .../paidsigns/command/ListCommand.java | 69 ++++++++++++------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/knarcraft/paidsigns/command/ListCommand.java b/src/main/java/net/knarcraft/paidsigns/command/ListCommand.java index 02e5d75..da173a1 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/ListCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/ListCommand.java @@ -3,12 +3,20 @@ package net.knarcraft.paidsigns.command; import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.container.PaidSign; import net.knarcraft.paidsigns.container.PaidSignCondition; +import net.knarcraft.paidsigns.formatting.StringFormatter; +import net.knarcraft.paidsigns.formatting.TranslatableMessage; +import net.knarcraft.paidsigns.formatting.Translator; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; import java.util.Map; +import static net.knarcraft.paidsigns.formatting.StringFormatter.getTranslatedErrorMessage; +import static net.knarcraft.paidsigns.formatting.StringFormatter.replacePlaceholder; +import static net.knarcraft.paidsigns.formatting.StringFormatter.replacePlaceholders; +import static net.knarcraft.paidsigns.formatting.StringFormatter.translateBoolean; + /** * A representation of the command for listing information about paid signs */ @@ -19,10 +27,7 @@ public class ListCommand extends TokenizedCommand { @NotNull String[] args) { super.onCommand(sender, command, label, args); if (argumentSize < 1) { - sender.sendMessage("Paid signs:"); - for (String signName : PaidSigns.getInstance().getSignManager().getAllPaidSigns().keySet()) { - sender.sendMessage(" | " + signName); - } + displaySigns(sender); return true; } else if (argumentSize < 3) { return parsePaidSignSelection(sender); @@ -30,6 +35,21 @@ public class ListCommand extends TokenizedCommand { return false; } + /** + * Displays all available signs + * + * @param sender

The command sender to display the signs to

+ */ + private void displaySigns(CommandSender sender) { + StringBuilder signs = new StringBuilder(); + for (String signName : PaidSigns.getInstance().getSignManager().getAllPaidSigns().keySet()) { + signs.append(StringFormatter.replacePlaceholder(Translator.getTranslatedMessage( + TranslatableMessage.PAID_SIGNS_INFO_FORMAT), "{name}", signName)); + } + sender.sendMessage(StringFormatter.replacePlaceholder(Translator.getTranslatedMessage( + TranslatableMessage.PAID_SIGNS_INFO), "{signs}", signs.toString())); + } + /** * Parses the given input and displays the wanted paid-sign information * @@ -39,7 +59,7 @@ public class ListCommand extends TokenizedCommand { private boolean parsePaidSignSelection(CommandSender sender) { PaidSign paidSign = PaidSigns.getInstance().getSignManager().getPaidSign(arguments.get(0)); if (paidSign == null) { - sender.sendMessage("No such paid sign"); + sender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.ERROR_PAID_SIGN_NOT_FOUND)); return false; } if (argumentSize < 2) { @@ -49,11 +69,12 @@ public class ListCommand extends TokenizedCommand { try { signLine = (short) (Short.parseShort(arguments.get(1)) - 1); } catch (NumberFormatException exception) { - sender.sendMessage("Invalid number given"); + sender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.ERROR_INVALID_NUMBER)); return false; } if (!paidSign.getConditions().containsKey(signLine)) { - sender.sendMessage("The paid sign you specified has no condition for line " + signLine); + sender.sendMessage(replacePlaceholder(getTranslatedErrorMessage( + TranslatableMessage.ERROR_NO_SUCH_CONDITION), "{line}", String.valueOf(signLine))); return false; } PaidSignCondition condition = paidSign.getConditions().get(signLine); @@ -72,13 +93,11 @@ public class ListCommand extends TokenizedCommand { */ private void displayPaidSignCondition(CommandSender sender, String signName, short signLine, PaidSignCondition condition) { - sender.sendMessage("Paid sign condition info: "); - sender.sendMessage("Paid sign name: " + signName); - sender.sendMessage("Condition line: " + signLine); - sender.sendMessage("Condition match string: " + condition.getStringToMatch()); - sender.sendMessage("Execute RegEx: " + condition.executeRegex()); - sender.sendMessage("Ignore case: " + condition.ignoreCase()); - sender.sendMessage("Ignore color: " + condition.ignoreColor()); + sender.sendMessage(StringFormatter.replacePlaceholders(Translator.getTranslatedMessage( + TranslatableMessage.PAID_SIGN_CONDITION_INFO), new String[]{"{name}", "{line}", "{match}", "{regex}", + "{case}", "{color}"}, new String[]{signName, String.valueOf(signLine + 1), condition.getStringToMatch(), + translateBoolean(condition.executeRegex()), translateBoolean(condition.ignoreCase()), + translateBoolean(condition.ignoreColor())})); } /** @@ -88,17 +107,19 @@ public class ListCommand extends TokenizedCommand { * @param paidSign

The paid sign to display information about

*/ private void displayPaidSign(CommandSender sender, PaidSign paidSign) { - sender.sendMessage("Paid sign info:"); - sender.sendMessage("Name: " + paidSign.getName()); - sender.sendMessage("Cost: " + paidSign.getCost()); - sender.sendMessage("Permission: " + paidSign.getPermission()); - sender.sendMessage("Ignore case: " + paidSign.getIgnoreCase()); - sender.sendMessage("Ignore color: " + paidSign.getIgnoreColor()); - sender.sendMessage("Sign conditions: "); - Map conditions = paidSign.getConditions(); - for (short lineIndex : conditions.keySet()) { - sender.sendMessage(" | " + (lineIndex + 1) + ". " + conditions.get(lineIndex).getStringToMatch()); + Map signConditions = paidSign.getConditions(); + StringBuilder conditions = new StringBuilder(); + for (short lineIndex : signConditions.keySet()) { + String format = Translator.getTranslatedMessage(TranslatableMessage.PAID_SIGN_INFO_CONDITION_FORMAT); + conditions.append(StringFormatter.replacePlaceholders(format, new String[]{"{line}", "{condition}"}, + new String[]{String.valueOf((lineIndex + 1)), signConditions.get(lineIndex).getStringToMatch()})); } + + sender.sendMessage(replacePlaceholders(Translator.getTranslatedMessage( + TranslatableMessage.PAID_SIGN_INFO), new String[]{"{name}", "{cost}", "{permission}", "{case}", + "{color}", "{conditions}"}, new String[]{paidSign.getName(), String.valueOf(paidSign.getCost()), + paidSign.getPermission(), translateBoolean(paidSign.getIgnoreCase()), + translateBoolean(paidSign.getIgnoreColor()), conditions.toString()})); } }