69 lines
No EOL
3 KiB
Markdown
69 lines
No EOL
3 KiB
Markdown
As of v2.2.0, a command for getting/setting coins of a player could look like this:
|
|
```java
|
|
public class CoinsCommand {
|
|
|
|
@Getter
|
|
private final CommandNode<CommandContext<CommandSender>> rootNode;
|
|
private final CommandArgument<CommandContext<CommandSender>, OfflinePlayer> playerArg;
|
|
private final CommandArgument<CommandContext<CommandSender>, Integer> amountArg;
|
|
|
|
public CoinsCommand() {
|
|
rootNode = ScrowAPI.getCommandManager().createNode("coins");
|
|
this.playerArg = ScrowAPI.getCommandManager().createArgument("player", OfflinePlayerArgumentType.getInstance());
|
|
this.amountArg = ScrowAPI.getCommandManager().createArgument("amount", IntegerArgumentType.getInstance());
|
|
|
|
{
|
|
var setLiteral = ScrowAPI.getCommandManager().createNode("set");
|
|
rootNode.addLiteral(setLiteral);
|
|
setLiteral.addArgument(playerArg);
|
|
setLiteral.addArgument(amountArg);
|
|
setLiteral.setExecutor(this::setCoins);
|
|
}
|
|
{
|
|
var getLiteral = ScrowAPI.getCommandManager().createNode("get");
|
|
rootNode.addLiteral(getLiteral);
|
|
getLiteral.addArgument(playerArg);
|
|
getLiteral.setExecutor(this::getCoins);
|
|
}
|
|
}
|
|
|
|
private void getCoins(CommandContext<CommandSender> ctx) {
|
|
var player = ctx.getArg(playerArg);
|
|
|
|
ScrowAPI.getEconomyService()
|
|
.getCoins(player.getUniqueId())
|
|
.subscribeOn(Schedulers.boundedElastic())
|
|
.publishOn(ScrowAPI.getMinecraftScheduler())
|
|
.subscribe(amount -> {
|
|
if (player == ctx.getSender()) {
|
|
ctx.getSender().sendMessage("You have " + amount + "$");
|
|
} else {
|
|
ctx.getSender().sendMessage(player.getName() + " has " + amount + "$");
|
|
}
|
|
}, err -> {
|
|
ctx.getSender().sendMessage("Unable to get coins: " + err.getMessage());
|
|
Bukkit.getLogger().log(Level.SEVERE, "Unable to get coins of " + player.getUniqueId(), err);
|
|
});
|
|
}
|
|
|
|
private void setCoins(CommandContext<CommandSender> ctx) {
|
|
var player = ctx.getArg(playerArg);
|
|
var amount = ctx.getArg(amountArg);
|
|
|
|
ScrowAPI.getEconomyService()
|
|
.setCoins(player.getUniqueId(), amount)
|
|
.subscribeOn(Schedulers.boundedElastic())
|
|
.publishOn(ScrowAPI.getMinecraftScheduler())
|
|
.subscribe(__ -> {
|
|
ctx.getSender().sendMessage(player.getName() + " now has " + amount + "$");
|
|
}, err -> {
|
|
ctx.getSender().sendMessage("Unable to set coins: " + err.getMessage());
|
|
Bukkit.getLogger().log(Level.SEVERE, "Unable to set coins of " + player.getUniqueId() + " to " + amount, err);
|
|
});
|
|
}
|
|
}
|
|
```
|
|
Registered with
|
|
```java
|
|
ScrowAPI.getCommandManager().register(new CoinsCommand().getRootNode());
|
|
``` |