4545import com .mojang .brigadier .exceptions .CommandSyntaxException ;
4646import com .mojang .brigadier .tree .LiteralCommandNode ;
4747
48+ import de .bluecolored .bluemap .api .BlueMapAPI ;
49+ import de .bluecolored .bluemap .api .BlueMapMap ;
50+ import de .bluecolored .bluemap .api .marker .MarkerAPI ;
51+ import de .bluecolored .bluemap .api .marker .MarkerSet ;
52+ import de .bluecolored .bluemap .api .marker .POIMarker ;
4853import de .bluecolored .bluemap .common .MapType ;
4954import de .bluecolored .bluemap .common .RenderTask ;
5055import de .bluecolored .bluemap .common .plugin .Plugin ;
5964import de .bluecolored .bluemap .core .world .World ;
6065
6166public class Commands <S > {
67+
68+ public static final String DEFAULT_MARKER_SET_ID = "markers" ;
6269
6370 private final Plugin plugin ;
6471 private final CommandDispatcher <S > dispatcher ;
@@ -95,15 +102,11 @@ public void init() {
95102 .requires (requirements ("bluemap.debug" ))
96103 .executes (this ::debugCommand )
97104
98- .then (argument ("world" , StringArgumentType .word ()).suggests (new WorldSuggestionProvider <>(plugin ))
105+ .then (argument ("world" , StringArgumentType .string ()).suggests (new WorldSuggestionProvider <>(plugin ))
99106 .then (argument ("x" , DoubleArgumentType .doubleArg ())
100107 .then (argument ("y" , DoubleArgumentType .doubleArg ())
101108 .then (argument ("z" , DoubleArgumentType .doubleArg ())
102- .executes (this ::debugCommand )
103- )
104- )
105- )
106- )
109+ .executes (this ::debugCommand )))))
107110 .build ();
108111
109112 LiteralCommandNode <S > pauseCommand =
@@ -124,45 +127,74 @@ public void init() {
124127 .executes (this ::renderCommand ) // /bluemap render
125128
126129 .then (argument ("radius" , IntegerArgumentType .integer ())
127- .executes (this ::renderCommand ) // /bluemap render <radius>
128- )
130+ .executes (this ::renderCommand )) // /bluemap render <radius>
129131
130132 .then (argument ("x" , DoubleArgumentType .doubleArg ())
131133 .then (argument ("z" , DoubleArgumentType .doubleArg ())
132134 .then (argument ("radius" , IntegerArgumentType .integer ())
133- .executes (this ::renderCommand ) // /bluemap render <x> <z> <radius>
134- )
135- )
136- )
135+ .executes (this ::renderCommand )))) // /bluemap render <x> <z> <radius>
137136
138- .then (argument ("world|map" , StringArgumentType .word ()).suggests (new WorldOrMapSuggestionProvider <>(plugin ))
137+ .then (argument ("world|map" , StringArgumentType .string ()).suggests (new WorldOrMapSuggestionProvider <>(plugin ))
139138 .executes (this ::renderCommand ) // /bluemap render <world|map>
140139
141140 .then (argument ("x" , DoubleArgumentType .doubleArg ())
142141 .then (argument ("z" , DoubleArgumentType .doubleArg ())
143142 .then (argument ("radius" , IntegerArgumentType .integer ())
144- .executes (this ::renderCommand ) // /bluemap render <world|map> <x> <z> <radius>
145- )
146- )
147- )
148- )
149-
143+ .executes (this ::renderCommand ))))) // /bluemap render <world|map> <x> <z> <radius>
150144 .build ();
151145
152146 LiteralCommandNode <S > prioRenderCommand =
153147 literal ("prioritize" )
154148 .requires (requirements ("bluemap.render" ))
155- .then (argument ("uuid" , StringArgumentType .word ())
156- .executes (this ::prioritizeRenderTaskCommand )
157- )
149+ .then (argument ("uuid" , StringArgumentType .string ())
150+ .executes (this ::prioritizeRenderTaskCommand ))
158151 .build ();
159152
160153 LiteralCommandNode <S > cancelRenderCommand =
161154 literal ("cancel" )
162155 .requires (requirements ("bluemap.render" ))
163- .then (argument ("uuid" , StringArgumentType .word ())
164- .executes (this ::cancelRenderTaskCommand )
165- )
156+ .then (argument ("uuid" , StringArgumentType .string ())
157+ .executes (this ::cancelRenderTaskCommand ))
158+ .build ();
159+
160+ LiteralCommandNode <S > worldsCommand =
161+ literal ("worlds" )
162+ .requires (requirements ("bluemap.status" ))
163+ .executes (this ::worldsCommand )
164+ .build ();
165+
166+ LiteralCommandNode <S > mapsCommand =
167+ literal ("maps" )
168+ .requires (requirements ("bluemap.status" ))
169+ .executes (this ::mapsCommand )
170+ .build ();
171+
172+ LiteralCommandNode <S > markerCommand =
173+ literal ("marker" )
174+ .requires (requirements ("bluemap.marker" ))
175+ .build ();
176+
177+ LiteralCommandNode <S > createMarkerCommand =
178+ literal ("create" )
179+ .requires (requirements ("bluemap.marker" ))
180+ .then (argument ("id" , StringArgumentType .word ())
181+ .then (argument ("map" , StringArgumentType .string ()).suggests (new MapSuggestionProvider <>(plugin ))
182+
183+ .then (argument ("label" , StringArgumentType .string ())
184+ .executes (this ::createMarkerCommand ))
185+
186+ .then (argument ("x" , DoubleArgumentType .doubleArg ())
187+ .then (argument ("y" , DoubleArgumentType .doubleArg ())
188+ .then (argument ("z" , DoubleArgumentType .doubleArg ())
189+ .then (argument ("label" , StringArgumentType .string ())
190+ .executes (this ::createMarkerCommand )))))))
191+ .build ();
192+
193+ LiteralCommandNode <S > removeMarkerCommand =
194+ literal ("remove" )
195+ .requires (requirements ("bluemap.marker" ))
196+ .then (argument ("id" , StringArgumentType .word ()).suggests (MarkerIdSuggestionProvider .getInstance ())
197+ .executes (this ::removeMarkerCommand ))
166198 .build ();
167199
168200 // command tree
@@ -174,6 +206,11 @@ public void init() {
174206 baseCommand .addChild (renderCommand );
175207 renderCommand .addChild (prioRenderCommand );
176208 renderCommand .addChild (cancelRenderCommand );
209+ baseCommand .addChild (worldsCommand );
210+ baseCommand .addChild (mapsCommand );
211+ baseCommand .addChild (markerCommand );
212+ markerCommand .addChild (createMarkerCommand );
213+ markerCommand .addChild (removeMarkerCommand );
177214 }
178215
179216 private Predicate <S > requirements (String permission ){
@@ -454,4 +491,135 @@ public int cancelRenderTaskCommand(CommandContext<S> context) {
454491 source .sendMessage (Text .of (TextColor .RED , "There is no render-task with this UUID: " + uuidString ));
455492 return 0 ;
456493 }
494+
495+ public int worldsCommand (CommandContext <S > context ) {
496+ CommandSource source = commandSourceInterface .apply (context .getSource ());
497+
498+ source .sendMessage (Text .of (TextColor .BLUE , "Worlds loaded by BlueMap:" ));
499+ for (World world : plugin .getWorlds ()) {
500+ source .sendMessage (Text .of (TextColor .GRAY , " - " , TextColor .WHITE , world .getName ()).setHoverText (Text .of (TextColor .GRAY , world .getUUID ())));
501+ }
502+
503+ return 1 ;
504+ }
505+
506+ public int mapsCommand (CommandContext <S > context ) {
507+ CommandSource source = commandSourceInterface .apply (context .getSource ());
508+
509+ source .sendMessage (Text .of (TextColor .BLUE , "Maps loaded by BlueMap:" ));
510+ for (MapType map : plugin .getMapTypes ()) {
511+ source .sendMessage (Text .of (TextColor .GRAY , " - " , TextColor .WHITE , map .getId (), TextColor .GRAY , " (" + map .getName () + ")" ).setHoverText (Text .of (TextColor .WHITE , "World: " , TextColor .GRAY , map .getWorld ().getName ())));
512+ }
513+
514+ return 1 ;
515+ }
516+
517+ public int createMarkerCommand (CommandContext <S > context ) {
518+ CommandSource source = commandSourceInterface .apply (context .getSource ());
519+
520+ String markerId = context .getArgument ("id" , String .class );
521+ String markerLabel = context .getArgument ("label" , String .class )
522+ .replace ("<" , "<" )
523+ .replace (">" , ">" ); //no html via commands
524+
525+ // parse world/map argument
526+ String mapString = context .getArgument ("map" , String .class );
527+ MapType map = parseMap (mapString ).orElse (null );
528+
529+ if (map == null ) {
530+ source .sendMessage (Text .of (TextColor .RED , "There is no " , helper .mapHelperHover (), " with this name: " , TextColor .WHITE , mapString ));
531+ return 0 ;
532+ }
533+
534+ // parse position
535+ Optional <Double > x = getOptionalArgument (context , "x" , Double .class );
536+ Optional <Double > y = getOptionalArgument (context , "y" , Double .class );
537+ Optional <Double > z = getOptionalArgument (context , "z" , Double .class );
538+
539+ Vector3d position ;
540+
541+ if (x .isPresent () && y .isPresent () && z .isPresent ()) {
542+ position = new Vector3d (x .get (), y .get (), z .get ());
543+ } else {
544+ position = source .getPosition ().orElse (null );
545+
546+ if (position == null ) {
547+ source .sendMessage (Text .of (TextColor .RED , "Can't detect a position from this command-source, you'll have to define the x,y,z coordinates for the marker!" ).setHoverText (Text .of (TextColor .GRAY , "/bluemap marker create " + markerId + " " + "[world|map] <x> <y> <z> <label>" )));
548+ return 0 ;
549+ }
550+ }
551+
552+ // get api
553+ BlueMapAPI api = BlueMapAPI .getInstance ().orElse (null );
554+ if (api == null ) {
555+ source .sendMessage (Text .of (TextColor .RED , "MarkerAPI is not available, try " , TextColor .GRAY , "/bluemap reload" ));
556+ return 0 ;
557+ }
558+
559+ // resolve api-map
560+ Optional <BlueMapMap > apiMap = api .getMap (map .getId ());
561+ if (!apiMap .isPresent ()) {
562+ source .sendMessage (Text .of (TextColor .RED , "Failed to get map from API, try " , TextColor .GRAY , "/bluemap reload" ));
563+ return 0 ;
564+ }
565+
566+ // add marker
567+ try {
568+ MarkerAPI markerApi = api .getMarkerAPI ();
569+
570+ MarkerSet set = markerApi .getMarkerSet (DEFAULT_MARKER_SET_ID ).orElse (null );
571+ if (set == null ) {
572+ set = markerApi .createMarkerSet (DEFAULT_MARKER_SET_ID );
573+ set .setLabel ("Markers" );
574+ }
575+
576+ if (set .getMarker (markerId ).isPresent ()) {
577+ source .sendMessage (Text .of (TextColor .RED , "There already is a marker with this id: " , TextColor .WHITE , markerId ));
578+ return 0 ;
579+ }
580+
581+ POIMarker marker = set .createPOIMarker (markerId , apiMap .get (), position );
582+ marker .setLabel (markerLabel );
583+
584+ markerApi .save ();
585+ MarkerIdSuggestionProvider .getInstance ().forceUpdate ();
586+ } catch (IOException e ) {
587+ source .sendMessage (Text .of (TextColor .RED , "There was an error trying to add the marker, please check the console for details!" ));
588+ Logger .global .logError ("Exception trying to add a marker!" , e );
589+ }
590+
591+ source .sendMessage (Text .of (TextColor .GREEN , "Marker added!" ));
592+ return 1 ;
593+ }
594+
595+ public int removeMarkerCommand (CommandContext <S > context ) {
596+ CommandSource source = commandSourceInterface .apply (context .getSource ());
597+
598+ String markerId = context .getArgument ("id" , String .class );
599+
600+ BlueMapAPI api = BlueMapAPI .getInstance ().orElse (null );
601+ if (api == null ) {
602+ source .sendMessage (Text .of (TextColor .RED , "MarkerAPI is not available, try " , TextColor .GRAY , "/bluemap reload" ));
603+ return 0 ;
604+ }
605+
606+ try {
607+ MarkerAPI markerApi = api .getMarkerAPI ();
608+
609+ MarkerSet set = markerApi .createMarkerSet ("markers" );
610+ if (!set .removeMarker (markerId )) {
611+ source .sendMessage (Text .of (TextColor .RED , "There is no marker with this id: " , TextColor .WHITE , markerId ));
612+ }
613+
614+ markerApi .save ();
615+ MarkerIdSuggestionProvider .getInstance ().forceUpdate ();
616+ } catch (IOException e ) {
617+ source .sendMessage (Text .of (TextColor .RED , "There was an error trying to remove the marker, please check the console for details!" ));
618+ Logger .global .logError ("Exception trying to remove a marker!" , e );
619+ }
620+
621+ source .sendMessage (Text .of (TextColor .GREEN , "Marker removed!" ));
622+ return 1 ;
623+ }
624+
457625}
0 commit comments