commands work but i think ill refactor
This commit is contained in:
parent
5b5cb04f8b
commit
36a13443db
54 changed files with 726 additions and 812 deletions
|
|
@ -18,17 +18,10 @@ dependencies {
|
|||
testImplementation("org.junit.jupiter:junit-jupiter")
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
|
||||
api("me.lucko:commodore:2.2")
|
||||
api("org.spigotmc:spigot-api:1.21.11-R0.1-SNAPSHOT")
|
||||
api("net.kyori:adventure-platform-bukkit:4.4.1")
|
||||
}
|
||||
|
||||
tasks.findByName("shadowJar")?.let {
|
||||
dependencies {
|
||||
"exclude"("dependency"("com.mojang.brigadier")!!)
|
||||
}
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
create<MavenPublication>("CoreBukkitApi") {
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
package de.kentoj.scrow.bukkit.services.command;
|
||||
|
||||
public interface CommandReader {
|
||||
public interface ArgumentReader {
|
||||
|
||||
boolean isEOF();
|
||||
|
||||
String readWord();
|
||||
|
||||
String readQuotedString();
|
||||
String readQuotedWord();
|
||||
|
||||
int getCursor();
|
||||
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
package de.kentoj.scrow.bukkit.services.command;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.type.ArgumentType;
|
||||
import de.kentoj.scrow.bukkit.services.command.context.CommandContext;
|
||||
import lombok.Value;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
// TODO convert to interface
|
||||
@Value
|
||||
public class CommandArgument<T> {
|
||||
int index;
|
||||
String name;
|
||||
ArgumentType<T> type;
|
||||
Function<CommandContext, T> defaultValue;
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package de.kentoj.scrow.bukkit.services.command;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.context.CommandContext;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.CommandContext;
|
||||
|
||||
public interface CommandExecutor {
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
package de.kentoj.scrow.bukkit.services.command;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.node.CommandNode;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.node.RootLiteral;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public interface CommandManager {
|
||||
|
||||
void register(CommandNode rootCommandNode, Plugin plugin);
|
||||
void register(RootLiteral rootLiteral, Plugin plugin);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,20 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.cmds2;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.context.CommandContext;
|
||||
import de.kentoj.scrow.bukkit.services.command.type.ArgumentType;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface CommandArgument<T> {
|
||||
|
||||
String getName();
|
||||
|
||||
ArgumentType<T> getType();
|
||||
|
||||
@Nullable T getDefaultValue(CommandContext ctx);
|
||||
|
||||
void setSuggestionProvider(@Nullable SuggestionProvider suggestionProvider);
|
||||
|
||||
/**
|
||||
* @return the explicitly set SuggestionProvider or the suggestions for the type if unset/null
|
||||
*/
|
||||
SuggestionProvider getSuggestionProvider();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.context;
|
||||
package de.kentoj.scrow.bukkit.services.command.cmds2;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.exception.CommandSyntaxException;
|
||||
import de.kentoj.scrow.bukkit.services.command.exception.CommandInvocationException;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface CommandContext {
|
||||
|
||||
|
|
@ -16,12 +15,11 @@ public interface CommandContext {
|
|||
|
||||
/**
|
||||
* @return Object that is parsed from the raw argument
|
||||
* @throws CommandSyntaxException if no value given and no default value supplied
|
||||
* @throws CommandInvocationException if no value given and no default value supplied
|
||||
*/
|
||||
<T> T getArg(String key);
|
||||
<T> T getArg(String name);
|
||||
|
||||
/**
|
||||
* @return string argument used at invocation
|
||||
*/
|
||||
@Nullable String getRawArgument(String key);
|
||||
default <T> T getArg(CommandArgument<T> arg) {
|
||||
return getArg(arg.getName());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.cmds2;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.node.Literal;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.node.RootLiteral;
|
||||
import de.kentoj.scrow.bukkit.services.command.type.ArgumentType;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public interface CommandFactory {
|
||||
|
||||
Literal literal(String name);
|
||||
|
||||
RootLiteral root(String name);
|
||||
|
||||
<T> CommandArgument<T> argument(String name, ArgumentType<T> type, Function<CommandContext, T> defaultValueFun);
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.cmds2;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.node.Literal;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.node.RootLiteral;
|
||||
import de.kentoj.scrow.bukkit.services.command.type.ArgumentType;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class Commands {
|
||||
|
||||
private static CommandFactory factory;
|
||||
|
||||
public static Literal literal(String name) {
|
||||
return factory.literal(name);
|
||||
}
|
||||
|
||||
public static RootLiteral root(String name) {
|
||||
return factory.root(name);
|
||||
}
|
||||
|
||||
public static <T> CommandArgument<T> argument0(String name, ArgumentType<T> type, Function<CommandContext, T> defaultValueFun) {
|
||||
return factory.argument(name, type, defaultValueFun);
|
||||
}
|
||||
|
||||
public static <T> CommandArgument<T> argument1(String name, ArgumentType<T> type, T defaultValue) {
|
||||
return factory.argument(name, type, __ -> defaultValue);
|
||||
}
|
||||
|
||||
public static <T> CommandArgument<T> argument0(String name, ArgumentType<T> type) {
|
||||
return factory.argument(name, type, __ -> null);
|
||||
}
|
||||
|
||||
@ApiStatus.Internal
|
||||
public static void setFactory(CommandFactory factory) {
|
||||
Commands.factory = factory;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,5 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.cmds2;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.context.CommandContext;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface SuggestionProvider {
|
||||
|
|
|
|||
|
|
@ -1,14 +0,0 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.cmds2.node;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.context.CommandContext;
|
||||
import de.kentoj.scrow.bukkit.services.command.type.ArgumentType;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public non-sealed interface ArgumentCommandNode extends CommandNode {
|
||||
|
||||
<T> ArgumentType<T> getArgument(String name);
|
||||
|
||||
void addArgument(String name, ArgumentType<?> arg);
|
||||
<T> void addArgument(String name, ArgumentType<T> arg, Function<CommandContext, T> getDefault);
|
||||
}
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.cmds2.node;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.CommandExecutor;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.SuggestionProvider;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public sealed interface CommandNode permits LiteralCommandNode, ArgumentCommandNode, RootCommandNode {
|
||||
|
||||
void setName(String name);
|
||||
String getName();
|
||||
|
||||
void addSubNode(CommandNode node);
|
||||
@Nullable CommandNode getSubNode(String rawArg);
|
||||
boolean hasNextNode();
|
||||
|
||||
void setExecutor(@Nullable CommandExecutor executor);
|
||||
@Nullable CommandExecutor getExecutor();
|
||||
|
||||
void setSuggestionProvider(SuggestionProvider suggestionProvider);
|
||||
SuggestionProvider getSuggestionProvider();
|
||||
|
||||
void setDescription(@Nullable String description);
|
||||
@Nullable String getDescription();
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.cmds2.node;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.CommandExecutor;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.CommandArgument;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Literal {
|
||||
|
||||
/**
|
||||
* String with no spaces!
|
||||
*/
|
||||
void setName(String name);
|
||||
|
||||
/**
|
||||
* String with no spaces!
|
||||
*/
|
||||
String getName();
|
||||
|
||||
void addLiteral(Literal node);
|
||||
|
||||
@Nullable Literal getLiteral(String rawArg);
|
||||
|
||||
boolean hasLiteral();
|
||||
|
||||
void setExecutor(@Nullable CommandExecutor executor);
|
||||
|
||||
@Nullable CommandExecutor getExecutor();
|
||||
|
||||
void setDescription(@Nullable String description);
|
||||
|
||||
@Nullable String getDescription();
|
||||
|
||||
Permission getRequiredPermission();
|
||||
|
||||
void setRequiredPermission(Permission permission);
|
||||
|
||||
<T> void addArgument(CommandArgument<T> argument);
|
||||
|
||||
List<CommandArgument<?>> getArguments();
|
||||
}
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.cmds2.node;
|
||||
|
||||
import org.bukkit.permissions.Permission;
|
||||
|
||||
public non-sealed interface LiteralCommandNode extends CommandNode {
|
||||
|
||||
Permission getRequiredPermission();
|
||||
|
||||
void setRequiredPermission(Permission permission);
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@ package de.kentoj.scrow.bukkit.services.command.cmds2.node;
|
|||
|
||||
import java.util.Set;
|
||||
|
||||
public non-sealed interface RootCommandNode extends CommandNode, LiteralCommandNode {
|
||||
public interface RootLiteral extends Literal {
|
||||
|
||||
Set<String> getAliases();
|
||||
|
||||
|
|
@ -4,19 +4,19 @@ package de.kentoj.scrow.bukkit.services.command.exception;
|
|||
* When a command has a syntax issue.
|
||||
* {@code CommandSyntaxException::getMessage} should return a user-friendly message explaining the issue.
|
||||
*/
|
||||
public class CommandSyntaxException extends RuntimeException {
|
||||
public class CommandInvocationException extends RuntimeException {
|
||||
|
||||
/**
|
||||
* @param message user-friendly message
|
||||
*/
|
||||
public CommandSyntaxException(String message) {
|
||||
public CommandInvocationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message user-friendly message
|
||||
*/
|
||||
public CommandSyntaxException(String message, Throwable throwable) {
|
||||
public CommandInvocationException(String message, Throwable throwable) {
|
||||
super(message + ": " + throwable.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.node;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.CommandExecutor;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
// TODO javadoc
|
||||
public interface CommandNode {
|
||||
|
||||
void setPermission(@Nullable Permission permission);
|
||||
|
||||
default void setPermission(String permission) {
|
||||
setPermission(new Permission(permission));
|
||||
}
|
||||
|
||||
void setDescription(String description);
|
||||
|
||||
void setAliases(Set<String> aliases);
|
||||
|
||||
void addAlias(String... aliases);
|
||||
|
||||
void setExecutor(@Nullable CommandExecutor executor);
|
||||
|
||||
@Nullable Permission getPermission();
|
||||
|
||||
String getName();
|
||||
|
||||
Set<String> getAliases();
|
||||
|
||||
String getDescription();
|
||||
|
||||
/**
|
||||
* Function that should be called when invoking this command.
|
||||
* The executor shall only be called when this node is the last CommandNode in the invocation.
|
||||
* <pre>
|
||||
* - RootCommand(CommandNode)
|
||||
* - has executor
|
||||
* - has sub-command(CommandNode)
|
||||
* - has executor
|
||||
* </pre>
|
||||
* In this case, the sub-command is optional. If the sub-command is used, the sub-command's executor will be used.
|
||||
* Else the RootCommand's executor will be used.
|
||||
*/
|
||||
@Nullable CommandExecutor getExecutor();
|
||||
|
||||
default boolean hasExecutor() {
|
||||
return getExecutor() != null;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.node;
|
||||
|
||||
public interface CommandNodeFactory {
|
||||
|
||||
CommandNodeWithSubCommand createNodeWithSubCommands(String name);
|
||||
|
||||
CommandNodeWithArguments createNodeWithArguments(String name);
|
||||
|
||||
CommandNode createRootNode(String name);
|
||||
}
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.node;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class CommandNodeRepository {
|
||||
|
||||
private static CommandNodeFactory commandNodeFactory;
|
||||
|
||||
public static CommandNodeWithArguments withArguments(String name) {
|
||||
return commandNodeFactory.createNodeWithArguments(name);
|
||||
}
|
||||
|
||||
public static CommandNodeWithSubCommand withSubCommands(String name) {
|
||||
return commandNodeFactory.createNodeWithSubCommands(name);
|
||||
}
|
||||
|
||||
@ApiStatus.Internal
|
||||
public static void setCommandNodeFactory(CommandNodeFactory commandNodeFactory) {
|
||||
CommandNodeRepository.commandNodeFactory = commandNodeFactory;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.node;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.CommandArgument;
|
||||
import de.kentoj.scrow.bukkit.services.command.context.CommandContext;
|
||||
import de.kentoj.scrow.bukkit.services.command.type.ArgumentType;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* A CommandNode which can have arguments
|
||||
*/
|
||||
public interface CommandNodeWithArguments extends CommandNode {
|
||||
|
||||
/**
|
||||
* Adds an argument to the node.
|
||||
* @param defaultValue Function returning a defaultValue if no value is given at invocation. \
|
||||
* May be null if the argument should be required and not optional.
|
||||
* @throws IllegalStateException if the previous argument is optional(defaultValue is set) and this is not.
|
||||
*/
|
||||
<T> void addArg(String name, ArgumentType<T> type, @Nullable Function<CommandContext, T> defaultValue);
|
||||
|
||||
/**
|
||||
* Adds a required argument to the node
|
||||
* @throws IllegalStateException if the previous argument is optional(defaultValue is set) and this is not.
|
||||
*/
|
||||
default <T> void addArg(String name, ArgumentType<T> type) {
|
||||
addArg(name, type, null);
|
||||
}
|
||||
|
||||
@Nullable CommandArgument<?> getArg(int index);
|
||||
|
||||
@Nullable CommandArgument<?> getArg(String name);
|
||||
}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.node;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A CommandNode which can have sub-commands
|
||||
*/
|
||||
public interface CommandNodeWithSubCommand extends CommandNode {
|
||||
|
||||
List<CommandNode> getSubCommands();
|
||||
|
||||
void addSubCommand(CommandNode subCommand);
|
||||
|
||||
/**
|
||||
* @return SubCommand node where a name or alias matches label
|
||||
*/
|
||||
@Nullable CommandNode getSubCommand(String label);
|
||||
}
|
||||
|
|
@ -1,19 +1,19 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.type;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.CommandReader;
|
||||
import de.kentoj.scrow.bukkit.services.command.context.CommandContext;
|
||||
import de.kentoj.scrow.bukkit.services.command.exception.CommandSyntaxException;
|
||||
import de.kentoj.scrow.bukkit.services.command.ArgumentReader;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.CommandContext;
|
||||
import de.kentoj.scrow.bukkit.services.command.exception.CommandInvocationException;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface ArgumentType<T> {
|
||||
|
||||
/**
|
||||
* @throws CommandSyntaxException if the given value may not be used
|
||||
* @throws CommandInvocationException if the given value may not be used
|
||||
*/
|
||||
void checkValue(CommandContext ctx, T value, String rawValue) throws CommandSyntaxException;
|
||||
void checkValue(CommandContext ctx, T value) throws CommandInvocationException;
|
||||
|
||||
Set<String> getDefaultSuggestions(CommandContext ctx);
|
||||
|
||||
T parseInput(CommandReader reader);
|
||||
T parseInput(ArgumentReader reader);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,47 +0,0 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.type;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.CommandReader;
|
||||
import de.kentoj.scrow.bukkit.services.command.context.CommandContext;
|
||||
import de.kentoj.scrow.bukkit.services.command.exception.CommandSyntaxException;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
// TODO add livingEntity
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class EntityArgumentType {
|
||||
|
||||
private static final ArgumentType<Player> player = new ArgumentType<>() {
|
||||
@Override
|
||||
public void checkValue(CommandContext ctx, Player value, String rawValue) throws CommandSyntaxException {
|
||||
if (value == null)
|
||||
throw new CommandSyntaxException("Player is not online -- " + rawValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getDefaultSuggestions(CommandContext ctx) {
|
||||
return Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player parseInput(CommandReader reader) {
|
||||
final var word = reader.readWord();
|
||||
if (word.length() == 36) {
|
||||
var uuid = UUID.fromString(word);
|
||||
return Bukkit.getPlayer(uuid);
|
||||
} else {
|
||||
return Bukkit.getPlayerExact(word);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public static ArgumentType<Player> player() {
|
||||
return player;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.type;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import de.kentoj.scrow.bukkit.services.command.ArgumentReader;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.CommandContext;
|
||||
import de.kentoj.scrow.bukkit.services.command.exception.CommandInvocationException;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
// TODO add livingEntity
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class NumberArgumentType {
|
||||
|
||||
public static ArgumentType<Integer> integer(int min, int max) {
|
||||
Preconditions.checkArgument(min < max, "min is greater than max");
|
||||
|
||||
return new ArgumentType<>() {
|
||||
@Override
|
||||
public void checkValue(CommandContext ctx, Integer value) throws CommandInvocationException {
|
||||
if (value < min) throw new CommandInvocationException("Value too small -- " + value);
|
||||
if (value > max) throw new CommandInvocationException("Value too large -- " + value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getDefaultSuggestions(CommandContext ctx) {
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer parseInput(ArgumentReader reader) {
|
||||
final var str = reader.readWord();
|
||||
try {
|
||||
return Integer.parseInt(str);
|
||||
} catch (NumberFormatException ex) {
|
||||
throw new CommandInvocationException("Invalid number -- " + str);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
package de.kentoj.scrow.bukkit.services.command.type;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.ArgumentReader;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.CommandContext;
|
||||
import de.kentoj.scrow.bukkit.services.command.exception.CommandInvocationException;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
// TODO add livingEntity
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class PlayerArgumentType {
|
||||
|
||||
private static final ArgumentType<Player> player = new ArgumentType<>() {
|
||||
@Override
|
||||
public void checkValue(CommandContext ctx, Player value) throws CommandInvocationException {
|
||||
if (value == null)
|
||||
throw new CommandInvocationException("Player is not online");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getDefaultSuggestions(CommandContext ctx) {
|
||||
return Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player parseInput(ArgumentReader reader) {
|
||||
final var word = reader.readWord();
|
||||
if (word.length() == 36) {
|
||||
var uuid = UUID.fromString(word);
|
||||
return Bukkit.getPlayer(uuid);
|
||||
} else {
|
||||
return Bukkit.getPlayerExact(word);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private static final ArgumentType<OfflinePlayer> offlinePlayer = new ArgumentType<>() {
|
||||
@Override
|
||||
public OfflinePlayer parseInput(ArgumentReader reader) {
|
||||
final var str = reader.readWord();
|
||||
OfflinePlayer player;
|
||||
|
||||
player = Bukkit.getPlayerExact(str);
|
||||
if (player == null) {
|
||||
try {
|
||||
player = Bukkit.getOfflinePlayer(UUID.fromString(str));
|
||||
} catch (IllegalArgumentException ex) {
|
||||
throw new CommandInvocationException("Not a UUID or name of an online player");
|
||||
}
|
||||
}
|
||||
return player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getDefaultSuggestions(CommandContext ctx) {
|
||||
return Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkValue(CommandContext ctx, OfflinePlayer value) throws CommandInvocationException {
|
||||
}
|
||||
};
|
||||
|
||||
public static ArgumentType<Player> player() {
|
||||
return player;
|
||||
}
|
||||
|
||||
public static ArgumentType<OfflinePlayer> offlinePlayer() {
|
||||
return offlinePlayer;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
package de.kentoj.scrow.bukkit.util.command;
|
||||
|
||||
import com.mojang.brigadier.LiteralMessage;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class PlayerArgument {
|
||||
|
||||
public static Player resolve(CommandContext<Object> ctx, String argumentName) throws CommandSyntaxException {
|
||||
var str = ctx.getArgument(argumentName, String.class);
|
||||
boolean isUUID = str.length() == 36;
|
||||
return isUUID ? Bukkit.getPlayer(parseUUID(str)) : Bukkit.getPlayerExact(str);
|
||||
}
|
||||
|
||||
private static UUID parseUUID(String string) throws CommandSyntaxException {
|
||||
try {
|
||||
return UUID.fromString(string);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
throw new SimpleCommandExceptionType(new LiteralMessage("Invalid UUID")).create();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue