8000 adding #360 (#369) · CommandAPI/CommandAPI@340902c · GitHub
[go: up one dir, main page]

Skip to content

Commit 340902c

Browse files
authored
adding #360 (#369)
* adding #360 * provide ExecutionInfo with a record * fixing JavaDocs * fixing some things * adding a CommandArguments class to hold provided arguments * remove method parameters, fix documentation code to match new syntax * fix Kotlin DSL to match new syntax * fixing Converter * fix some more documentation issues * fixing some annotation examples to match new syntax * fix annotation generation to match new syntax * handle String[] for converted commands correctly * parse arguments only once * Make argsToObjectArr return a CommandArguments object. Change name of argsToObjectArr to argsToCommandArgs * fixing generateCommand method * fix Brigadier#parseArguments method * add Javadocs to ExecutionInfo * Remove test command thingy from CommandAPIMain. Add Javadocs to CommandArguments * clean up after merge * fixing a method call in Brigadier#parseArguments * fixing CommandTree DSL * optimizing some imports * fixing some formatting issues * fixing documentation example code * fix commands not being executable * fixing build error
1 parent 6c715cb commit 340902c

File tree

47 files changed

+1260
-396
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1260
-396
lines changed

commandapi-annotations/src/main/java/dev/jorel/commandapi/annotations/Annotations.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,9 @@ private int emitExecutes(PrintWriter out, Map<Integer, String> argumentMapping,
353353
} else {
354354
out.print(simpleFromQualified(fromArgumentMap));
355355
}
356-
out.print(") args[");
356+
out.print(") args.get(");
357357
out.print(i);
358-
out.print("]");
358+
out.print(")");
359359
}
360360
//populate stuff here
361361

commandapi-annotations/src/test/java/WarpCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class Examples {
124124
warps.keySet().toArray(new String[0])
125125
)))
126126
.executesPlayer((player, args) -> {
127-
player.teleport(warps.get((String) args[0]));
127+
player.teleport(warps.get((String) args.get(0)));
128128
})
129129
.register();
130130

@@ -135,7 +135,7 @@ class Examples {
135135
.withPermission("warps.create")
136136
.withArguments(new StringArgument("warpname"))
137137
.executesPlayer((player, args) -> {
138-
warps.put((String) args[0], player.getLocation());
138+
warps.put((String) args.get(0), player.getLocation());
139139
})
140140
)
141141
.register();

