11package org .mvplugins .multiverse .core .commandtools ;
22
3+ import java .util .ArrayList ;
34import java .util .Arrays ;
45import java .util .Collection ;
56import java .util .Collections ;
2021import com .google .common .collect .Sets ;
2122import io .vavr .control .Try ;
2223import jakarta .inject .Inject ;
24+ import org .apache .commons .lang .Validate ;
25+ import org .bukkit .Bukkit ;
2326import org .bukkit .Difficulty ;
2427import org .bukkit .GameMode ;
2528import org .bukkit .GameRule ;
2629import org .bukkit .World ;
30+ import org .bukkit .command .CommandSender ;
2731import org .bukkit .entity .Player ;
2832import org .jetbrains .annotations .NotNull ;
2933import org .jvnet .hk2 .annotations .Service ;
@@ -75,6 +79,7 @@ public class MVCommandCompletions extends PaperCommandCompletions {
7579 registerAsyncCompletion ("mvworlds" , this ::suggestMVWorlds );
7680 registerAsyncCompletion ("mvworldpropsname" , this ::suggestMVWorldPropsName );
7781 registerAsyncCompletion ("mvworldpropsvalue" , this ::suggestMVWorldPropsValue );
82+ registerAsyncCompletion ("playersarray" , this ::suggestPlayersArray );
7883 registerStaticCompletion ("propsmodifyaction" , suggestEnums (PropertyModifyAction .class ));
7984
8085 setDefaultCompletion ("destinations" , DestinationInstance .class );
@@ -86,6 +91,39 @@ public class MVCommandCompletions extends PaperCommandCompletions {
8691 setDefaultCompletion ("mvworlds" , LoadedMultiverseWorld .class );
8792 }
8893
94+ @ Override
95+ public CommandCompletionHandler registerCompletion (String id , CommandCompletionHandler <BukkitCommandCompletionContext > handler ) {
96+ return super .registerCompletion (id , context ->
97+ completeWithPreconditions (context , handler ));
98+ }
99+
100+ @ Override
101+ public CommandCompletionHandler registerAsyncCompletion (String id , AsyncCommandCompletionHandler <BukkitCommandCompletionContext > handler ) {
102+ return super .registerAsyncCompletion (id , context ->
103+ completeWithPreconditions (context , handler ));
104+ }
105+
106+ private Collection <String > completeWithPreconditions (
107+ BukkitCommandCompletionContext context ,
108+ CommandCompletionHandler <BukkitCommandCompletionContext > handler ) {
109+ if (context .hasConfig ("playerOnly" ) && !context .getIssuer ().isPlayer ()) {
110+ return Collections .emptyList ();
111+ }
112+ if (context .hasConfig ("resolveUntil" )) {
113+ if (!Try .run (() -> context .getContextValueByName (Object .class , context .getConfig ("resolveUntil" ))).isSuccess ()) {
114+ return Collections .emptyList ();
115+ }
116+ }
117+ if (context .hasConfig ("checkPermissions" )) {
118+ for (String permission : context .getConfig ("checkPermissions" ).split (";" )) {
119+ if (!commandManager .getCommandPermissions ().hasPermission (context .getIssuer (), permission )) {
120+ return Collections .emptyList ();
121+ }
122+ }
123+ }
124+ return handler .getCompletions (context );
125+ }
126+
89127 /**
90128 * Shortcut to suggest enums values
91129 *
@@ -128,9 +166,6 @@ private boolean checkPerms(CommandIssuer issuer, RegisteredCommand<?> command) {
128166 }
129167
130168 private Collection <String > suggestDestinations (BukkitCommandCompletionContext context ) {
131- if (context .hasConfig ("playerOnly" ) && !context .getIssuer ().isPlayer ()) {
132- return Collections .emptyList ();
133- }
134169 return Try .of (() -> context .getContextValue (Player [].class ))
135170 .map (players -> {
136171 Player player = Arrays .stream (players )
@@ -141,6 +176,9 @@ private Collection<String> suggestDestinations(BukkitCommandCompletionContext co
141176 // Most likely console did not specify a player
142177 return Collections .<String >emptyList ();
143178 }
179+ if (context .hasConfig ("othersOnly" ) && player .equals (context .getPlayer ())) {
180+ return Collections .<String >emptyList ();
181+ }
144182 return suggestDestinationsWithPerms ((BukkitCommandIssuer ) context .getIssuer (), player , context .getInput ());
145183 })
146184 .getOrElse (Collections .emptyList ());
@@ -185,22 +223,10 @@ private Collection<String> suggestMVConfigValues(BukkitCommandCompletionContext
185223 }
186224
187225 private Collection <String > suggestMVWorlds (BukkitCommandCompletionContext context ) {
188- if (context .hasConfig ("playerOnly" ) && !context .getIssuer ().isPlayer ()) {
189- return Collections .emptyList ();
190- }
191-
192226 if (!context .hasConfig ("multiple" )) {
193227 return getMVWorldNames (context );
194228 }
195-
196- String input = context .getInput ();
197- int lastComma = input .lastIndexOf (',' );
198- String currentWorldsString = input .substring (0 , lastComma + 1 );
199- Set <String > currentWorlds = Sets .newHashSet (input .split ("," ));
200- return getMVWorldNames (context ).stream ()
201- .filter (world -> !currentWorlds .contains (world ))
202- .map (world -> currentWorldsString + world )
203- .collect (Collectors .toList ());
229+ return addonToCommaSeperated (context .getInput (), getMVWorldNames (context ));
204230 }
205231
206232 private List <String > getMVWorldNames (BukkitCommandCompletionContext context ) {
@@ -248,4 +274,31 @@ private Collection<String> suggestMVWorldPropsValue(BukkitCommandCompletionConte
248274 return world .getStringPropertyHandle ().getSuggestedPropertyValue (propertyName , context .getInput (), action );
249275 }).getOrElse (Collections .emptyList ());
250276 }
277+
278+
279+ private Collection <String > suggestPlayersArray (BukkitCommandCompletionContext context ) {
280+ CommandSender sender = context .getSender ();
281+ Validate .notNull (sender , "Sender cannot be null" );
282+ Player senderPlayer = sender instanceof Player ? (Player )sender : null ;
283+ List <String > matchedPlayers = new ArrayList <>();
284+
285+ for (Player player : Bukkit .getOnlinePlayers ()) {
286+ String name = player .getName ();
287+ if ((senderPlayer == null || senderPlayer .canSee (player ))
288+ && (!context .hasConfig ("excludeSelf" ) || !player .equals (senderPlayer ))) {
289+ matchedPlayers .add (name );
290+ }
291+ }
292+ return addonToCommaSeperated (context .getInput (), matchedPlayers );
293+ }
294+
295+ private Collection <String > addonToCommaSeperated (String input , Collection <String > addons ) {
296+ int lastComma = input .lastIndexOf (',' );
297+ String previousInputs = input .substring (0 , lastComma + 1 );
298+ Set <String > inputSet = Sets .newHashSet (input .split ("," ));
299+ return addons .stream ()
300+ .filter (suggestion -> !inputSet .contains (suggestion ))
301+ .map (suggestion -> previousInputs + suggestion )
302+ .toList ();
303+ }
251304}
0 commit comments