Use Protocollib to get locale packet for lower server version (< 1.15.2)

This commit is contained in:
Dreeam
2024-06-02 18:59:27 +08:00
parent e89b6182a7
commit 32ed70f099
5 changed files with 84 additions and 18 deletions
@@ -115,5 +115,5 @@ public final class Utils {
private final static int mcFirstVersion = Integer.parseInt(serverVersion[0]);
public final static int majorVersion = Integer.parseInt(serverVersion[1]);
public final static int minorVersion = Integer.parseInt(serverVersion[2]);
public final static int minorVersion = serverVersion.length == 3 ? Integer.parseInt(serverVersion[2]) : 0;
}
@@ -5,15 +5,16 @@ import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public class I18NUtils {
private static Method spigotGetLocale;
private static Map<UUID, String> PLAYER_LOCALE = new ConcurrentHashMap<>();
private static final Map<String, String> LOCALE_MAP = new HashMap<>();
private static final List<String> LOCALE_LIST = Arrays.asList(
"en", "bg", "de", "eo", "es", "et", "eu", "fi", "fr", "gl", "hu", "id", "it", "ja", "ko", "lt", "nl", "pl",
@@ -21,13 +22,6 @@ public class I18NUtils {
);
static {
try {
spigotGetLocale = Player.Spigot.class.getMethod("getLocale");
spigotGetLocale.setAccessible(true);
} catch (NoSuchMethodException e) {
spigotGetLocale = null;
}
LOCALE_MAP.put("pt_br", "br");
LOCALE_MAP.put("cs_cz", "cz");
LOCALE_MAP.put("nds_de", "de");
@@ -40,7 +34,7 @@ public class I18NUtils {
LOCALE_MAP.put("zh_cn", "zhcn");
LOCALE_MAP.put("zh_hk", "zhhk");
LOCALE_MAP.put("zh_tw", "zhtw");
// LOCALE_MAP.put("zhmc", "zhmc");
//LOCALE_MAP.put("zhmc", "zhmc");
}
/**
@@ -49,18 +43,30 @@ public class I18NUtils {
* @param player The player
*/
public static String getLocale(Player player) {
if (Utils.majorVersion >= 12) {
if (Utils.majorVersion > 15) {
return player.getLocale().toLowerCase();
} else {
try {
return ((String) spigotGetLocale.invoke(player.spigot())).toLowerCase();
} catch (Exception e) {
e.printStackTrace();
return null;
long startTime = System.currentTimeMillis();
for (;;) {
if (PLAYER_LOCALE.containsKey(player.getUniqueId())) {
return PLAYER_LOCALE.get(player.getUniqueId());
}
if (System.currentTimeMillis() - startTime > 3000) {
return null;
}
}
}
}
public static void addLocale(UUID uuid, String locale) {
if (PLAYER_LOCALE == null) {
PLAYER_LOCALE = new ConcurrentHashMap<>();
}
PLAYER_LOCALE.put(uuid, locale);
}
/**
* Returns the AuthMe messages file language code, by given locale and settings.
* Dreeam - Hard mapping, based on mc1.20.6, 5/29/2024