Last active
May 9, 2020 10:59
-
-
Save LoganDark/3a2a4bdb8870e71f222528c0c4198c8f to your computer and use it in GitHub Desktop.
Allows the Minecraft dedicated server to see localized (TranslatableText) messages. Activate in your ModInitializer only on the server side
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
package net.logandark.languagehack | |
import com.google.gson.Gson | |
import com.google.gson.JsonObject | |
import net.fabricmc.loader.api.FabricLoader | |
import net.logandark.languagehack.mixin.MixinLanguage | |
import net.minecraft.util.JsonHelper | |
import net.minecraft.util.Language | |
import org.apache.logging.log4j.core.util.Closer | |
import java.io.InputStreamReader | |
import java.nio.charset.StandardCharsets | |
object LanguageHack { | |
fun activate(modid: String) { | |
val language = Language.getInstance() as MixinLanguage | |
val inputStream = Files.newInputStream( | |
FabricLoader.getInstance() | |
.getModContainer(modid).get() | |
.getPath("assets/$modid/lang/en_us.json") | |
) | |
try { | |
val jsonObject = Gson().fromJson( | |
InputStreamReader(inputStream, StandardCharsets.UTF_8), | |
JsonObject::class.java | |
) | |
jsonObject.entrySet().forEach { entry -> | |
val string = language.field_11489 | |
.matcher(JsonHelper.asString(entry.value, entry.key)) | |
.replaceAll("%$1s") | |
language.translations[entry.key] = string | |
} | |
} finally { | |
Closer.closeSilently(inputStream) | |
} | |
} | |
} |
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
package net.logandark.languagehack.mixin; | |
import net.minecraft.util.Language; | |
import org.spongepowered.asm.mixin.Mixin; | |
import org.spongepowered.asm.mixin.gen.Accessor; | |
import java.util.Map; | |
import java.util.regex.Pattern; | |
@Mixin(Language.class) | |
public interface MixinLanguage { | |
@Accessor | |
Map<String, String> getTranslations(); | |
@Accessor | |
Pattern getField_11489(); | |
} |
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
package net.logandark.example | |
import net.fabricmc.api.EnvType | |
import net.fabricmc.api.ModInitializer | |
import net.fabricmc.loader.api.FabricLoader | |
import net.logandark.languagehack.LanguageHack | |
@Suppress("unused") | |
object ExampleMod : ModInitializer { | |
const val modid = "example-mod" | |
override fun onInitialize() { | |
// Activate the language hack for localizations on server. | |
if (FabricLoader.getInstance().environmentType == EnvType.SERVER) | |
LanguageHack.activate(modid) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment