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 c0301a8014 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -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<String, SteamwarResourceBundle> bundles = new ConcurrentHashMap<>();
private static final Map<String, ResourceBundle> 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<SteamwarResourceBundle> 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<SteamwarResourceBundle> 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);
}
}
}