separate reading from parsing
This commit is contained in:
parent
36a13443db
commit
aae7a160ac
22 changed files with 137 additions and 78 deletions
|
|
@ -5,7 +5,7 @@ import com.mongodb.MongoClientSettings;
|
|||
import com.mongodb.reactivestreams.client.MongoClient;
|
||||
import com.mongodb.reactivestreams.client.MongoClients;
|
||||
import de.kentoj.scrow.bukkit.cmds2.CommandFactoryImpl;
|
||||
import de.kentoj.scrow.bukkit.cmds2.CommandManagerImpl;
|
||||
import de.kentoj.scrow.bukkit.cmds2.bukkit.BukkitCommandManager;
|
||||
import de.kentoj.scrow.bukkit.economy.InMemoryEconomyService;
|
||||
import de.kentoj.scrow.bukkit.economy.MongoEconomyService;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.Commands;
|
||||
|
|
@ -49,7 +49,7 @@ public class ScrowAPISurface {
|
|||
);
|
||||
|
||||
Commands.setFactory(new CommandFactoryImpl());
|
||||
ScrowAPI.setCommandManager(new CommandManagerImpl());
|
||||
ScrowAPI.setCommandManager(new BukkitCommandManager());
|
||||
}
|
||||
|
||||
public static void destroyScrowAPI() {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package de.kentoj.scrow.bukkit.cmds2.types;
|
||||
package de.kentoj.scrow.bukkit.cmds2;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.CommandArgument;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.CommandContext;
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package de.kentoj.scrow.bukkit.cmds2;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.ArgumentData;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.CommandContext;
|
||||
import de.kentoj.scrow.bukkit.services.command.exception.CommandInvocationException;
|
||||
import lombok.Getter;
|
||||
|
|
@ -16,13 +17,13 @@ public class CommandContextImpl implements CommandContext {
|
|||
|
||||
@Getter
|
||||
private final CommandSender sender;
|
||||
private final Map<String, ?> arguments;
|
||||
private final Map<String, ArgumentData<?>> arguments;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getArg(String name) {
|
||||
final T value = (T) arguments.get(name);
|
||||
Preconditions.checkState(value != null, "no value for '" + name + "'-argument");
|
||||
return value;
|
||||
public <T> ArgumentData<T> getArgData(String name) {
|
||||
final var data = (ArgumentData<T>) arguments.get(name);
|
||||
Preconditions.checkState(data != null, "no data for '" + name + "'-argument");
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -1,13 +1,10 @@
|
|||
package de.kentoj.scrow.bukkit.cmds2;
|
||||
|
||||
import de.kentoj.scrow.bukkit.cmds2.types.CommandArgumentImpl;
|
||||
import de.kentoj.scrow.bukkit.cmds2.types.LiteralImpl;
|
||||
import de.kentoj.scrow.bukkit.cmds2.types.RootLiteralImpl;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.CommandArgument;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.CommandContext;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.CommandFactory;
|
||||
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.cmds2.literal.Literal;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.literal.RootLiteral;
|
||||
import de.kentoj.scrow.bukkit.services.command.type.ArgumentType;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
|
|
|||
|
|
@ -1,18 +1,15 @@
|
|||
package de.kentoj.scrow.bukkit.cmds2.types;
|
||||
package de.kentoj.scrow.bukkit.cmds2;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.CommandExecutor;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.CommandArgument;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.node.Literal;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.literal.Literal;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
@Getter
|
||||
public class LiteralImpl implements Literal {
|
||||
|
|
@ -51,6 +48,11 @@ public class LiteralImpl implements Literal {
|
|||
return !literals.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Literal> getLiterals() {
|
||||
return new HashSet<>(literals.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void addArgument(CommandArgument<T> argument) {
|
||||
arguments.add(argument);
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package de.kentoj.scrow.bukkit.cmds2.types;
|
||||
package de.kentoj.scrow.bukkit.cmds2;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.node.RootLiteral;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.literal.RootLiteral;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
package de.kentoj.scrow.bukkit.cmds2.parsing;
|
||||
package de.kentoj.scrow.bukkit.cmds2.bukkit;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.node.RootLiteral;
|
||||
import de.kentoj.scrow.bukkit.cmds2.parsing.ArgumentReaderImpl;
|
||||
import de.kentoj.scrow.bukkit.cmds2.parsing.CommandProcessor;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.literal.RootLiteral;
|
||||
import de.kentoj.scrow.bukkit.services.command.exception.CommandInvocationException;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
|
|
@ -1,17 +1,16 @@
|
|||
package de.kentoj.scrow.bukkit.cmds2;
|
||||
package de.kentoj.scrow.bukkit.cmds2.bukkit;
|
||||
|
||||
import de.kentoj.scrow.bukkit.cmds2.parsing.BukkitCommand;
|
||||
import de.kentoj.scrow.bukkit.services.command.CommandManager;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.node.RootLiteral;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.literal.RootLiteral;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandMap;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class CommandManagerImpl implements CommandManager {
|
||||
public class BukkitCommandManager implements CommandManager {
|
||||
|
||||
private final CommandMap commandMap;
|
||||
|
||||
public CommandManagerImpl() {
|
||||
public BukkitCommandManager() {
|
||||
try {
|
||||
var field = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
|
||||
field.setAccessible(true);
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package de.kentoj.scrow.bukkit.cmds2.parsing;
|
||||
package de.kentoj.scrow.bukkit.cmds2.bukkit;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.node.RootLiteral;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.literal.RootLiteral;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package de.kentoj.scrow.bukkit.cmds2.parsing;
|
||||
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.ArgumentData;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.CommandArgument;
|
||||
import lombok.Value;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@Value
|
||||
public class ArgumentDataImpl<T> implements ArgumentData<T> {
|
||||
T value;
|
||||
CommandArgument<T> argument;
|
||||
@Nullable String rawValue;
|
||||
}
|
||||
|
|
@ -2,12 +2,12 @@ package de.kentoj.scrow.bukkit.cmds2.parsing;
|
|||
|
||||
import de.kentoj.scrow.bukkit.cmds2.CommandContextImpl;
|
||||
import de.kentoj.scrow.bukkit.services.command.ArgumentReader;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.ArgumentData;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.CommandArgument;
|
||||
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.cmds2.literal.Literal;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.literal.RootLiteral;
|
||||
import de.kentoj.scrow.bukkit.services.command.exception.CommandInvocationException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
|
@ -16,7 +16,7 @@ import java.util.Map;
|
|||
@RequiredArgsConstructor
|
||||
public class CommandProcessor {
|
||||
|
||||
private final Map<String, Object> arguments = new HashMap<>();
|
||||
private final Map<String, ArgumentData<?>> arguments = new HashMap<>();
|
||||
|
||||
private final CommandSender sender;
|
||||
private final ArgumentReader argumentReader;
|
||||
|
|
@ -48,20 +48,21 @@ public class CommandProcessor {
|
|||
|
||||
private void processArguments(Literal node) {
|
||||
node.getArguments().forEach(arg -> {
|
||||
Object parsed = parseArgument(arg);
|
||||
var parsed = parseArgument(arg);
|
||||
arguments.put(arg.getName(), parsed);
|
||||
});
|
||||
}
|
||||
|
||||
private <T> Object parseArgument(CommandArgument<T> argument) {
|
||||
private <T> ArgumentData<T> parseArgument(CommandArgument<T> argument) {
|
||||
if (argumentReader.isEOF()) {
|
||||
var defaultValue = argument.getDefaultValue(new CommandContextImpl(sender, arguments));
|
||||
if (defaultValue == null) throw new CommandInvocationException("Missing argument -- " + argument.getName());
|
||||
return defaultValue;
|
||||
return new ArgumentDataImpl<>(defaultValue, argument, null);
|
||||
}
|
||||
|
||||
T value = argument.getType().parseInput(argumentReader);
|
||||
final var rawValue = argument.getType().readRawInput(argumentReader);
|
||||
T value = argument.getType().parseInput(rawValue);
|
||||
argument.getType().checkValue(new CommandContextImpl(sender, arguments), value);
|
||||
return value;
|
||||
return new ArgumentDataImpl<>(value, argument, rawValue);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import de.kentoj.scrow.bukkit.ScrowAPI;
|
|||
import de.kentoj.scrow.bukkit.services.command.cmds2.CommandArgument;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.CommandContext;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.Commands;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.node.RootLiteral;
|
||||
import de.kentoj.scrow.bukkit.services.command.cmds2.literal.RootLiteral;
|
||||
import de.kentoj.scrow.bukkit.services.command.type.NumberArgumentType;
|
||||
import de.kentoj.scrow.bukkit.services.command.type.PlayerArgumentType;
|
||||
import org.bukkit.Bukkit;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue