This commit is contained in:
beisenser 2026-01-08 09:12:22 +00:00 committed by GitHub
commit 1ac5add992
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 57 additions and 1 deletions

View File

@ -0,0 +1,42 @@
package cn.revaria.chatplus.mixin;
import net.minecraft.screen.AnvilScreenHandler;
import net.minecraft.screen.Property;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
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.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import static cn.revaria.chatplus.util.TextStyleFormatter.applySimpleStyle;
@Mixin(AnvilScreenHandler.class)
public abstract class AnvilMixin extends ScreenHandler {
@Shadow
@Nullable
private String newItemName;
@Shadow
@Final
private Property levelCost;
protected AnvilMixin(@Nullable ScreenHandlerType<?> type, int syncId) {
super(type, syncId);
}
@Inject(method = "updateResult", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;isEmpty()Z", ordinal = 0))
private void applyColorToName(CallbackInfo ci) {
if (this.newItemName != null && !this.newItemName.isEmpty()) {
String formattedName = applySimpleStyle(this.newItemName);
if (!formattedName.equals(this.newItemName)) {
this.newItemName = formattedName;
// In some versions, we might need to ensure the level cost is updated or the result is recalculated.
// But usually, modifying newItemName before the result item is constructed is enough.
}
}
}
}

View File

@ -41,3 +41,4 @@ public abstract class ChatMixin {
}
#endif
}
// 拦截玩家发送的聊天数据包在消息发送前将其中的 & 颜色代码和 [item] 占位符替换为 Minecraft 内部的文本样式组件

View File

@ -77,4 +77,16 @@ public class TextStyleFormatter {
return finalText;
}
/**
* Simple style application that only replaces color codes.
* Used for anvil renaming where [item] tags are not appropriate.
*
* @param text The text to format
* @return Formatted text with § color codes
*/
public static String applySimpleStyle(String text) {
if (text == null) return null;
return text.replace('&', '§').replace("§§", "&");
}
}

View File

@ -4,6 +4,7 @@
"plugin": "cn.revaria.chatplus.plugin.MixinConfigPlugin",
"mixins": [
"ChatMixin",
"AnvilMixin",
"compat.DynmapMixin",
"compat.StyledChatMixin"
],