SteamWar/BungeeCore
Archiviert
13
2

Fix Message again for the final time! #313

Zusammengeführt
Lixfel hat 11 Commits von ResourceBundles2.0 nach master 2022-03-16 16:38:46 +01:00 zusammengeführt
Nur Änderungen aus Commit 4df39ebe75 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -36,7 +36,7 @@ import java.util.*;
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){
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<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);
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);
}
}
}