Skip to content

Commit 254b441

Browse files
committed
原始JSON文本
1 parent 73dfd95 commit 254b441

File tree

9 files changed

+267
-127
lines changed

9 files changed

+267
-127
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package top.mcfpp.mni;
2+
3+
public class JsonTextConcreteData {
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package top.mcfpp.mni;
2+
3+
import top.mcfpp.annotations.MNIRegister;
4+
5+
public class JsonTextData {
6+
7+
8+
9+
}

src/main/java/top/mcfpp/mni/System.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static void print(@NotNull MCInt var) {
3333
//是确定的,直接输出数值
3434
Function.Companion.addCommand("tellraw @a " + varC.getValue());
3535
}else {
36-
Function.Companion.addCommand("tellraw @a " + new ScoreChatComponent(var).toJson());
36+
Function.Companion.addCommand("tellraw @a " + new ScoreChatComponent(var).toCommandPart());
3737
}
3838
}
3939

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

Lines changed: 0 additions & 99 deletions
This file was deleted.
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
package top.mcfpp.lang
2+
3+
import net.querz.nbt.tag.CompoundTag
4+
import top.mcfpp.command.Command
5+
import top.mcfpp.command.Commands
6+
import top.mcfpp.lang.type.MCFPPBaseType
7+
import top.mcfpp.lang.type.MCFPPClassType
8+
import top.mcfpp.lang.type.MCFPPNBTType
9+
import top.mcfpp.lang.type.MCFPPType
10+
import top.mcfpp.lang.value.MCFPPValue
11+
import top.mcfpp.lib.ChatComponent
12+
import top.mcfpp.model.CompoundData
13+
import top.mcfpp.model.FieldContainer
14+
import top.mcfpp.model.Member
15+
import top.mcfpp.model.function.Function
16+
import top.mcfpp.util.LogProcessor
17+
import java.util.*
18+
19+
/**
20+
* JsonText代表了Minecraft中的富文本格式,即文本组件,又叫原始JSON文本。在MCFPP中,使用类型jtext来定义。
21+
*
22+
* 原始JSON文本有多种格式,但是对于MCFPP的jtext,其原始JSON文本永远只会是列表形式。
23+
*
24+
* 对于非编译期的JSONText,其本质是一个和原始JSON文本格式一致的NBT列表数据结构。因此,可以使用整数索引访问原始JSON文本中的每一个部分。
25+
*
26+
* 访问所得的依然是一个jtext。
27+
*
28+
*/
29+
open class JsonText : NBTBasedData<CompoundTag> {
30+
31+
var isElement = false
32+
33+
constructor(
34+
curr: FieldContainer,
35+
identifier: String = UUID.randomUUID().toString()
36+
) : this(curr.prefix + identifier) {
37+
this.identifier = identifier
38+
}
39+
40+
/**
41+
* 创建一个int值。它的标识符和mc名相同。
42+
* @param identifier identifier
43+
*/
44+
constructor(identifier: String = UUID.randomUUID().toString()) : super(identifier)
45+
46+
/**
47+
* 复制一个int
48+
* @param b 被复制的int值
49+
*/
50+
constructor(b: JsonText) : super(b)
51+
52+
override fun assign(b: Var<*>): NBTBasedData<CompoundTag> {
53+
when(b){
54+
is JsonText -> assignCommand(b)
55+
else -> {
56+
LogProcessor.error("Cannot assign [${b::class.simpleName}] to [${this::class.simpleName}]")
57+
}
58+
}
59+
return this
60+
}
61+
62+
override fun cast(type: MCFPPType): Var<*> {
63+
return when(type){
64+
MCFPPBaseType.JText -> this
65+
MCFPPNBTType.NBT -> NBTBasedData(this)
66+
MCFPPBaseType.Any -> MCAnyConcrete(this)
67+
else -> {
68+
LogProcessor.error("Cannot cast [${this.type}] to [$type]")
69+
UnknownVar(this.identifier)
70+
}
71+
}
72+
}
73+
74+
override fun clone(): NBTBasedData<CompoundTag> {
75+
return JsonText(this)
76+
}
77+
78+
override fun getTempVar(): Var<*> {
79+
val temp = JsonText()
80+
temp.isTemp = true
81+
return temp.assignCommand(this)
82+
}
83+
84+
override fun getByIndex(index: Var<*>): NBTBasedData<*> {
85+
if(isElement){
86+
throw IllegalArgumentException("Cannot get index of jtext element")
87+
}
88+
return when(index){
89+
is MCInt -> getByIntIndex(index)
90+
else -> throw IllegalArgumentException("Invalid index type ${index.type}")
91+
}
92+
}
93+
94+
override fun getByIntIndex(index: MCInt): NBTBasedData<*> {
95+
val re = JsonText(this)
96+
re.nbtPath.intIndex(index)
97+
re.isElement = true
98+
return re
99+
}
100+
101+
override fun getMemberVar(key: String, accessModifier: Member.AccessModifier): Pair<Var<*>?, Boolean> {
102+
val v = data.getVar(key)
103+
if(!isElement) v?.nbtPath?.iteratorIndex()
104+
v?.nbtPath?.memberIndex(key)
105+
return v to true
106+
}
107+
108+
companion object {
109+
val data = CompoundData("JsonText","mcfpp.lang")
110+
111+
init {
112+
data.initialize()
113+
data.extends(NBTBasedData.data)
114+
115+
data.addMember(MCInt("color"))
116+
data.addMember(MCBool("bold"))
117+
data.addMember(MCBool("italic"))
118+
data.addMember(MCBool("underlined"))
119+
data.addMember(MCBool("strikethrough"))
120+
data.addMember(MCBool("obfuscated"))
121+
data.addMember(MCString("insertion"))
122+
123+
}
124+
}
125+
}
126+
127+
class JsonTextConcrete : MCFPPValue<ChatComponent>, JsonText {
128+
129+
override var value: ChatComponent
130+
131+
/**
132+
* 创建一个固定的int
133+
*
134+
* @param identifier 标识符
135+
* @param curr 域容器
136+
* @param value 值
137+
*/
138+
constructor(
139+
curr: FieldContainer,
140+
value: ChatComponent,
141+
identifier: String = UUID.randomUUID().toString()
142+
) : super(curr.prefix + identifier) {
143+
this.value = value
144+
}
145+
146+
/**
147+
* 创建一个固定的int。它的标识符和mc名一致/
148+
* @param identifier 标识符。如不指定,则为随机uuid
149+
* @param value 值
150+
*/
151+
constructor(value: ChatComponent, identifier: String = UUID.randomUUID().toString()) : super(identifier) {
152+
this.value = value
153+
}
154+
155+
constructor(jsonText: JsonText, value: ChatComponent) : super(jsonText){
156+
this.value = value
157+
}
158+
159+
constructor(int: JsonTextConcrete) : super(int){
160+
this.value = int.value
161+
}
162+
163+
override fun toDynamic(replace: Boolean): Var<*> {
164+
val parent = parent
165+
if (parent != null) {
166+
val cmd = when(parent){
167+
is ClassPointer -> {
168+
Commands.selectRun(parent)
169+
}
170+
is MCFPPClassType -> {
171+
arrayOf(Command.build("execute as ${parent.cls.uuid} run "))
172+
}
173+
else -> TODO()
174+
}
175+
if(cmd.size == 2){
176+
Function.addCommand(cmd[0])
177+
}
178+
Function.addCommand(cmd.last().build(
179+
"data modify entity @s data.${identifier} set value ")
180+
.build(value.toCommandPart())
181+
)
182+
} else {
183+
val cmd = Command.build("data modify")
184+
.build(nbtPath.toCommandPart())
185+
.build("set value ")
186+
.build(value.toCommandPart())
187+
Function.addCommand(cmd)
188+
}
189+
val re = NBTBasedData(this)
190+
if(replace){
191+
Function.currFunction.field.putVar(identifier, re, true)
192+
}
193+
return re
194+
}
195+
196+
companion object {
197+
val data = CompoundData("JsonTextConcrete","mcfpp.lang")
198+
199+
init {
200+
data.initialize()
201+
data.extends(JsonText.data)
202+
}
203+
}
204+
205+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ open class NBTBasedData<T : Tag<*>> : Var<T>, Indexable<NBTBasedData<*>>{
213213
return re
214214
}
215215

216-
protected fun getByIntIndex(index: MCInt): NBTBasedData<*> {
216+
open protected fun getByIntIndex(index: MCInt): NBTBasedData<*> {
217217
if(nbtType != NBTTypeWithTag.LIST && nbtType != NBTTypeWithTag.ANY){
218218
LogProcessor.error("Invalid nbt type")
219219
}

src/main/kotlin/top/mcfpp/lang/type/MCFPPBaseType.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ class MCFPPBaseType {
1616
override val typeName: kotlin.String
1717
get() = "string"
1818
}
19+
20+
object JText: MCFPPType(parentType = listOf(Any)){
21+
override val typeName: kotlin.String
22+
get() = "jtext"
23+
}
24+
25+
object JTextElement : MCFPPType(parentType = listOf(Any)){
26+
override val typeName: kotlin.String
27+
get() = "jtextE"
28+
}
29+
1930
object Float:MCFPPType(parentType = listOf(Any)){
2031
override val typeName: kotlin.String
2132
get() = "float"

0 commit comments

Comments
 (0)