Created
May 15, 2017 09:08
-
-
Save DarkSeraphim/1cc917e0540cdcf3904e51df702fbf18 to your computer and use it in GitHub Desktop.
Fetches the Minecraft locale files for a set of locale keys (i.e. en_gb)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static final String LANG_URL = "http://resources.download.minecraft.net/%s/%s"; | |
| private static final String ASSET_INDEX_URL = "https://s3.amazonaws.com/Minecraft.Download/indexes/%s.json"; | |
| private static final String LOCALE_KEY = "minecraft/lang/%s.lang"; | |
| public static void loadLocales(String... languages) { | |
| if (languages.length == 0) { | |
| return; | |
| } | |
| String version = getVersion(); | |
| String json = getAssetIndex(version); | |
| sonParser parser = new JsonParser(); | |
| JsonObject root = parser.parse(json).getAsJsonObject(); | |
| if (!root.has("objects")) { | |
| throw new RuntimeException("Invalid assets index"); | |
| } | |
| Map<String, Language> languages = new HashSet<>(); | |
| JsonObject objects = root.getAsJsonObject("objects"); | |
| Arrays.stream(languages) | |
| .filter(key -> !objects.has(String.format(LOCALE_KEY, key))) | |
| .forEach(key -> { | |
| String hash = objects.getAsJsonObject(String.format(LOCALE_KEY, key)); | |
| String locale = getLocale(hash); | |
| Language lang = locale; // TODO: parse | |
| languages.put(key, lang); | |
| }) | |
| return languages; | |
| } | |
| private static String request(String link) { | |
| URL url = new URL(link); | |
| HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
| if (conn.getResponseCode() / 100 == 2) { | |
| InputStream in = conn.getInputStream() | |
| final int maxlength = conn.getHeaderFieldInt("Content-Length", 0); | |
| int read = 0; | |
| byte[] buffer = new byte[1024]; | |
| int len; | |
| StringBuilder response = new StringBuilder(maxlength); | |
| while (read < maxlength && (len = in.read(buffer, 0, Math.min(buffer.length, maxlength - read)))) { | |
| read += len; | |
| response.append(new String(buffer, 0, len, StandardCharsets.UTF_8)); | |
| } | |
| return response.toString(); | |
| } | |
| return null; | |
| } | |
| private static String getLocale(String hash) { | |
| String h2 = hash.substring(0, 2); | |
| String url = String.format(LANG_URL, h2, hash); | |
| return request(url); | |
| } | |
| private static String getAssetIndex(String version) throws IOException { | |
| while (true) { | |
| String response = request(String.format(ASSET_INDEX_URL, version)); | |
| if (response != null) { | |
| return response; | |
| } | |
| int lastDot = version.lastIndexOf('.'); | |
| if (lastDot < 0) { | |
| throw new RuntimeException("Failed to fetch asset index"); | |
| } | |
| version = version.substring(0, lastDot); | |
| } | |
| } | |
| private static String getVersion() { | |
| for (Field field : Bukkit.getServer().getClass().getDeclaredFields()) { | |
| if ("MinecraftServer".equals(field.getType().getSimpleName())) { | |
| field.setAccessible(true); | |
| Object mc = field.get(Bukkit.getServer()); | |
| Method getVersion = mc.getMethod("getVersion"); | |
| return (String) getVersion.invoke(mc); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment