Skip to content

Commit

Permalink
📝 ChatHudMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
kitUIN committed Jul 23, 2024
1 parent 699c9ed commit 13dd68d
Show file tree
Hide file tree
Showing 34 changed files with 1,023 additions and 329 deletions.
142 changes: 62 additions & 80 deletions .idea/workspace.xml

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions fabric/fabric-1.16.5/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ command_api=fabric-command-api-v1

# IF fabric-1.21 || fabric-1.20.5
#targetJavaVersion=21
# ELSE IF fabric-1.16.5
targetJavaVersion=11
# ELSE
#targetJavaVersion=17
targetJavaVersion=17
# END IF

# IF fabric-1.21 || fabric-1.20.5
Expand Down
Original file line number Diff line number Diff line change
@@ -1,119 +1,158 @@
package io.github.kituin.chatimage.mixin;


import io.github.kituin.chatimage.tool.ChatImageStyle;
import io.github.kituin.ChatImageCode.ChatImageBoolean;
import io.github.kituin.ChatImageCode.ChatImageCode;
import io.github.kituin.ChatImageCode.ChatImageCodeTool;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import io.github.kituin.chatimage.tool.ChatImageStyle;
import net.minecraft.client.MinecraftClient;
// IF < fabric-1.20
import net.minecraft.client.gui.DrawableHelper;
// END IF
import net.minecraft.client.gui.hud.ChatHud;
import net.minecraft.text.*;
import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;

import java.util.List;

import static io.github.kituin.ChatImageCode.ChatImageCodeInstance.LOGGER;
import static io.github.kituin.chatimage.client.ChatImageClient.CONFIG;
import static io.github.kituin.chatimage.tool.SimpleUtil.createLiteralText;
import static io.github.kituin.chatimage.tool.SimpleUtil.createTranslatableText;


/**
* 注入修改文本显示,自动将CICode转换为可鼠标悬浮格式文字
*
* @author kitUIN
*/
@Environment(EnvType.CLIENT)
@Mixin(ChatHud.class)
// IF >= fabric-1.20
//public class ChatHudMixin {
// ELSE
public class ChatHudMixin extends DrawableHelper {
@Shadow
@Final
private static Logger LOGGER;
// END IF
@ModifyVariable(at = @At("HEAD"),
// IF > fabric-1.18.2
// method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;Lnet/minecraft/client/gui/hud/MessageIndicator;)V",
// ELSE
method = "addMessage(Lnet/minecraft/text/Text;IIZ)V",
// END IF
argsOnly = true)
public Text addMessage(Text message) {
return replaceMessage(message);
}


@SuppressWarnings("t")
@Unique
private static Text replaceCode(Text text) {
String checkedText;
String key;
String key = "";
MutableText player = null;
boolean isSelf = false;
if (text instanceof TranslatableText) {
TranslatableText ttc = (TranslatableText) text;
MutableText t = text.copy();
t.getSiblings().clear();
Style style = text.getStyle();
t = t.setStyle(style);
// IF > fabric-1.20
// if (text.getContent() instanceof PlainTextContent) {
// checkedText = ((PlainTextContent) text.getContent()).string();
// } else if (text.getContent() instanceof TranslatableTextContent ttc) {
// key = ttc.getKey();
// Object[] args = ttc.getArgs();
// if (ChatImageCodeTool.checkKey(key)) {
// player = (MutableText) args[0];
// isSelf = player.getContent().toString().equals(MinecraftClient.getInstance().player.getName().getContent().toString());
// MutableText contents = (MutableText) args[1];
// if (contents.getContent() instanceof PlainTextContent) {
// checkedText = ((PlainTextContent) contents.getContent()).string();
// } else {
// checkedText = contents.getContent().toString();
// }
// } else {
// return t;
// }
// } else {
// checkedText = text.getContent().toString();
// }
// ELSE IF > fabric-1.18.2
// if (text.getContent() instanceof LiteralTextContent) {
// checkedText = ((LiteralTextContent) text.getContent()).string();
// } else if (text.getContent() instanceof TranslatableTextContent ttc) {
// key = ttc.getKey();
// Object[] args = ttc.getArgs();
// if (ChatImageCodeTool.checkKey(key)) {
// player = (MutableText) args[0];
// isSelf = player.getContent().toString().equals(MinecraftClient.getInstance().player.getName().getContent().toString());
// MutableText contents = (MutableText) args[1];
// if (contents.getContent() instanceof LiteralTextContent) {
// checkedText = ((LiteralTextContent) contents.getContent()).string();
// } else {
// checkedText = contents.getContent().toString();
// }
// } else {
// return t;
// }
// } else {
// checkedText = text.getContent().toString();
// }
// ELSE
if (text instanceof TranslatableText ttc) {
key = ttc.getKey();
Object[] args = ttc.getArgs();
if (ChatImageCodeTool.checkKey(key)) {
player = (LiteralText) args[0];
isSelf = player.asString().equals(MinecraftClient.getInstance().player.getName().asString());
}
if (args[1] instanceof String) {
String content = (String) args[1];
if (args[1] instanceof String content) {
checkedText = content;
} else {
MutableText contents = (MutableText) args[1];
checkedText = contents.asString();
}
} else {
key = "";
checkedText = text.asString();
}
// END IF


// 尝试解析CQ码
if(CONFIG.cqCode) checkedText = ChatImageCodeTool.checkCQCode(checkedText);
if (CONFIG.cqCode) checkedText = ChatImageCodeTool.checkCQCode(checkedText);

Style style = text.getStyle();
ChatImageBoolean allString = new ChatImageBoolean(false);

// 尝试解析CICode
List<Object> texts = ChatImageCodeTool.sliceMsg(checkedText, isSelf, allString, (e)->LOGGER.error(e.getMessage()));

List<Object> texts = ChatImageCodeTool.sliceMsg(checkedText, isSelf, allString, (e) -> LOGGER.error(e.getMessage()));
// 尝试解析URL
if(CONFIG.checkImageUri) ChatImageCodeTool.checkImageUri(texts, isSelf,allString);
if (CONFIG.checkImageUri) ChatImageCodeTool.checkImageUri(texts, isSelf, allString);

// 无识别则返回原样
if (allString.isValue()) {
if(style.getHoverEvent()!=null)
{
ChatImageCode action = style.getHoverEvent().getValue(ChatImageStyle.SHOW_IMAGE);
if(action != null) action.retry();
}
MutableText t = text.copy();
t.getSiblings().clear();
return t.setStyle(style);
ChatImageCode action = style.getHoverEvent() == null ? null : style.getHoverEvent().getValue(ChatImageStyle.SHOW_IMAGE);
if (action != null) action.retry();
return t;
}
MutableText res = new LiteralText("");
MutableText res = createLiteralText("");
ChatImageCodeTool.buildMsg(texts,
(obj) -> res.append(new LiteralText(obj).setStyle(style)),
(obj) -> res.append(createLiteralText(obj).setStyle(style)),
(obj) -> res.append(ChatImageStyle.messageFromCode(obj))
);
if (player != null) {
return new TranslatableText(key, player, res).setStyle(style);
} else {
return res;
}
return player == null ? res : createTranslatableText(key, player, res).setStyle(style);
}

@Unique
private static Text replaceMessage(Text message) {
try{
try {
MutableText res = (MutableText) replaceCode(message);
for (Text t : message.getSiblings()) {
res.append(replaceMessage(t));
}
return res;
}
catch (Exception e){
} catch (Exception e) {
return message;
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ public static MutableText createTranslatableText(String text){
return new TranslatableText(text);
// ELSE
// return Text.translatable(text);
// END IF
}
public static MutableText createTranslatableText(String key, Object... args){
// IF fabric-1.16.5 || fabric-1.18.2
return new TranslatableText(key, args);
// ELSE
// return Text.translatable(key, args);
// END IF
}

Expand Down
2 changes: 0 additions & 2 deletions fabric/fabric-1.18.2/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ command_api=fabric-command-api-v1

# IF fabric-1.21 || fabric-1.20.5
#targetJavaVersion=21
# ELSE IF fabric-1.16.5
#targetJavaVersion=11
# ELSE
targetJavaVersion=17
# END IF
Expand Down
Loading

0 comments on commit 13dd68d

Please sign in to comment.