1
1
package top.mcfpp.lib
2
2
3
+ import net.querz.nbt.tag.IntArrayTag
4
+ import top.mcfpp.command.Command
3
5
import top.mcfpp.lang.MCInt
4
6
import top.mcfpp.lang.NBTBasedData
7
+ import top.mcfpp.lang.NBTBasedDataConcrete
8
+ import top.mcfpp.lang.resource.EntityTypeConcrete
9
+ import top.mcfpp.util.LogProcessor
5
10
6
- interface ChatComponent {
11
+ abstract class ChatComponent {
12
+
13
+ val styles : ArrayList <ChatComponentStyle > = ArrayList ()
7
14
8
15
/* *
9
16
* 返回这个原始json文本的字符串形式
10
17
*
11
18
* @return
12
19
*/
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
+ }
14
31
}
15
32
16
33
/* *
17
34
* 一个json原始文本
18
35
*
19
36
*/
20
- class ListChatComponent : ChatComponent {
37
+ class ListChatComponent : ChatComponent () {
21
38
22
39
/* *
23
40
* 这个原始json文本中含有的聊天组件
24
41
*/
25
42
val components = ArrayList <ChatComponent >()
26
43
27
- override fun toJson (): String {
28
- val str = StringBuilder (" [" )
44
+ override fun toJson (): Command {
45
+ val str = Command (" [" )
29
46
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 )
32
49
}
33
- str.replace(str.length - 1 ,str.length, " ]" )
34
- return str.toString()
50
+ str.build( " ]" , false )
51
+ return str
35
52
}
36
53
}
37
54
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() }""" )
41
58
}
42
59
43
60
}
44
61
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 \" " )
49
65
if (fallback != null ){
50
- str.append (" ,\" fallback\" :\" $fallback \" " )
66
+ str.build (" ,\" fallback\" :\" $fallback \" " , false )
51
67
}
52
68
if (args != null ){
53
- str.append (" ,\" with\" :[" )
69
+ str.build (" ,\" with\" :[" , false )
54
70
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 )
57
73
}
58
- str.replace(str.length - 1 ,str.length, " ]" )
74
+ str.build( " ]" , false )
59
75
}
60
- str.append( " } " )
61
- return str.toString()
76
+ str.build( " , ${styleToString()} " , false )
77
+ return str
62
78
}
63
79
}
64
80
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()} } " )
68
84
}
69
85
}
70
86
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() }"
75
91
}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 " )
78
147
}
79
148
}
80
149
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 " )
84
153
}
85
154
}
86
155
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
91
221
}
92
222
}
0 commit comments