1010import net .minecraft .commands .CommandSourceStack ;
1111import net .minecraft .commands .Commands ;
1212import net .minecraft .network .chat .Component ;
13+ import net .minecraft .network .chat .MutableComponent ;
14+ import net .minecraft .resources .ResourceLocation ;
1315import net .minecraft .server .MinecraftServer ;
1416
1517import org .valkyrienskies .core .api .ships .Ship ;
2022
2123public final class VSCCommands {
2224 public static final String ROOT_LITERAL = "vschunkloader" ;
25+ public static final ResourceLocation FORCELOAD_TOKEN = new ResourceLocation (Constants .MOD_ID , "command" );
2326
2427 private VSCCommands () {}
2528
@@ -28,33 +31,81 @@ public static void register(final CommandDispatcher<CommandSourceStack> dispatch
2831 .requires ((source ) -> source .hasPermission (2 ))
2932 .then (Commands .literal ("forceload" )
3033 .then (Commands .argument ("ships" , ShipArgument .Companion .ships ())
31- .then (Commands .argument ("forceload" , BoolArgumentType .bool ())
32- .executes (VSCCommands ::setForceLoad )
33- )
34+ .executes (VSCCommands ::forceLoad )
35+ )
36+ )
37+ .then (Commands .literal ("unforceload" )
38+ .then (Commands .argument ("ships" , ShipArgument .Companion .ships ())
39+ .executes (VSCCommands ::unforceLoad )
40+ )
41+ )
42+ .then (Commands .literal ("unforceload-all" )
43+ .then (Commands .argument ("ships" , ShipArgument .Companion .ships ())
44+ .executes (VSCCommands ::unforceLoadAll )
3445 )
3546 )
3647 .then (Commands .literal ("is-forceloaded" )
3748 .then (Commands .argument ("ships" , ShipArgument .Companion .ships ())
3849 .executes (VSCCommands ::isForceLoaded )
3950 )
4051 )
52+ .then (Commands .literal ("query-forceload-tokens" )
53+ .then (Commands .argument ("ships" , ShipArgument .Companion .ships ())
54+ .executes (VSCCommands ::queryForceLoadTokens )
55+ )
56+ )
57+ );
58+ }
59+
60+ private static int forceLoad (final CommandContext <CommandSourceStack > context ) throws CommandSyntaxException {
61+ final CommandSourceStack source = context .getSource ();
62+ final MinecraftServer server = source .getServer ();
63+ final Set <Ship > ships = ShipArgument .Companion .getShips ((CommandContext <VSCommandSource >)((CommandContext <?>)(context )), "ships" );
64+ int successCount = 0 ;
65+ for (final Ship ship : ships ) {
66+ if (VSCApi .forceLoad (server , ship .getId (), FORCELOAD_TOKEN , true )) {
67+ successCount ++;
68+ }
69+ }
70+ final int finalSuccessCount = successCount ;
71+ source .sendSuccess (() ->
72+ Component .translatable ("command." + Constants .MOD_ID + ".forceload" , finalSuccessCount ),
73+ true
74+ );
75+ return finalSuccessCount ;
76+ }
77+
78+ private static int unforceLoad (final CommandContext <CommandSourceStack > context ) throws CommandSyntaxException {
79+ final CommandSourceStack source = context .getSource ();
80+ final MinecraftServer server = source .getServer ();
81+ final Set <Ship > ships = ShipArgument .Companion .getShips ((CommandContext <VSCommandSource >)((CommandContext <?>)(context )), "ships" );
82+ int successCount = 0 ;
83+ for (final Ship ship : ships ) {
84+ if (VSCApi .forceLoad (server , ship .getId (), FORCELOAD_TOKEN , false )) {
85+ successCount ++;
86+ }
87+ }
88+ final int finalSuccessCount = successCount ;
89+ source .sendSuccess (() ->
90+ Component .translatable ("command." + Constants .MOD_ID + ".unforceload" , finalSuccessCount ),
91+ true
4192 );
93+ return finalSuccessCount ;
4294 }
4395
44- private static int setForceLoad (final CommandContext <CommandSourceStack > context ) throws CommandSyntaxException {
96+ private static int unforceLoadAll (final CommandContext <CommandSourceStack > context ) throws CommandSyntaxException {
4597 final CommandSourceStack source = context .getSource ();
4698 final MinecraftServer server = source .getServer ();
4799 final Set <Ship > ships = ShipArgument .Companion .getShips ((CommandContext <VSCommandSource >)((CommandContext <?>)(context )), "ships" );
48- final boolean forceload = BoolArgumentType .getBool (context , "forceload" );
49100 int successCount = 0 ;
50101 for (final Ship ship : ships ) {
51- if (VSCApi .forceLoad (server , ship .getId (), Constants . MOD_ID , forceload )) {
102+ if (VSCApi .clearForceLoadTokens (server , ship .getId ())) {
52103 successCount ++;
53104 }
54105 }
55106 final int finalSuccessCount = successCount ;
56107 source .sendSuccess (() ->
57- Component .translatable ("command." + Constants .MOD_ID + ".forceload." + ( forceload ? "load" : "unload" ) , finalSuccessCount ),
108+ Component .translatable ("command." + Constants .MOD_ID + ".unforceload" , finalSuccessCount ),
58109 true
59110 );
60111 return finalSuccessCount ;
@@ -79,4 +130,39 @@ private static int isForceLoaded(final CommandContext<CommandSourceStack> contex
79130 );
80131 return finalLoadedCount ;
81132 }
133+
134+ private static int queryForceLoadTokens (final CommandContext <CommandSourceStack > context ) throws CommandSyntaxException {
135+ final CommandSourceStack source = context .getSource ();
136+ final MinecraftServer server = source .getServer ();
137+ final Set <Ship > ships = ShipArgument .Companion .getShips ((CommandContext <VSCommandSource >)((CommandContext <?>)(context )), "ships" );
138+ if (ships .isEmpty ()) {
139+ source .sendFailure (Component .translatable ("argument.valkyrienskies.ship.no_found" ));
140+ return 0 ;
141+ }
142+ if (ships .size () > 1 ) {
143+ source .sendFailure (Component .translatable ("argument.valkyrienskies.ship.multiple_found" ));
144+ return 0 ;
145+ }
146+ final Ship ship = ships .iterator ().next ();
147+ final Set <ResourceLocation > tokens = VSCApi .getForceLoadTokens (server , ship .getId ());
148+ if (tokens == null ) {
149+ source .sendFailure (Component .translatable ("argument.valkyrienskies.ship.no_found" ));
150+ return 0 ;
151+ }
152+ final int count = tokens .size ();
153+ if (count == 0 ) {
154+ source .sendSuccess (() -> Component .translatable ("command." + Constants .MOD_ID + ".query_forceload.none" ), false );
155+ return 1 ;
156+ }
157+ source .sendSuccess (() -> {
158+ final MutableComponent component = Component .translatable ("command." + Constants .MOD_ID + ".query_forceload.title" , count );
159+ tokens .stream ()
160+ .map (ResourceLocation ::toString )
161+ .sorted ()
162+ .map ((token ) -> Component .literal ("\n - " ).append (token ))
163+ .forEach (component ::append );
164+ return component ;
165+ }, false );
166+ return count + 1 ;
167+ }
82168}
0 commit comments