From c0301a80140cbf8f49104455a9468625da01f8b2 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Wed, 16 Mar 2022 16:24:19 +0100 Subject: [PATCH] Code simplification Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/Message.java | 52 +++++++------------------ 1 file changed, 15 insertions(+), 37 deletions(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index 47d4b82..cca89f7 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -33,12 +33,10 @@ import java.io.InputStream; import java.text.DateFormat; import java.text.MessageFormat; import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicReference; public class Message { - private static Map bundles = new ConcurrentHashMap<>(); + private static final Map bundles = new HashMap<>(); public static TextComponent parseToComponent(String message, boolean prefixed, CommandSender sender, Object... params){ return new TextComponent(TextComponent.fromLegacyText(parse(message, prefixed, locale(sender), params))); @@ -60,45 +58,29 @@ public class Message { return sender instanceof ProxiedPlayer ? ((ProxiedPlayer)sender).getLocale() : Locale.getDefault(); } - private static SteamwarResourceBundle getOrCreateSingle(String name, String cacheKey) { - return bundles.computeIfAbsent(cacheKey, s -> { - InputStream inputStream = Message.class.getResourceAsStream(name); - if(inputStream == null) return null; + private static final String BASE_PATH = "/" + "de.steamwar.messages.BungeeCore".replace('.', '/'); + + private static ResourceBundle getResourceBundle(String locale, ResourceBundle parent) { + return bundles.computeIfAbsent(locale, locale1 -> { + InputStream inputStream = Message.class.getResourceAsStream(BASE_PATH + ("".equals(locale) ? "" : "_" + locale) + ".properties"); + if(inputStream == null) + return parent; try { - return new SteamwarResourceBundle(inputStream); + return new SteamwarResourceBundle(inputStream, parent); } catch (IOException e) { - return null; + return parent; } }); } - private static ResourceBundle getResourceBundle(String baseName, Locale locale) { - if (bundles.containsKey(locale.toString())) { - return bundles.get(locale.toString()); - } - String path = baseName.replace('.', '/'); - - AtomicReference current = new AtomicReference<>(); - apply(current, getOrCreateSingle("/" + path + "_" + locale.toString() + ".properties", locale.toString()), locale.toString()); - apply(current, getOrCreateSingle("/" + path + "_" + locale.getLanguage() + ".properties", locale.getLanguage()), locale.toString()); - apply(current, getOrCreateSingle("/" + path + ".properties", ""), locale.toString()); - - if (current.get() == null) throw new SecurityException("Could not find resource bundle for " + path); - return bundles.get(locale.toString()); - } - - private static void apply(AtomicReference current, SteamwarResourceBundle now, String bundleKey) { - if (current.get() != null) current.get().setParent(now); - if (now != null) { - current.set(now); - bundles.putIfAbsent(bundleKey, now); - } + private static ResourceBundle getResourceBundle(Locale locale) { + return bundles.computeIfAbsent(locale.toString(), locale1 -> getResourceBundle(locale.toString(), getResourceBundle(locale.getLanguage(), getResourceBundle( "", (ResourceBundle) null)))); } private static String parse(String message, boolean prefixed, Locale locale, Object... params){ if(locale == null) locale = Locale.getDefault(); - ResourceBundle resourceBundle = getResourceBundle("de.steamwar.messages.BungeeCore", locale); + ResourceBundle resourceBundle = getResourceBundle(locale); String pattern = ""; if(prefixed) pattern = resourceBundle.getObject("PREFIX") + " "; @@ -208,13 +190,9 @@ public class Message { } private static class SteamwarResourceBundle extends PropertyResourceBundle { - public SteamwarResourceBundle(InputStream stream) throws IOException { + public SteamwarResourceBundle(InputStream stream, ResourceBundle parent) throws IOException { super(stream); - } - - @Override - protected void setParent(ResourceBundle parent) { - super.setParent(parent); + setParent(parent); } } } \ No newline at end of file