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

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 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)));
@ -68,10 +58,29 @@ public class Message {
return sender instanceof ProxiedPlayer ? ((ProxiedPlayer)sender).getLocale() : Locale.getDefault();
}
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, parent);
} catch (IOException e) {
return parent;
}
});
}
private static ResourceBundle getResourceBundle(Locale locale) {
return 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 = ResourceBundle.getBundle("de.steamwar.messages.BungeeCore", locale, CONTROL);
ResourceBundle resourceBundle = getResourceBundle(locale);
String pattern = "";
if(prefixed)
pattern = resourceBundle.getObject("PREFIX") + " ";
@ -179,4 +188,11 @@ public class Message {
public Object[] getParams() {
return params;
}
private static class SteamwarResourceBundle extends PropertyResourceBundle {
public SteamwarResourceBundle(InputStream stream, ResourceBundle parent) throws IOException {
super(stream);
setParent(parent);
}
}
}