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 {
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) {
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();
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 Optional.empty();
return null;
}
}
private static ResourceBundle getResourceBundle(String baseName, Locale locale) {
return bundles.computeIfAbsent(locale, l -> {
if (bundles.containsKey(locale.toString())) {
return bundles.get(locale.toString());
}
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()));
});
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);
}
}
}