Skip to content

Commit

Permalink
Fix tooltips not refreshing properly after language/keybind changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hlysine committed Jul 17, 2024
1 parent c6ceedc commit 68a17ab
Showing 1 changed file with 12 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.copycatsplus.copycats.foundation.tooltip;

import com.copycatsplus.copycats.CCKeys;
import com.simibubi.create.foundation.item.TooltipHelper;
import com.simibubi.create.foundation.item.TooltipModifier;
import com.simibubi.create.foundation.utility.Components;
Expand All @@ -9,9 +8,8 @@
import com.simibubi.create.foundation.utility.Pair;
import com.tterrag.registrate.util.nullness.NonNullConsumer;
import dev.architectury.injectables.annotations.ExpectPlatform;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.locale.Language;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.world.item.Item;
Expand All @@ -38,9 +36,9 @@ public static <T extends ItemLike> NonNullConsumer<? super T> register(CopycatCh
}

protected final Item item;
private static String cachedLanguage = null;
private static Map<String, List<Object>> cachedArgs = new HashMap<>();
private static Map<CopycatCharacteristics, Pair<List<Component>, List<Component>>> descriptions;
private Language cachedLanguage = null;
private Map<String, List<Object>> cachedArgs = new HashMap<>();
private Map<CopycatCharacteristics, Pair<List<Component>, List<Component>>> descriptions;
@Nullable
private List<Component> shortDescription = null;
@Nullable
Expand Down Expand Up @@ -99,37 +97,32 @@ public void modify(Item item, List<Component> tooltip) {
}
}

private static boolean shouldInvalidateCache() {
String currentLanguage = Minecraft.getInstance()
.getLanguageManager()
.getSelected();
if (!currentLanguage.equals(cachedLanguage)) {
cachedLanguage = currentLanguage;
return true;
}
private boolean shouldInvalidateCache() {
Language currentLanguage = Language.getInstance();
Map<String, List<Object>> newArgs = new HashMap<>();
for (CopycatCharacteristics characteristics : CopycatCharacteristics.all()) {
newArgs.put(characteristics.getSerializedName(), Arrays.stream(characteristics.getArgs()).map(Supplier::get).toList());
}
if (!newArgs.equals(cachedArgs)) {
if (!currentLanguage.equals(cachedLanguage) || !newArgs.equals(cachedArgs)) {
cachedLanguage = currentLanguage;
cachedArgs = newArgs;
return true;
}
return false;
}

private static void populateDescriptions() {
private void populateDescriptions() {
descriptions = new HashMap<>();
for (CopycatCharacteristics characteristics : CopycatCharacteristics.all()) {
String titleKey = characteristics.getTitleKey();
String descKey = characteristics.getDescriptionKey();

if (!I18n.exists(titleKey) || !I18n.exists(descKey))
if (!cachedLanguage.has(titleKey) || !cachedLanguage.has(descKey))
continue;

descriptions.put(characteristics, Pair.of(
List.of(Components.literal("- " + I18n.get(titleKey)).withStyle(GRAY)),
TooltipHelper.cutStringTextComponent(String.format(I18n.get(descKey), cachedArgs.get(characteristics.getSerializedName()).toArray()), TooltipHelper.Palette.STANDARD_CREATE)
List.of(Components.literal("- " + cachedLanguage.getOrDefault(titleKey)).withStyle(GRAY)),
TooltipHelper.cutStringTextComponent(String.format(cachedLanguage.getOrDefault(descKey), cachedArgs.get(characteristics.getSerializedName()).toArray()), TooltipHelper.Palette.STANDARD_CREATE)
));
}
}
Expand Down

0 comments on commit 68a17ab

Please sign in to comment.