Archiviert
1
0

Fix Message again for the final time!

Dieser Commit ist enthalten in:
yoyosource 2022-03-15 17:53:50 +01:00
Ursprung 9accdb599e
Commit 4df39ebe75

Datei anzeigen

@ -36,7 +36,7 @@ import java.util.*;
public class Message { public class Message {
private static Map<Locale, ResourceBundle> bundles = new HashMap<>(); private static Map<String, SteamwarResourceBundle> bundles = new HashMap<>();
public static TextComponent parseToComponent(String message, boolean prefixed, CommandSender sender, Object... params){ public static TextComponent parseToComponent(String message, boolean prefixed, CommandSender sender, Object... params){
return new TextComponent(TextComponent.fromLegacyText(parse(message, prefixed, locale(sender), 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(); return sender instanceof ProxiedPlayer ? ((ProxiedPlayer)sender).getLocale() : Locale.getDefault();
} }
private static Optional<ResourceBundle> 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); InputStream inputStream = Message.class.getResourceAsStream(name);
if(inputStream == null) return Optional.empty(); if(inputStream == null) return null;
try { try {
PropertyResourceBundle propertyResourceBundle = new PropertyResourceBundle(inputStream); SteamwarResourceBundle steamwarResourceBundle = new SteamwarResourceBundle(inputStream);
if (propertyResourceBundle.getKeys().hasMoreElements()) { bundles.put(cacheKey, steamwarResourceBundle);
return Optional.of(propertyResourceBundle); return steamwarResourceBundle;
} else {
return Optional.empty();
}
} catch (IOException e) { } catch (IOException e) {
return Optional.empty(); return null;
} }
} }
private static ResourceBundle getResourceBundle(String baseName, Locale locale) { private static ResourceBundle getResourceBundle(String baseName, Locale locale) {
return bundles.computeIfAbsent(locale, l -> { if (bundles.containsKey(locale.toString())) {
String path = baseName.replace('.', '/'); return bundles.get(locale.toString());
return createSingle("/" + path + "_" + locale.toString() + ".properties") }
.or(() -> createSingle("/" + path + "_" + locale.getLanguage() + ".properties")) String path = baseName.replace('.', '/');
.or(() -> createSingle("/" + path + ".properties"))
.orElseThrow(() -> new SecurityException("Could not find resource bundle for " + path + " with locale " + locale + " or " + locale.getLanguage())); 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){ private static String parse(String message, boolean prefixed, Locale locale, Object... params){
@ -194,4 +217,15 @@ public class Message {
public Object[] getParams() { public Object[] getParams() {
return params; 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);
}
}
} }