4343import com .mojang .brigadier .exceptions .CommandSyntaxException ;
4444import com .mojang .brigadier .exceptions .SimpleCommandExceptionType ;
4545import com .mojang .brigadier .suggestion .SuggestionProvider ;
46+ import java .util .function .Consumer ;
4647import net .minecraft .commands .CommandSourceStack ;
4748import net .minecraft .commands .SharedSuggestionProvider ;
4849import net .minecraft .core .BlockPos ;
@@ -104,12 +105,19 @@ public static void init() {
104105 return buildMultiblock (ctx .getSource (), getLoadedBlockPos (ctx , "controller_pos" ));
105106 }))
106107 )
107- .then (literal ("test_structure" )
108- .then (argument ("structure_id" , id ())
109- .then (argument ("controller_pos" , blockPos ())
110- .executes (ctx -> {
111- return testStructure (ctx .getSource (), getId (ctx , "structure_id" ), getLoadedBlockPos (ctx , "controller_pos" ));
112- }))))
108+ .then (literal ("structures" )
109+ .then (literal ("test" )
110+ .then (argument ("structure_id" , id ())
111+ .then (argument ("controller_pos" , blockPos ())
112+ .executes (ctx -> {
113+ return structuresTest (ctx .getSource (), getId (ctx , "structure_id" ), getLoadedBlockPos (ctx , "controller_pos" ));
114+ }))))
115+ .then (literal ("build" )
116+ .then (argument ("structure_id" , id ())
117+ .then (argument ("controller_pos" , blockPos ())
118+ .executes (ctx -> {
119+ return structuresBuild (ctx .getSource (), getId (ctx , "structure_id" ), getLoadedBlockPos (ctx , "controller_pos" ));
120+ })))))
113121 )
114122 );
115123 });
@@ -195,7 +203,7 @@ private static int buildMultiblock(CommandSourceStack src, BlockPos controllerPo
195203 return Command .SINGLE_SUCCESS ;
196204 }
197205
198- private static int testStructure (CommandSourceStack src , ResourceLocation id , BlockPos controllerPos ) {
206+ private static int structures (CommandSourceStack src , ResourceLocation id , BlockPos controllerPos , Consumer < ShapeMatcher > action ) {
199207 BlockState controllerState = src .getLevel ().getBlockState (controllerPos );
200208 if (controllerState .is (MIBlock .STRUCTURE_MULTIBLOCK_CONTROLLER .get ())) {
201209 CompoundTag tag = MIStructureTemplateManager .load (id );
@@ -206,11 +214,26 @@ private static int testStructure(CommandSourceStack src, ResourceLocation id, Bl
206214 ShapeTemplate shape = MIStructureTemplateManager .deserialize (MachineCasings .CLEAN_STAINLESS_STEEL , tag );
207215 ShapeMatcher matcher = new ShapeMatcher (src .getLevel (), controllerPos , controllerState .getValue (BlockStateProperties .HORIZONTAL_FACING ),
208216 shape );
209- matcher .rematch (src .getLevel ());
210- src .sendSuccess (() -> Component .literal ("Match test results: %s" .formatted (matcher .isMatchSuccessful ())), true );
217+ action .accept (matcher );
211218 } else {
212- src .sendFailure (Component .literal ("Block at position %s is not a controller." .formatted (controllerPos )));
219+ src .sendFailure (Component .literal ("Block at position %s is not a structure controller." .formatted (controllerPos . toShortString () )));
213220 }
214221 return Command .SINGLE_SUCCESS ;
215222 }
223+
224+ private static int structuresTest (CommandSourceStack src , ResourceLocation id , BlockPos controllerPos ) {
225+ return structures (src , id , controllerPos , (matcher ) -> {
226+ matcher .rematch (src .getLevel ());
227+ boolean success = matcher .isMatchSuccessful ();
228+ matcher .unlinkHatches ();
229+ src .sendSuccess (() -> Component .literal ("Match test results for %s at position %s: %s" .formatted (id .toString (), controllerPos .toShortString (), success )), true );
230+ });
231+ }
232+
233+ private static int structuresBuild (CommandSourceStack src , ResourceLocation id , BlockPos controllerPos ) {
234+ return structures (src , id , controllerPos , (matcher ) -> {
235+ matcher .buildMultiblock (src .getLevel ());
236+ src .sendSuccess (() -> Component .literal ("Built multiblock %s at position %s" .formatted (id .toString (), controllerPos .toShortString ())), true );
237+ });
238+ }
216239}
0 commit comments