2525package org .spongepowered .api .service .command ;
2626
2727
28+ import static org .spongepowered .api .service .command .TranslationPlaceholder .t ;
29+
2830import com .flowpowered .math .vector .Vector3d ;
2931import com .google .common .base .Function ;
3032import com .google .common .base .Optional ;
3436import org .spongepowered .api .Game ;
3537import org .spongepowered .api .entity .player .Player ;
3638import org .spongepowered .api .text .Text ;
37- import org .spongepowered .api .text .Texts ;
3839import org .spongepowered .api .util .StartsWithPredicate ;
3940import org .spongepowered .api .util .command .CommandSource ;
4041import org .spongepowered .api .util .command .args .ArgumentParseException ;
4142import org .spongepowered .api .util .command .args .CommandArgs ;
4243import org .spongepowered .api .util .command .args .CommandContext ;
4344import org .spongepowered .api .util .command .args .CommandElement ;
45+ import org .spongepowered .api .world .DimensionType ;
46+ import org .spongepowered .api .world .Location ;
47+ import org .spongepowered .api .world .World ;
4448
4549import java .util .List ;
4650import java .util .UUID ;
5054public class GameArguments {
5155 private GameArguments () {}
5256
53- private static Text t (String input ) {
54- return Texts .of (input );
57+ /**
58+ * Expect an argument to represent an online player,
59+ * or if nothing matches and the source is a {@link Player}, give the player.
60+ * Gives value of type {@link Player}
61+ *
62+ * @param key The key to store under
63+ * @param game The game to find players in
64+ * @return the argument
65+ */
66+ public static CommandElement playerOrSource (Text key , Game game ) {
67+ return new PlayerCommandElement (key , game , true );
5568 }
5669
5770 /**
5871 * Expect an argument to represent an online player.
72+ * Gives value of type {@link Player}
5973 *
6074 * @param key The key to store under
6175 * @param game The game to find players in
6276 * @return the argument
6377 */
6478 public static CommandElement player (Text key , Game game ) {
65- return new PlayerCommandElement (key , game );
79+ return new PlayerCommandElement (key , game , false );
6680 }
6781
6882 private static class PlayerCommandElement extends CommandElement {
6983 private final Game game ;
84+ private final boolean returnSource ;
7085
71- protected PlayerCommandElement (Text key , Game game ) {
86+ protected PlayerCommandElement (Text key , Game game , boolean returnSource ) {
7287 super (key );
7388 this .game = game ;
89+ this .returnSource = returnSource ;
7490 }
7591
7692 @ Override
7793 protected Object parseValue (CommandSource source , CommandArgs args ) throws ArgumentParseException {
7894 // TODO: Make player name resolution better -- support selectors, etc
95+ if (!args .hasNext () && this .returnSource ) {
96+ return tryReturnSource (source , args );
97+ }
98+
7999 final String playerName = args .next ();
80100 Optional <Player > ret ;
81101 try {
@@ -85,12 +105,24 @@ protected Object parseValue(CommandSource source, CommandArgs args) throws Argum
85105 ret = this .game .getServer ().getPlayer (playerName );
86106 }
87107 if (!ret .isPresent ()) {
88- throw args .createError (t ("No matching players found!" ));
108+ if (this .returnSource ) {
109+ return tryReturnSource (source , args );
110+ } else {
111+ throw args .createError (t ("No matching players found!" ));
112+ }
89113 } else {
90114 return ret .get ();
91115 }
92116 }
93117
118+ private Player tryReturnSource (CommandSource source , CommandArgs args ) throws ArgumentParseException {
119+ if (source instanceof Player ) {
120+ return ((Player ) source );
121+ } else {
122+ throw args .createError (t ("No players matched and source was not a player!" ));
123+ }
124+ }
125+
94126 @ Override
95127 public List <String > complete (CommandSource src , CommandArgs args , CommandContext context ) {
96128 Iterable <String > players = Iterables .transform (this .game .getServer ().getOnlinePlayers (), new Function <Player , String >() {
@@ -110,6 +142,7 @@ public String apply(@Nullable Player input) {
110142
111143 /**
112144 * Expect an argument to represent a world.
145+ * Gives values of type {@link World}
113146 *
114147 * @param key The key to store under
115148 * @param game The game to find worlds from
@@ -129,15 +162,43 @@ protected WorldCommandElement(Text key, Game game) {
129162
130163 @ Override
131164 protected Object parseValue (CommandSource source , CommandArgs args ) throws ArgumentParseException {
132- return null ;
165+ final String worldName = args .next ();
166+ Optional <World > ret = this .game .getServer ().getWorld (worldName );
167+ if (!ret .isPresent ()) {
168+ throw args .createError (t ("Unable to find world for name %s" , worldName ));
169+ }
170+ return ret .get ();
133171 }
134172
135173 @ Override
136174 public List <String > complete (CommandSource src , CommandArgs args , CommandContext context ) {
137- return null ;
175+ final Optional <String > worldNameComponent = args .nextIfPresent ();
176+ Iterable <String > worldsList = Iterables .transform (this .game .getServer ().getWorlds (), new Function <World , String >() {
177+ @ Nullable
178+ @ Override
179+ public String apply (@ Nullable World input ) {
180+ return input == null ? null : input .getName ();
181+ }
182+ });
183+ if (worldNameComponent .isPresent ()) {
184+ worldsList = Iterables .filter (worldsList , new StartsWithPredicate (worldNameComponent .get ()));
185+ }
186+ return ImmutableList .copyOf (worldsList );
138187 }
139188 }
140189
190+ /**
191+ * Expect an argument to represent a dimension.
192+ * Gives values of tye {@link DimensionType}
193+ *
194+ * @param key The key to store under
195+ * @param game The game to find dimensions from
196+ * @return the argument
197+ */
198+ public static CommandElement dimension (Text key , Game game ) {
199+ return catalogedElement (key , game , DimensionType .class );
200+ }
201+
141202 /**
142203 * Expect an argument to represent a {@link Vector3d}.
143204 *
@@ -166,7 +227,7 @@ public List<String> complete(CommandSource src, CommandArgs args, CommandContext
166227 }
167228
168229 /**
169- * Expect an argument to represent a location .
230+ * Expect an argument to represent a {@link Location} .
170231 *
171232 * @param key The key to store under
172233 * @param game The game to find worlds from
@@ -196,7 +257,7 @@ public List<String> complete(CommandSource src, CommandArgs args, CommandContext
196257 }
197258
198259 /**
199- * Expect an argument that is a member of the specified catalog type.
260+ * Expect an argument that is a member of the specified catalog type {@link T} .
200261 *
201262 * @param key The key to store the resolved value under
202263 * @param game The game to get the registry from
0 commit comments