-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
179 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
134 changes: 134 additions & 0 deletions
134
forge/origin/src/main/java/io/github/kituin/chatimage/mixin/ChatComponentMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
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.minecraft.client.Minecraft; | ||
import org.slf4j.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.chatimage.ChatImage.CONFIG; | ||
import static io.github.kituin.chatimage.tool.SimpleUtil.*; | ||
|
||
|
||
/** | ||
* 注入修改文本显示,自动将CICode转换为可鼠标悬浮格式文字 | ||
* | ||
* @author kitUIN | ||
*/ | ||
@SuppressWarnings("t") | ||
@Mixin(#ChatComponent#.class) | ||
public class #kituin$ChatComponentMixinClass# { | ||
@Shadow | ||
@Final | ||
private Minecraft minecraft; | ||
@Shadow @Final private static Logger LOGGER; | ||
|
||
@ModifyVariable(at = @At("HEAD"), | ||
method = "#kituin$addMessageMixin#", | ||
argsOnly = true) | ||
public #Component# addMessage(#Component# p_241484_) { | ||
return chatimage$replaceMessage(p_241484_); | ||
} | ||
|
||
// IF >= forge-1.19 | ||
// @Unique | ||
// private #Component#Contents chatImage$getContents(#Component# text){ | ||
// return text.getContents(); | ||
//} | ||
// ELSE | ||
// @Unique | ||
// private #Component# chatImage$getContents(#Component# text){ | ||
// return text; | ||
// } | ||
// END IF | ||
|
||
@Unique | ||
private String chatImage$getText(#PlainTextContents# text){ | ||
// IF >= forge-1.19 | ||
// return text.text(); | ||
// ELSE | ||
// return text.getContents(); | ||
// END IF | ||
} | ||
|
||
@Unique | ||
private #Component# chatimage$replaceCode(#Component# text) { | ||
String checkedText = ""; | ||
String key = ""; | ||
#MutableComponent# player = null; | ||
boolean isSelf = false; | ||
if (chatImage$getContents(text) instanceof #PlainTextContents# lc) { | ||
checkedText = chatImage$getText(lc); | ||
} else if (chatImage$getContents(text) instanceof #TranslatableContents# ttc) { | ||
key = ttc.getKey(); | ||
Object[] args = ttc.getArgs(); | ||
if (ChatImageCodeTool.checkKey(key)) { | ||
player = (#MutableComponent#) args[0]; | ||
isSelf = chatImage$getContents(player).toString().equals(chatImage$getContents(this.minecraft.player.getName()).toString()); | ||
#MutableComponent# contents = (#MutableComponent#) args[1]; | ||
if (chatImage$getContents(contents) instanceof #PlainTextContents# lc) { | ||
checkedText = chatImage$getText(lc); | ||
} else { | ||
checkedText = chatImage$getContents(contents).toString(); | ||
} | ||
} | ||
} else { | ||
checkedText = chatImage$getContents(text).toString(); | ||
} | ||
|
||
// 尝试解析CQ码 | ||
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())); | ||
// 尝试解析URL | ||
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(); | ||
} | ||
#MutableComponent# res = text.copy(); | ||
res.getSiblings().clear(); | ||
return res; | ||
} | ||
#MutableComponent# res = createLiteralComponent(""); | ||
ChatImageCodeTool.buildMsg(texts, | ||
(obj) -> res.append(createLiteralComponent(obj).setStyle(style)), | ||
(obj) -> res.append(ChatImageStyle.messageFromCode(obj)) | ||
); | ||
if (player == null) { | ||
return res; | ||
} else { | ||
return createTranslatableComponent(key, player, res).setStyle(style); | ||
} | ||
} | ||
|
||
@Unique | ||
private #Component# chatimage$replaceMessage(#Component# message) { | ||
try { | ||
#MutableComponent# res = (#MutableComponent#) chatimage$replaceCode(message); | ||
for (#Component# t : message.getSiblings()) { | ||
res.append(chatimage$replaceMessage(t)); | ||
} | ||
return res; | ||
} catch (Exception e) { | ||
LOGGER.warn("识别失败:{}", e.getMessage()); | ||
return message; | ||
} | ||
} | ||
} |