-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathBuildCommand.kt
More file actions
102 lines (95 loc) · 4.28 KB
/
BuildCommand.kt
File metadata and controls
102 lines (95 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Copyright 2025 Lambda
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.lambda.command.commands
import com.lambda.brigadier.CommandResult.Companion.failure
import com.lambda.brigadier.CommandResult.Companion.success
import com.lambda.brigadier.argument.greedyString
import com.lambda.brigadier.argument.literal
import com.lambda.brigadier.argument.value
import com.lambda.brigadier.executeWithResult
import com.lambda.brigadier.required
import com.lambda.command.LambdaCommand
import com.lambda.config.AutomationConfig
import com.lambda.interaction.construction.StructureRegistry
import com.lambda.interaction.construction.blueprint.Blueprint.Companion.toStructure
import com.lambda.interaction.construction.blueprint.StaticBlueprint.Companion.toBlueprint
import com.lambda.task.RootTask.run
import com.lambda.task.tasks.BuildTask
import com.lambda.task.tasks.BuildTask.Companion.build
import com.lambda.threading.runSafe
import com.lambda.util.Communication.info
import com.lambda.util.extension.CommandBuilder
import com.lambda.util.extension.move
import java.nio.file.InvalidPathException
import java.nio.file.NoSuchFileException
import java.nio.file.Path
object BuildCommand : LambdaCommand(
name = "Build",
description = "Builds a structure",
usage = "build <structure>"
) {
private var lastBuildTask: BuildTask? = null
override fun CommandBuilder.create() {
required(literal("place")) {
required(greedyString("structure")) { structure ->
suggests { _, builder ->
StructureRegistry.forEach { key, _ -> builder.suggest(key) }
builder.buildFuture()
}
executeWithResult {
val pathString = structure().value()
runSafe<Unit> {
try {
StructureRegistry
.loadStructureByRelativePath(Path.of(pathString))
.let { template ->
info("Building structure $pathString with dimensions ${template.size.toShortString()} created by ${template.author}")
lastBuildTask = with(AutomationConfig.Companion.DEFAULT) {
template.toStructure()
.move(player.blockPos)
.toBlueprint()
.build()
.run()
}
return@executeWithResult success()
}
} catch (e: InvalidPathException) {
return@executeWithResult failure("Invalid path $pathString")
} catch (e: NoSuchFileException) {
return@executeWithResult failure("Structure $pathString not found")
} catch (e: Exception) {
return@executeWithResult failure(
e.message ?: "Failed to load structure $pathString"
)
}
}
failure("Structure $pathString not found")
}
}
}
required(literal("cancel")) {
executeWithResult {
lastBuildTask?.cancel() ?: run {
return@executeWithResult failure("No build task to cancel")
}
this@BuildCommand.info("$lastBuildTask cancelled")
lastBuildTask = null
success()
}
}
}
}