Skip to content

Commit 6a95bfd

Browse files
committed
修复了数据模板变量不能被正常识别为concrete,支持非Concrete的nbt数据输出
1 parent 058ca0a commit 6a95bfd

14 files changed

+117
-50
lines changed

.mclib

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"namespaces":[
3+
{
4+
"id":"default",
5+
"functions":[
6+
7+
],
8+
"classes":[
9+
10+
],
11+
"template":[
12+
13+
],
14+
"enum":[
15+
16+
]
17+
},
18+
{
19+
"id":"test",
20+
"functions":[
21+
{
22+
"id":"main",
23+
"normalParams":[
24+
25+
],
26+
"returnType":"void",
27+
"isAbstract":false,
28+
"tags":[
29+
30+
]
31+
}
32+
],
33+
"classes":[
34+
35+
],
36+
"template":[
37+
{
38+
"id":"Test",
39+
"parents":[
40+
"mcfpp.lang:DataObject"
41+
],
42+
"field":{
43+
"vars":[
44+
{
45+
"id":"a",
46+
"type":"int"
47+
},
48+
{
49+
"id":"b",
50+
"type":"int"
51+
},
52+
{
53+
"id":"c",
54+
"type":"int"
55+
},
56+
{
57+
"id":"d",
58+
"type":"int"
59+
}
60+
],
61+
"functions":[
62+
63+
]
64+
}
65+
}
66+
],
67+
"enum":[
68+
69+
]
70+
}
71+
]
72+
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package top.mcfpp.mni;
22

3+
import net.querz.nbt.io.SNBTUtil;
34
import org.jetbrains.annotations.NotNull;
45
import top.mcfpp.annotations.MNIRegister;
56
import top.mcfpp.lang.DataTemplateObject;
@@ -10,13 +11,15 @@
1011
import top.mcfpp.lib.PlainChatComponent;
1112
import top.mcfpp.util.ValueWrapper;
1213

