@Context
To get more information about the sender or the command's execution context, such as CommandSender
, Player
, Location
, World
, etc. you can use the @Context
annotation.
java
@Command(name = "health")
public class HealthCommand {
@Execute // console can't execute it!
void health(@Context Player sender) {
// /health
double health = sender.getHealth();
sender.sendMessage("Your current health is: " + health);
}
}
WARNING
If you use the @Context
annotation, the command runs only when the context is resolved.
In the example, it executes only if the sender is a player; otherwise, it shows an LiteMessages.PLAYER_ONLY
message.
You can also use @Context
with other annotations, such as @Bind
, @Arg
, @Flag
and more!
E.g. to teleport a player to another player /teleport <target>
:
java
@Command(name = "teleport")
public class TeleportCommand {
@Execute
void teleport(
@Bind Server server,
@Context Player sender,
@Arg Player target
) {
sender.teleport(target.getLocation());
}
}
TIP
You can also create a custom context provider for your types! Check out the Custom Context.