SteamWar/BungeeCore
Archiviert
13
2

Merge pull request 'Fix Message again for the final time!' (#313) from ResourceBundles2.0 into master
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Reviewed-on: #313
Dieser Commit ist enthalten in:
Lixfel 2022-03-16 16:38:45 +01:00
Commit 6489ae51cf

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.chat.TextComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.Date; import java.util.*;
import java.util.Locale;
import java.util.ResourceBundle;
public class Message { public class Message {
private static final ResourceBundle.Control CONTROL = new ResourceBundle.Control() { private static final Map<String, ResourceBundle> bundles = new HashMap<>();
@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;
}
};
public static TextComponent parseToComponent(String message, boolean prefixed, CommandSender sender, Object... params){ public static TextComponent parseToComponent(String message, boolean prefixed, CommandSender sender, Object... params){
return new TextComponent(TextComponent.fromLegacyText(parse(message, prefixed, locale(sender), 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(); 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){ private static String parse(String message, boolean prefixed, Locale locale, Object... params){
if(locale == null) if(locale == null)
locale = Locale.getDefault(); locale = Locale.getDefault();
ResourceBundle resourceBundle = ResourceBundle.getBundle("de.steamwar.messages.BungeeCore", locale, CONTROL); ResourceBundle resourceBundle = getResourceBundle(locale);
String pattern = ""; String pattern = "";
if(prefixed) if(prefixed)
pattern = resourceBundle.getObject("PREFIX") + " "; pattern = resourceBundle.getObject("PREFIX") + " ";
@ -179,4 +188,11 @@ public class Message {
public Object[] getParams() { public Object[] getParams() {
return params; return params;
} }
private static class SteamwarResourceBundle extends PropertyResourceBundle {
public SteamwarResourceBundle(InputStream stream, ResourceBundle parent) throws IOException {
super(stream);
setParent(parent);
}
}
} }