package io.gitlab.jfronny.translater.mixin; import io.gitlab.jfronny.translater.ModInit; import io.gitlab.jfronny.translater.TransformingMap; import io.gitlab.jfronny.translater.YnTransformer; import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.MappingResolver; import net.minecraft.client.resource.language.TranslationStorage; import net.minecraft.util.Language; import org.apache.commons.lang3.reflect.FieldUtils; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import java.lang.reflect.Field; import java.util.Map; @Mixin(Language.class) public class MixinLanguage { private static final MappingResolver mr = FabricLoader.getInstance().getMappingResolver(); private static final Field FIELD = FieldUtils.getField(TranslationStorage.class, mr.mapFieldName("intermediary", "net.minecraft.class_1078", "field_5330", "Ljava/util/Map;"), true); private static final YnTransformer transformer = new YnTransformer(); @Inject(at = @At("HEAD"), method = "setInstance") private static void languageSetInstance(Language language, CallbackInfo ci) { if (FIELD == null) { ModInit.Warn("Could not set language map, field not found"); return; } if (language instanceof TranslationStorage) { try { ModInit.map = new TransformingMap((Map) FIELD.get(language), transformer); ModInit.map.init(); FIELD.set(language, ModInit.map); } catch (IllegalAccessException | ClassCastException e) { e.printStackTrace(); } } } }