Skip to content

Commit 97b7c29

Browse files
committed
[2.3.1] Refactor & Fix
1 parent 08c47f5 commit 97b7c29

File tree

35 files changed

+2134
-39
lines changed

35 files changed

+2134
-39
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ subprojects {
2727
"database-player"
2828
)
2929
install(
30-
"minecraft-chat",
30+
// "minecraft-chat",
3131
"minecraft-command-helper",
3232
"minecraft-i18n",
3333
"minecraft-kether",

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group=me.arasple.mc.trchat
2-
version=2.3.0
2+
version=2.3.1
33
kotlin.incremental=true
44
kotlin.incremental.java=true
55
kotlin.incremental.useClasspathSnapshot=true

project/common/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
dependencies {
2+
compileOnly(project(":project:module-chat"))
23
compileOnly("com.eatthepath:fast-uuid:0.2.0")
34
}
45

project/module-adventure/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
dependencies {
22
compileOnly(project(":project:common"))
3-
compileOnly(project(":project:module-nms"))
3+
compileOnly(project(":project:module-chat"))
44
compileOnly("net.kyori:adventure-platform-bukkit:4.4.0")
5+
// paper native
56
compileOnly(fileTree(rootDir.resolve("libs")))
67
}
78

project/module-adventure/src/main/kotlin/me/arasple/mc/trchat/module/adventure/Adventure.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
55
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
66
import org.bukkit.inventory.ItemStack
77
import taboolib.module.chat.ComponentText
8-
import taboolib.module.chat.Components
8+
import taboolib.module.chat.impl.AdventureComponent
99

1010
private val legacySerializer: Any? = try {
1111
LegacyComponentSerializer.legacySection()
@@ -23,10 +23,13 @@ fun gson(component: Component) = (gsonSerializer as GsonComponentSerializer).ser
2323

2424
fun gson(string: String) = (gsonSerializer as GsonComponentSerializer).deserialize(string)
2525

26-
fun Component.toNative() = Components.parseRaw(gson(this))
27-
28-
fun ComponentText.toAdventure() = gson(toRawMessage())
26+
fun ComponentText.toAdventure(): Component {
27+
if (this is AdventureComponent) return this.component
28+
else return gson(this.toRawMessage())
29+
}
2930

3031
fun ComponentText.hoverItemAdventure(item: ItemStack): ComponentText {
31-
return toAdventure().hoverEvent(item.asHoverEvent()).toNative()
32+
this as? AdventureComponent ?: error("Unsupported component type.")
33+
this.latest.hoverEvent(item.asHoverEvent())
34+
return this
3235
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
dependencies {
2+
compileOnly("com.google.code.gson:gson:2.8.7")
3+
compileOnly("net.md-5:bungeecord-api:1.21-R0.3")
4+
compileOnly("net.kyori:adventure-platform-bukkit:4.4.0")
5+
compileOnly("net.kyori:adventure-text-serializer-plain:4.21.0")
6+
}
7+
8+
taboolib { subproject = true }
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package taboolib.module.chat;
2+
3+
import net.md_5.bungee.api.ChatColor;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
import java.awt.*;
7+
import java.util.Optional;
8+
9+
/**
10+
* @author sky
11+
* @since 2021/1/18 2:02 下午
12+
*/
13+
public class HexColor {
14+
15+
private static boolean isLegacy = false;
16+
17+
static {
18+
try {
19+
ChatColor.of(Color.BLACK);
20+
} catch (NoSuchMethodError ignored) {
21+
isLegacy = true;
22+
}
23+
}
24+
25+
/**
26+
* 对字符串中的特殊颜色表达式进行转换<br>
27+
* 可供转换的格式有:
28+
* <p>
29+
* &amp;{255-255-255} —— RGB 代码
30+
* <p>
31+
* &amp;{255,255,255} —— RGB 代码
32+
* <p>
33+
* &amp;{#FFFFFF} —— HEX 代码
34+
* <p>
35+
* &amp;{BLUE} —— 已知颜色(英文)
36+
* <p>
37+
* &amp;{蓝} —— 已知颜色(中文)
38+
*
39+
* @param in 字符串
40+
* @return String
41+
*/
42+
@SuppressWarnings("CallToPrintStackTrace")
43+
@NotNull
44+
public static String translate(String in) {
45+
if (isLegacy) {
46+
return ChatColor.translateAlternateColorCodes('&', in);
47+
}
48+
StringBuilder builder = new StringBuilder();
49+
char[] chars = in.toCharArray();
50+
for (int i = 0; i < chars.length; i++) {
51+
if (i + 1 < chars.length && chars[i] == '&' && chars[i + 1] == '{') {
52+
ChatColor chatColor = null;
53+
char[] match = new char[0];
54+
for (int j = i + 2; j < chars.length && chars[j] != '}'; j++) {
55+
match = arrayAppend(match, chars[j]);
56+
}
57+
if (match.length == 11 && (match[3] == ',' || match[3] == '-') && (match[7] == ',' || match[7] == '-')) {
58+
chatColor = ChatColor.of(new Color(toInt(match, 0, 3), toInt(match, 4, 7), toInt(match, 8, 11)));
59+
} else if (match.length == 7 && match[0] == '#') {
60+
try {
61+
chatColor = ChatColor.of(toString(match));
62+
} catch (IllegalArgumentException ignored) {
63+
}
64+
} else {
65+
Optional<StandardColors> knownColor = StandardColors.match(toString(match));
66+
if (knownColor.isPresent()) {
67+
chatColor = knownColor.get().toChatColor();
68+
}
69+
}
70+
if (chatColor != null) {
71+
builder.append(chatColor);
72+
i += match.length + 2;
73+
}
74+
} else {
75+
builder.append(chars[i]);
76+
}
77+
}
78+
String colorString = builder.toString();
79+
// 1.20.4 不再支持该写法,该模块无法判断版本,因此全部替换为白色
80+
// 若需要恢复默认色请使用 SimpleComponent 中的 reset 属性
81+
colorString = colorString.replace("&r", "&f").replace("§r", "§f");
82+
return ChatColor.translateAlternateColorCodes('&', colorString);
83+
}
84+
85+
public static String getColorCode(int color) {
86+
return ChatColor.of(new Color(color)).toString();
87+
}
88+
89+
private static char[] arrayAppend(char[] chars, char in) {
90+
char[] newChars = new char[chars.length + 1];
91+
System.arraycopy(chars, 0, newChars, 0, chars.length);
92+
newChars[chars.length] = in;
93+
return newChars;
94+
}
95+
96+
private static String toString(char[] chars) {
97+
StringBuilder builder = new StringBuilder();
98+
for (char c : chars) {
99+
builder.append(c);
100+
}
101+
return builder.toString();
102+
}
103+
104+
private static int toInt(char[] chars, int start, int end) {
105+
StringBuilder builder = new StringBuilder();
106+
for (int i = start; i < end; i++) {
107+
builder.append(chars[i]);
108+
}
109+
return Integer.parseInt(builder.toString());
110+
}
111+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package taboolib.module.chat;
2+
3+
import net.md_5.bungee.api.ChatColor;
4+
5+
import java.util.Optional;
6+
7+
/**
8+
* @author sky
9+
* @since 2021/1/18 2:02 下午
10+
*/
11+
public enum StandardColors {
12+
13+
BLACK("黑", ChatColor.BLACK),
14+
15+
DARK_BLUE("深蓝", ChatColor.DARK_BLUE),
16+
17+
DARK_GREEN("深绿", ChatColor.DARK_GREEN),
18+
19+
DARK_AQUA("深青", ChatColor.DARK_AQUA),
20+
21+
DARK_RED("深红", ChatColor.DARK_RED),
22+
23+
DARK_PURPLE("深紫", ChatColor.DARK_PURPLE),
24+
25+
GOLD("金", ChatColor.GOLD),
26+
27+
GRAY("灰", ChatColor.GRAY),
28+
29+
DARK_GRAY("深灰", ChatColor.DARK_GRAY),
30+
31+
BLUE("蓝", ChatColor.BLUE),
32+
33+
GREEN("绿", ChatColor.GREEN),
34+
35+
AQUA("青", ChatColor.AQUA),
36+
37+
RED("红", ChatColor.RED),
38+
39+
LIGHT_PURPLE("浅紫", ChatColor.LIGHT_PURPLE),
40+
41+
YELLOW("黄", ChatColor.YELLOW),
42+
43+
WHITE("白", ChatColor.WHITE),
44+
45+
RESET("重置", ChatColor.RESET);
46+
47+
final String display;
48+
final ChatColor chatColor;
49+
50+
StandardColors(String display, ChatColor chatColor) {
51+
this.display = display;
52+
this.chatColor = chatColor;
53+
}
54+
55+
public String getDisplay() {
56+
return display;
57+
}
58+
59+
public ChatColor toChatColor() {
60+
return chatColor;
61+
}
62+
63+
public static Optional<StandardColors> match(String in) {
64+
for (StandardColors knownColor : values()) {
65+
if (knownColor.name().equalsIgnoreCase(in) || knownColor.display.equalsIgnoreCase(in)) {
66+
return Optional.of(knownColor);
67+
}
68+
}
69+
return Optional.empty();
70+
}
71+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package taboolib.module.chat
2+
3+
/**
4+
* TabooLib
5+
* taboolib.module.chat.ClickAction
6+
*
7+
* @author 坏黑
8+
* @since 2023/2/9 20:32
9+
*/
10+
enum class ClickAction {
11+
12+
OPEN_URL, OPEN_FILE, RUN_COMMAND, SUGGEST_COMMAND, CHANGE_PAGE, COPY_TO_CLIPBOARD, INSERTION
13+
}

0 commit comments

Comments
 (0)