commandapi-core/src/main/java/dev/jorel/commandapi/Brigadier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ Command fromCommand(AbstractCommandAPICommand<?, Argument, CommandSender> comman
193193
*/
194194
public static <Argument extends AbstractArgument<?, ?, Argument, ?>> Object[] parseArguments(CommandContext cmdCtx, List<Argument> args) throws CommandSyntaxException {
195195
CommandAPIHandler<Argument, ?, ?> handler = (CommandAPIHandler<Argument, ?, ?>) CommandAPIHandler.getInstance();
196-
return handler.argsToObjectArr(cmdCtx, (Argument[]) args.toArray(AbstractArgument[]::new));
196+
return handler.argsToCommandArgs(cmdCtx, (Argument[]) args.toArray(AbstractArgument[]::new)).args();
197197
}
198198

199199
/**

commandapi-core/src/main/java/dev/jorel/commandapi/CommandAPIExecutor.java

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
2626
import dev.jorel.commandapi.commandsenders.*;
2727
import dev.jorel.commandapi.exceptions.WrapperCommandSyntaxException;
28-
import dev.jorel.commandapi.executors.ExecutorType;
29-
import dev.jorel.commandapi.executors.IExecutorNormal;
30-
import dev.jorel.commandapi.executors.IExecutorResulting;
31-
import dev.jorel.commandapi.executors.IExecutorTyped;
28+
import dev.jorel.commandapi.executors.*;
3229

3330
import java.util.ArrayList;
3431
import java.util.List;
@@ -63,13 +60,13 @@ public void addResultingExecutor(IExecutorResulting<?, ?> executor) {
6360
this.resultingExecutors.add((IExecutorResulting<CommandSender, WrapperType>) executor);
6461
}
6562

66-
public int execute(WrapperType sender, Object[] arguments) throws CommandSyntaxException {
63+
public int execute(AbstractExecutionInfo<CommandSender, WrapperType> info) throws CommandSyntaxException {
6764

6865
// Parse executor type
6966
if (!resultingExecutors.isEmpty()) {
7067
// Run resulting executor
7168
try {
72-
return execute(resultingExecutors, sender, arguments);
69+
return execute(resultingExecutors, info);
7370
} catch (WrapperCommandSyntaxException e) {
7471
throw e.getException();
7572
} catch (Exception e) {
@@ -79,7 +76,7 @@ public int execute(WrapperType sender, Object[] arguments) throws CommandSyntaxE
7976
} else {
8077
// Run normal executor
8178
try {
82-
return execute(normalExecutors, sender, arguments);
79+
return execute(normalExecutors, info);
8380
} catch (WrapperCommandSyntaxException e) {
8481
throw e.getException();
8582
} catch (Exception e) {
@@ -89,35 +86,35 @@ public int execute(WrapperType sender, Object[] arguments) throws CommandSyntaxE
8986
}
9087
}
9188

92-
private int execute(List<? extends IExecutorTyped<WrapperType>> executors, WrapperType sender, Object[] args)
89+
private int execute(List<? extends IExecutorTyped<CommandSender, WrapperType>> executors, AbstractExecutionInfo<CommandSender, WrapperType> info)
9390
throws WrapperCommandSyntaxException {
9491
if (isForceNative()) {
95-
return execute(executors, sender, args, ExecutorType.NATIVE);
96-
} else if (sender instanceof AbstractPlayer && matches(executors, ExecutorType.PLAYER)) {
97-
return execute(executors, sender, args, ExecutorType.PLAYER);
98-
} else if (sender instanceof AbstractEntity && matches(executors, ExecutorType.ENTITY)) {
99-
return execute(executors, sender, args, ExecutorType.ENTITY);
100-
} else if (sender instanceof AbstractConsoleCommandSender && matches(executors, ExecutorType.CONSOLE)) {
101-
return execute(executors, sender, args, ExecutorType.CONSOLE);
102-
} else if (sender instanceof AbstractBlockCommandSender && matches(executors, ExecutorType.BLOCK)) {
103-
return execute(executors, sender, args, ExecutorType.BLOCK);
104-
} else if (sender instanceof AbstractProxiedCommandSender && matches(executors, ExecutorType.PROXY)) {
105-
return execute(executors, sender, args, ExecutorType.PROXY);
92+
return execute(executors, info, ExecutorType.NATIVE);
93+
} else if (info.senderWrapper() instanceof AbstractPlayer && matches(executors, ExecutorType.PLAYER)) {
94+
return execute(executors, info, ExecutorType.PLAYER);
95+
} else if (info.senderWrapper() instanceof AbstractEntity && matches(executors, ExecutorType.ENTITY)) {
96+
return execute(executors, info, ExecutorType.ENTITY);
97+
} else if (info.senderWrapper() instanceof AbstractConsoleCommandSender && matches(executors, ExecutorType.CONSOLE)) {
98+
return execute(executors, info, ExecutorType.CONSOLE);
99+
} else if (info.senderWrapper() instanceof AbstractBlockCommandSender && matches(executors, ExecutorType.BLOCK)) {
100+
return execute(executors, info, ExecutorType.BLOCK);
101+
} else if (info.senderWrapper() instanceof AbstractProxiedCommandSender && matches(executors, ExecutorType.PROXY)) {
102+
return execute(executors, info, ExecutorType.PROXY);
106103
} else if (matches(executors, ExecutorType.ALL)) {
107-
return execute(executors, sender, args, ExecutorType.ALL);
104+
return execute(executors, info, ExecutorType.ALL);
108105
} else {
109106
throw new WrapperCommandSyntaxException(new SimpleCommandExceptionType(
110107
new LiteralMessage(CommandAPI.getConfiguration().getMissingImplementationMessage()
111-
.replace("%s", sender.getClass().getSimpleName().toLowerCase())
112-
.replace("%S", sender.getClass().getSimpleName()))).create());
108+
.replace("%s", info.sender().getClass().getSimpleName().toLowerCase())
109+
.replace("%S", info.sender().getClass().getSimpleName()))).create());
113110
}
114111
}
115112

116-
private int execute(List<? extends IExecutorTyped<WrapperType>> executors, WrapperType sender, Object[] args,
117-
ExecutorType type) throws WrapperCommandSyntaxException {
118-
for (IExecutorTyped<WrapperType> executor : executors) {
113+
private int execute(List<? extends IExecutorTyped<CommandSender, WrapperType>> executors,
114+
AbstractExecutionInfo<CommandSender, WrapperType> info, ExecutorType type) throws WrapperCommandSyntaxException {
115+
for (IExecutorTyped<CommandSender, WrapperType> executor : executors) {
119116
if (executor.getType() == type) {
120-
return executor.executeWith(sender, args);
117+
return executor.executeWith(info);
121118
}
122119
}
123120
throw new NoSuchElementException("Executor had no valid executors for type " + type.toString());
@@ -139,8 +136,8 @@ public boolean isForceNative() {
139136
return matches(normalExecutors, ExecutorType.NATIVE) || matches(resultingExecutors, ExecutorType.NATIVE);
140137
}
141138

142-
private boolean matches(List<? extends IExecutorTyped<?>> executors, ExecutorType type) {
143-
for (IExecutorTyped<?> executor : executors) {
139+
private boolean matches(List<? extends IExecutorTyped<?, ?>> executors, ExecutorType type) {
140+
for (IExecutorTyped<?, ?> executor : executors) {
144141
if (executor.getType() == type) {
145142
return true;
146143
}

commandapi-core/src/main/java/dev/jorel/commandapi/CommandAPIHandler.java

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import com.mojang.brigadier.tree.LiteralCommandNode;
3535
import dev.jorel.commandapi.arguments.*;
3636
import dev.jorel.commandapi.commandsenders.AbstractCommandSender;
37+
import dev.jorel.commandapi.executors.AbstractExecutionInfo;
38+
import dev.jorel.commandapi.executors.CommandArguments;
3739
import dev.jorel.commandapi.preprocessor.RequireField;
3840
import dev.jorel.commandapi.wrappers.PreviewableFunction;
3941

@@ -42,8 +44,8 @@
4244
import java.lang.invoke.MethodHandles;
4345
import java.lang.invoke.VarHandle;
4446
import java.lang.reflect.Field;
45-
import java.util.List;
4647
import java.util.*;
48+
import java.util.List;
4749
import java.util.concurrent.CompletableFuture;
4850
import java.util.function.Predicate;
4951

@@ -161,21 +163,54 @@ Command<Source> generateCommand(Argument[] args, CommandAPIExecutor<CommandSende
161163
// Generate our command from executor
162164
return (cmdCtx) -> {
163165
AbstractCommandSender<? extends CommandSender> sender = platform.getSenderForCommand(cmdCtx, executor.isForceNative());
166+
CommandArguments commandArguments = argsToCommandArgs(cmdCtx, args);
167+
AbstractExecutionInfo<CommandSender, AbstractCommandSender<? extends CommandSender>> executionInfo = new AbstractExecutionInfo<>() {
168+
@Override
169+
public CommandSender sender() {
170+
return sender.getSource();
171+
}
172+
173+
@Override
174+
public AbstractCommandSender<? extends CommandSender> senderWrapper() {
175+
return sender;
176+
}
177+
178+
@Override
179+
public CommandArguments args() {
180+
return commandArguments;
181+
}
182+
};
164183
if (converted) {
165-
Object[] argObjs = argsToObjectArr(cmdCtx, args);
166184
int resultValue = 0;
167185

168186
// Return a String[] of arguments for converted commands
169187
String[] argsAndCmd = cmdCtx.getRange().get(cmdCtx.getInput()).split(" ");
170188
String[] result = new String[argsAndCmd.length - 1];
189+
AbstractExecutionInfo<CommandSender, AbstractCommandSender<? extends CommandSender>> convertedExecutionInfo = new AbstractExecutionInfo<>() {
190+
@Override
191+
public CommandSender sender() {
192+
return sender.getSource();
193+
}
194+
195+
@Override
196+
public AbstractCommandSender<? extends CommandSender> senderWrapper() {
197+
return sender;
198+
}
199+
200+
@Override
201+
public CommandArguments args() {
202+
return new CommandArguments(result, new LinkedHashMap<>());
203+
}
204+
};
205+
171206
System.arraycopy(argsAndCmd, 1, result, 0, argsAndCmd.length - 1);
172207

173208
// As stupid as it sounds, it's more performant and safer to use
174209
// a List<?>[] instead of a List<List<?>>, due to NPEs and AIOOBEs.
175210
@SuppressWarnings("unchecked")
176211
List<String>[] entityNamesForArgs = new List[args.length];
177212
for (int i = 0; i < args.length; i++) {
178-
entityNamesForArgs[i] = args[i].getEntityNames(argObjs[i]);
213+
entityNamesForArgs[i] = args[i].getEntityNames(commandArguments.get(i));
179214
}
180215
Li 10000 st<List<String>> product = CartesianProduct.getDescartes(Arrays.asList(entityNamesForArgs));
181216

@@ -189,12 +224,12 @@ Command<Source> generateCommand(Argument[] args, CommandAPIExecutor<CommandSende
189224
}
190225
}
191226
}
192-
resultValue += executor.execute(sender, result);
227+
resultValue += executor.execute(convertedExecutionInfo);
193228
}
194229

195230
return resultValue;
196231
} else {
197-
return executor.execute(sender, argsToObjectArr(cmdCtx, args));
232+
return executor.execute(executionInfo);
198233
}
199234
};
200235
}
@@ -204,22 +239,27 @@ Command<Source> generateCommand(Argument[] args, CommandAPIExecutor<CommandSende
204239
*
205240
* @param cmdCtx the command context that will execute this command
206241
* @param args the map of strings to arguments
207-
* @return an Object[] which can be used in (sender, args) ->
208-
* @throws CommandSyntaxException when an argument isn't formatted correctly
242+
* @return an CommandArguments object which can be used in (sender, args) ->
243+
* @throws CommandSyntaxException
209244
*/
210-
Object[] argsToObjectArr(CommandContext<Source> cmdCtx, Argument[] args)
245+
CommandArguments argsToCommandArgs(CommandContext<Source> cmdCtx, Argument[] args)
211246
throws CommandSyntaxException {
212247
// Array for arguments for executor
213248
List<Object> argList = new ArrayList<>();
214249

250+
// LinkedHashMap for arguments for executor
251+
Map<String, Object> argsMap = new LinkedHashMap<>();
252+
215253
// Populate array
216254
for (Argument argument : args) {
217255
if (argument.isListed()) {
218-
argList.add(parseArgument(cmdCtx, argument.getNodeName(), argument, argList.toArray()));
256+
Object parsedArgument = parseArgument(cmdCtx, argument.getNodeName(), argument, argList.toArray());
257+
argList.add(parsedArgument);
258+
argsMap.put(argument.getNodeName(), parsedArgument);
219259
}
220260
}
221261

222-
return argList.toArray();
262+
D7AE return new CommandArguments(argList.toArray(), argsMap);
223263
}
224264

225265
/**
@@ -633,8 +673,7 @@ LiteralArgumentBuilder<Source> getLiteralArgumentBuilder(String commandName) {
633673
* @param permission the permission required to use this literal
634674
* @return a brigadier LiteralArgumentBuilder representing a literal
635675
*/
636-
LiteralArgumentBuilder<Source> getLiteralArgumentBuilderArgument(String commandName,
637-
CommandPermission permission, Predicate<CommandSender> requirements) {
676+
LiteralArgumentBuilder<Source> getLiteralArgumentBuilderArgument(String commandName, CommandPermission permission, Predicate<CommandSender> requirements) {
638677
LiteralArgumentBuilder<Source> builder = LiteralArgumentBuilder.literal(commandName);
639678
return builder.requires((Source css) -> permissionCheck(platform.getCommandSenderFromCommandSource(css),
640679
permission, requirements));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package dev.jorel.commandapi.executors;
2+
3+
import dev.jorel.commandapi.commandsenders.AbstractCommandSender;
4+
5+
/**
6+
* This interface represents an AbstractExecutionInfo for a command. It provides the sender of a command, as well as it's arguments
7+
*
8+
* @param <Sender> The type of the sender of a command this AbstractExecutionInfo belongs to
9+
*/
10+
public interface AbstractExecutionInfo<Sender, WrapperType extends AbstractCommandSender<? extends Sender>> {
11+
12+
/**
13+
* @return The sender of this command
14+
*/
15+
Sender sender();
16+
17+
/**
18+
* @return The wrapper type of this command
19+
*/
20+
WrapperType senderWrapper();
21+
22+
/**
23+
* @return The arguments of this command
24+
*/
25+
CommandArguments args();
26+
27+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package dev.jorel.commandapi.executors;
2+
3+
import java.util.Map;
4+
5+
/**
6+
* This class stores the arguments for this command
7+
*/
8+
public class CommandArguments {
9+
10+
private final Object[] args;
11+
private final Map<String, Object> argsMap;
12+
13+
/**
14+
* Constructs a new CommandArguments instance
15+
*
16+
* @param args The arguments for this command
17+
* @param argsMap The arguments for this command mapped to the node names. This is an ordered map
18+
*/
19+
public CommandArguments(Object[] args, Map<String, Object> argsMap) {
20+
this.args = args;
21+
this.argsMap = argsMap;
22+
}
23+
24+
/**
25+
* @return The complete argument array of this command
26+
*/
27+
public Object[] args() {
28+
return args;
29+
}
30+
31+
/**
32+
* Returns an argument by its position
33+
*
34+
* @param index The position of this argument
35+
* @return an argument which is placed at the given index
36+
*/
37+
public Object get(int index) {
38+
return args[index];
39+
}
40+
41+
/**
42+
* Returns an argument by its node name
43+
*
44+
* @param nodeName The node name of this argument. This was set when initializing an argument
45+
* @return an argument which has the given node name
46+
*/
47+
public Object get(String nodeName) {
48+
return argsMap.get(nodeName);
49+
}
50+
}

commandapi-core/src/main/java/dev/jorel/commandapi/executors/IExecutorNormal.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,24 @@
2828
* @param <CommandSender> The CommandSender for this executor
2929
* @param <WrapperType> The AbstractCommandSender that wraps the CommandSender
3030
*/
31-
public interface IExecutorNormal<CommandSender, WrapperType extends AbstractCommandSender<? extends CommandSender>> extends IExecutorTyped<WrapperType> {
32-
31+
public interface IExecutorNormal<CommandSender, WrapperType extends AbstractCommandSender<? extends CommandSender>> extends IExecutorTyped<CommandSender, WrapperType> {
3332
/**
3433
* Executes the command executor with the provided command sender and the provided arguments.
35-
* @param sender the command sender for this command
36-
* @param args the arguments provided to this command
34+
* @param info The AbstractExecutionInfo for this command
3735
* @return 1 if the command succeeds, 0 if the command fails
3836
* @throws WrapperCommandSyntaxException if an error occurs during the execution of this command
3937
*/
4038
@Override
41-
default int executeWith(WrapperType sender, Object[] args) throws WrapperCommandSyntaxException {
42-
this.run(sender.getSource(), args);
39+
default int executeWith(AbstractExecutionInfo<CommandSender, WrapperType> info) throws WrapperCommandSyntaxException {
40+
this.run(info);
4341
return 1;
4442
}
45-
43+
4644
/**
4745
* Executes the command.
48-
* @param sender the command sender for this command
49-
* @param args the arguments provided to this command
46+
* @param info The AbstractExecutionInfo for this command
5047
* @throws WrapperCommandSyntaxException if an error occurs during the execution of this command
5148
*/
52-
void run(CommandSender sender, Object[] args) throws WrapperCommandSyntaxException;
49+
void run(AbstractExecutionInfo<CommandSender, WrapperType> info) throws WrapperCommandSyntaxException;
5350

5451
}

0 commit comments

Comments
 (0)
0