Skip to content
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

Command build rewrite #602

Draft
wants to merge 42 commits into
base: dev/dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
6517ca5
Initial rewrite of command building system
willkroboth Sep 2, 2023
a321c9f
Disable broken test: `OptionalArgumentTests.kt#executionTestWithComma…
willkroboth Sep 2, 2023
96e7338
Initial rewrite of optional Arguments
willkroboth Oct 14, 2023
0c9bea6
Remove { } from KotlinDSL inline if-else's
willkroboth Oct 14, 2023
365580a
Fix usage of `setOptional` in ExamplesKotlinDSL
willkroboth Oct 14, 2023
5c41d50
Remove optional arguments from CommandTrees
willkroboth Oct 14, 2023
9bcbb2a
Update optional Argument use in example CommandTree
willkroboth Oct 14, 2023
84b3887
Give default value for `block` in DSL's `CommandAPICommand.addArgumen…
willkroboth Oct 14, 2023
8957047
Address some SonarCloud code smells
willkroboth Oct 14, 2023
e176f2a
Remove unused `AbstractArgument#unpackCombinedArguments`
willkroboth Oct 14, 2023
100c8bb
Update tests added to `dev/command-build-rewrite` from `dev/dev`
willkroboth Oct 15, 2023
bd0cbdb
Expand command-building test coverage
willkroboth Oct 15, 2023
963a5cf
Extract logic for generating the SuggestionsProvider for an Argument …
willkroboth Oct 15, 2023
d6e8a5d
Fix MultiLiteralArguments - give nodes multiple parents instead of us…
willkroboth Dec 24, 2023
f464df1
Initial implementation of `FlagsArgument` (and other changes)
willkroboth Dec 27, 2023
f36c43a
Fix `CommandNamespaceTests`
willkroboth Feb 28, 2024
91e1023
Update `dev/command-build-rewrite` after rebasing #509 and #526
willkroboth Mar 19, 2024
aa5947c
Re-implement command conflict detection
willkroboth Apr 3, 2024
a862694
Add tests for `CommandConflictException`
willkroboth Apr 4, 2024
5219af2
Extract common node stacking logic
willkroboth Apr 4, 2024
7b24440
Add tests for `FlagsArgument`
willkroboth Apr 8, 2024
4565eb9
Rewrite `RegisteredCommand`
willkroboth Apr 29, 2024
6f8b66c
Fix & update `Previewable` arguments
willkroboth Apr 30, 2024
8d7a315
Help topic rewrite
willkroboth May 13, 2024
2d1187a
Address a few SonarCloud code smells
willkroboth May 13, 2024
c874ab5
Executor Rewrite
willkroboth Jun 3, 2024
d390ccb
Update CommandExecutionTests after rebasing `dev/command-build-rewrite`
willkroboth Jun 3, 2024
b27e7d4
Fully split converted arguments
willkroboth Jun 6, 2024
2438daf
Split CommandRegistrationTests into separate classes for each exception
willkroboth Jun 9, 2024
288d6ef
Add ArgumentListabilityTests
willkroboth Jun 17, 2024
3649509
Implement `DynamicMultiLiteralArgument`
willkroboth Jun 20, 2024
9007237
Add FlagsArgument to Velocity (and other changes)
willkroboth Jul 4, 2024
f77ee00
Update Examples.kt after rebasing https://github.com/JorelAli/Command…
willkroboth Jul 4, 2024
dee00f1
Clean up files (unused imports and spaces when other lines have tabs)
willkroboth Aug 26, 2024
3dfa38f
Address some SonarCloud issues
willkroboth Aug 27, 2024
8c20d4e
Add serializers for custom CommandNodes
willkroboth Aug 30, 2024
0e66ac8
Make `CommandAPI#unregister` remove commands from `CommandAPIHandler.…
willkroboth Aug 31, 2024
1bc6e7a
Add tests for `DynamicMultiLiteralArgument`
willkroboth Sep 1, 2024
ad5f426
Remove test commands
willkroboth Sep 1, 2024
2d7ec35
Fix proxy senders
willkroboth Sep 1, 2024
c02421f
Fix Brigadier node serialization on Bukkit 1.16.5 and 1.17
willkroboth Sep 2, 2024
4844a0e
Resolve https://github.com/JorelAli/CommandAPI/issues/310
willkroboth Sep 2, 2024
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package dev.jorel.commandapi;

import com.mojang.brigadier.tree.CommandNode;
import dev.jorel.commandapi.arguments.AbstractArgument;
import dev.jorel.commandapi.arguments.AbstractArgument.NodeInformation;
import dev.jorel.commandapi.arguments.AbstractArgument.TerminalNodeModifier;
import dev.jorel.commandapi.exceptions.MissingCommandExecutorException;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -21,19 +25,19 @@ public abstract class AbstractArgumentTree<Impl
/// @endcond
, CommandSender> extends Executable<Impl, CommandSender> {

final List<AbstractArgumentTree<?, Argument, CommandSender>> arguments = new ArrayList<>();
final Argument argument;
private final Argument argument;
private List<AbstractArgumentTree<?, Argument, CommandSender>> arguments = new ArrayList<>();

/**
* Instantiates an {@link AbstractArgumentTree}. This can only be called if the class
* that extends this is an {@link AbstractArgument}
*/
@SuppressWarnings("unchecked")
protected AbstractArgumentTree() {
if (this instanceof AbstractArgument<?, ?, Argument, CommandSender>) {
if (this instanceof AbstractArgument<?, ?, ?, ?>) {
this.argument = (Argument) this;
} else {
throw new IllegalArgumentException("Implicit inherited constructor must be from Argument");
throw new IllegalArgumentException("Implicit inherited constructor must be from AbstractArgument");
}
}

Expand All @@ -49,6 +53,10 @@ protected AbstractArgumentTree(final Argument argument) {
this.executor = argument.executor;
}

/////////////////////
// Builder methods //
/////////////////////

/**
* Create a child branch on this node
*
Expand All @@ -60,19 +68,84 @@ public Impl then(final AbstractArgumentTree<?, Argument, CommandSender> tree) {
return instance();
}

List<Execution<CommandSender, Argument>> getExecutions() {
List<Execution<CommandSender, Argument>> executions = new ArrayList<>();
// If this is executable, add its execution
if (this.executor.hasAnyExecutors()) {
executions.add(new Execution<>(List.of(this.argument), this.executor));
/////////////////////////
// Getters and setters //
/////////////////////////

/**
* @return The child branches added to this tree by {@link #then(AbstractArgumentTree)}.
*/
public List<AbstractArgumentTree<?, Argument, CommandSender>> getArguments() {
return arguments;
}

/**
* Sets the child branches that this node has
*
* @param arguments A new list of branches for this node
*/
public void setArguments(List<AbstractArgumentTree<?, Argument, CommandSender>> arguments) {
this.arguments = arguments;
}

//////////////////
// Registration //
//////////////////
/**
* Builds the Brigadier {@link CommandNode} structure for this argument tree.
*
* @param previousNodeInformation The {@link NodeInformation} of the argument this argument is being added to.
* @param previousArguments A List of CommandAPI arguments that came before this argument tree.
* @param previousArgumentNames A List of Strings containing the node names that came before this argument.
* @param <Source> The Brigadier Source object for running commands.
*/
public <Source> void buildBrigadierNode(
NodeInformation<CommandSender, Source> previousNodeInformation,
List<Argument> previousArguments, List<String> previousArgumentNames
) {
CommandAPIHandler<Argument, CommandSender, Source> handler = CommandAPIHandler.getInstance();

// Check preconditions
if (!executor.hasAnyExecutors() && arguments.isEmpty()) {
// If we don't have any executors then no branches is bad because this path can't be run at all
throw new MissingCommandExecutorException(previousArguments, argument);
}
// Add all executions from all arguments
for (AbstractArgumentTree<?, Argument, CommandSender> tree : arguments) {
for (Execution<CommandSender, Argument> execution : tree.getExecutions()) {
// Prepend this argument to the arguments of the executions
executions.add(execution.prependedBy(this.argument));

// Create executor, if it exists
TerminalNodeModifier<Argument, CommandSender, Source> terminalNodeModifier = (builder, args) -> {
if (executor.hasAnyExecutors()) {
builder.executes(handler.generateBrigadierCommand(args, executor));
}

return builder.build();
};

// Create node for this argument
previousNodeInformation = argument.addArgumentNodes(
previousNodeInformation,
previousArguments, previousArgumentNames,
terminalNodeModifier
);

// Collect children into our own list
List<RegisteredCommand.Node<CommandSender>> childrenNodeInformation = new ArrayList<>();

// Add our branches as children to the node
for (AbstractArgumentTree<?, Argument, CommandSender> child : arguments) {
// Collect children into our own list
NodeInformation<CommandSender, Source> newPreviousNodeInformation = new NodeInformation<>(
previousNodeInformation.lastCommandNodes(),
childrenNodeInformation::addAll
);

// We need a new list so each branch acts independently
List<Argument> newPreviousArguments = new ArrayList<>(previousArguments);
List<String> newPreviousArgumentNames = new ArrayList<>(previousArgumentNames);

child.buildBrigadierNode(newPreviousNodeInformation, newPreviousArguments, newPreviousArgumentNames);
}
return executions;

// Create registered nodes now that all children are generated
previousNodeInformation.childrenConsumer().createNodeWithChildren(childrenNodeInformation);
}
}
Loading
Loading