From 01b500e2e7a5393799703ecb265dc5900e956846 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 17:16:08 +0100 Subject: [PATCH] Fix Message again for the final time! --- src/de/steamwar/bungeecore/Message.java | 45 ++++++++++++++++--------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index 992cedff..3975eeb4 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -28,25 +28,15 @@ import net.md_5.bungee.api.chat.HoverEvent; import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.connection.ProxiedPlayer; +import java.io.IOException; +import java.io.InputStream; import java.text.DateFormat; import java.text.MessageFormat; -import java.util.Date; -import java.util.Locale; -import java.util.ResourceBundle; +import java.util.*; public class Message { - private static final ResourceBundle.Control CONTROL = new ResourceBundle.Control() { - @Override - public long getTimeToLive(String arg0, Locale arg1) { - return 60000; //Cache only 1 minute - } - - @Override - public boolean needsReload(String baseName, Locale locale, String format, ClassLoader loader, ResourceBundle bundle, long loadTime) { - return true; - } - }; + 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))); @@ -68,10 +58,35 @@ public class Message { return sender instanceof ProxiedPlayer ? ((ProxiedPlayer)sender).getLocale() : Locale.getDefault(); } + private static Optional createSingle(String name) { + InputStream inputStream = Message.class.getResourceAsStream(name); + if(inputStream == null) return Optional.empty(); + try { + PropertyResourceBundle propertyResourceBundle = new PropertyResourceBundle(inputStream); + if (propertyResourceBundle.getKeys().hasMoreElements()) { + return Optional.of(propertyResourceBundle); + } else { + return Optional.empty(); + } + } catch (IOException e) { + return Optional.empty(); + } + } + + private static ResourceBundle createResourceBundle(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())); + }); + } + private static String parse(String message, boolean prefixed, Locale locale, Object... params){ if(locale == null) locale = Locale.getDefault(); - ResourceBundle resourceBundle = ResourceBundle.getBundle("de.steamwar.messages.BungeeCore", locale, CONTROL); + ResourceBundle resourceBundle = createResourceBundle("de.steamwar.messages.BungeeCore", locale); String pattern = ""; if(prefixed) pattern = resourceBundle.getObject("PREFIX") + " ";