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
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();
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 >.<
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.
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().