|
2 | 2 |
|
3 | 3 | import com.lclc98.checklist.client.gui.element.TaskElement; |
4 | 4 | import net.minecraft.text.Text; |
5 | | -import net.minecraftforge.api.distmarker.Dist; |
6 | | -import net.minecraftforge.api.distmarker.OnlyIn; |
7 | 5 | import org.objectweb.asm.Opcodes; |
8 | | -import org.spongepowered.asm.mixin.Final; |
9 | | -import org.spongepowered.asm.mixin.Mixin; |
10 | | -import org.spongepowered.asm.mixin.Mutable; |
11 | | -import org.spongepowered.asm.mixin.Shadow; |
| 6 | +import org.spongepowered.asm.mixin.*; |
12 | 7 | import org.spongepowered.asm.mixin.injection.At; |
13 | 8 | import org.spongepowered.asm.mixin.injection.Redirect; |
14 | 9 |
|
15 | | -@OnlyIn(Dist.CLIENT) |
| 10 | +import java.util.regex.Matcher; |
| 11 | +import java.util.regex.Pattern; |
| 12 | + |
16 | 13 | @Mixin(TaskElement.class) |
17 | 14 | public class TaskElementMixin { |
18 | 15 | @Mutable |
19 | 16 | @Shadow(remap = false) @Final protected String text; |
20 | 17 |
|
21 | 18 | @Redirect(method = "<init>", at = @At(value = "FIELD", target = "Lcom/lclc98/checklist/client/gui/element/TaskElement;text:Ljava/lang/String;", opcode = Opcodes.PUTFIELD), remap = false) |
22 | 19 | private void injected(TaskElement instance, String value) { |
23 | | - this.text = Text.translatable(value).getString(); |
| 20 | + Pattern pattern = Pattern.compile("\\[(.*?)\\]"); |
| 21 | + Matcher matcher = pattern.matcher(value); |
| 22 | + |
| 23 | + if (text.isEmpty() | value.contains("===")) { |
| 24 | + this.text = value; |
| 25 | + } else if (matcher.find()) { |
| 26 | + this.text = Text.translatable(getLinkText(value, matcher)).getString(); |
| 27 | + } else { |
| 28 | + this.text = Text.translatable(value).getString(); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + @Unique |
| 33 | + private String getLinkText(String text, Matcher matcher) { |
| 34 | + if (matcher.find()) { |
| 35 | + // group(0) 是整个匹配的字符串,group(1) 是第一个括号内的内容 |
| 36 | + String content = matcher.group(1); |
| 37 | + System.out.println("Extracted content:" + content); |
| 38 | + return content; |
| 39 | + } else { |
| 40 | + System.out.println("No match found."); |
| 41 | + } |
| 42 | + return text; |
24 | 43 | } |
25 | 44 | } |
0 commit comments