Skip to content

Commit b32da00

Browse files
committed
修复了逻辑语句
1 parent 2d2800f commit b32da00

File tree

3 files changed

+75
-51
lines changed

3 files changed

+75
-51
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -587,15 +587,13 @@ open class MCFPPImVisitor: mcfppParserBaseVisitor<Any?>() {
587587
return null
588588
}
589589

590+
private lateinit var doWhileFunction: InternalFunction
590591
@InsertCommand
591592
fun enterDoWhileStatement(ctx: mcfppParser.DoWhileStatementContext) {
592593
//进入do-while函数
593594
Project.ctx = ctx
594595
Function.addComment("do-while start")
595-
val doWhileFunction = InternalFunction("_dowhile_", Function.currFunction)
596-
Function.addCommand("data modify storage mcfpp:system " + Project.config.rootNamespace + ".stack_frame prepend value {}")
597-
Function.addCommand("function " + doWhileFunction.namespaceID)
598-
Function.addCommand("data remove storage mcfpp:system " + Project.config.rootNamespace + ".stack_frame[0]")
596+
doWhileFunction = InternalFunction("_dowhile_", Function.currFunction)
599597
Function.currFunction = doWhileFunction
600598
if(!GlobalField.localNamespaces.containsKey(doWhileFunction.namespace))
601599
GlobalField.localNamespaces[doWhileFunction.namespace] = Namespace(doWhileFunction.namespace)
@@ -643,16 +641,18 @@ open class MCFPPImVisitor: mcfppParserBaseVisitor<Any?>() {
643641
}
644642
GlobalField.localNamespaces[f.namespace]!!.field.addFunction(f,false)
645643
//给子函数开栈
646-
Function.addCommand("data modify storage mcfpp:system " + Project.config.rootNamespace + ".stack_frame prepend value {}")
647-
Function.addCommand(
644+
Function.currFunction.parent[0].commands.add("data modify storage mcfpp:system " + Project.config.rootNamespace + ".stack_frame prepend value {}")
645+
Function.currFunction.parent[0].commands.add(
648646
"execute " +
649647
"unless function " + f.namespaceID + " " +
650648
"run return 1"
651649
)
652-
val parent = ctx.parent as mcfppParser.DoWhileStatementContext
653-
MCFPPExprVisitor().visit(parent.expression()) as ScoreBool
654-
Function.addCommand("data remove storage mcfpp:system " + Project.config.rootNamespace + ".stack_frame[0]")
650+
Function.currFunction.parent[0].commands.add("data remove storage mcfpp:system " + Project.config.rootNamespace + ".stack_frame[0]")
651+
Function.currFunction.parent[0].commands.add("data modify storage mcfpp:system " + Project.config.rootNamespace + ".stack_frame prepend value {}")
652+
Function.currFunction.parent[0].commands.add("function " + doWhileFunction.namespaceID)
653+
Function.currFunction.parent[0].commands.add("data remove storage mcfpp:system " + Project.config.rootNamespace + ".stack_frame[0]")
655654
//递归调用
655+
val parent = ctx.parent as mcfppParser.DoWhileStatementContext
656656
when(val exp = MCFPPExprVisitor().visit(parent.expression())){
657657
is ScoreBoolConcrete -> {
658658
if(exp.value){

src/main/kotlin/top/mcfpp/io/DatapackCreator.kt

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,18 @@ object DatapackCreator {
100100
if (f is Native) {
101101
return@run
102102
}
103-
LogProcessor.debug("Writing File: $currPath\\functions\\${f.nameWithNamespace}.mcfunction")
104-
Files.createDirectories(Paths.get("$currPath/functions"))
103+
LogProcessor.debug("Writing File: $currPath\\function\\${f.nameWithNamespace}.mcfunction")
104+
Files.createDirectories(Paths.get("$currPath/function"))
105105
Files.write(
106-
Paths.get("$currPath/functions/${f.nameWithNamespace}.mcfunction"),
106+
Paths.get("$currPath/function/${f.nameWithNamespace}.mcfunction"),
107107
f.cmdStr.toByteArray()
108108
)
109109
if(f.compiledFunctions.isNotEmpty()){
110110
for (cf in f.compiledFunctions.values){
111-
LogProcessor.debug("Writing File: $currPath\\functions\\${cf.nameWithNamespace}.mcfunction")
112-
Files.createDirectories(Paths.get("$currPath/functions"))
111+
LogProcessor.debug("Writing File: $currPath\\function\\${cf.nameWithNamespace}.mcfunction")
112+
Files.createDirectories(Paths.get("$currPath/function"))
113113
Files.write(
114-
Paths.get("$currPath/functions/${cf.nameWithNamespace}.mcfunction"),
114+
Paths.get("$currPath/function/${cf.nameWithNamespace}.mcfunction"),
115115
cf.cmdStr.toByteArray()
116116
)
117117
}
@@ -127,42 +127,42 @@ object DatapackCreator {
127127
if (f is Native) {
128128
return@run
129129
}
130-
LogProcessor.debug("Writing File: $currPath\\functions\\" + f.nameWithNamespace + ".mcfunction")
130+
LogProcessor.debug("Writing File: $currPath\\function\\" + f.nameWithNamespace + ".mcfunction")
131131
//TODO 可能无法正确创建文件夹
132-
Files.createDirectories(Paths.get("$currPath/functions/" + StringHelper.toLowerCase(cls.identifier)))
132+
Files.createDirectories(Paths.get("$currPath/function/" + StringHelper.toLowerCase(cls.identifier)))
133133
if (f is ExtensionFunction){
134-
Files.createDirectories(Paths.get("$currPath/functions/" + StringHelper.toLowerCase(cls.identifier) + "/ex"))
134+
Files.createDirectories(Paths.get("$currPath/function/" + StringHelper.toLowerCase(cls.identifier) + "/ex"))
135135
}
136136
Files.write(
137-
Paths.get("$currPath/functions/" + f.nameWithNamespace + ".mcfunction"),
137+
Paths.get("$currPath/function/" + f.nameWithNamespace + ".mcfunction"),
138138
f.cmdStr.toByteArray()
139139
)
140140
if(f.compiledFunctions.isNotEmpty()){
141141
for (cf in f.compiledFunctions.values) {
142-
LogProcessor.debug("Writing File: $currPath\\functions\\" + cf.nameWithNamespace + ".mcfunction")
142+
LogProcessor.debug("Writing File: $currPath\\function\\" + cf.nameWithNamespace + ".mcfunction")
143143
//TODO 可能无法正确创建文件夹
144-
Files.createDirectories(Paths.get("$currPath/functions/" + StringHelper.toLowerCase(cls.identifier)))
145-
Files.write(Paths.get("$currPath/functions/" + cf.nameWithNamespace + ".mcfunction"), cf.cmdStr.toByteArray())
144+
Files.createDirectories(Paths.get("$currPath/function/" + StringHelper.toLowerCase(cls.identifier)))
145+
Files.write(Paths.get("$currPath/function/" + cf.nameWithNamespace + ".mcfunction"), cf.cmdStr.toByteArray())
146146
}
147147
}
148148
}
149149
}
150150
//构造函数
151151
cls.constructors.forEach{ c ->
152152
run {
153-
LogProcessor.debug("Writing File: $currPath\\functions\\" + c.nameWithNamespace + ".mcfunction")
153+
LogProcessor.debug("Writing File: $currPath\\function\\" + c.nameWithNamespace + ".mcfunction")
154154
//TODO 可能无法正确创建文件夹
155-
Files.createDirectories(Paths.get("$currPath/functions/" + StringHelper.toLowerCase(cls.identifier)))
155+
Files.createDirectories(Paths.get("$currPath/function/" + StringHelper.toLowerCase(cls.identifier)))
156156
Files.write(
157-
Paths.get("$currPath/functions/" + c.nameWithNamespace + ".mcfunction"),
157+
Paths.get("$currPath/function/" + c.nameWithNamespace + ".mcfunction"),
158158
c.cmdStr.toByteArray()
159159
)
160160
if(c.compiledFunctions.isNotEmpty()){
161161
for (cf in c.compiledFunctions.values) {
162-
LogProcessor.debug("Writing File: $currPath\\functions\\" + cf.nameWithNamespace + ".mcfunction")
162+
LogProcessor.debug("Writing File: $currPath\\function\\" + cf.nameWithNamespace + ".mcfunction")
163163
//TODO 可能无法正确创建文件夹
164-
Files.createDirectories(Paths.get("$currPath/functions/" + StringHelper.toLowerCase(cls.identifier)))
165-
Files.write(Paths.get("$currPath/functions/" + cf.nameWithNamespace + ".mcfunction"), cf.cmdStr.toByteArray())
164+
Files.createDirectories(Paths.get("$currPath/function/" + StringHelper.toLowerCase(cls.identifier)))
165+
Files.write(Paths.get("$currPath/function/" + cf.nameWithNamespace + ".mcfunction"), cf.cmdStr.toByteArray())
166166
}
167167
}
168168
}
@@ -178,22 +178,22 @@ object DatapackCreator {
178178
if (f is Native) {
179179
return@run
180180
}
181-
LogProcessor.debug("Writing File: $currPath\\functions\\" + f.nameWithNamespace + ".mcfunction")
181+
LogProcessor.debug("Writing File: $currPath\\function\\" + f.nameWithNamespace + ".mcfunction")
182182
//TODO 可能无法正确创建文件夹
183-
Files.createDirectories(Paths.get("$currPath/functions/" + StringHelper.toLowerCase(t.identifier)))
183+
Files.createDirectories(Paths.get("$currPath/function/" + StringHelper.toLowerCase(t.identifier)))
184184
if (f is ExtensionFunction){
185-
Files.createDirectories(Paths.get("$currPath/functions/" + StringHelper.toLowerCase(t.identifier) + "/ex"))
185+
Files.createDirectories(Paths.get("$currPath/function/" + StringHelper.toLowerCase(t.identifier) + "/ex"))
186186
}
187187
Files.write(
188-
Paths.get("$currPath/functions/" + f.nameWithNamespace + ".mcfunction"),
188+
Paths.get("$currPath/function/" + f.nameWithNamespace + ".mcfunction"),
189189
f.cmdStr.toByteArray()
190190
)
191191
if(f.compiledFunctions.isNotEmpty()){
192192
for (cf in f.compiledFunctions.values) {
193-
LogProcessor.debug("Writing File: $currPath\\functions\\" + cf.nameWithNamespace + ".mcfunction")
193+
LogProcessor.debug("Writing File: $currPath\\function\\" + cf.nameWithNamespace + ".mcfunction")
194194
//TODO 可能无法正确创建文件夹
195-
Files.createDirectories(Paths.get("$currPath/functions/" + StringHelper.toLowerCase(t.identifier)))
196-
Files.write(Paths.get("$currPath/functions/" + cf.nameWithNamespace + ".mcfunction"), cf.cmdStr.toByteArray())
195+
Files.createDirectories(Paths.get("$currPath/function/" + StringHelper.toLowerCase(t.identifier)))
196+
Files.write(Paths.get("$currPath/function/" + cf.nameWithNamespace + ".mcfunction"), cf.cmdStr.toByteArray())
197197
}
198198
}
199199
}
@@ -208,22 +208,22 @@ object DatapackCreator {
208208
if (f is Native) {
209209
return@run
210210
}
211-
LogProcessor.debug("Writing File: $currPath\\functions\\" + f.nameWithNamespace + ".mcfunction")
211+
LogProcessor.debug("Writing File: $currPath\\function\\" + f.nameWithNamespace + ".mcfunction")
212212
//TODO 可能无法正确创建文件夹
213-
Files.createDirectories(Paths.get("$currPath\\functions\\" + StringHelper.toLowerCase(obj.identifier) + "\\static"))
213+
Files.createDirectories(Paths.get("$currPath\\function\\" + StringHelper.toLowerCase(obj.identifier) + "\\static"))
214214
if (f is ExtensionFunction){
215-
Files.createDirectories(Paths.get("$currPath\\functions\\" + StringHelper.toLowerCase(obj.identifier) + "\\static\\ex"))
215+
Files.createDirectories(Paths.get("$currPath\\function\\" + StringHelper.toLowerCase(obj.identifier) + "\\static\\ex"))
216216
}
217217
Files.write(
218-
Paths.get("$currPath/functions/" + f.nameWithNamespace + ".mcfunction"),
218+
Paths.get("$currPath/function/" + f.nameWithNamespace + ".mcfunction"),
219219
f.cmdStr.toByteArray()
220220
)
221221
if(f.compiledFunctions.isNotEmpty()){
222222
for (cf in f.compiledFunctions.values) {
223-
LogProcessor.debug("Writing File: $currPath\\functions\\" + cf.nameWithNamespace + ".mcfunction")
223+
LogProcessor.debug("Writing File: $currPath\\function\\" + cf.nameWithNamespace + ".mcfunction")
224224
//TODO 可能无法正确创建文件夹
225-
Files.createDirectories(Paths.get("$currPath\\functions/" + StringHelper.toLowerCase(obj.identifier)))
226-
Files.write(Paths.get("$currPath/functions/" + cf.nameWithNamespace + ".mcfunction"), cf.cmdStr.toByteArray())
225+
Files.createDirectories(Paths.get("$currPath\\function/" + StringHelper.toLowerCase(obj.identifier)))
226+
Files.write(Paths.get("$currPath/function/" + cf.nameWithNamespace + ".mcfunction"), cf.cmdStr.toByteArray())
227227
}
228228
}
229229
}

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

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ abstract class ChatComponent {
2020
abstract fun toCommandPart(): Command
2121

2222
fun styleToString(): Command{
23-
val str = Command("")
23+
val str = Command()
2424
for (style in styles){
2525
str.build(style.toCommandPart(), false)
2626
if(style != styles.last()) str.build(", ", false)
@@ -53,7 +53,11 @@ class ListChatComponent: ChatComponent() {
5353

5454
class PlainChatComponent(val value: String) : ChatComponent() {
5555
override fun toCommandPart(): Command {
56-
return Command("""{"type": "text", "text": "$value", ${styleToString()}""")
56+
val c = Command("""{""type"": "text", "text": "$value"}""")
57+
if(styles.isNotEmpty()){
58+
c.build(",").build(styleToString())
59+
}
60+
return c
5761
}
5862

5963
}
@@ -80,29 +84,49 @@ class TranslatableChatComponent(val key: String, val fallback: String? = null, v
8084
class ScoreChatComponent(val value: MCInt) : ChatComponent() {
8185

8286
override fun toCommandPart(): Command {
83-
return Command("{\"type\":\"score\",\"score\":{\"name\":\"${value.name}\",\"objective\":\"${value.sbObject.name}\"}, ${styleToString()}}")
87+
val c = Command("{\"type\":\"score\",\"score\":{\"name\":\"${value.name}\",\"objective\":\"${value.sbObject.name}\"}}")
88+
if(styles.isNotEmpty()){
89+
c.build(",").build(styleToString())
90+
}
91+
return c
8492
}
8593
}
8694

8795
class SelectorChatComponent(val selector: String, val separator: ChatComponent? = null) : ChatComponent() {
8896
override fun toCommandPart(): Command {
89-
return Command(if(separator != null){
90-
"{\"type\":\"selector\",\"selector\":\"$selector\",\"separator\":${separator.toCommandPart()}, ${styleToString()}"
97+
val c = if(separator != null){
98+
Command("{\"type\":\"selector\",\"selector\":\"$selector\",\"separator\":${separator.toCommandPart()}")
9199
}else{
92-
"{\"type\":\"selector\",\"selector\":\"$selector\", ${styleToString()}"
93-
})
100+
Command("{\"type\":\"selector\",\"selector\":\"$selector\"")
101+
}
102+
if(styles.isNotEmpty()){
103+
c.build(",").build(styleToString())
104+
}
105+
return c
94106
}
95107
}
96108

97109
class KeybindChatComponent(val key: String) : ChatComponent() {
98110
override fun toCommandPart(): Command {
99-
return Command("{\"type\":\"keybind\",\"keybind\":\"$key\", ${styleToString()}")
111+
val c = Command("{\"type\":\"keybind\",\"keybind\":\"$key\"")
112+
if(styles.isNotEmpty()){
113+
c.build(",").build(styleToString())
114+
}
115+
return c
100116
}
101117
}
102118

103119
class NBTChatComponent(val nbt: NBTBasedData, val interpret: Boolean = false, val separator: ChatComponent? = null) : ChatComponent() {
104120
override fun toCommandPart(): Command {
105-
return Command("{\"type\":\"nbt\",\"nbt\":\"").build(nbt.nbtPath.toCommandPart(), false).build("\",\"interpret\":$interpret, ${styleToString()}}", false)
121+
val c = if(separator != null){
122+
Command("{\"type\":\"nbt\",\"nbt\":\"").build(nbt.nbtPath.toCommandPart(), false).build("\",\"interpret\":$interpret,\"separator\":${separator.toCommandPart()}}", false)
123+
}else{
124+
Command("{\"type\":\"nbt\",\"nbt\":\"").build(nbt.nbtPath.toCommandPart(), false).build("\",\"interpret\":$interpret}", false)
125+
}
126+
if(styles.isNotEmpty()){
127+
c.build(",").build(styleToString())
128+
}
129+
return c
106130
}
107131
}
108132

0 commit comments

Comments
 (0)