Fixes some problems with color formatting and allows specifying the format of the permission list

This commit is contained in:
Kristian Knarvik 2022-01-25 13:38:16 +01:00
parent 967b533e55
commit ea77c36564
6 changed files with 25 additions and 18 deletions

View File

@ -13,7 +13,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import static net.knarcraft.permissionsigns.formatting.StringFormatter.replacePlaceholder; import static net.knarcraft.permissionsigns.formatting.StringFormatter.replacePlaceholder;
import static net.knarcraft.permissionsigns.formatting.StringFormatter.translateAllColorCodes;
/** /**
* This class represents a placed and active permission sign * This class represents a placed and active permission sign
@ -132,9 +131,9 @@ public class PermissionSign {
*/ */
public String[] getSignLines() { public String[] getSignLines() {
String[] lines = new String[4]; String[] lines = new String[4];
lines[0] = translateAllColorCodes(Translator.getTranslatedMessage(TranslatableMessage.SIGN_PREFIX)); lines[0] = Translator.getTranslatedMessage(TranslatableMessage.SIGN_PREFIX);
lines[1] = translateAllColorCodes(replacePlaceholder(Translator.getTranslatedMessage( lines[1] = replacePlaceholder(Translator.getTranslatedMessage(
TranslatableMessage.SIGN_NAME_FORMAT), "{name}", getName())); TranslatableMessage.SIGN_NAME_FORMAT), "{name}", getName());
lines[2] = getDurationString(); lines[2] = getDurationString();
lines[3] = getCostString(); lines[3] = getCostString();
return lines; return lines;

View File

@ -84,8 +84,8 @@ public class StringFormatter {
* @return <p>The formatted message</p> * @return <p>The formatted message</p>
*/ */
private static String formatMessage(String message) { private static String formatMessage(String message) {
return translateAllColorCodes(Translator.getTranslatedMessage(TranslatableMessage.PREFIX)) + " " + return Translator.getTranslatedMessage(TranslatableMessage.PREFIX) + " " +
ChatColor.RESET + translateAllColorCodes(message); ChatColor.RESET + message;
} }
/** /**

View File

@ -173,6 +173,11 @@ public enum TranslatableMessage {
/** /**
* The text describing all properties of a permission sign * The text describing all properties of a permission sign
*/ */
SIGN_INFO SIGN_INFO,
/**
* The format used for each permission displayed in a list when displaying permission sign info
*/
SIGN_INFO_PERMISSION_FORMAT
} }

View File

@ -42,13 +42,15 @@ public final class Translator {
if (translatedMessages == null) { if (translatedMessages == null) {
return "Translated strings not loaded"; return "Translated strings not loaded";
} }
String translatedMessage;
if (translatedMessages.containsKey(translatableMessage)) { if (translatedMessages.containsKey(translatableMessage)) {
return translatedMessages.get(translatableMessage); translatedMessage = translatedMessages.get(translatableMessage);
} else if (backupTranslatedMessages.containsKey(translatableMessage)) { } else if (backupTranslatedMessages.containsKey(translatableMessage)) {
return backupTranslatedMessages.get(translatableMessage); translatedMessage = backupTranslatedMessages.get(translatableMessage);
} else { } else {
return translatableMessage.toString(); translatedMessage = translatableMessage.toString();
} }
return StringFormatter.translateAllColorCodes(translatedMessage);
} }
/** /**

View File

@ -101,15 +101,14 @@ public class SignListener implements Listener {
permissionString = new StringBuilder(permissionNodes.get(0)); permissionString = new StringBuilder(permissionNodes.get(0));
} else { } else {
for (String permissionNode : permissionNodes) { for (String permissionNode : permissionNodes) {
permissionString.append("\n" + "&f| &7- ").append(permissionNode); String format = Translator.getTranslatedMessage(TranslatableMessage.SIGN_INFO_PERMISSION_FORMAT);
permissionString.append(StringFormatter.replacePlaceholder(format, "{permission}", permissionNode));
} }
permissionString.append("\n");
} }
String replacedString = StringFormatter.replacePlaceholders(rawSignInfo, new String[]{"{Name}", "{Permissions}", return StringFormatter.replacePlaceholders(rawSignInfo, new String[]{"{Name}", "{Permissions}",
"{Cost}", "{Duration}"}, new String[]{sign.getName(), permissionString.toString(), sign.getCostString(), "{Cost}", "{Duration}"}, new String[]{sign.getName(), permissionString.toString(), sign.getCostString(),
sign.getDurationString()}); sign.getDurationString()});
return StringFormatter.translateAllColorCodes(replacedString);
} }
/** /**

View File

@ -14,7 +14,7 @@ en:
SIGN_DURATION_FORMAT: "{duration} {unit}" SIGN_DURATION_FORMAT: "{duration} {unit}"
SIGN_PERMANENT: "Permanent" SIGN_PERMANENT: "Permanent"
SIGN_COST_FREE: "Free" SIGN_COST_FREE: "Free"
SIGN_COST_FORMAT: "{cost}#66ff00{currency}" SIGN_COST_FORMAT: "{cost}{currency}"
COST_INVALID_NUMBER: "&7The given cost is not a valid number" COST_INVALID_NUMBER: "&7The given cost is not a valid number"
DURATION_INVALID_NUMBER: "&7The given duration is not a valid number" DURATION_INVALID_NUMBER: "&7The given duration is not a valid number"
COMMAND_PLAYER_ONLY: "&7This command is only available to players" COMMAND_PLAYER_ONLY: "&7This command is only available to players"
@ -33,6 +33,7 @@ en:
CREATION_REQUEST_CANCELLED: "&7Your last permission sign creation request has been cancelled" CREATION_REQUEST_CANCELLED: "&7Your last permission sign creation request has been cancelled"
RELOAD_SUCCESSFUL: "&7The plugin has been successfully reloaded" RELOAD_SUCCESSFUL: "&7The plugin has been successfully reloaded"
SIGN_INFO: "&f---- &4Permission Sign&f ----\n&f| &bName: &7{Name}\n&f| &bPermission(s): &7{Permissions}\n&f| &bCost: &7{Cost}\n&f| &bDuration: &7{Duration}\n&f| &7Right-click the sign while sneaking to buy the permission(s)\n&f-------------------------" SIGN_INFO: "&f---- &4Permission Sign&f ----\n&f| &bName: &7{Name}\n&f| &bPermission(s): &7{Permissions}\n&f| &bCost: &7{Cost}\n&f| &bDuration: &7{Duration}\n&f| &7Right-click the sign while sneaking to buy the permission(s)\n&f-------------------------"
SIGN_INFO_PERMISSION_FORMAT: "\n&f| &7- {permission}"
nb-no: nb-no:
PREFIX: "[Tilgangsrettighetsskilt]" PREFIX: "[Tilgangsrettighetsskilt]"
SIGN_PREFIX: "&4[PermSign]" SIGN_PREFIX: "&4[PermSign]"
@ -46,10 +47,10 @@ nb-no:
UNIT_DAY: "dag" UNIT_DAY: "dag"
UNIT_DAYS: "dager" UNIT_DAYS: "dager"
SIGN_NAME_FORMAT: "{name}" SIGN_NAME_FORMAT: "{name}"
SIGN_DURATION_FORMAT: "{duration} {unit}" SIGN_DURATION_FORMAT: "{duration} {unit}"
SIGN_PERMANENT: "Permanent" SIGN_PERMANENT: "Permanent"
SIGN_COST_FREE: "Gratis" SIGN_COST_FREE: "Gratis"
SIGN_COST_FORMAT: "{cost}#66ff00{currency}" SIGN_COST_FORMAT: "{cost}{currency}"
COST_INVALID_NUMBER: "&7Den gitte kostnaden er ikke et gyldig nummer" COST_INVALID_NUMBER: "&7Den gitte kostnaden er ikke et gyldig nummer"
DURATION_INVALID_NUMBER: "&7Den gitte varigheten er ikke et gyldig nummer" DURATION_INVALID_NUMBER: "&7Den gitte varigheten er ikke et gyldig nummer"
COMMAND_PLAYER_ONLY: "&7Denne kommandoen kan bare brukes av spillere" COMMAND_PLAYER_ONLY: "&7Denne kommandoen kan bare brukes av spillere"
@ -68,3 +69,4 @@ nb-no:
CREATION_REQUEST_CANCELLED: "&7Din siste tilgangsrettighetsskiltsforespørsel har blitt kansellert" CREATION_REQUEST_CANCELLED: "&7Din siste tilgangsrettighetsskiltsforespørsel har blitt kansellert"
RELOAD_SUCCESSFUL: "&7Plugin-modulen har blitt lastet inn på nytt" RELOAD_SUCCESSFUL: "&7Plugin-modulen har blitt lastet inn på nytt"
SIGN_INFO: "&f---- &4Tilgangsrettighetsskilt&f ----\n&f| &bNavn: &7{Name}\n&f| &bTilgangsrettighet(er): &7{Permissions}\n&f| &bKostnad: &7{Cost}\n&f| &bVarighet: &7{Duration}\n&f| &7Høyreklikk skiltet mens du sniker for å kjøpe tilgangsrettighet(en/ene)\n&f-------------------------" SIGN_INFO: "&f---- &4Tilgangsrettighetsskilt&f ----\n&f| &bNavn: &7{Name}\n&f| &bTilgangsrettighet(er): &7{Permissions}\n&f| &bKostnad: &7{Cost}\n&f| &bVarighet: &7{Duration}\n&f| &7Høyreklikk skiltet mens du sniker for å kjøpe tilgangsrettighet(en/ene)\n&f-------------------------"
SIGN_INFO_PERMISSION_FORMAT: "\n&f| &7- {permission}"