1818package com.lambda.command.commands
1919
2020import com.lambda.brigadier.CommandResult
21+ import com.lambda.brigadier.CommandResult.Companion.failure
22+ import com.lambda.brigadier.CommandResult.Companion.success
2123import com.lambda.brigadier.argument.greedyString
2224import com.lambda.brigadier.argument.literal
2325import com.lambda.brigadier.argument.value
@@ -28,6 +30,7 @@ import com.lambda.interaction.construction.StructureRegistry
2830import com.lambda.interaction.construction.blueprint.Blueprint.Companion.toStructure
2931import com.lambda.interaction.construction.blueprint.StaticBlueprint.Companion.toBlueprint
3032import com.lambda.task.TaskFlow.run
33+ import com.lambda.task.tasks.BuildTask
3134import com.lambda.task.tasks.BuildTask.Companion.build
3235import com.lambda.threading.runSafe
3336import com.lambda.util.Communication.info
@@ -42,6 +45,8 @@ object BuildCommand : LambdaCommand(
4245 description = " Builds a structure" ,
4346 usage = " build <structure>"
4447) {
48+ private var lastBuildTask: BuildTask ? = null
49+
4550 override fun CommandBuilder.create () {
4651 required(literal(" place" )) {
4752 required(greedyString(" structure" )) { structure ->
@@ -66,18 +71,29 @@ object BuildCommand : LambdaCommand(
6671 return @executeWithResult CommandResult .success()
6772 }
6873 } catch (e: InvalidPathException ) {
69- return @executeWithResult CommandResult . failure(" Invalid path $pathString " )
74+ return @executeWithResult failure(" Invalid path $pathString " )
7075 } catch (e: NoSuchFileException ) {
71- return @executeWithResult CommandResult . failure(" Structure $pathString not found" )
76+ return @executeWithResult failure(" Structure $pathString not found" )
7277 } catch (e: Exception ) {
73- return @executeWithResult CommandResult . failure(
78+ return @executeWithResult failure(
7479 e.message ? : " Failed to load structure $pathString "
7580 )
7681 }
7782 }
7883
79- CommandResult .failure(" Structure $pathString not found" )
84+ failure(" Structure $pathString not found" )
85+ }
86+ }
87+ }
88+
89+ required(literal(" cancel" )) {
90+ executeWithResult {
91+ lastBuildTask?.cancel() ? : run {
92+ return @executeWithResult failure(" No build task to cancel" )
8093 }
94+ this @BuildCommand.info(" $lastBuildTask cancelled" )
95+ lastBuildTask = null
96+ success()
8197 }
8298 }
8399 }
0 commit comments