14+
import java.io.IOException;
15+
1316
public class DataObjectData {
1417

1518
@MNIRegister(caller = "DataObject", returnType = "text", override = true)
16-
public static void toText(@NotNull DataTemplateObject caller, ValueWrapper<JsonTextConcrete> returnValue){
19+
public static void toText(@NotNull DataTemplateObject caller, ValueWrapper<JsonTextConcrete> returnValue) throws IOException {
1720
var l = new ListChatComponent();
1821
if(caller instanceof DataTemplateObjectConcrete callerC){
19-
l.getComponents().add(new PlainChatComponent(callerC.getValue().valueToString()));
22+
l.getComponents().add(new PlainChatComponent(SNBTUtil.toSNBT(callerC.getValue())));
2023
}else {
2124
l.getComponents().add(new NBTChatComponent(caller.toNBTVar(), false, null));
2225
}

src/main/kotlin/top/mcfpp/antlr/McfppFieldVisitor.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,6 @@ open class McfppFieldVisitor : mcfppParserBaseVisitor<Any?>() {
792792
val `var` = Var.build(ctx.Identifier().text, MCFPPType.parseFromContext(ctx.type(), typeScope))
793793
//是否是静态的
794794
`var`.isStatic = isStatic
795-
//只有可能是结构体成员
796795
if (DataTemplate.currTemplate!!.field.containVar(ctx.Identifier().text)
797796
) {
798797
LogProcessor.error("Duplicate defined variable name:" + ctx.Identifier().text)

src/main/kotlin/top/mcfpp/antlr/McfppImVisitor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
164164
if (Function.field.containVar(c.Identifier().text)) {
165165
LogProcessor.error("Duplicate defined variable name:" + c.Identifier().text)
166166
}
167+
Function.field.putVar(`var`.identifier, `var`, true)
167168
Function.addComment("field: " + ctx.type().text + " " + c.Identifier().text + if (c.expression() != null) " = " + c.expression().text else "")
168169
//变量初始化
169170
if (c.expression() != null) {
@@ -175,7 +176,6 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
175176
throw VariableConverseException(e)
176177
}
177178
}
178-
Function.field.putVar(`var`.identifier, `var`, true)
179179
when(fieldModifier){
180180
"const" -> {
181181
if(!`var`.hasAssigned){

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

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import net.querz.nbt.io.SNBTUtil
44
import net.querz.nbt.tag.CompoundTag
55
import top.mcfpp.command.Command
66
import top.mcfpp.command.Commands
7-
import top.mcfpp.exception.VariableConverseException
87
import top.mcfpp.lang.type.MCFPPDataTemplateType
98
import top.mcfpp.lang.type.MCFPPClassType
109
import top.mcfpp.lang.type.MCFPPNBTType
@@ -52,7 +51,6 @@ open class DataTemplateObject : Var<DataTemplateObject> {
5251
instanceField = templateObject.instanceField
5352
}
5453

55-
5654
override fun doAssign(b: Var<*>): DataTemplateObject {
5755
when (b) {
5856
is NBTBasedDataConcrete<*> -> {
@@ -70,7 +68,7 @@ open class DataTemplateObject : Var<DataTemplateObject> {
7068
}
7169

7270
is DataTemplateObjectConcrete -> {
73-
if (templateType.checkCompoundStruct(b.value)) {
71+
if (b.type.compoundData.canCastTo(this.templateType)) {
7472
this.assignMembers(b.value)
7573
return this
7674
} else {
@@ -207,17 +205,15 @@ open class DataTemplateObject : Var<DataTemplateObject> {
207205
if(this is DataTemplateObjectConcrete) return true
208206
var re = true
209207
instanceField.forEachVar {
210-
run@{
211-
if(it is DataTemplateObject){
212-
if(!it.isConcrete()) {
213-
re = false
214-
return@run
215-
}
216-
}else{
217-
if (it !is MCFPPValue<*>) {
218-
re = false
219-
return@run
220-
}
208+
if(it is DataTemplateObject){
209+
if(!it.isConcrete()) {
210+
re = false
211+
return@forEachVar
212+
}
213+
}else{
214+
if (it !is MCFPPValue<*>) {
215+
re = false
216+
return@forEachVar
221217
}
222218
}
223219
}
@@ -236,13 +232,6 @@ open class DataTemplateObject : Var<DataTemplateObject> {
236232
}
237233
return DataTemplateObjectConcrete(this, compoundTag)
238234
}
239-
240-
companion object{
241-
242-
243-
244-
}
245-
246235
}
247236

248237
class DataTemplateObjectConcrete: DataTemplateObject, MCFPPValue<CompoundTag>{

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,13 @@ class NBTBasedDataConcrete<T: Tag<*>> : NBTBasedData<T>, MCFPPValue<T> {
450450
}
451451

452452
override fun implicitCast(type: MCFPPType): Var<*> {
453+
if(type is MCFPPDataTemplateType && value is CompoundTag){
454+
return if(type.template.checkCompoundStruct(value as CompoundTag)){
455+
DataTemplateObjectConcrete(type.template, value as CompoundTag)
456+
}else{
457+
buildCastErrorVar(type)
458+
}
459+
}
453460
val t = JavaVar.javaToMC(value.toJava())
454461
if(t.type == type) return t
455462
return buildCastErrorVar(type)

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serial
8080
/**
8181
* 访问修饰符
8282
*/
83-
override var accessModifier: Member.AccessModifier = Member.AccessModifier.PRIVATE
83+
override var accessModifier: Member.AccessModifier = Member.AccessModifier.PUBLIC
8484

8585
/**
8686
* 变量的类型
@@ -159,7 +159,6 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serial
159159
}
160160
hasAssigned = true
161161
val re = doAssign(v)
162-
parent?.onMemberChanged(re)
163162
return re
164163
}
165164

@@ -438,6 +437,7 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serial
438437
if(other !is Var<*>) return false
439438
if(this.parent != other.parent) return false
440439
if(this.name != other.name) return false
440+
if(this is MCFPPValue<*> != other is MCFPPValue<*>) return false
441441
return true
442442
}
443443

@@ -455,16 +455,14 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serial
455455
fun replacedBy(v : Var<*>){
456456
if(v == this) return
457457
if(parent == null){
458-
Function.currFunction.field.putVar(v.identifier, v, true)
458+
Function.currFunction.field.putVar(identifier, v, true)
459459
}else{
460460
v.parent = this.parent
461-
//TODO
462461
when (val parent = parent){
463462
is ClassPointer -> {
464-
parent.clazz.field.putVar(v.identifier, v, true)
463+
//TODO 类暂不支持替换
465464
}
466465
is MCFPPTypeVar -> {
467-
TODO()
468466
when(val type = parent.type){
469467
is MCFPPClassType ->{
470468
type.cls.field.putVar(v.identifier, v, true)
@@ -476,10 +474,11 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serial
476474
}
477475
}
478476
is DataTemplateObject -> {
479-
parent.instanceField.putVar(v.identifier, v, true)
477+
parent.instanceField.putVar(identifier, v, true)
480478
}
481479
else -> {}
482480
}
481+
parent?.onMemberChanged(v)
483482
}
484483
}
485484

src/main/kotlin/top/mcfpp/lib/ChatComponent.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class KeybindChatComponent(val key: String) : ChatComponent() {
101101

102102
class NBTChatComponent(val nbt: NBTBasedData<*>, val interpret: Boolean = false, val separator: ChatComponent? = null) : ChatComponent() {
103103
override fun toCommandPart(): Command {
104-
return Command("{\"type\":\"nbt\",\"nbt\":\"$nbt\",\"interpret\":$interpret, ${styleToString()}")
104+
return Command("{\"type\":\"nbt\",\"nbt\":\"").build(nbt.nbtPath.toCommandPart()).build("\",\"interpret\":$interpret, ${styleToString()}")
105105
}
106106
}
107107

src/main/kotlin/top/mcfpp/model/field/CompoundDataField.kt

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import top.mcfpp.model.*
88
import top.mcfpp.model.function.Function
99
import top.mcfpp.model.function.UnknownFunction
1010
import top.mcfpp.model.generic.Generic
11-
import top.mcfpp.util.LazyWrapper
1211
import java.util.HashMap
1312

1413
/**
@@ -20,7 +19,7 @@ class CompoundDataField : IFieldWithFunction, IFieldWithVar, IFieldWithType {
2019
/**
2120
* 字段
2221
*/
23-
private val vars: HashMap<String, LazyWrapper<Var<*>>> = HashMap()
22+
private val vars: HashMap<String, Var<*>> = HashMap()
2423

2524
/**
2625
* 类型
@@ -33,10 +32,9 @@ class CompoundDataField : IFieldWithFunction, IFieldWithVar, IFieldWithType {
3332
* @param action 要对字段执行的操作
3433
* @receiver
3534
*/
36-
override fun forEachVar(action: (Var<*>) -> Any?){
35+
override fun forEachVar(action: (Var<*>) -> Unit){
3736
for (`var` in vars.values){
38-
//TODO 未检查获取变量的情况
39-
action(`var`.get()!!)
37+
action(`var`)
4038
}
4139
}
4240

@@ -87,8 +85,8 @@ class CompoundDataField : IFieldWithFunction, IFieldWithVar, IFieldWithType {
8785
parent = field.parent
8886
//变量复制
8987
for (key in field.vars.keys) {
90-
val `var`: LazyWrapper<Var<*>>? = field.vars[key]
91-
vars[key] = `var`!!.clone()
88+
val `var`: Var<*> = field.vars[key]!!
89+
vars[key] = `var`.clone()
9290
}
9391
//函数
9492
functions.addAll(field.functions)
@@ -99,27 +97,27 @@ class CompoundDataField : IFieldWithFunction, IFieldWithVar, IFieldWithType {
9997
//region Var<*>
10098
override fun putVar(key: String, `var`: Var<*>, forced: Boolean): Boolean {
10199
if(forced){
102-
vars[key] = LazyWrapper(`var`)
100+
vars[key] = `var`
103101
return true
104102
}
105103
return if (vars.containsKey(key)) {
106104
false
107105
} else {
108-
vars[key] = LazyWrapper(`var`)
106+
vars[key] = `var`
109107
true
110108
}
111109
}
112110

113111
override fun getVar(key: String): Var<*>? {
114-
return vars.getOrDefault(key, null)?.get()
112+
return vars.getOrDefault(key, null)
115113
}
116114

117115

118116
override val allVars: Collection<Var<*>>
119117
get() {
120118
val vs = ArrayList<Var<*>>()
121119
for (lv in vars.values){
122-
vs.add(lv.get()!!)
120+
vs.add(lv)
123121
}
124122
return vs
125123
}
@@ -129,7 +127,7 @@ class CompoundDataField : IFieldWithFunction, IFieldWithVar, IFieldWithType {
129127
}
130128

131129
override fun removeVar(id : String): Var<*>?{
132-
return vars.remove(id)?.get()
130+
return vars.remove(id)
133131
}
134132

135133
//endregion

src/main/kotlin/top/mcfpp/model/field/FunctionField.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ open class FunctionField : IFieldWithVar, IFieldWithType {
113113
return vars.remove(id)
114114
}
115115

116-
override fun forEachVar(action: (Var<*>) -> Any?) {
116+
override fun forEachVar(action: (Var<*>) -> Unit) {
117117
for (v in vars.values){
118118
action(v)
119119
}

0 commit comments

Comments
 (0)