8000 adding #360 by DerEchtePilz · Pull Request #369 · CommandAPI/CommandAPI · GitHub
[go: up one dir, main page]

Skip to content

adding #360 #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9e519fd
adding #360
DerEchtePilz Nov 24, 2022
832128e
provide ExecutionInfo with a record
DerEchtePilz Nov 25, 2022
ad5ef54
fixing JavaDocs
DerEchtePilz Nov 25, 2022
31d9b61
fixing some things
DerEchtePilz Nov 25, 2022
e7626e8
adding a CommandArguments class to hold provided arguments
DerEchtePilz Dec 9, 2022
f23361a
remove method parameters, fix documentation code to match new syntax
DerEchtePilz Dec 9, 2022
bfe45a3
Merge branch 'JorelAli:dev/dev' into dev/dev
DerEchtePilz Dec 9, 2022
45753b1
fix Kotlin DSL to match new syntax
DerEchtePilz Dec 9, 2022
0601bfc
fixing Converter
DerEchtePilz Dec 9, 2022
f512772
fix some more documentation issues
DerEchtePilz Dec 9, 2022
d21d50b
fixing some annotation examples to match new syntax
DerEchtePilz Dec 9, 2022 8000
15f29ff
fix annotation generation to match new syntax
DerEchtePilz Dec 9, 2022
47b86f8
handle String[] for converted commands correctly
DerEchtePilz Dec 9, 2022
92f4e37
parse arguments only once
DerEchtePilz Dec 13, 2022
88b232f
Make argsToObjectArr return a CommandArguments object. Change name of…
DerEchtePilz Dec 13, 2022
328295e
fixing generateCommand method
DerEchtePilz Dec 13, 2022
18eb95d
fix Brigadier#parseArguments method
DerEchtePilz Dec 13, 2022
d4d07c9
add Javadocs to ExecutionInfo
DerEchtePilz Dec 13, 2022
374ebcc
Remove test command thingy from CommandAPIMain. Add Javadocs to Comma…
DerEchtePilz Dec 13, 2022
e6d8cab
updating branch to support new project structure
DerEchtePilz Dec 14, 2022
43d3382
clean up after merge
DerEchtePilz Dec 14, 2022
9fbf5a7
fixing a method call in Brigadier#parseArguments
DerEchtePilz Dec 14, 2022
5c01e6a
fixing CommandTree DSL
DerEchtePilz Dec 14, 2022
6d4fa44
optimizing some imports
DerEchtePilz Dec 14, 2022
9142d85
fixing some formatting issues
DerEchtePilz Dec 15, 2022
7057cf5
Merge branch 'JorelAli:dev/dev' into dev/dev
DerEchtePilz Dec 15, 2022
a02a8b8
fixing documentation example code
DerEchtePilz Dec 15, 2022
eb5bf6e
fix commands not being executable
DerEchtePilz Dec 17, 2022
104391c
fixing build error
DerEchtePilz Dec 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
adding #360
  • Loading branch information
