Skip to content

Commit

Permalink
Use more robust ChatComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
glowredman committed Sep 13, 2024
1 parent f30241b commit b1fdc4c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 19 deletions.
6 changes: 3 additions & 3 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ dependencies {

compileOnly("com.gtnewhorizons.retrofuturabootstrap:RetroFuturaBootstrap:1.0.2") { transitive = false }

transformedMod("com.github.GTNewHorizons:NotEnoughItems:2.6.35-GTNH:dev") // force a more up-to-date NEI version
transformedMod("com.github.GTNewHorizons:NotEnoughItems:2.6.36-GTNH:dev") // force a more up-to-date NEI version
transformedModCompileOnly("com.github.GTNewHorizons:Applied-Energistics-2-Unofficial:rv3-beta-454-GTNH")
transformedModCompileOnly("com.github.GTNewHorizons:Baubles:1.0.4:dev")
// Transitive updates to make runClient17 work
transformedModCompileOnly("com.github.GTNewHorizons:ForgeMultipart:1.5.0:dev")
transformedModCompileOnly("com.github.GTNewHorizons:GT5-Unofficial:5.09.49.88:dev")
transformedModCompileOnly("com.github.GTNewHorizons:GT5-Unofficial:5.09.49.94:dev")
transformedModCompileOnly("com.github.GTNewHorizons:harvestcraft:1.2.1-GTNH:dev")
transformedModCompileOnly("com.github.GTNewHorizons:HungerOverhaul:1.1.0-GTNH:dev")
transformedModCompileOnly("com.github.GTNewHorizons:MrTJPCore:1.2.1:dev") // Do not update, fixed afterwards
transformedModCompileOnly("com.github.GTNewHorizons:Railcraft:9.15.14:dev") { exclude group: "thaumcraft", module: "Thaumcraft" }
transformedModCompileOnly("com.github.GTNewHorizons:TinkersConstruct:1.12.9-GTNH:dev")
transformedModCompileOnly(rfg.deobf("curse.maven:bibliocraft-228027:2423369"))
transformedModCompileOnly("curse.maven:biomes-o-plenty-220318:2499612")
transformedModCompileOnly(rfg.deobf("curse.maven:biomes-o-plenty-220318:2499612"))
transformedModCompileOnly("curse.maven:cofh-core-69162:2388751")
transformedModCompileOnly("curse.maven:minefactory-reloaded-66672:2277486")
transformedModCompileOnly(rfg.deobf('curse.maven:damage-indicators-mod-59489:2692129'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.sugar.Local;
import com.mitchej123.hodgepodge.Common;
import com.mitchej123.hodgepodge.util.ChatComponentItemTranslation;

import cpw.mods.fml.common.registry.GameRegistry;

Expand Down Expand Up @@ -59,20 +59,9 @@ public class MixinGameRegistry {

@Unique
private static StatCrafting hodgepodge$createAndRegisterStat(String key, Item item) {
String unlocalizedName;
try {
unlocalizedName = item.getUnlocalizedName();
} catch (Exception e) {
String registryName = item.delegate.name();
unlocalizedName = "item." + registryName + ".name";
Common.log.warn(
"An Exception occured while invoking Item.getUnlocalizedName() after registering the item {} ({})! Using fallback unlocalized name.",
registryName,
item.getClass().getName());
}
StatCrafting stat = new StatCrafting(
key + ".autogen." + item.delegate.name(),
new ChatComponentTranslation(key, new ChatComponentTranslation(unlocalizedName)),
new ChatComponentTranslation(key, new ChatComponentItemTranslation(item)),
item);
stat.registerStat();
return stat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public class MixinMorpheusWakePlayers {
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/WorldProvider;resetRainAndThunder()V",
shift = At.Shift.BEFORE,
remap = true))
shift = At.Shift.BEFORE))
private void hodgepodge$fixWakePlayers(World world, CallbackInfo c) {
if (!(world instanceof WorldServer worldServer)) return;
worldServer.wakeAllPlayers();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.mitchej123.hodgepodge.util;

import java.util.Locale;
import java.util.regex.Pattern;

import net.minecraft.item.Item;
import net.minecraft.util.ChatComponentStyle;
import net.minecraft.util.IChatComponent;
import net.minecraft.util.StatCollector;

public class ChatComponentItemTranslation extends ChatComponentStyle {

private static final Pattern FORMATTING_CODE_PATTERN = Pattern.compile("(?i)\u00a7[0-9A-FK-OR]");

private final Item item;

public ChatComponentItemTranslation(Item item) {
this.item = item;
}

@Override
public String getUnformattedTextForChat() {
String unlocalizedName = this.item.getUnlocalizedName() + ".name";
String localizedName = StatCollector.translateToLocal(unlocalizedName);
StringBuilder sb = new StringBuilder(getTextWithoutFormattingCodes(localizedName));
for (IChatComponent sibling : this.getSiblings()) {
sb.append(sibling.getUnformattedTextForChat());
}
return sb.toString();
}

@Override
public IChatComponent createCopy() {
ChatComponentItemTranslation component = new ChatComponentItemTranslation(this.item);
for (IChatComponent sibling : this.getSiblings()) {
component.appendSibling(sibling.createCopy());
}
return component;
}

@Override
public int hashCode() {
return 31 * super.hashCode() + this.item.hashCode();
}

@Override
public String toString() {
return String.format(
Locale.ROOT,
"TranslatableItemComponent{item=%s, siblings=%s, style=%s}",
this.item.delegate.name(),
this.getSiblings(),
this.getChatStyle());
}

private static String getTextWithoutFormattingCodes(String text) {
return text == null ? null : FORMATTING_CODE_PATTERN.matcher(text).replaceAll("");
}

}
20 changes: 19 additions & 1 deletion src/main/resources/assets/hodgepodge/lang/en_US.lang
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
key.hodgepodge.category=Hodgepodge
key.fastBlockPlacing.desc=Fast Block Placing
key.fastBlockPlacing.enabled=§fFast Block Placing §aEnabled
key.fastBlockPlacing.disabled=§fFast Block Placing §cDisabled
key.fastBlockPlacing.disabled=§fFast Block Placing §cDisabled

tile.dirt.name=Dirt
tile.sapling.name=Sapling
tile.sand.name=Sand
tile.flower1.name=Flower
tile.flower2.name=Flower
tile.stoneSlab.name=Slab
tile.monsterStoneEgg.name=Stone Monster Egg
tile.woodSlab.name=Slab
tile.cobbleWall.name=Wall
tile.quartzBlock.name=Quartz Block
tile.clayHardenedStained.name=Stained Clay
tile.woolCarpet.name=Carpet
tile.doublePlant.name=Large Plant

item.dyePowder.name=Dye
item.skull.name=Skull
item.fish.name=Fish

0 comments on commit b1fdc4c

Please sign in to comment.