From 4df39ebe7565defd73b542c443a6006ba478fba2 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 17:53:50 +0100 Subject: [PATCH] Fix Message again for the final time! --- src/de/steamwar/bungeecore/Message.java | 68 ++++++++++++++++++------- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index 98dceccc..06fa54fe 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -36,7 +36,7 @@ import java.util.*; public class Message { - private static Map bundles = new HashMap<>(); + private static 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))); @@ -58,29 +58,52 @@ public class Message { return sender instanceof ProxiedPlayer ? ((ProxiedPlayer)sender).getLocale() : Locale.getDefault(); } - private static Optional createSingle(String name) { + private static SteamwarResourceBundle getOrCreateSingle(String name, String cacheKey) { + if (bundles.containsKey(cacheKey)) { + return bundles.get(cacheKey); + } InputStream inputStream = Message.class.getResourceAsStream(name); - if(inputStream == null) return Optional.empty(); + if(inputStream == null) return null; try { - PropertyResourceBundle propertyResourceBundle = new PropertyResourceBundle(inputStream); - if (propertyResourceBundle.getKeys().hasMoreElements()) { - return Optional.of(propertyResourceBundle); - } else { - return Optional.empty(); - } + SteamwarResourceBundle steamwarResourceBundle = new SteamwarResourceBundle(inputStream); + bundles.put(cacheKey, steamwarResourceBundle); + return steamwarResourceBundle; } catch (IOException e) { - return Optional.empty(); + return null; } } private static ResourceBundle getResourceBundle(String baseName, Locale locale) { - return bundles.computeIfAbsent(locale, l -> { - String path = baseName.replace('.', '/'); - return createSingle("/" + path + "_" + locale.toString() + ".properties") - .or(() -> createSingle("/" + path + "_" + locale.getLanguage() + ".properties")) - .or(() -> createSingle("/" + path + ".properties")) - .orElseThrow(() -> new SecurityException("Could not find resource bundle for " + path + " with locale " + locale + " or " + locale.getLanguage())); - }); + if (bundles.containsKey(locale.toString())) { + return bundles.get(locale.toString()); + } + String path = baseName.replace('.', '/'); + + SteamwarResourceBundle current = null; + SteamwarResourceBundle temp = getOrCreateSingle("/" + path + "_" + locale + ".properties", locale.toString()); + if (temp != null) { + current = temp; + bundles.put(locale.toString(), temp); + } + + temp = getOrCreateSingle("/" + path + "_" + locale.getLanguage() + ".properties", locale.getLanguage()); + if (current != null) current.setParent(temp); + if (temp != null) { + current = temp; + bundles.put(locale.toString(), temp); + } + + temp = getOrCreateSingle("/" + path + ".properties", ""); + if (current != null) current.setParent(temp); + if (temp != null) { + current = temp; + bundles.put(locale.toString(), temp); + } + + if (current == null) { + throw new SecurityException("Could not find resource bundle for " + path + " with locale " + locale + " or " + locale.getLanguage()); + } + return bundles.get(locale.toString()); } private static String parse(String message, boolean prefixed, Locale locale, Object... params){ @@ -194,4 +217,15 @@ public class Message { public Object[] getParams() { return params; } + + private static class SteamwarResourceBundle extends PropertyResourceBundle { + public SteamwarResourceBundle(InputStream stream) throws IOException { + super(stream); + } + + @Override + protected void setParent(ResourceBundle parent) { + super.setParent(parent); + } + } } \ No newline at end of file