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

Datei anzeigen

@ -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<Locale, 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)));
@ -68,10 +58,35 @@ 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();
}
} 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") + " ";