.
This commit is contained in:
parent
bcce74a050
commit
9fd2752b3a
5 changed files with 113 additions and 3 deletions
69
README.md
Normal file
69
README.md
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
As of v1.3.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());
|
||||||
|
```
|
||||||
|
|
@ -9,7 +9,7 @@ plugins {
|
||||||
}
|
}
|
||||||
|
|
||||||
group = "de.kentoj"
|
group = "de.kentoj"
|
||||||
version = "1.2.0"
|
version = "1.3.0"
|
||||||
|
|
||||||
fun scrowRepo(handler: RepositoryHandler) = handler.maven {
|
fun scrowRepo(handler: RepositoryHandler) = handler.maven {
|
||||||
name = "scrow"
|
name = "scrow"
|
||||||
|
|
@ -38,7 +38,7 @@ dependencies {
|
||||||
// https://mvnrepository.com/artifact/org.jetbrains/annotations
|
// https://mvnrepository.com/artifact/org.jetbrains/annotations
|
||||||
implementation("org.jetbrains:annotations:26.0.2-1")
|
implementation("org.jetbrains:annotations:26.0.2-1")
|
||||||
|
|
||||||
api("de.kentoj:kencommandapi-core:1.2.0")
|
api("de.kentoj:kencommandapi-core:1.3.0")
|
||||||
|
|
||||||
api("org.spigotmc:spigot-api:1.21.11-R0.1-SNAPSHOT")
|
api("org.spigotmc:spigot-api:1.21.11-R0.1-SNAPSHOT")
|
||||||
implementation("org.spigotmc:spigot-api:1.21.11-R0.1-SNAPSHOT")
|
implementation("org.spigotmc:spigot-api:1.21.11-R0.1-SNAPSHOT")
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
package de.kentoj.kencommandapi;
|
||||||
|
|
||||||
|
import de.kentoj.kencommandapi.api.KenCommandApi;
|
||||||
|
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||||
|
import de.kentoj.kencommandapi.api.structure.node.CommandNode;
|
||||||
|
import org.bukkit.command.Command;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public class BukkitKenCommand extends Command {
|
||||||
|
|
||||||
|
private final CommandNode<CommandContext<CommandSender>> rootNode;
|
||||||
|
private final KenCommandApi<CommandSender, CommandContext<CommandSender>> commandManager;
|
||||||
|
|
||||||
|
protected BukkitKenCommand(KenCommandApi<CommandSender, CommandContext<CommandSender>> commandManager, CommandNode<CommandContext<CommandSender>> rootNode) {
|
||||||
|
super(rootNode.getNames()[0]);
|
||||||
|
this.rootNode = rootNode;
|
||||||
|
this.commandManager = commandManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
|
||||||
|
commandManager.invoke(rootNode, sender, args);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,9 +2,13 @@ package de.kentoj.kencommandapi;
|
||||||
|
|
||||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||||
import de.kentoj.kencommandapi.api.processing.ParsedArgument;
|
import de.kentoj.kencommandapi.api.processing.ParsedArgument;
|
||||||
|
import de.kentoj.kencommandapi.api.structure.node.CommandNode;
|
||||||
import de.kentoj.kencommandapi.impl.AbstractKenCommandApi;
|
import de.kentoj.kencommandapi.impl.AbstractKenCommandApi;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.command.SimpleCommandMap;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class BukkitKenCommandApi extends AbstractKenCommandApi<CommandSender, CommandContext<CommandSender>> {
|
public class BukkitKenCommandApi extends AbstractKenCommandApi<CommandSender, CommandContext<CommandSender>> {
|
||||||
|
|
@ -13,4 +17,15 @@ public class BukkitKenCommandApi extends AbstractKenCommandApi<CommandSender, Co
|
||||||
public CommandContext<CommandSender> createContext(CommandSender sender, Map<String, ParsedArgument<CommandContext<CommandSender>, ?>> parsedArguments) {
|
public CommandContext<CommandSender> createContext(CommandSender sender, Map<String, ParsedArgument<CommandContext<CommandSender>, ?>> parsedArguments) {
|
||||||
return new BukkitCommandContext(sender, parsedArguments);
|
return new BukkitCommandContext(sender, parsedArguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void register(CommandNode<CommandContext<CommandSender>> rootNode) {
|
||||||
|
try {
|
||||||
|
Field field = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
|
||||||
|
field.setAccessible(true);
|
||||||
|
SimpleCommandMap commandMap = (SimpleCommandMap) field.get(Bukkit.getPluginManager());
|
||||||
|
commandMap.register(rootNode.getNames()[0], new BukkitKenCommand(this, rootNode));
|
||||||
|
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ plugins {
|
||||||
}
|
}
|
||||||
|
|
||||||
group = "de.kentoj"
|
group = "de.kentoj"
|
||||||
version = "1.2.0"
|
version = "1.3.0"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue