Skip to content

Commit ecb0418

Browse files
committed
Loading NBT structure files per command
1 parent 550873e commit ecb0418

File tree

6 files changed

+189
-22
lines changed

6 files changed

+189
-22
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.lambda.brigadier.argument
2+
3+
/*
4+
* Copyright 2024 The Quilt Project
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import com.lambda.brigadier.*
20+
import com.lambda.brigadier.assumeSourceNotUsed
21+
import net.minecraft.advancement.AdvancementEntry
22+
import net.minecraft.command.argument.IdentifierArgumentType
23+
import net.minecraft.loot.condition.LootCondition
24+
import net.minecraft.loot.function.LootFunction
25+
import net.minecraft.recipe.RecipeEntry
26+
import net.minecraft.server.command.ServerCommandSource
27+
import net.minecraft.util.Identifier
28+
29+
/**
30+
* Reads the [Identifier] value from the
31+
* argument in the receiver [ArgumentReader].
32+
*
33+
* @see IdentifierArgumentType.getIdentifier
34+
*/
35+
@JvmName("valueIdentifierArg")
36+
@BrigadierDsl
37+
fun DefaultArgumentReader<IdentifierArgumentType>.value(): Identifier {
38+
return IdentifierArgumentType.getIdentifier(context.assumeSourceNotUsed(), name)
39+
}
40+
41+
/**
42+
* Reads the [Identifier] value from the
43+
* argument in the receiver [ArgumentReader]
44+
* as an [AdvancementEntry].
45+
*
46+
* @see IdentifierArgumentType.getAdvancementArgument
47+
*/
48+
@BrigadierDsl
49+
fun ArgumentReader<
50+
ServerCommandSource,
51+
DefaultArgumentDescriptor<
52+
IdentifierArgumentType
53+
>
54+
>.asAdvancement(): AdvancementEntry {
55+
return IdentifierArgumentType.getAdvancementArgument(context, name)
56+
}
57+
58+
/**
59+
* Reads the [Identifier] value from the
60+
* argument in the receiver [ArgumentReader]
61+
* as a [LootCondition].
62+
*
63+
* @see IdentifierArgumentType.getPredicateArgument
64+
*/
65+
@BrigadierDsl
66+
fun ArgumentReader<
67+
ServerCommandSource,
68+
DefaultArgumentDescriptor<
69+
IdentifierArgumentType
70+
>
71+
>.asPredicate(): LootCondition {
72+
return IdentifierArgumentType.getPredicateArgument(context, name)
73+
}
74+
75+
/**
76+
* Reads the [Identifier] value from the
77+
* argument in the receiver [ArgumentReader]
78+
* as a [LootFunction].
79+
*
80+
* @see IdentifierArgumentType.getItemModifierArgument
81+
*/
82+
@BrigadierDsl
83+
fun ArgumentReader<
84+
ServerCommandSource,
85+
DefaultArgumentDescriptor<
86+
IdentifierArgumentType
87+
>
88+
>.asItemModifier(): LootFunction {
89+
return IdentifierArgumentType.getItemModifierArgument(context, name)
90+
}
91+
92+
/**
93+
* Reads the [Identifier] value from the
94+
* argument in the receiver [ArgumentReader]
95+
* as a [RecipeEntry].
96+
*
97+
* @see IdentifierArgumentType.getRecipeArgument
98+
*/
99+
@BrigadierDsl
100+
fun ArgumentReader<
101+
ServerCommandSource,
102+
DefaultArgumentDescriptor<
103+
IdentifierArgumentType
104+
>
105+
>.asRecipe(): RecipeEntry<*> {
106+
return IdentifierArgumentType.getRecipeArgument(context, name)
107+
}
108+
109+
/**
110+
* Creates an identifier argument with [name] as the parameter name.
111+
*/
112+
@BrigadierDsl
113+
fun <S> identifier(
114+
name: String
115+
): DefaultArgumentConstructor<S, IdentifierArgumentType> {
116+
return argument(name, IdentifierArgumentType.identifier())
117+
}

common/src/main/kotlin/com/lambda/command/commands/BuildCommand.kt

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
package com.lambda.command.commands
22

3+
import com.lambda.brigadier.CommandResult
4+
import com.lambda.brigadier.argument.identifier
35
import com.lambda.brigadier.argument.literal
6+
import com.lambda.brigadier.argument.value
47
import com.lambda.brigadier.execute
8+
import com.lambda.brigadier.executeWithResult
59
import com.lambda.brigadier.required
610
import com.lambda.command.LambdaCommand
711
import com.lambda.interaction.construction.Blueprint.Companion.toStructure
812
import com.lambda.interaction.construction.DynamicBlueprint.Companion.toBlueprint
13+
import com.lambda.interaction.construction.StructureManager
14+
import com.lambda.interaction.construction.StructureManager.templateManager
915
import com.lambda.interaction.construction.verify.TargetState
1016
import com.lambda.task.tasks.BuildTask.Companion.build
1117
import com.lambda.threading.runSafe
18+
import com.lambda.util.Communication.info
1219
import com.lambda.util.extension.CommandBuilder
1320
import net.minecraft.block.Blocks
21+
import net.minecraft.util.Identifier
1422
import net.minecraft.util.math.BlockBox
1523

