mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-18 11:29:15 +00:00
Fix dedicated server not loading default language files. Fixes #10555
This commit is contained in:
1 file changed
+46
-28
@@ -8,17 +8,18 @@ package net.minecraftforge.server;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.minecraft.server.packs.resources.Resource;
|
||||
import net.minecraft.server.packs.resources.ResourceManager;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.util.GsonHelper;
|
||||
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.packs.PackResources;
|
||||
import net.minecraft.server.packs.PackType;
|
||||
import net.minecraft.server.packs.resources.IoSupplier;
|
||||
import net.minecraft.util.GsonHelper;
|
||||
import net.minecraftforge.common.ForgeI18n;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -40,43 +41,60 @@ public class LanguageHook {
|
||||
CAPTURED_TABLES.forEach(t -> t.putAll(modTable));
|
||||
}
|
||||
|
||||
// The below is based on client side net.minecraft.client.resources.Locale code
|
||||
private static void loadLocaleData(final List<Resource> allResources) {
|
||||
for (var res : allResources) {
|
||||
try (var stream = res.open()) {
|
||||
LanguageHook.loadLocaleData(stream);
|
||||
} catch(IOException e) {
|
||||
LOGGER.error("Failed to read locale data from {}", res.toString(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadLocaleData(final InputStream inputstream) {
|
||||
try {
|
||||
JsonElement jsonelement = GSON.fromJson(new InputStreamReader(inputstream, StandardCharsets.UTF_8), JsonElement.class);
|
||||
JsonObject jsonobject = GsonHelper.convertToJsonObject(jsonelement, "strings");
|
||||
|
||||
jsonobject.entrySet().forEach(entry -> {
|
||||
for (var entry : jsonobject.entrySet()) {
|
||||
String s = PATTERN.matcher(GsonHelper.convertToString(entry.getValue(), entry.getKey())).replaceAll("%$1s");
|
||||
modTable.put(entry.getKey(), s);
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
IOUtils.closeQuietly(inputstream);
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadLanguage(String langName, MinecraftServer server) {
|
||||
String langFile = String.format(Locale.ROOT, "lang/%s.json", langName);
|
||||
ResourceManager resourceManager = server.getServerResources().resourceManager();
|
||||
resourceManager.getNamespaces().forEach(namespace -> {
|
||||
try {
|
||||
ResourceLocation langResource = ResourceLocation.fromNamespaceAndPath(namespace, langFile);
|
||||
loadLocaleData(resourceManager.getResourceStack(langResource));
|
||||
} catch (Exception exception) {
|
||||
LOGGER.warn("Skipped language file: {}:{}", namespace, langFile, exception);
|
||||
}
|
||||
});
|
||||
var langFile = String.format(Locale.ROOT, "lang/%s.json", langName);
|
||||
var resourceManager = server.getServerResources().resourceManager();
|
||||
var byNamespace = new HashMap<String, List<PackResources>>();
|
||||
|
||||
var packs = new ArrayList<>(resourceManager.listPacks().toList());
|
||||
var seen = new HashSet<>(packs); // Jut in case of recursion
|
||||
|
||||
while (!packs.isEmpty()) {
|
||||
var pack = packs.removeFirst();
|
||||
if (pack.getChildren() != null) {
|
||||
for (var child : pack.getChildren()) {
|
||||
if (seen.add(child))
|
||||
packs.add(child);
|
||||
}
|
||||
}
|
||||
|
||||
for (var namespace : pack.getNamespaces(PackType.CLIENT_RESOURCES)) {
|
||||
byNamespace.computeIfAbsent(namespace, k -> new ArrayList<>()).add(pack);
|
||||
}
|
||||
}
|
||||
|
||||
for (var entry : byNamespace.entrySet()) {
|
||||
var languages = new ArrayList<IoSupplier<InputStream>>();
|
||||
|
||||
var path = ResourceLocation.fromNamespaceAndPath(entry.getKey(), langFile);
|
||||
for (var pack : entry.getValue()) {
|
||||
var io = pack.getResource(PackType.CLIENT_RESOURCES, path);
|
||||
if (io != null)
|
||||
languages.add(io);
|
||||
}
|
||||
|
||||
while (!languages.isEmpty()) {
|
||||
try (var stream = languages.removeLast().get()) {
|
||||
loadLocaleData(stream);
|
||||
} catch (Exception exception) {
|
||||
LOGGER.warn("Skipped language file: {}:{}", entry.getKey(), langFile, exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void loadForgeAndMCLangs() {
|
||||
|
||||
Reference in new issue
Block a user