Skip to content

Commit

Permalink
check for the same tag before comparing tier index
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Feb 14, 2024
1 parent 1e6ff72 commit 8657351
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
10 changes: 2 additions & 8 deletions src/main/java/mcp/mobius/waila/service/ApiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.function.Supplier;

import com.google.common.base.Suppliers;
import com.google.common.collect.Streams;
import com.mojang.blaze3d.vertex.BufferBuilder;
import mcp.mobius.waila.Waila;
Expand Down Expand Up @@ -167,7 +165,8 @@ public <T> IInstanceRegistry<T> createInstanceRegistry(boolean reversed) {
return registry;
}

public static final Supplier<List<Tier>> TIERS = Suppliers.memoize(() -> {
@Override
public List<Tier> getTiers() {
var vanilla = List.of(Tiers.values());
var custom = new LinkedHashSet<Tier>();

Expand All @@ -178,11 +177,6 @@ public <T> IInstanceRegistry<T> createInstanceRegistry(boolean reversed) {
}

return Streams.concat(vanilla.stream(), custom.stream()).sorted(Comparator.comparingInt(Tier::getLevel)).toList();
});

@Override
public List<Tier> getTiers() {
return TIERS.get();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void onHandleTooltip(ITooltip tooltip, ICommonAccessor accessor, IPluginC
matches = tool.itemPredicate.test(heldStack);
if (highestTier != ToolTier.NONE && heldStack.getItem() instanceof TieredItem tiered) {
var heldTier = ToolTier.get(tiered.getTier());
matches = matches && heldTier.index >= highestTier.index;
matches = matches && heldTier.isBetterThanOrEqualTo(highestTier);
}
}
component = new ToolComponent(icon, matches);
Expand Down Expand Up @@ -196,12 +196,12 @@ private static MutableComponent getToolText(List<ToolType> tools, ItemStack held

@NotNull
private static MutableComponent getTierText(ToolTier highestTier, ItemStack heldStack) {
var tierText = I18n.exists(highestTier.tlKey)
? Component.translatable(highestTier.tlKey)
var tierText = I18n.exists(highestTier.tlKey())
? Component.translatable(highestTier.tlKey())
: Component.literal(String.valueOf(highestTier.index));
if (heldStack.getItem() instanceof TieredItem tiered) {
var heldTier = ToolTier.get(tiered.getTier());
tierText.withStyle(heldTier.index >= highestTier.index ? ChatFormatting.GREEN : ChatFormatting.RED);
tierText.withStyle(heldTier.isBetterThanOrEqualTo(highestTier) ? ChatFormatting.GREEN : ChatFormatting.RED);
} else {
tierText.withStyle(ChatFormatting.RED);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mcp.mobius.waila.plugin.harvest.tool;

import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.function.Supplier;
Expand All @@ -10,6 +11,7 @@
import mcp.mobius.waila.api.__internal__.IApiService;
import mcp.mobius.waila.api.__internal__.Internals;
import mcp.mobius.waila.buildconst.Tl;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Tier;
import net.minecraft.world.item.Tiers;
Expand All @@ -30,22 +32,39 @@ public final class ToolTier {
return builder.build();
});

private static final Supplier<Map<ResourceLocation, String>> VANILLA_TIER_TL_KEYS = Suppliers.memoize(() -> {
var map = new HashMap<ResourceLocation, String>();
for (var tier : Tiers.values()) {
var tag = IApiService.INSTANCE.getTierTag(tier);
if (tag == null) continue;
map.put(tag.location(), tier.name().toLowerCase(Locale.ROOT));
}
return map;
});

public final Tier tier;
public final int index;
public final @Nullable TagKey<Block> tag;
public final String tlKey;

private final Supplier<String> tlKey;

public ToolTier(Tier tier, int index) {
this.tier = tier;
this.index = index;
this.tag = IApiService.INSTANCE.getTierTag(tier);

if (tier instanceof Tiers vanilla)
this.tlKey = Tl.Tooltip.Harvest.TIER + "." + vanilla.name().toLowerCase(Locale.ROOT);
else if (tag != null)
this.tlKey = Tl.Tooltip.Harvest.TIER + "." + tag.location().toLanguageKey();
else
this.tlKey = Tl.Tooltip.Harvest.TIER + "." + tier.getLevel();
this.tlKey = Suppliers.memoize(() -> {
String key;

if (tag != null) {
var vanilla = VANILLA_TIER_TL_KEYS.get().get(tag.location());
key = vanilla != null ? vanilla : tag.location().toLanguageKey();
} else {
key = String.valueOf(tier.getLevel());
}

return Tl.Tooltip.Harvest.TIER + "." + key;
});
}

public static Collection<ToolTier> all() {
Expand All @@ -56,4 +75,19 @@ public static ToolTier get(Tier tier) {
return TIERS.get().get(tier);
}

public String tlKey() {
return tlKey.get();
}

public boolean isEqualTo(ToolTier other) {
if (this == other) return true;
if (this.tier == other.tier) return true;
if (this.tag != null && other.tag != null) return this.tag.location().equals(other.tag.location());
return false;
}

public boolean isBetterThanOrEqualTo(ToolTier other) {
return isEqualTo(other) || this.index >= other.index;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ToolType implements IToolType, IToolType.Builder0, IToolType.Builde
var stack = item.getDefaultInstance();
if (itemPredicate.test(stack)) {
for (var tier : tiers) {
if (!map.containsKey(tier) && item instanceof TieredItem tiered && tiered.getTier() == tier.tier) {
if (!map.containsKey(tier) && item instanceof TieredItem tiered && ToolTier.get(tiered.getTier()).isEqualTo(tier)) {
map.put(tier, stack);
}
}
Expand Down

0 comments on commit 8657351

Please sign in to comment.