11package net.mcbrawls.blueprint.command
22
33import com.mojang.brigadier.CommandDispatcher
4+ import com.mojang.brigadier.arguments.DoubleArgumentType
5+ import com.mojang.brigadier.arguments.StringArgumentType
46import com.mojang.brigadier.context.CommandContext
57import com.mojang.brigadier.exceptions.SimpleCommandExceptionType
68import net.mcbrawls.blueprint.editor.BlueprintEditorHandler
79import net.mcbrawls.blueprint.editor.BlueprintEditorWorld
10+ import net.mcbrawls.blueprint.region.CuboidRegion
11+ import net.mcbrawls.blueprint.region.SphericalRegion
812import net.mcbrawls.blueprint.resource.BlueprintManager
913import net.minecraft.command.argument.IdentifierArgumentType
14+ import net.minecraft.command.argument.Vec3ArgumentType
1015import net.minecraft.server.command.CommandManager.argument
1116import net.minecraft.server.command.CommandManager.literal
1217import net.minecraft.server.command.ServerCommandSource
1318import net.minecraft.text.Text
19+ import net.minecraft.util.math.Vec3d
20+ import kotlin.math.max
21+ import kotlin.math.min
1422
1523object 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}
0 commit comments