Archiviert
1
0

Fix Message again for the final time!

Dieser Commit ist enthalten in:
yoyosource 2022-03-15 17:16:08 +01:00
Ursprung d32488a4b7
Commit 01b500e2e7

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 Map<Locale, 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,35 @@ public class Message {
return sender instanceof ProxiedPlayer ? ((ProxiedPlayer)sender).getLocale() : Locale.getDefault(); 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){ 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 = createResourceBundle("de.steamwar.messages.BungeeCore", locale);
String pattern = ""; String pattern = "";
if(prefixed) if(prefixed)
pattern = resourceBundle.getObject("PREFIX") + " "; pattern = resourceBundle.getObject("PREFIX") + " ";