DerEchtePilz committed Nov 24, 2022
commit 9e519fd9f5be6a4e4ca0044d4c6fe438b0e20fb9
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public static SuggestionProvider toSuggestions(Argument<?> argument, List<Argume
* @throws CommandSyntaxException if there was an error during parsing
*/
public static Object[] parseArguments(CommandContext cmdCtx, List<Argument> args) throws CommandSyntaxException {
return CommandAPIHandler.getInstance().argsToObjectArr(cmdCtx, args.toArray(new Argument[0]));
return (Object[]) CommandAPIHandler.getInstance().argsToObjectArr(cmdCtx, args.toArray(new Argument[0])).get(0);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import org.bukkit.command.BlockCommandSender;
Expand Down Expand Up @@ -69,13 +70,13 @@ public <S extends IExecutorResulting<?>> void addResultingExecutor(S executor) {
this.resultingExecutors.add((IExecutorResulting<T>) executor);
}

public int execute(CommandSender sender, Object[] arguments) throws CommandSyntaxException {
public int execute(CommandSender sender, Object[] arguments, Map<String, Object> argumentsMap) throws CommandSyntaxException {

// Parse executor type
if (!resultingExecutors.isEmpty()) {
// Run resulting executor
try {
return execute(resultingExecutors, sender, arguments);
return execute(resultingExecutors, sender, arguments, argumentsMap);
} catch (WrapperCommandSyntaxException e) {
throw e.getException();
} catch (Exception e) {
Expand All @@ -85,7 +86,7 @@ public int execute(CommandSender sender, Object[] arguments) throws CommandSynta
} else {
// Run normal executor
try {
return execute(normalExecutors, sender, arguments);
return execute(normalExecutors, sender, arguments, argumentsMap);
} catch (WrapperCommandSyntaxException e) {
throw e.getException();
} catch (Exception e) {
Expand All @@ -95,22 +96,22 @@ public int execute(CommandSender sender, Object[] arguments) throws CommandSynta
}
}

private int execute(List<? extends IExecutorTyped> executors, CommandSender sender, Object[] args)
private int execute(List<? extends IExecutorTyped> executors, CommandSender sender, Object[] args, Map<String, Object> argsMap)
throws WrapperCommandSyntaxException {
if (isForceNative()) {
return execute(executors, sender, args, ExecutorType.NATIVE);
return execute(executors, sender, args, argsMap, ExecutorType.NATIVE);
} else if (sender instanceof Player && matches(executors, ExecutorType.PLAYER)) {
return execute(executors, sender, args, ExecutorType.PLAYER);
return execute(executors, sender, args, argsMap, ExecutorType.PLAYER);
} else if (sender instanceof Entity && matches(executors, ExecutorType.ENTITY)) {
return execute(executors, sender, args, ExecutorType.ENTITY);
return execute(executors, sender, args, argsMap, ExecutorType.ENTITY);
} else if (sender instanceof ConsoleCommandSender && matches(executors, ExecutorType.CONSOLE)) {
return execute(executors, sender, args, ExecutorType.CONSOLE);
return execute(executors, sender, args, argsMap, ExecutorType.CONSOLE);
} else if (sender instanceof BlockCommandSender && matches(executors, ExecutorType.BLOCK)) {
return execute(executors, sender, args, ExecutorType.BLOCK);
return execute(executors, sender, args, argsMap, ExecutorType.BLOCK);
} else if (sender instanceof ProxiedCommandSender && matches(executors, ExecutorType.PROXY)) {
return execute(executors, sender, args, ExecutorType.PROXY);
return execute(executors, sender, args, argsMap, ExecutorType.PROXY);
} else if (matches(executors, ExecutorType.ALL)) {
return execute(executors, sender, args, ExecutorType.ALL);
return execute(executors, sender, args, argsMap, ExecutorType.ALL);
} else {
throw new WrapperCommandSyntaxException(new SimpleCommandExceptionType(
new LiteralMessage(CommandAPI.getConfiguration().getMissingImplementationMessage()
Expand All @@ -120,10 +121,10 @@ private int execute(List<? extends IExecutorTyped> executors, CommandSender send
}

private int execute(List<? extends IExecutorTyped> executors, CommandSender sender, Object[] args,
ExecutorType type) throws WrapperCommandSyntaxException {
Map<String, Object> argsMap, ExecutorType type) throws WrapperCommandSyntaxException {
for (IExecutorTyped executor : executors) {
if (executor.getType() == type) {
return executor.executeWith(sender, args);
return executor.executeWith(sender, args, argsMap);
}
}
throw new NoSuchElementException("Executor had no valid executors for type " + type.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,8 @@
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;

Expand Down Expand Up @@ -301,19 +293,21 @@ void unregister(String commandName, boolean force) {
*
* @param args set of ordered argument pairs which contain the prompt text
* and their argument types
* @param actualArgs
* @param executor code to be ran when the command is executed
* @return a brigadier command which is registered internally
* @throws CommandSyntaxException if an error occurs when the command is ran
*/
@SuppressWarnings("unchecked")
Command<CommandSourceStack> generateCommand(Argument<?>[] args,
CommandAPIExecutor<? extends CommandSender> executor, boolean converted) throws CommandSyntaxException {
CommandAPIExecutor<? extends CommandSender> executor, boolean converted) throws CommandSyntaxException {

// Generate our command from executor
return (cmdCtx) -> {
CommandSender sender = NMS.getSenderForCommand(cmdCtx, executor.isForceNative());
Map<Integer, Object> arguments = argsToObjectArr(cmdCtx, args);
Object[] argObjs = (Object[]) arguments.get(0);
Map<String, Object> argsMap = (LinkedHashMap<String, Object>) arguments.get(1);
if (converted) {
Object[] argObjs = argsToObjectArr(cmdCtx, args);
int resultValue = 0;

// Return a String[] of arguments for converted commands
Expand All @@ -340,12 +334,12 @@ Command<CommandSourceStack> generateCommand(Argument<?>[] args,
}
}
}
resultValue += executor.execute(sender, result);
resultValue += executor.execute(sender, result, argsMap);
}

return resultValue;
} else {
return executor.execute(sender, argsToObjectArr(cmdCtx, args));
return executor.execute(sender, argObjs, argsMap);
}
};
}
Expand All @@ -358,30 +352,35 @@ Command<CommandSourceStack> generateCommand(Argument<?>[] args,
* @return an Object[] which can be used in (sender, args) ->
* @throws CommandSyntaxException
*/
Object[] argsToObjectArr(CommandContext<CommandSourceStack> cmdCtx, Argument<?>[] args)
Map<Integer, Object> argsToObjectArr(CommandContext<CommandSourceStack> cmdCtx, Argument<?>[] args)
throws CommandSyntaxException {
// Array for arguments for executor
List<Object> argList = new ArrayList<>();

// LinkedHashMap for arguments for executor
Map<String, Object> argsMap = new LinkedHashMap<>();

// Populate array
for (Argument<?> argument : args) {
if (argument.isListed()) {
argList.add(parseArgument(cmdCtx, argument.getNodeName(), argument, argList.toArray()));
argsMap.put(argument.getNodeName(), parseArgument(cmdCtx, argument.getNodeName(), argument, argList.toArray()));
}
}

return argList.toArray();
Map<Integer, Object> arguments = new HashMap<>();
arguments.put(0, argList.toArray());
arguments.put(1, argsMap);
return arguments;
}

/**
* Parses an argument and converts it into its standard Bukkit type (as defined
* in NMS.java)
*
* @param type the argument type
*
* @param cmdCtx the command context
* @param key the key (declared in arguments)
* @param value the value (the argument declared in arguments)
* @param sender the command sender
* @return the standard Bukkit type
* @throws CommandSyntaxException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -210,7 +206,7 @@ private static void convertPluginCommand(JavaPlugin plugin, String commandName,
.withAliases(aliases)
.withFullDescription(fullDescription)
.executesNative((sender, args) -> {
executor.executeWith(sender, new String[0]);
executor.executeWith(sender, new String[0], new LinkedHashMap<>());
})
.register();

Expand Down
Loading
0