Skip to content

Commit 73dfd95

Browse files
committed
文本组件
1 parent c6a87ae commit 73dfd95

File tree

2 files changed

+178
-40
lines changed

2 files changed

+178
-40
lines changed

src/main/kotlin/top/mcfpp/lang/NBTBasedData.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,15 @@ open class NBTBasedData<T : Tag<*>> : Var<T>, Indexable<NBTBasedData<*>>{
222222
return re
223223
}
224224

225+
fun toJson(): NBTBasedData<*> {
226+
when(nbtType){
227+
NBTTypeWithTag.INT_ARRAY -> {
228+
TODO()
229+
}
230+
else -> TODO()
231+
}
232+
}
233+
225234
//TODO 逻辑待优化。这里的处理不是很优雅
226235
companion object {
227236

@@ -302,7 +311,6 @@ open class NBTBasedData<T : Tag<*>> : Var<T>, Indexable<NBTBasedData<*>>{
302311
return MCFPPCompoundType(t.getMCFPPType())
303312
}
304313

305-
306314
val data = CompoundData("nbt","mcfpp")
307315

308316
enum class NBTTypeWithTag(val type: NBTType){
Lines changed: 169 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,222 @@
11
package top.mcfpp.lib
22

3+
import net.querz.nbt.tag.IntArrayTag
4+
import top.mcfpp.command.Command
35
import top.mcfpp.lang.MCInt
46
import top.mcfpp.lang.NBTBasedData
7+
import top.mcfpp.lang.NBTBasedDataConcrete
8+
import top.mcfpp.lang.resource.EntityTypeConcrete
9+
import top.mcfpp.util.LogProcessor
510

6-
interface ChatComponent {
11+
abstract class ChatComponent {
12+
13+
val styles : ArrayList<ChatComponentStyle> = ArrayList()
714

815
/**
916
* 返回这个原始json文本的字符串形式
1017
*
1118
* @return
1219
*/
13-
fun toJson(): String
20+
abstract fun toJson(): Command
21+
22+
fun styleToString(): Command{
23+
val str = Command("{")
24+
for (style in styles){
25+
str.build(style.toJson(), false)
26+
if(style != styles.last()) str.build(", ", false)
27+
}
28+
str.build("}", false)
29+
return str
30+
}
1431
}
1532

1633
/**
1734
* 一个json原始文本
1835
*
1936
*/
20-
class ListChatComponent: ChatComponent {
37+
class ListChatComponent: ChatComponent() {
2138

2239
/**
2340
* 这个原始json文本中含有的聊天组件
2441
*/
2542
val components = ArrayList<ChatComponent>()
2643

27-
override fun toJson(): String {
28-
val str = StringBuilder("[")
44+
override fun toJson(): Command {
45+
val str = Command("[")
2946
for (component in components){
30-
str.append(component.toJson())
31-
str.append(",")
47+
str.build(component.toJson(), false)
48+
if(component != components.last()) str.build(",", false)
3249
}
33-
str.replace(str.length-1,str.length,"]")
34-
return str.toString()
50+
str.build("]", false)
51+
return str
3552
}
3653
}
3754

38-
class PlainChatComponent(val value: String) : ChatComponent {
39-
override fun toJson(): String {
40-
return """{"type": "text", "text": "$value"}"""
55+
class PlainChatComponent(val value: String) : ChatComponent() {
56+
override fun toJson(): Command {
57+
return Command("""{"type": "text", "text": "$value", ${styleToString()}""")
4158
}
4259

4360
}
4461

45-
class TranslatableChatComponent(val key: String, val fallback: String? = null, val args: List<ChatComponent>? = null) :
46-
ChatComponent {
47-
override fun toJson(): String {
48-
val str = StringBuilder("{\"type\": \"translatable\", \"translate\":\"$key\"")
62+
class TranslatableChatComponent(val key: String, val fallback: String? = null, val args: List<ChatComponent>? = null) : ChatComponent() {
63+
override fun toJson(): Command {
64+
val str = Command.build("{\"type\": \"translatable\", \"translate\":\"$key\"")
4965
if(fallback != null){
50-
str.append(",\"fallback\":\"$fallback\"")
66+
str.build(",\"fallback\":\"$fallback\"", false)
5167
}
5268
if(args != null){
53-
str.append(",\"with\":[")
69+
str.build(",\"with\":[", false)
5470
for (component in args){
55-
str.append(component.toJson())
56-
str.append(",")
71+
str.build(component.toJson(), false)
72+
if(component != args.last()) str.build(",", false)
5773
}
58-
str.replace(str.length-1,str.length,"]")
74+
str.build("]", false)
5975
}
60-
str.append("}")
61-
return str.toString()
76+
str.build(", ${styleToString()}", false)
77+
return str
6278
}
6379
}
6480

65-
class ScoreChatComponent(val value: MCInt) : ChatComponent {
66-
override fun toJson(): String {
67-
return "{\"type\":\"score\",\"score\":{\"name\":\"${value.name}\",\"objective\":\"${value.sbObject.name}\"}}"
81+
class ScoreChatComponent(val value: MCInt) : ChatComponent() {
82+
override fun toJson(): Command {
83+
return Command("{\"type\":\"score\",\"score\":{\"name\":\"${value.name}\",\"objective\":\"${value.sbObject.name}\"}, ${styleToString()}}")
6884
}
6985
}
7086

71-
class SelectorChatComponent(val selector: String, val separator: ChatComponent? = null) : ChatComponent {
72-
override fun toJson(): String {
73-
return if(separator != null){
74-
"{\"type\":\"selector\",\"selector\":\"$selector\",\"separator\":${separator.toJson()}}"
87+
class SelectorChatComponent(val selector: String, val separator: ChatComponent? = null) : ChatComponent() {
88+
override fun toJson(): Command {
89+
return Command(if(separator != null){
90+
"{\"type\":\"selector\",\"selector\":\"$selector\",\"separator\":${separator.toJson()}, ${styleToString()}"
7591
}else{
76-
"{\"type\":\"selector\",\"selector\":\"$selector\"}"
77-
}
92+
"{\"type\":\"selector\",\"selector\":\"$selector\", ${styleToString()}"
93+
})
94+
}
95+
}
96+
97+
class KeybindChatComponent(val key: String) : ChatComponent() {
98+
override fun toJson(): Command {
99+
return Command("{\"type\":\"keybind\",\"keybind\":\"$key\", ${styleToString()}")
100+
}
101+
}
102+
103+
class NBTChatComponent(val nbt: NBTBasedData<*>, val interpret: Boolean = false, val separator: ChatComponent? = null) : ChatComponent() {
104+
override fun toJson(): Command {
105+
return Command("{\"type\":\"nbt\",\"nbt\":\"$nbt\",\"interpret\":$interpret, ${styleToString()}")
106+
}
107+
}
108+
109+
interface ChatComponentStyle{
110+
fun toJson(): Command
111+
}
112+
113+
class ColorStyle(val hex: Int): ChatComponentStyle{
114+
override fun toJson(): Command {
115+
return Command("\"color\": \"#$hex\"")
116+
}
117+
118+
companion object{
119+
val BLACK = ColorStyle(0x000000)
120+
val DARK_BLUE = ColorStyle(0x0000AA)
121+
val DARK_GREEN = ColorStyle(0x00AA00)
122+
val DARK_AQUA = ColorStyle(0x00AAAA)
123+
val DARK_RED = ColorStyle(0xAA0000)
124+
val DARK_PURPLE = ColorStyle(0xAA00AA)
125+
val GOLD = ColorStyle(0xFFAA00)
126+
val GRAY = ColorStyle(0xAAAAAA)
127+
val DARK_GRAY = ColorStyle(0x555555)
128+
val BLUE = ColorStyle(0x5555FF)
129+
val GREEN = ColorStyle(0x55FF55)
130+
val AQUA = ColorStyle(0x55FFFF)
131+
val RED = ColorStyle(0xFF5555)
132+
val LIGHT_PURPLE = ColorStyle(0xFF55FF)
133+
val YELLOW = ColorStyle(0xFFFF55)
134+
val WHITE = ColorStyle(0xFFFFFF)
135+
}
136+
}
137+
138+
class BoldStyle(val bold: Boolean): ChatComponentStyle{
139+
override fun toJson(): Command {
140+
return Command("\"bold\": $bold")
141+
}
142+
}
143+
144+
class ItalicStyle(val italic: Boolean): ChatComponentStyle{
145+
override fun toJson(): Command {
146+
return Command("\"italic\": $italic")
78147
}
79148
}
80149

81-
class KeybindChatComponent(val key: String) : ChatComponent {
82-
override fun toJson(): String {
83-
return "{\"type\":\"keybind\",\"keybind\":\"$key\"}"
150+
class UnderlineStyle(val underline: Boolean): ChatComponentStyle{
151+
override fun toJson(): Command {
152+
return Command("\"underline\": $underline")
84153
}
85154
}
86155

87-
class NBTChatComponent(val nbt: NBTBasedData<*>, val interpret: Boolean = false, val separator: ChatComponent? = null) :
88-
ChatComponent {
89-
override fun toJson(): String {
90-
return "{\"type\":\"nbt\",\"nbt\":\"$nbt\",\"interpret\":$interpret}"
156+
class StrikethroughStyle(val strikethrough: Boolean): ChatComponentStyle{
157+
override fun toJson(): Command {
158+
return Command("\"strikethrough\": $strikethrough")
159+
}
160+
}
161+
162+
class ObfuscatedStyle(val obfuscated: Boolean): ChatComponentStyle{
163+
override fun toJson(): Command {
164+
return Command("\"obfuscated\": $obfuscated")
165+
}
166+
}
167+
168+
class InsertionStyle(val string: String): ChatComponentStyle{
169+
override fun toJson(): Command {
170+
return Command("\"insertion\": $string")
171+
}
172+
}
173+
174+
class ClickEventStyle(val action: ClickEventAction, val value: String): ChatComponentStyle{
175+
override fun toJson(): Command {
176+
return Command("\"clickEvent\": {\"action\": \"${action.name.lowercase()}\", \"value\": \"$value\"}")
177+
}
178+
179+
enum class ClickEventAction{
180+
OPEN_URL,
181+
OPEN_FILE,
182+
RUN_COMMAND,
183+
SUGGEST_COMMAND,
184+
CHANGE_PAGE
185+
}
186+
}
187+
188+
class HoverEventShowTextStyle(val content: ChatComponent): ChatComponentStyle{
189+
override fun toJson(): Command {
190+
return Command("\"hoverEvent\": {\"action\": \"show_text\", \"value\": \"$content\"}")
191+
}
192+
}
193+
194+
//class HoverEventShowItemStyle(val item: String): ChatComponentStyle{
195+
// override fun toJson(): Command {
196+
// return Command("\"hoverEvent\": {\"action\": \"show_item\", \"value\": \"$item\"}")
197+
// }
198+
//}
199+
200+
class HoverEventShowEntityStyle(val name: ChatComponent?, val type: EntityTypeConcrete, val uuid: NBTBasedData<*>): ChatComponentStyle{
201+
override fun toJson(): Command {
202+
val c = Command("\"hoverEvent\":{\"action\": \"show_entity\", \"contents\": {")
203+
if(name != null){
204+
c.build("\"name\": \"$name\", ", false)
205+
}
206+
c.build("\"type\": \"${type.value}\"", false)
207+
if(uuid is NBTBasedDataConcrete<*>){
208+
if(uuid.value is IntArrayTag && (uuid.value as IntArrayTag).value.size == 4){
209+
c.build("\"id\": \"${uuid.value}\"", false)
210+
}else{
211+
LogProcessor.error("Invalid UUID: $uuid")
212+
}
213+
}else{
214+
if(uuid.nbtType == NBTBasedData.Companion.NBTTypeWithTag.INT_ARRAY){
215+
c.build("\"id\": ", false).buildMacro(uuid.toJson().identifier, false)
216+
}
217+
c.build("\"id\": ").buildMacro(uuid.identifier, false)
218+
}
219+
c.build("}}")
220+
return c
91221
}
92222
}

0 commit comments

Comments
 (0)