1
+ package top.mcfpp.lang
2
+
3
+ import top.mcfpp.annotations.InsertCommand
4
+ import top.mcfpp.command.Command
5
+ import top.mcfpp.command.Commands
6
+ import top.mcfpp.exception.VariableConverseException
7
+ import top.mcfpp.lang.type.MCFPPBaseType
8
+ import top.mcfpp.lang.type.MCFPPClassType
9
+ import top.mcfpp.lang.type.MCFPPType
10
+ import top.mcfpp.lang.value.MCFPPValue
11
+ import top.mcfpp.model.Enum
12
+ import top.mcfpp.model.FieldContainer
13
+ import top.mcfpp.model.Member
14
+ import top.mcfpp.model.function.Function
15
+ import top.mcfpp.util.LogProcessor
16
+ import java.util.*
17
+
18
+ open class EnumVar : Var <Int > {
19
+
20
+ var sbObject: SbObject = SbObject .MCFPP_default
21
+
22
+ var enum: Enum
23
+
24
+ /* *
25
+ * 创建一个枚举类型的变量。它的mc名和变量所在的域容器有关。
26
+ *
27
+ * @param identifier 标识符。默认为
28
+ */
29
+ constructor (
30
+ enum: Enum ,
31
+ curr: FieldContainer ,
32
+ identifier: String = UUID .randomUUID().toString()
33
+ ) : this (enum, curr.prefix + identifier) {
34
+ this .identifier = identifier
35
+ }
36
+
37
+ /* *
38
+ * 创建一个枚举值。它的标识符和mc名相同。
39
+ * @param identifier identifier
40
+ */
41
+ constructor (enum: Enum , identifier: String = UUID .randomUUID().toString()) : super (identifier){
42
+ this .enum = enum
43
+ type = enum.getType()
44
+ }
45
+
46
+ /* *
47
+ * 复制一个int
48
+ * @param b 被复制的int值
49
+ */
50
+ constructor (b: EnumVar ) : super (b){
51
+ enum = b.enum
52
+ type = enum.getType()
53
+ }
54
+
55
+ constructor (b: MCInt , enum : Enum ): super (b){
56
+ sbObject = b.sbObject
57
+ this .enum = enum
58
+ type = enum.getType()
59
+ }
60
+
61
+ override fun assign (b : Var <* >): Var <Int > {
62
+ return when (b){
63
+ is EnumVar -> {
64
+ val i = this .getIntVar().assign(b.getIntVar())
65
+ if (i is MCIntConcrete ) EnumVarConcrete (i, enum)
66
+ else EnumVar (i, enum)
67
+ }
68
+ else -> {
69
+ throw Exception (" Cannot assign ${b::class .simpleName} to EnumVar" )
70
+ }
71
+ }
72
+ }
73
+
74
+ override fun cast (type : MCFPPType ): Var <* > {
75
+ return when (type) {
76
+ this .type -> this
77
+ MCFPPBaseType .Any -> MCAnyConcrete (this )
78
+ else -> {
79
+ LogProcessor .error(" Cannot cast [${this .type} ] to [$type ]" )
80
+ throw VariableConverseException ()
81
+ }
82
+ }
83
+ }
84
+
85
+ override fun clone (): Var <* > {
86
+ return EnumVar (this )
87
+ }
88
+
89
+ override fun getTempVar (): Var <* > {
90
+ if (isTemp) return this
91
+ val re = EnumVar (enum)
92
+ re.isTemp = true
93
+ return re.assign(this )
94
+ }
95
+
96
+ override fun storeToStack () {
97
+ if (parent != null ) return
98
+ Function .addCommand(
99
+ Command (" execute store result" )
100
+ .build(nbtPath.toCommandPart())
101
+ .build(" int 1 run scoreboard players get $name ${SbObject .MCFPP_default } " )
102
+ )
103
+ }
104
+
105
+ override fun getFromStack () {
106
+ if (parent != null ) return
107
+ Function .addCommand(
108
+ Command (" execute store result score $name ${SbObject .MCFPP_default } run data get" )
109
+ .build(nbtPath.toCommandPart())
110
+ )
111
+ }
112
+
113
+ override fun getMemberVar (key : String , accessModifier : Member .AccessModifier ): Pair <Var <* >? , Boolean> {
114
+ TODO (" Not yet implemented" )
115
+ }
116
+
117
+ override fun getMemberFunction (
118
+ key : String ,
119
+ readOnlyParams : List <MCFPPType >,
120
+ normalParams : List <MCFPPType >,
121
+ accessModifier : Member .AccessModifier
122
+ ): Pair <Function , Boolean > {
123
+ TODO (" Not yet implemented" )
124
+ }
125
+
126
+ open fun getIntVar (): MCInt {
127
+ return MCInt (this )
128
+ }
129
+ }
130
+
131
+ class EnumVarConcrete : EnumVar , MCFPPValue <Int >{
132
+
133
+ override var value: Int
134
+
135
+ /* *
136
+ * 创建一个固定的int
137
+ *
138
+ * @param identifier 标识符
139
+ * @param curr 域容器
140
+ * @param value 值
141
+ */
142
+ constructor (
143
+ enum: Enum ,
144
+ curr: FieldContainer ,
145
+ value: Int ,
146
+ identifier: String = UUID .randomUUID().toString()
147
+ ) : super (enum, curr.prefix + identifier) {
148
+ this .value = value
149
+ }
150
+
151
+ /* *
152
+ * 创建一个固定的int。它的标识符和mc名一致/
153
+ * @param identifier 标识符。如不指定,则为随机uuid
154
+ * @param value 值
155
+ */
156
+ constructor (enum: Enum , value: Int , identifier: String = UUID .randomUUID().toString()) : super (enum ,identifier) {
157
+ this .value = value
158
+ }
159
+
160
+ constructor (enum: EnumVar , value: Int ) : super (enum){
161
+ this .value = value
162
+ }
163
+
164
+ constructor (enum: EnumVarConcrete ) : super (enum){
165
+ this .value = enum.value
166
+ }
167
+
168
+ constructor (b: MCIntConcrete , enum : Enum ): super (b, enum){
169
+ value = b.value
170
+ }
171
+
172
+ override fun clone (): EnumVarConcrete {
173
+ return EnumVarConcrete (this )
174
+ }
175
+
176
+ /* *
177
+ * 动态化
178
+ *
179
+ */
180
+ override fun toDynamic (replace : Boolean ): Var <* > {
181
+ // 避免错误 Smart cast to 'ClassPointer' is impossible, because 'parent' is a mutable property that could have been changed by this time
182
+ val parent = parent
183
+
184
+ if (parent != null ) {
185
+ val cmd = when (parent){
186
+ is ClassPointer -> {
187
+ Commands .selectRun(parent)
188
+ }
189
+ is MCFPPClassType -> {
190
+ arrayOf(Command .build(" execute as ${parent.cls.uuid} run " ))
191
+ }
192
+ else -> TODO ()
193
+ }
194
+ if (cmd.size == 2 ){
195
+ Function .addCommand(cmd[0 ])
196
+ }
197
+ Function .addCommand(cmd.last().build(" scoreboard players set @s ${SbObject .MCFPP_default } $value " ))
198
+ } else {
199
+ val cmd = if (! isTemp)
200
+ Command (" execute store result" ).build(nbtPath.toCommandPart()).build(" int 1 run " )
201
+ else
202
+ Command (" " )
203
+ Function .addCommand(cmd.build(" scoreboard players set $name ${SbObject .MCFPP_default } $value " , false ))
204
+ }
205
+ val re = EnumVar (this )
206
+ if (replace){
207
+ Function .currFunction.field.putVar(identifier, re, true )
208
+ }
209
+ return re
210
+ }
211
+
212
+ @Override
213
+ override fun cast (type : MCFPPType ): Var <* > {
214
+ // TODO 类支持
215
+ return when (type) {
216
+ this .type -> this
217
+ MCFPPBaseType .Any -> this
218
+ else -> {
219
+ LogProcessor .error(" Cannot cast [${this .type} ] to [$type ]" )
220
+ throw VariableConverseException ()
221
+ }
222
+ }
223
+ }
224
+
225
+ /* *
226
+ * 获取临时变量
227
+ *
228
+ * @return 返回临时变量
229
+ */
230
+ @Override
231
+ @InsertCommand
232
+ override fun getTempVar (): Var <* > {
233
+ if (isTemp) return this
234
+ return EnumVarConcrete (enum ,value)
235
+ }
236
+
237
+ override fun getIntVar (): MCInt {
238
+ return MCIntConcrete (this )
239
+ }
240
+ }
0 commit comments