mirror of
https://github.com/CPTProgrammer/ChatPlus.git
synced 2026-02-04 04:02:00 +08:00
Merge 34e81368b1 into 3b9e607f24
This commit is contained in:
commit
1ac5add992
42
src/main/java/cn/revaria/chatplus/mixin/AnvilMixin.java
Normal file
42
src/main/java/cn/revaria/chatplus/mixin/AnvilMixin.java
Normal 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.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -41,3 +41,4 @@ public abstract class ChatMixin {
|
||||
}
|
||||
#endif
|
||||
}
|
||||
// 拦截玩家发送的聊天数据包,在消息发送前将其中的 & 颜色代码和 [item] 占位符替换为 Minecraft 内部的文本样式组件。
|
||||
@ -76,5 +76,17 @@ 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("§§", "&");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
"plugin": "cn.revaria.chatplus.plugin.MixinConfigPlugin",
|
||||
"mixins": [
|
||||
"ChatMixin",
|
||||
"AnvilMixin",
|
||||
"compat.DynmapMixin",
|
||||
"compat.StyledChatMixin"
|
||||
],
|
||||
|
||||
Loading…
Reference in New Issue
Block a user