1624
object BuildCommand : LambdaCommand(
@@ -20,27 +28,43 @@ object BuildCommand : LambdaCommand(
2028
) {
2129
override fun CommandBuilder.create() {
2230
required(literal("place")) {
23-
execute {
24-
runSafe {
25-
val materials = setOf(
26-
TargetState.Block(Blocks.NETHERRACK),
27-
TargetState.Block(Blocks.AIR),
28-
TargetState.Block(Blocks.COBBLESTONE),
29-
TargetState.Block(Blocks.AIR),
30-
)
31-
val facing = player.horizontalFacing
32-
val pos = player.blockPos.add(facing.vector.multiply(2))
33-
34-
BlockBox.create(pos, pos.add(facing.rotateYClockwise().vector.multiply(3)))
35-
.toStructure(TargetState.Block(Blocks.NETHERRACK))
36-
.toBlueprint {
37-
it.mapValues { (_, _) ->
38-
materials.elementAt((System.currentTimeMillis() / 5000).toInt() % materials.size)
39-
}
40-
}
41-
.build(finishOnDone = false)
42-
.start(null)
31+
required(identifier("structure")) { structure ->
32+
suggests { _, builder ->
33+
templateManager.streamTemplates().forEach {
34+
builder.suggest(it.path)
35+
}
36+
builder.buildFuture()
37+
}
38+
executeWithResult {
39+
templateManager.getTemplate(structure().value()).ifPresent { template ->
40+
info("Building structure: ${template.size} author: ${template.author}")
41+
}
42+
CommandResult.success()
4343
}
44+
45+
// execute {
46+
// runSafe {
47+
//
48+
//// val materials = setOf(
49+
//// TargetState.Block(Blocks.NETHERRACK),
50+
//// TargetState.Block(Blocks.AIR),
51+
//// TargetState.Block(Blocks.COBBLESTONE),
52+
//// TargetState.Block(Blocks.AIR),
53+
//// )
54+
//// val facing = player.horizontalFacing
55+
//// val pos = player.blockPos.add(facing.vector.multiply(2))
56+
////
57+
//// BlockBox.create(pos, pos.add(facing.rotateYClockwise().vector.multiply(3)))
58+
//// .toStructure(TargetState.Block(Blocks.NETHERRACK))
59+
//// .toBlueprint {
60+
//// it.mapValues { (_, _) ->
61+
//// materials.elementAt((System.currentTimeMillis() / 5000).toInt() % materials.size)
62+
//// }
63+
//// }
64+
//// .build(finishOnDone = false)
65+
//// .start(null)
66+
// }
67+
// }
4468
}
4569
}
4670
}

common/src/main/kotlin/com/lambda/core/Loader.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.lambda.gui.GuiConfigurable
1010
import com.lambda.gui.HudGuiConfigurable
1111
import com.lambda.interaction.PlayerPacketManager
1212
import com.lambda.interaction.RotationManager
13+
import com.lambda.interaction.construction.StructureManager
1314
import com.lambda.interaction.material.ContainerManager
1415
import com.lambda.module.ModuleRegistry
1516
import com.lambda.sound.SoundRegistry
@@ -37,7 +38,8 @@ object Loader {
3738
SoundRegistry,
3839
TimerManager,
3940
PingManager,
40-
ContainerManager
41+
ContainerManager,
42+
StructureManager
4143
)
4244

4345
fun initialize() {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.lambda.interaction.construction
2+
3+
import com.lambda.Lambda.mc
4+
import com.lambda.core.Loadable
5+
import com.lambda.util.FolderRegister
6+
import net.minecraft.registry.Registries
7+
import net.minecraft.structure.StructureTemplateManager
8+
9+
object StructureManager : Loadable {
10+
// ToDo: Rewrite the StructureTemplateManager: Remove clutter, clean file structure
11+
lateinit var templateManager: StructureTemplateManager
12+
13+
override fun load(): String {
14+
templateManager = StructureTemplateManager(
15+
mc.resourceManager,
16+
mc.levelStorage.createSession(FolderRegister.structure.path),
17+
mc.dataFixer,
18+
Registries.BLOCK.readOnlyWrapper
19+
)
20+
21+
return "StructureManager loaded ${templateManager.streamTemplates().count()} templates"
22+
}
23+
}

common/src/main/kotlin/com/lambda/task/tasks/BuildTask.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class BuildTask @Ta5kBuilder constructor(
100100
result.collectDrop = collectDrops
101101
}
102102

103-
if (!(result is BreakResult.Break && result.collectDrop)) {
103+
if (result !is BreakResult.Break || !result.collectDrop) {
104104
if (pathing) BaritoneUtils.setGoalAndPath(
105105
GoalNear(result.blockPos, 4)
106106
)

common/src/main/kotlin/com/lambda/util/FolderRegister.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ object FolderRegister {
3232
val packetLogs: File = File(lambda, "packet-log")
3333
val replay: File = File(lambda, "replay")
3434
val cache: File = File(lambda, "cache")
35+
val structure: File = File(lambda, "structure")
3536

3637
fun File.createIfNotExists() {
3738
createFileIfNotExists(this.name, this.parentFile)

0 commit comments

Comments
 (0)