Files
MinecraftForge/src/main/java/net/minecraftforge/server/LanguageHook.java
T
4ed9117799 Forge 1.19
* Bump pack.mcmeta formats
* 1.19 biome modifiers
* Mark ClientPlayerNetworkEvent.LoggedOutEvent's getters as nullable
* Add docs and package-info to client extension interfaces package
* Move RenderBlockOverlayEvent hooks to ForgeHooksClient
* Add package-infos to client events package
* Rename SoundLoadEvent to SoundEngineLoadEvent

This reduces confusion from consumers which may think the
name SoundLoadEvent refers to an individual sound being loaded rather
than the sound engine.
* Document and change SoundLoadEvent to fire on mod bus

Previously, it fired on both the mod bus and the Forge bus, which is
confusing for consumers.
* Delete SoundSetupEvent

Looking at its original implementation shows that there isn't an
appropriate place in the new sound code to reinsert the event, and the
place of 'sound engine/manager initialization event' is taken already by SoundLoadEvent.
* Perform some cleanup on client events

 - Removed nullable annotations from ClientPlayerNetworkEvent
 - Renamed #getPartialTicks methods to #getPartialTick, to be consistent
  with vanilla's naming of the partial tick
 - Cleanup documentation to remove line breaks, use the
  spelling 'cancelled' over
  'canceled', and improve docs on existing and
   new methods.
* Remove EntityEvent.CanUpdate

Closes MinecraftForge/MinecraftForge#6394
* Switch to Jetbrains nullability annotations
* New PlayLevelSoundEvent; replaces old PlaySoundAtEntityEvent
* Remove ForgeWorldPresetScreens
* Remove IForgeRegistryEntry
* Remove use of List<Throwable> in FML's CompletableFutures
* Add docs to mod loading stages, stages, and phases
* Gradle 7.4.2
* Use SLF4J in FMLLoader and other subprojects
* Switch dynamic versions in subprojects to pinned ones
* Switch ForgeRoot and MDK to FG plugin markers
* Configure Forge javadoc task

The task now uses a custom stylesheet with MCForge elements, and
configured to combine the generation from the four FML subprojects
(fmlloader, fmlcore, javafmllanguage, mclanguage) and the Forge project
into the javadoc output.
* Update docs/md files, for 1.19 update and the move away from IRC to Discord.
* Make "Potentially dangerous alternative prefix" a debug warning, not info.

Co-authored-by: Curle <curle@gemwire.uk>
Co-authored-by: sciwhiz12 <arnoldnunag12@gmail.com>
Signed-off-by: SizableShrimp <sizableshrimp@sizableshrimp.me>
2022-06-07 15:31:26 -05:00

108 lines
4.1 KiB
Java

/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.server;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.locale.Language;
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.minecraftforge.common.ForgeI18n;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Pattern;
public class LanguageHook
{
private static final Logger LOGGER = LogManager.getLogger();
private static final Gson GSON = new Gson();
private static final Pattern PATTERN = Pattern.compile("%(\\d+\\$)?[\\d\\.]*[df]");
private static List<Map<String, String>> capturedTables = new ArrayList<>(2);
private static Map<String, String> modTable;
/**
* Loads lang files on the server
*/
public static void captureLanguageMap(Map<String, String> table)
{
capturedTables.add(table);
if (modTable != null) {
capturedTables.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) {
allResources.forEach(res -> {
try {
LanguageHook.loadLocaleData(res.open());
} catch (IOException ignored) {} // TODO: this should not be ignored -C
});
}
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 -> {
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 = new ResourceLocation(namespace, langFile);
loadLocaleData(resourceManager.getResourceStack(langResource));
} catch (Exception exception) {
LOGGER.warn("Skipped language file: {}:{}", namespace, langFile, exception);
}
});
}
public static void loadForgeAndMCLangs() {
modTable = new HashMap<>(5000);
final InputStream mc = Thread.currentThread().getContextClassLoader().getResourceAsStream("assets/minecraft/lang/en_us.json");
final InputStream forge = Thread.currentThread().getContextClassLoader().getResourceAsStream("assets/forge/lang/en_us.json");
loadLocaleData(mc);
loadLocaleData(forge);
capturedTables.forEach(t->t.putAll(modTable));
ForgeI18n.loadLanguageData(modTable);
}
static void loadLanguagesOnServer(MinecraftServer server) {
modTable = new HashMap<>(5000);
// Possible multi-language server support?
for (String lang : Arrays.asList("en_us")) {
loadLanguage(lang, server);
}
capturedTables.forEach(t->t.putAll(modTable));
ForgeI18n.loadLanguageData(modTable);
}
}