Turning args into a Player [4.1.0]

Hello

I’m trying to understand how to do this

Turn the args from a command such as:
/command [args]

Into something like
args = Player target

So I can use these:
https://jd.spongepowered.org/3.0.0/org/spongepowered/api/entity/living/player/Player.html

So for example
Command:
/pinfo [player]
Result:
Name: [player]
First Joined: [date]

I just don’t understand how to do it, I’ve been looking at forums, Wiki’s, other plugin code, the Sponge docs for the last hour and I am still no closer to understanding

I’m sorry that my knowledge is lacking and this may seem simple, but It’s really not to me.

Any help is appreciated

  • Alice

EDIT noticed that <> made what was inside them disappear o.O

You may find the UserStorageService useful.

I don’t know how that will help me with this, but it’ll definitely be useful with other stuff, thank you

IUsually the way to get a player from arguments is by using this inside your execute method inside your executor
But you also have to make sure you set your arguments correctly when building your command spec.

//What your executor should look like, somewhat.
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException{
    if(!(src instanceof Player)){
        //Return a message or whatever
        return CommandResult.sucess();
    }
    Player player = args.<Player>.getOne("player").get();
    //Do whatever with player down here
}
//What your CommandSpec builder should look like.
CommandSpec cmdSpec = CommandSpec.builder()
    .description(Text.of("Whatever"))
    .arguments(GenericArguments.player(Text.of("player")))
    .build();
2 Likes

Highly recommend reading through the docs on commands, it covers this exact use case and many others: https://docs.spongepowered.org/en/plugin/commands/index.html

1 Like

So this is what makes it into a Player type Object?

And just to give you a bit of background/ context, this is what I have so far:
https://github.com/DragonTechMC/DTEssentials

I’m a Modded MC community owner, very new to Java and making plugins (Did a little with Bukkit, but didn’t see much point going into it as it was already discontinued by then)

What I’ve got on my github so far took me a good… 20 hours to do, I really struggle to understand how it works.

CommandSpec allows you to automatically process command arguments. So you tell it that the first argument will be a player, it will take a name/uuid and find the corresponding player, and insert that into the CommandArgs object.

So you can then check/access it with args.getOne(“player”). the casts that object to the correct type, in this case, Player.

Yes, getOne() returns an optional which you can get the player from. Again as @ZephireNZ said, you can learn a lot from reading the docs and javadocs I Reccomended you read them throroughly enough so you understand completely how it works. There are even some example uses and some very useful examples that are on there. They can become your best friend sometimes.

I must have read that command section at least 5 times (while I was trying to work out how to make commands), I just don’t understand it until I make it work for myself, and even then… I’m so bad at this >.<

Thanks for the assist :slight_smile:

Here’s a working example:

Sponge will automatically parse the argument for you when you use GenericArguments.player(Text.of("player")). Allowing you to then use Player player = args.<Player>getOne("player").get(); inside your executor.

Since the the argument isn’t described as optional, it’s safe to skip the isPresent check.

2 Likes

IIRC this will throw exceptions because GenericArgs.player returns a list, not a singular. You would use #getAll, or else GenericArguments.onlyOne().

Not sure if you’re referring to my example - but my example works on the latest master.

Thank you so very much, that makes a lot more sense.

Thanks for taking the time to help :slight_smile:

- Alice

I am referring to your example, and it will throw an exception if multiple players are passed. You should always wrap player() in onlyOne() if you plan on using getOne().