From 01b500e2e7a5393799703ecb265dc5900e956846 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 17:16:08 +0100 Subject: [PATCH 1/9] 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 992cedf..3975eeb 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") + " "; From 9accdb599e15233f3d33be91d5e892184372ba7a Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 17:18:16 +0100 Subject: [PATCH 2/9] Fix Message again for the final time! --- src/de/steamwar/bungeecore/Message.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index 3975eeb..98dcecc 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -73,7 +73,7 @@ public class Message { } } - private static ResourceBundle createResourceBundle(String baseName, Locale locale) { + private static ResourceBundle getResourceBundle(String baseName, Locale locale) { return bundles.computeIfAbsent(locale, l -> { String path = baseName.replace('.', '/'); return createSingle("/" + path + "_" + locale.toString() + ".properties") @@ -86,7 +86,7 @@ public class Message { private static String parse(String message, boolean prefixed, Locale locale, Object... params){ if(locale == null) locale = Locale.getDefault(); - ResourceBundle resourceBundle = createResourceBundle("de.steamwar.messages.BungeeCore", locale); + ResourceBundle resourceBundle = getResourceBundle("de.steamwar.messages.BungeeCore", locale); String pattern = ""; if(prefixed) pattern = resourceBundle.getObject("PREFIX") + " "; From 4df39ebe7565defd73b542c443a6006ba478fba2 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 17:53:50 +0100 Subject: [PATCH 3/9] 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 98dcecc..06fa54f 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 From 8ee960e0028d171f1e7c215cc5491f8eaf9adfa8 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 17:54:10 +0100 Subject: [PATCH 4/9] Fix message --- src/de/steamwar/bungeecore/Message.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index 06fa54f..4356bcf 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -101,7 +101,7 @@ public class Message { } if (current == null) { - throw new SecurityException("Could not find resource bundle for " + path + " with locale " + locale + " or " + locale.getLanguage()); + throw new SecurityException("Could not find resource bundle for " + path); } return bundles.get(locale.toString()); } From a78da0e2cf558b113a159cbf0412ec720b12867b Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 18:00:28 +0100 Subject: [PATCH 5/9] Update Message stuff --- src/de/steamwar/bungeecore/Message.java | 58 ++++++++++--------------- 1 file changed, 23 insertions(+), 35 deletions(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index 4356bcf..feca756 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -33,6 +33,7 @@ import java.io.InputStream; import java.text.DateFormat; import java.text.MessageFormat; import java.util.*; +import java.util.concurrent.atomic.AtomicReference; public class Message { @@ -59,18 +60,15 @@ public class Message { } 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 null; - try { - SteamwarResourceBundle steamwarResourceBundle = new SteamwarResourceBundle(inputStream); - bundles.put(cacheKey, steamwarResourceBundle); - return steamwarResourceBundle; - } catch (IOException e) { - return null; - } + return bundles.computeIfAbsent(cacheKey, s -> { + InputStream inputStream = Message.class.getResourceAsStream(name); + if(inputStream == null) return null; + try { + return new SteamwarResourceBundle(inputStream); + } catch (IOException e) { + return null; + } + }); } private static ResourceBundle getResourceBundle(String baseName, Locale locale) { @@ -79,33 +77,23 @@ public class Message { } 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); - } + 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()); - 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); - } + 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.put(bundleKey, now); + } + } + private static String parse(String message, boolean prefixed, Locale locale, Object... params){ if(locale == null) locale = Locale.getDefault(); From 1fd34f8ccd2dbb75611eb17f1057552125c808d3 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 18:12:55 +0100 Subject: [PATCH 6/9] Update Message stuff --- src/de/steamwar/bungeecore/Message.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index feca756..2ab6963 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -90,7 +90,7 @@ public class Message { if (current.get() != null) current.get().setParent(now); if (now != null) { current.set(now); - bundles.put(bundleKey, now); + bundles.putIfAbsent(bundleKey, now); } } From 1f50bf15aa8323daad95bc22d07c920283be4f59 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 19:05:08 +0100 Subject: [PATCH 7/9] Fix Message --- src/de/steamwar/bungeecore/Message.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index 2ab6963..47d4b82 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -33,11 +33,12 @@ 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 HashMap<>(); + private static Map bundles = new ConcurrentHashMap<>(); public static TextComponent parseToComponent(String message, boolean prefixed, CommandSender sender, Object... params){ return new TextComponent(TextComponent.fromLegacyText(parse(message, prefixed, locale(sender), params))); From c0301a80140cbf8f49104455a9468625da01f8b2 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Wed, 16 Mar 2022 16:24:19 +0100 Subject: [PATCH 8/9] 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 From c08bfe75b907e81014d4749756bbc3ce3d5cd6fd Mon Sep 17 00:00:00 2001 From: Lixfel Date: Wed, 16 Mar 2022 16:30:38 +0100 Subject: [PATCH 9/9] Fix ConcurrentModificationException Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/Message.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index cca89f7..acaa189 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -74,7 +74,7 @@ public class Message { } private static ResourceBundle getResourceBundle(Locale locale) { - return bundles.computeIfAbsent(locale.toString(), locale1 -> getResourceBundle(locale.toString(), getResourceBundle(locale.getLanguage(), getResourceBundle( "", (ResourceBundle) null)))); + return getResourceBundle(locale.toString(), getResourceBundle(locale.getLanguage(), getResourceBundle( "", (ResourceBundle) null))); } private static String parse(String message, boolean prefixed, Locale locale, Object... params){