Skip to content

Commit 058ca0a

Browse files
committed
将onAssign重命名为doAssign,删除了doAssign中的冗余转换
1 parent 542ecc2 commit 058ca0a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+221
-345
lines changed

.mclib

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +0,0 @@
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/kotlin/top/mcfpp/lang/Accessor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Accessor(override var value: Var<*>, identifier: String = UUID.randomUUID(
4646
return value.explicitCast(type)
4747
}
4848

49-
override fun onAssign(b: Var<*>): Accessor {
49+
override fun doAssign(b: Var<*>): Accessor {
5050
TODO()
5151
}
5252

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,23 @@ class ClassPointer : Var<ClassPointer>{
8787
@Override
8888
@InsertCommand
8989
@Throws(VariableConverseException::class)
90-
override fun onAssign(b: Var<*>): ClassPointer {
91-
var v = b.implicitCast(this.type)
92-
if(!v.isError){
93-
v = b
94-
}
95-
hasAssigned = true
90+
override fun doAssign(b: Var<*>): ClassPointer {
9691
//TODO 不支持指针作为类成员的时候
97-
when (v) {
92+
when (b) {
9893
is ClassPointer -> {
99-
if (!v.clazz.canCastTo(clazz)) {
100-
LogProcessor.error(TextTranslator.ASSIGN_ERROR.translate(v.type.typeName, type.typeName))
94+
if (!b.clazz.canCastTo(clazz)) {
95+
LogProcessor.error(TextTranslator.ASSIGN_ERROR.translate(b.type.typeName, type.typeName))
10196
return this
10297
}
10398
if (!isNull) {
10499
//原实体中的实例减少一个指针
105-
val c = Commands.selectRun(this,Commands.sbPlayerRemove(MCInt("@s").setObj(SbObject.MCFPP_POINTER_COUNTER) as MCInt, 1))
100+
val c = Commands.selectRun(
101+
this,
102+
Commands.sbPlayerRemove(MCInt("@s").setObj(SbObject.MCFPP_POINTER_COUNTER) as MCInt, 1)
103+
)
106104
Function.addCommands(c)
107105
}
108-
isNull = v.isNull
106+
isNull = b.isNull
109107
//地址储存
110108
Function.addCommand(
111109
Command.build("data modify")
@@ -114,12 +112,15 @@ class ClassPointer : Var<ClassPointer>{
114112
.build(b.nbtPath.toCommandPart())
115113
)
116114
//实例中的指针列表
117-
val c = Commands.selectRun(this,Commands.sbPlayerAdd(MCInt("@s").setObj(SbObject.MCFPP_POINTER_COUNTER) as MCInt, 1))
115+
val c = Commands.selectRun(
116+
this,
117+
Commands.sbPlayerAdd(MCInt("@s").setObj(SbObject.MCFPP_POINTER_COUNTER) as MCInt, 1)
118+
)
118119
Function.addCommands(c)
119120
}
120121

121122
else -> {
122-
LogProcessor.error(TextTranslator.ASSIGN_ERROR.translate(v.type.typeName, type.typeName))
123+
LogProcessor.error(TextTranslator.ASSIGN_ERROR.translate(b.type.typeName, type.typeName))
123124
}
124125
}
125126
return this

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

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import top.mcfpp.model.function.Function
1717
import top.mcfpp.model.function.UnknownFunction
1818
import top.mcfpp.util.LogProcessor
1919
import top.mcfpp.util.NBTUtil
20+
import top.mcfpp.util.TextTranslator
21+
import top.mcfpp.util.TextTranslator.translate
2022
import java.util.*
2123

2224
/**
@@ -51,45 +53,40 @@ open class DataTemplateObject : Var<DataTemplateObject> {
5153
}
5254

5355

54-
override fun onAssign(b: Var<*>): DataTemplateObject {
55-
var v = b.implicitCast(this.type)
56-
if(!v.isError){
57-
v = b
58-
}
59-
hasAssigned = true
60-
when(v){
56+
override fun doAssign(b: Var<*>): DataTemplateObject {
57+
when (b) {
6158
is NBTBasedDataConcrete<*> -> {
62-
if(v.value !is CompoundTag){
63-
throw VariableConverseException()
59+
if (b.value !is CompoundTag) {
60+
LogProcessor.error("Not a compound tag: ${b.value}")
61+
return this
6462
}
65-
if(templateType.checkCompoundStruct(v.value as CompoundTag)){
66-
this.assignMembers(v.value as CompoundTag)
63+
if (templateType.checkCompoundStruct(b.value as CompoundTag)) {
64+
this.assignMembers(b.value as CompoundTag)
65+
return this
66+
} else {
67+
LogProcessor.error("Error compound struct: ${b.value}")
6768
return this
68-
}else{
69-
throw VariableConverseException()
7069
}
7170
}
7271

7372
is DataTemplateObjectConcrete -> {
74-
if(templateType.checkCompoundStruct(v.value)){
75-
this.assignMembers(v.value)
73+
if (templateType.checkCompoundStruct(b.value)) {
74+
this.assignMembers(b.value)
75+
return this
76+
} else {
77+
LogProcessor.error("Error compound struct: ${b.value}")
7678
return this
77-
}else{
78-
throw VariableConverseException()
7979
}
8080
}
8181

8282
is DataTemplateObject -> {
83-
if (!v.templateType.canCastTo(templateType)) {
84-
throw VariableConverseException()
85-
}else{
86-
assignCommand(v)
87-
return this
88-
}
83+
assignCommand(b)
84+
return this
8985
}
9086

9187
else -> {
92-
throw VariableConverseException()
88+
LogProcessor.error(TextTranslator.ASSIGN_ERROR.translate(b.type.typeName, this.type.typeName))
89+
return this
9390
}
9491
}
9592
}

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,13 @@ open class EntityVar : NBTBasedData<IntArrayTag>{
6969
return SelectorVar.data.getFunction(key, readOnlyParams, normalParams) to true
7070
}
7171

72-
override fun onAssign(b: Var<*>): EntityVar {
73-
var v = b.implicitCast(this.type)
74-
if(!v.isError){
75-
v = b
76-
}
77-
hasAssigned = true
78-
when (v) {
72+
override fun doAssign(b: Var<*>): EntityVar {
73+
when (b) {
7974
is EntityVar -> {
80-
assignCommand(v)
75+
assignCommand(b)
8176
}
8277
else -> {
83-
LogProcessor.error(TextTranslator.ASSIGN_ERROR.translate(v.type.typeName, type.typeName))
78+
LogProcessor.error(TextTranslator.ASSIGN_ERROR.translate(b.type.typeName, type.typeName))
8479
}
8580
}
8681
return this

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,15 @@ open class EnumVar : Var<EnumVar> {
6161
type = enum.getType()
6262
}
6363

64-
override fun onAssign(b: Var<*>): EnumVar {
65-
var v = b.implicitCast(this.type)
66-
if(!v.isError){
67-
v = b
68-
}
69-
hasAssigned = true
70-
return when(v){
64+
override fun doAssign(b: Var<*>): EnumVar {
65+
return when(b){
7166
is EnumVar -> {
72-
val i = this.getIntVar().assign(v.getIntVar()) as MCInt
67+
val i = this.getIntVar().assign(b.getIntVar()) as MCInt
7368
if(i is MCIntConcrete) EnumVarConcrete(i, enum)
7469
else EnumVar(i, enum)
7570
}
7671
else -> {
77-
LogProcessor.error(TextTranslator.ASSIGN_ERROR.translate(v.type.typeName, type.typeName))
72+
LogProcessor.error(TextTranslator.ASSIGN_ERROR.translate(b.type.typeName, type.typeName))
7873
this
7974
}
8075
}

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

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package top.mcfpp.lang
22

33
import net.querz.nbt.tag.CompoundTag
4+
import net.querz.nbt.tag.ListTag
5+
import net.querz.nbt.tag.StringTag
46
import net.querz.nbt.tag.Tag
57
import top.mcfpp.exception.OperationNotImplementException
6-
import top.mcfpp.exception.VariableConverseException
78
import top.mcfpp.lang.type.*
89
import top.mcfpp.lang.value.MCFPPValue
910
import top.mcfpp.model.*
@@ -69,18 +70,14 @@ class JavaVar : Var<JavaVar>, MCFPPValue<Any?>{
6970
* 将b中的值赋值给此变量
7071
* @param b 变量的对象
7172
*/
72-
override fun onAssign(b: Var<*>): JavaVar {
73-
var v = b.implicitCast(this.type)
74-
if(!v.isError){
75-
v = b
76-
}
77-
hasAssigned = true
78-
when (v) {
73+
override fun doAssign(b: Var<*>): JavaVar {
74+
when (b) {
7975
is JavaVar -> {
80-
this.value = v.value
76+
this.value = b.value
8177
}
78+
8279
else -> {
83-
this.value = v
80+
this.value = b
8481
}
8582
}
8683
return this
@@ -216,6 +213,27 @@ class JavaVar : Var<JavaVar>, MCFPPValue<Any?>{
216213
}
217214
return re
218215
}
216+
217+
fun javaToMC(v : Any) : Var<*>{
218+
return when(v){
219+
is Int -> MCIntConcrete(v)
220+
is Float -> MCFloatConcrete(v)
221+
is Boolean -> MCBoolConcrete(v)
222+
is String -> MCStringConcrete(StringTag(v))
223+
is CompoundTag -> NBTBasedDataConcrete(v)
224+
//is ArrayList<*> -> NBTListConcrete()
225+
//is HashMap<*,*> -> NBTDictionaryConcrete(v.map { javaToMC(it.key!!) to javaToMC(it.value!!) }.toMap())
226+
else -> JavaVar(v)
227+
}
228+
}
229+
230+
fun javaToMC(v: ArrayList<Any>): ArrayList<Var<*>>{
231+
val re = ArrayList<Var<*>>()
232+
for (i in v){
233+
re.add(javaToMC(i))
234+
}
235+
return re
236+
}
219237
}
220238

221239
}

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import top.mcfpp.command.Command
55
import top.mcfpp.command.Commands
66
import top.mcfpp.lang.type.MCFPPBaseType
77
import top.mcfpp.lang.type.MCFPPClassType
8-
import top.mcfpp.lang.type.MCFPPNBTType
98
import top.mcfpp.lang.type.MCFPPType
109
import top.mcfpp.lang.value.MCFPPValue
1110
import top.mcfpp.lib.ChatComponent
@@ -53,15 +52,10 @@ open class JsonText : NBTBasedData<CompoundTag> {
5352
*/
5453
constructor(b: JsonText) : super(b)
5554

56-
override fun onAssign(b: Var<*>): NBTBasedData<CompoundTag> {
57-
var v = b.implicitCast(this.type)
58-
if(!v.isError){
59-
v = b
60-
}
61-
hasAssigned = true
62-
when(v){
63-
is JsonText -> assignCommand(v)
64-
else -> LogProcessor.error(TextTranslator.ASSIGN_ERROR.translate(v.type.typeName, type.typeName))
55+
override fun doAssign(b: Var<*>): NBTBasedData<CompoundTag> {
56+
when (b) {
57+
is JsonText -> assignCommand(b)
58+
else -> LogProcessor.error(TextTranslator.ASSIGN_ERROR.translate(b.type.typeName, type.typeName))
6559
}
6660
return this
6761
}

0 commit comments

Comments
 (0)