Skip to content

Commit 3c2859b

Browse files
committed
feat: /blueprint-editor region
1 parent eac648f commit 3c2859b

File tree

3 files changed

+117
-2
lines changed

3 files changed

+117
-2
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ yarn_build=1
99
loader_version=0.17.2
1010

1111
# Mod Properties
12-
mod_version=1.16.3
12+
mod_version=1.17.0
1313
maven_group=net.mcbrawls
1414
mod_id=blueprint
1515

src/main/kotlin/net/mcbrawls/blueprint/command/BlueprintEditorCommand.kt

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
package net.mcbrawls.blueprint.command
22

33
import com.mojang.brigadier.CommandDispatcher
4+
import com.mojang.brigadier.arguments.DoubleArgumentType
5+
import com.mojang.brigadier.arguments.StringArgumentType
46
import com.mojang.brigadier.context.CommandContext
57
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType
68
import net.mcbrawls.blueprint.editor.BlueprintEditorHandler
79
import net.mcbrawls.blueprint.editor.BlueprintEditorWorld
10+
import net.mcbrawls.blueprint.region.CuboidRegion
11+
import net.mcbrawls.blueprint.region.SphericalRegion
812
import net.mcbrawls.blueprint.resource.BlueprintManager
913
import net.minecraft.command.argument.IdentifierArgumentType
14+
import net.minecraft.command.argument.Vec3ArgumentType
1015
import net.minecraft.server.command.CommandManager.argument
1116
import net.minecraft.server.command.CommandManager.literal
1217
import net.minecraft.server.command.ServerCommandSource
1318
import net.minecraft.text.Text
19+
import net.minecraft.util.math.Vec3d
20+
import kotlin.math.max
21+
import kotlin.math.min
1422

1523
object BlueprintEditorCommand {
1624
const val BLUEPRINT_KEY = "blueprint"
1725
const val BLUEPRINT_ID_KEY = "blueprint_id"
26+
const val NAME_KEY = "name"
27+
const val POS_KEY = "pos"
28+
const val FIRST_KEY = "first"
29+
const val SECOND_KEY = "second"
30+
const val RADIUS_KEY = "radius"
1831

1932
private val NOT_BLUEPRINT_EDITOR_WORLD_EXCEPTION = SimpleCommandExceptionType(Text.literal("You are not in a Blueprint editor."))
2033
private val IN_BLUEPRINT_EDITOR_WORLD_EXCEPTION = SimpleCommandExceptionType(Text.literal("You are already in a Blueprint editor. Use /blueprint-editor close to leave."))
@@ -44,6 +57,32 @@ object BlueprintEditorCommand {
4457
.executes(::executeSave)
4558
)
4659
)
60+
.then(
61+
literal("region")
62+
.then(
63+
argument(NAME_KEY, StringArgumentType.word())
64+
.then(
65+
literal("cuboid")
66+
.then(
67+
argument(FIRST_KEY, Vec3ArgumentType.vec3())
68+
.then(
69+
argument(SECOND_KEY, Vec3ArgumentType.vec3())
70+
.executes(::executeCuboidRegion)
71+
)
72+
)
73+
)
74+
.then(
75+
literal("sphere")
76+
.then(
77+
argument(POS_KEY, Vec3ArgumentType.vec3())
78+
.then(
79+
argument(RADIUS_KEY, DoubleArgumentType.doubleArg(0.0))
80+
.executes(::executeSphericalRegion)
81+
)
82+
)
83+
)
84+
)
85+
)
4786
)
4887
}
4988

@@ -66,6 +105,42 @@ object BlueprintEditorCommand {
66105
return 1
67106
}
68107

