From 187132bc1ddf9aa191d5847e9b9a9ff31d0b151a Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 4 Feb 2023 19:00:18 -0800 Subject: [PATCH] Refactor to reduce complixty. Added comments. --- .../bentobox/bentobox/api/user/User.java | 87 +++++++++++-------- 1 file changed, 49 insertions(+), 38 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/user/User.java b/src/main/java/world/bentobox/bentobox/api/user/User.java index 37355443d..68dd641e3 100644 --- a/src/main/java/world/bentobox/bentobox/api/user/User.java +++ b/src/main/java/world/bentobox/bentobox/api/user/User.java @@ -430,59 +430,70 @@ public class User implements MetaDataAble { } private String translate(String addonPrefix, String reference, String[] variables) { + // Try to get the translation for this specific addon String translation = plugin.getLocalesManager().get(this, addonPrefix + reference); if (translation == null) { + // No luck, try to get the generic translation translation = plugin.getLocalesManager().get(this, reference); if (translation == null) { - - // Then replace variables - if (variables.length > 1) { - for (int i = 0; i < variables.length; i += 2) { - reference = reference.replace(variables[i], variables[i + 1]); - } - } - - // Then replace Placeholders, this will only work if this is a player - if (player != null) { - reference = plugin.getPlaceholdersManager().replacePlaceholders(player, reference); - } - - // If no translation has been found, return the reference for debug purposes. - return reference; + // Nothing found. Replace vars (probably will do nothing) and return + return replaceVars(reference, variables); } } // If this is a prefix, just gather and return the translation if (!reference.startsWith("prefixes.")) { // Replace the prefixes - for (String prefix : plugin.getLocalesManager().getAvailablePrefixes(this)) { - String prefixTranslation = getTranslation("prefixes." + prefix); - // Replace the [gamemode] text variable - prefixTranslation = prefixTranslation.replace("[gamemode]", addon != null ? addon.getDescription().getName() : "[gamemode]"); - // Replace the [friendly_name] text variable - prefixTranslation = prefixTranslation.replace("[friendly_name]", isPlayer() ? plugin.getIWM().getFriendlyName(getWorld()) : "[friendly_name]"); - - // Replace the prefix in the actual message - translation = translation.replace("[prefix_" + prefix + "]", prefixTranslation); - } - - // Then replace variables - if (variables.length > 1) { - for (int i = 0; i < variables.length; i += 2) { - translation = translation.replace(variables[i], variables[i + 1]); - } - } - - // Then replace Placeholders, this will only work if this is a player - if (player != null) { - translation = plugin.getPlaceholdersManager().replacePlaceholders(player, translation); - } - + return replacePrefixes(translation, variables); } return translation; } + private String replacePrefixes(String translation, String[] variables) { + for (String prefix : plugin.getLocalesManager().getAvailablePrefixes(this)) { + String prefixTranslation = getTranslation("prefixes." + prefix); + // Replace the [gamemode] text variable + prefixTranslation = prefixTranslation.replace("[gamemode]", addon != null ? addon.getDescription().getName() : "[gamemode]"); + // Replace the [friendly_name] text variable + prefixTranslation = prefixTranslation.replace("[friendly_name]", isPlayer() ? plugin.getIWM().getFriendlyName(getWorld()) : "[friendly_name]"); + + // Replace the prefix in the actual message + translation = translation.replace("[prefix_" + prefix + "]", prefixTranslation); + } + + // Then replace variables + if (variables.length > 1) { + for (int i = 0; i < variables.length; i += 2) { + translation = translation.replace(variables[i], variables[i + 1]); + } + } + + // Then replace Placeholders, this will only work if this is a player + if (player != null) { + translation = plugin.getPlaceholdersManager().replacePlaceholders(player, translation); + } + return translation; + } + + private String replaceVars(String reference, String[] variables) { + + // Then replace variables + if (variables.length > 1) { + for (int i = 0; i < variables.length; i += 2) { + reference = reference.replace(variables[i], variables[i + 1]); + } + } + + // Then replace Placeholders, this will only work if this is a player + if (player != null) { + reference = plugin.getPlaceholdersManager().replacePlaceholders(player, reference); + } + + // If no translation has been found, return the reference for debug purposes. + return reference; + } + /** * Gets a translation of this reference for this user. * @param reference - reference found in a locale file