Skip to content

Commit

Permalink
Merge commit '87053c7f' into 1.20.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerozgen committed Oct 27, 2024
2 parents e3f25da + 87053c7 commit b96c633
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

@Environment(EnvType.CLIENT)
public interface ITranslationStorage {
String languagereload_get(String key);

@Nullable String languagereload_getTargetLanguage();

void languagereload_setTargetLanguage(@Nullable String value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
@Mixin(LanguageOptionsScreen.class)
public abstract class LanguageOptionsScreenMixin extends GameOptionsScreen implements ILanguageOptionsScreen {
@Shadow @Final private static Text LANGUAGE_WARNING_TEXT;
@Shadow private LanguageOptionsScreen.LanguageSelectionListWidget languageSelectionList;

@Unique private LanguageListWidget availableLanguageList;
@Unique private LanguageListWidget selectedLanguageList;
Expand Down Expand Up @@ -59,6 +60,8 @@ void onConstructed(Screen parent, GameOptions options, LanguageManager languageM

@Inject(method = "init", at = @At("HEAD"), cancellable = true)
void onInit(CallbackInfo ci) {
languageSelectionList = LanguageSelectionListWidgetAccessor.languagereload_init(it(), client);

searchBox = new TextFieldWidget(textRenderer, width / 2 - 100, 22, 200, 20, searchBox, Text.empty()) {
@Override
public void setFocused(boolean focused) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package jerozgen.languagereload.mixin;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.option.LanguageOptionsScreen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(LanguageOptionsScreen.LanguageSelectionListWidget.class)
public interface LanguageSelectionListWidgetAccessor {
@Invoker("<init>")
static LanguageOptionsScreen.LanguageSelectionListWidget languagereload_init(LanguageOptionsScreen screen, MinecraftClient client) {
throw new AssertionError();
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
package jerozgen.languagereload.mixin;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import jerozgen.languagereload.access.ILanguage;
import jerozgen.languagereload.access.ITranslationStorage;
import jerozgen.languagereload.config.Config;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.util.Language;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

import java.util.ArrayList;
import java.util.List;

@Mixin(value = MinecraftClient.class, priority = 990)
abstract class MinecraftClientMixin {
@ModifyExpressionValue(method = {"method_1581" /* Creative Inventory */, "method_53866" /* Recipe Book */},
at = @At(value = "INVOKE", target = "Lnet/minecraft/text/Text;getString()Ljava/lang/String;"))
private static String addFallbackTranslationsToSearchTooltips(String original, Text text) {
@WrapOperation(method = {"method_1485" /* Creative Inventory */, "method_53863" /* Recipe Book */},
at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getTooltip(Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/client/item/TooltipContext;)Ljava/util/List;"))
private static List<Text> addFallbackTranslationsToSearchTooltips(ItemStack instance, PlayerEntity player, TooltipContext context, Operation<List<Text>> operation) {
var original = operation.call(instance, player, context);

if (Config.getInstance() == null) return original;
if (!Config.getInstance().multilingualItemSearch) return original;

var translationStorage = ((ILanguage) Language.getInstance()).languagereload_getTranslationStorage();
if (translationStorage == null) return original;

var stringBuilder = new StringBuilder(original);
for (String fallbackCode : Config.getInstance().fallbacks) {
var result = new ArrayList<>(original);
for (var fallbackCode : Config.getInstance().fallbacks) {
((ITranslationStorage) translationStorage).languagereload_setTargetLanguage(fallbackCode);
stringBuilder.append('\n').append(text.getString());
operation.call(instance, player, context)
.stream()
.map(Text::getString)
.map(Text::literal)
.forEach(result::add);
}

((ITranslationStorage) translationStorage).languagereload_setTargetLanguage(null);
return stringBuilder.toString();
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package jerozgen.languagereload.mixin;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import jerozgen.languagereload.access.ILanguage;
import jerozgen.languagereload.access.ITranslationStorage;
import jerozgen.languagereload.config.Config;
Expand All @@ -10,71 +11,54 @@
import net.minecraft.text.TranslatableTextContent;
import net.minecraft.text.TranslationException;
import net.minecraft.util.Language;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;

@Mixin(TranslatableTextContent.class)
abstract class TranslatableTextContentMixin implements TextContent {
@Unique private @Nullable String previousTargetLanguage;
@Unique private final Map<String, List<StringVisitable>> separateTranslationsCache = Maps.newHashMap();
@Unique private @Nullable List<StringVisitable> savedTranslations;

@Shadow @Final private String key;
@Shadow private @Nullable Language languageCache;
@Shadow private List<StringVisitable> translations;

@Inject(method = "updateTranslations", at = @At("RETURN"))
void onUpdateTranslations(CallbackInfo ci) {
if (Config.getInstance() == null) return;
if (!Config.getInstance().multilingualItemSearch) return;
if (languageCache == null) return;
@WrapOperation(method = "visit(Lnet/minecraft/text/StringVisitable$Visitor;)Ljava/util/Optional;",
at = @At(value = "FIELD", target = "Lnet/minecraft/text/TranslatableTextContent;translations:Ljava/util/List;"))
List<StringVisitable> onVisit(TranslatableTextContent instance, Operation<List<StringVisitable>> translationsGetter) {
var overriddenTranslations = getOverriddenTranslations();
if (overriddenTranslations != null) return overriddenTranslations;
return translationsGetter.call(instance);
}

var translationStorage = ((ILanguage) languageCache).languagereload_getTranslationStorage();
if (translationStorage == null) return;
@WrapOperation(method = "visit(Lnet/minecraft/text/StringVisitable$StyledVisitor;Lnet/minecraft/text/Style;)Ljava/util/Optional;",
at = @At(value = "FIELD", target = "Lnet/minecraft/text/TranslatableTextContent;translations:Ljava/util/List;"))
List<StringVisitable> onVisitStyled(TranslatableTextContent instance, Operation<List<StringVisitable>> translationsGetter) {
var overriddenTranslations = getOverriddenTranslations();
if (overriddenTranslations != null) return overriddenTranslations;
return translationsGetter.call(instance);
}

var targetLanguage = ((ITranslationStorage) translationStorage).languagereload_getTargetLanguage();
if (Objects.equals(previousTargetLanguage, targetLanguage)) return;
@Unique
List<StringVisitable> getOverriddenTranslations() {
if (!Config.getInstance().multilingualItemSearch) return null;

if (targetLanguage == null) {
previousTargetLanguage = null;
translations = savedTranslations;
savedTranslations = null;
return;
}
var language = Language.getInstance();
var translationStorage = ((ILanguage) language).languagereload_getTranslationStorage();
if (translationStorage == null) return null;

if (previousTargetLanguage == null) {
savedTranslations = translations;
}
previousTargetLanguage = targetLanguage;
translations = separateTranslationsCache.computeIfAbsent(targetLanguage, k -> {
var string = languageCache.get(key);
try {
var builder = new ImmutableList.Builder<StringVisitable>();
this.forEachPart(string, builder::add);
return builder.build();
} catch (TranslationException e) {
return ImmutableList.of(StringVisitable.plain(string));
}
});
}
var targetLanguage = ((ITranslationStorage) translationStorage).languagereload_getTargetLanguage();
if (targetLanguage == null) return null;

@Inject(method = "updateTranslations", at = @At(value = "INVOKE",
target = "Lnet/minecraft/util/Language;get(Ljava/lang/String;)Ljava/lang/String;"))
void onUpdateTranslations$clearCache(CallbackInfo ci) {
previousTargetLanguage = null;
separateTranslationsCache.clear();
savedTranslations = null;
var string = ((ITranslationStorage) translationStorage).languagereload_get(key);
try {
var builder = new ImmutableList.Builder<StringVisitable>();
this.forEachPart(string, builder::add);
return builder.build();
} catch (TranslationException e) {
return ImmutableList.of(StringVisitable.plain(string));
}
}

@Shadow protected abstract void forEachPart(String translation, Consumer<StringVisitable> partsConsumer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

@Mixin(TranslationStorage.class)
abstract class TranslationStorageMixin extends Language implements ITranslationStorage {
@Unique private @Nullable String targetLanguage;
@Unique private final Map<Long, String> targetLanguageByThread = Maps.newConcurrentMap();
@Unique private static Map<String, Map<String, String>> separateTranslationsOnLoad;
@Unique private Map<String, Map<String, String>> separateTranslations;

Expand All @@ -47,21 +47,25 @@ private static void onLoad(ResourceManager resourceManager, List<String> definit
} else Language.load(inputStream, entryConsumer);
}

@Inject(method = "get", at = @At(value = "HEAD"), cancellable = true)
void onGet(String key, String fallback, CallbackInfoReturnable<String> cir) {
@Override
public String languagereload_get(String key) {
var targetLanguage = languagereload_getTargetLanguage();
if (targetLanguage != null) {
var targetTranslations = separateTranslations.get(targetLanguage);
cir.setReturnValue(targetTranslations == null ? "" : targetTranslations.getOrDefault(key, ""));
return targetTranslations == null ? "" : targetTranslations.getOrDefault(key, "");
}
return this.get(key);
}

@Override
public @Nullable String languagereload_getTargetLanguage() {
return targetLanguage;
return targetLanguageByThread.get(Thread.currentThread().threadId());
}

@Override
public void languagereload_setTargetLanguage(@Nullable String value) {
targetLanguage = value;
var threadId = Thread.currentThread().threadId();
if (value == null) targetLanguageByThread.remove(threadId);
else targetLanguageByThread.put(threadId, value);
}
}
1 change: 1 addition & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
{"name": "BroxyZF — Spanish", "contact": {"homepage": "https://github.com/BroxyZF"}},
{"name": "Calvineries — French", "contact": {"homepage": "https://github.com/Calvineries"}},
{"name": "glaav — Italian", "contact": {"homepage": "https://github.com/glaav"}},
{"name": "ookkoouu — Japanese", "contact": {"homepage": "https://github.com/ookkoouu"}},
{"name": "wavgado — Korean", "contact": {"homepage": "https://github.com/wavgado"}},
{"name": "PRO100KatYT — Polish", "contact": {"homepage": "https://github.com/PRO100KatYT"}},
{"name": "FITFC — Brazilian Portuguese", "contact": {"homepage": "https://github.com/FITFC"}},
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/languagereload.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"LanguageManagerMixin",
"LanguageMixin",
"LanguageOptionsScreenMixin",
"LanguageSelectionListWidgetAccessor",
"MinecraftClientAccessor",
"MinecraftClientMixin",
"RecipeBookWidgetMixin",
Expand Down

0 comments on commit b96c633

Please sign in to comment.