108+
private fun executeCuboidRegion(context: CommandContext<ServerCommandSource>): Int {
109+
val regionId = StringArgumentType.getString(context, NAME_KEY)
110+
val firstPos = Vec3ArgumentType.getVec3(context, FIRST_KEY)
111+
val secondPos = Vec3ArgumentType.getVec3(context, SECOND_KEY)
112+
113+
val min = minVec(firstPos, secondPos)
114+
val max = maxVec(firstPos, secondPos)
115+
val size = max.subtract(min)
116+
117+
val rootPos = min.subtract(Vec3d.of(BlueprintEditorWorld.BLUEPRINT_PLACEMENT_POS))
118+
val region = CuboidRegion(rootPos, size)
119+
120+
val source = context.source
121+
val world = source.world as? BlueprintEditorWorld ?: throw NOT_BLUEPRINT_EDITOR_WORLD_EXCEPTION.create()
122+
world.addRegion(regionId, region)
123+
124+
source.sendFeedback({ Text.literal("Marked region \"$regionId\" at relative $rootPos (size $size)") }, true)
125+
return 1
126+
}
127+
128+
private fun executeSphericalRegion(context: CommandContext<ServerCommandSource>): Int {
129+
val regionId = StringArgumentType.getString(context, NAME_KEY)
130+
val pos = Vec3ArgumentType.getVec3(context, POS_KEY)
131+
val radius = DoubleArgumentType.getDouble(context, RADIUS_KEY)
132+
133+
val rootPos = pos.subtract(Vec3d.of(BlueprintEditorWorld.BLUEPRINT_PLACEMENT_POS))
134+
val region = SphericalRegion(rootPos, radius)
135+
136+
val source = context.source
137+
val world = source.world as? BlueprintEditorWorld ?: throw NOT_BLUEPRINT_EDITOR_WORLD_EXCEPTION.create()
138+
world.addRegion(regionId, region)
139+
140+
source.sendFeedback({ Text.literal("Marked region \"$regionId\" at relative $rootPos (radius $radius)") }, true)
141+
return 1
142+
}
143+
69144
private fun executeClose(context: CommandContext<ServerCommandSource>): Int {
70145
val source = context.source
71146
val world = source.world as? BlueprintEditorWorld ?: throw NOT_BLUEPRINT_EDITOR_WORLD_EXCEPTION.create()
@@ -91,4 +166,20 @@ object BlueprintEditorCommand {
91166

92167
return 1
93168
}
169+
170+
fun minVec(a: Vec3d, b: Vec3d): Vec3d {
171+
return Vec3d(
172+
min(a.getX(), b.getX()),
173+
min(a.getY(), b.getY()),
174+
min(a.getZ(), b.getZ())
175+
)
176+
}
177+
178+
fun maxVec(a: Vec3d, b: Vec3d): Vec3d {
179+
return Vec3d(
180+
max(a.getX(), b.getX()),
181+
max(a.getY(), b.getY()),
182+
max(a.getZ(), b.getZ())
183+
)
184+
}
94185
}

src/main/kotlin/net/mcbrawls/blueprint/editor/BlueprintEditorWorld.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.mcbrawls.blueprint.editor
22

3+
import net.mcbrawls.blueprint.region.serialization.SerializableRegion
34
import net.mcbrawls.blueprint.resource.BlueprintManager
45
import net.mcbrawls.blueprint.structure.Blueprint
56
import net.minecraft.block.BlockState
@@ -34,6 +35,8 @@ class BlueprintEditorWorld(
3435
val minPos: BlockPos get() = BlockPos(minX, minY, minZ)
3536
val maxPos: BlockPos get() = BlockPos(maxX, maxY, maxZ)
3637

38+
private val regions: MutableMap<String, SerializableRegion> = mutableMapOf()
39+
3740
/**
3841
* Initializes this world with the blueprint id.
3942
* @return whether this created a new blueprint
@@ -48,6 +51,7 @@ class BlueprintEditorWorld(
4851

4952
blueprint.place(this, BLUEPRINT_PLACEMENT_POS)
5053
blueprint.placeCreatorMarkers(this, BLUEPRINT_PLACEMENT_POS)
54+
regions.putAll(blueprint.regions)
5155
false
5256
} else {
5357
setBlockState(minPos, Blocks.STONE.defaultState)
@@ -73,6 +77,19 @@ class BlueprintEditorWorld(
7377
players.forEach { player ->
7478
updateExpectedSize(player.blockPos)
7579
}
80+
81+
/*regions.forEach { _, region ->
82+
if (region is CuboidRegion) {
83+
val pos = region.rootPosition.add(Vec3d.of(BLUEPRINT_PLACEMENT_POS))
84+
spawnParticles(DustParticleEffect(0xFF0000, 1.0f), pos.x, pos.y, pos.z, 1, 0.0, 0.0, 0.0, 0.0)
85+
val otherPos = region.rootPosition.add(region.size).add(Vec3d.of(BLUEPRINT_PLACEMENT_POS))
86+
spawnParticles(DustParticleEffect(0x0000FF, 1.0f), otherPos.x, otherPos.y, otherPos.z, 1, 0.0, 0.0, 0.0, 0.0)
87+
}
88+
if (region is SphericalRegion) {
89+
val pos = region.rootPosition.add(Vec3d.of(BLUEPRINT_PLACEMENT_POS))
90+
spawnParticles(DustParticleEffect(0xFF00FF, 1.0f), pos.x, pos.y, pos.z, 1, 0.0, 0.0, 0.0, 0.0)
91+
}
92+
}*/
7693
}
7794

7895
private fun updateExpectedSize(pos: BlockPos) {
@@ -126,10 +143,17 @@ class BlueprintEditorWorld(
126143
*/
127144
fun saveBlueprint(id: Identifier? = null): String {
128145
val (min, max) = getRoughBlueprintBoundingBox()
129-
val blueprint = Blueprint.save(this, min, max)
146+
val blueprint = Blueprint.save(this, min, max).let {
147+
it.copy(regions = it.regions + regions)
148+
}
149+
130150
return BlueprintManager.saveGenerated(server, id ?: blueprintId, blueprint)
131151
}
132152

153+
fun addRegion(id: String, region: SerializableRegion) {
154+
regions[id] = region
155+
}
156+
133157
companion object {
134158
val BLUEPRINT_PLACEMENT_POS = BlockPos(0, 128, 0)
135159
}

0 commit comments

Comments
 (0)