Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi committed Mar 1, 2025
1 parent d4a26fc commit fb9d43e
Show file tree
Hide file tree
Showing 57 changed files with 565 additions and 454 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.lang.annotation.Annotation;

@Deprecated
@FunctionalInterface
public interface AnnotatedValidator<SENDER, T, A extends Annotation> {

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

import java.lang.annotation.Annotation;

@Deprecated
public class AnnotatedValidatorProcessor<SENDER, T, A extends Annotation> implements AnnotationProcessor<SENDER> {

private final Class<A> annotationClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.lang.annotation.Annotation;

@Deprecated
class RequirementAnnotatedValidatorImpl<SENDER, T, A extends Annotation> implements RequirementValidator<SENDER, T> {

private final AnnotatedValidator<SENDER, T, A> validator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dev.rollczi.litecommands.command.builder.CommandBuilderCollector;
import dev.rollczi.litecommands.editor.EditorService;
import dev.rollczi.litecommands.message.MessageRegistry;
import dev.rollczi.litecommands.permission.PermissionService;
import dev.rollczi.litecommands.platform.PlatformSettings;
import dev.rollczi.litecommands.platform.Platform;
import dev.rollczi.litecommands.scheduler.Scheduler;
Expand Down Expand Up @@ -39,6 +40,9 @@ public interface LiteCommandsInternal<SENDER, C extends PlatformSettings> {
@ApiStatus.Internal
EditorService<SENDER> getEditorService();

@ApiStatus.Internal
PermissionService getPermissionService();

@ApiStatus.Internal
ValidatorService<SENDER> getValidatorService();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ static <EXPECTED> ParseCompletedResult<EXPECTED> failure(Object failedReason) {
return new ParseCompletedResult<>(null, FailedReason.of(failedReason), false, Collections.emptyList());
}

@Deprecated
static <EXPECTED> ParseCompletedResult<EXPECTED> failure() {
return new ParseCompletedResult<>(null, FailedReason.empty(), false, Collections.emptyList());
}

@ApiStatus.Experimental
static <EXPECTED> ParseCompletedResult<EXPECTED> conditional(EXPECTED parsed, List<RequirementCondition> conditions) {
return new ParseCompletedResult<>(parsed, null, false, Collections.unmodifiableList(conditions));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package dev.rollczi.litecommands.command;

import dev.rollczi.litecommands.meta.MetaHolder;
import org.jetbrains.annotations.Nullable;

/**
* CommandNode is an any node of the command tree.
* @see CommandRoute
* @see dev.rollczi.litecommands.command.executor.CommandExecutor
*/
public interface CommandNode<SENDER> {
public interface CommandNode<SENDER> extends MetaHolder {

CommandRoute<SENDER> getParent();

@Override
@Nullable
default MetaHolder parentMeta() {
return getParent();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static CommandExecuteResult failed(CommandExecutor<?> executor, FailedRea
Preconditions.notNull(executor, "executor");
Preconditions.notNull(failedReason, "failed cannot be null");

return new CommandExecuteResult(executor, null, null, failedReason.getReasonOr(null));
return new CommandExecuteResult(executor, null, null, failedReason.getReason());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@
import dev.rollczi.litecommands.LiteCommandsException;
import dev.rollczi.litecommands.argument.parser.input.ParseableInputMatcher;
import dev.rollczi.litecommands.command.CommandRoute;
import dev.rollczi.litecommands.command.executor.event.CandidateExecutorFoundEvent;
import dev.rollczi.litecommands.command.executor.event.CandidateExecutorMatchEvent;
import dev.rollczi.litecommands.command.executor.event.CommandExecutorFoundEvent;
import dev.rollczi.litecommands.command.executor.event.CommandExecutorNotFoundEvent;
import dev.rollczi.litecommands.command.executor.event.CommandPostExecutionEvent;
import dev.rollczi.litecommands.command.executor.event.CommandPreExecutionEvent;
import dev.rollczi.litecommands.command.executor.flow.ExecuteFlow;
import dev.rollczi.litecommands.invalidusage.InvalidUsage.Cause;
import dev.rollczi.litecommands.invalidusage.InvalidUsageException;
import dev.rollczi.litecommands.event.EventPublisher;
import dev.rollczi.litecommands.handler.result.ResultHandleService;
import dev.rollczi.litecommands.priority.MutablePrioritizedList;
import dev.rollczi.litecommands.priority.PriorityLevel;
import dev.rollczi.litecommands.requirement.RequirementMatchService;
import dev.rollczi.litecommands.shared.FailedReason;
import dev.rollczi.litecommands.flow.Flow;
import dev.rollczi.litecommands.invalidusage.InvalidUsage;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.meta.Meta;
import dev.rollczi.litecommands.scheduler.Scheduler;
import dev.rollczi.litecommands.scheduler.SchedulerPoll;
import dev.rollczi.litecommands.validator.ValidatorService;
import java.util.Iterator;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
Expand All @@ -30,20 +28,17 @@

public class CommandExecuteService<SENDER> {

private final ValidatorService<SENDER> validatorService;
private final ResultHandleService<SENDER> resultResolver;
private final Scheduler scheduler;
private final RequirementMatchService<SENDER> requirementMatchService;
private final EventPublisher publisher;

public CommandExecuteService(
ValidatorService<SENDER> validatorService,
ResultHandleService<SENDER> resultResolver,
Scheduler scheduler,
RequirementMatchService<SENDER> requirementMatchService,
EventPublisher publisher
) {
this.validatorService = validatorService;
this.resultResolver = resultResolver;
this.scheduler = scheduler;
this.requirementMatchService = requirementMatchService;
Expand Down Expand Up @@ -92,86 +87,51 @@ private <MATCHER extends ParseableInputMatcher<MATCHER>> CompletableFuture<Comma
ParseableInputMatcher<MATCHER> matcher,
CommandRoute<SENDER> commandRoute
) {
return this.execute(commandRoute.getExecutors().iterator(), invocation, matcher, commandRoute, null);
return this.execute(commandRoute.getExecutors().iterator(), invocation, matcher, commandRoute, new MutablePrioritizedList<>());
}

private <MATCHER extends ParseableInputMatcher<MATCHER>> CompletableFuture<CommandExecuteResult> execute(
Iterator<CommandExecutor<SENDER>> executors,
Invocation<SENDER> invocation,
ParseableInputMatcher<MATCHER> matcher,
CommandRoute<SENDER> commandRoute,
@Nullable FailedReason last
MutablePrioritizedList<FailedReason> failedReasons
) {
// Handle failed
if (!executors.hasNext()) {
// Route valid
Flow validate = validatorService.validate(invocation, commandRoute);
if (validate.isTerminate() || validate.isStopCurrent()) {
return completedFuture(CommandExecuteResult.failed(validate.getReason()));
} // TODO: event (CommandExcutorNotFoundEvent)

CommandExecutor<SENDER> executor = commandRoute.getExecutors().isEmpty()
? null :
commandRoute.getExecutors().last();

if (last != null && last.hasResult()) {
return completedFuture(CommandExecuteResult.failed(executor, last));
if (commandRoute.getExecutors().isEmpty()) {
failedReasons.add(FailedReason.of(Cause.UNKNOWN_COMMAND, PriorityLevel.LOW));
}

return completedFuture(CommandExecuteResult.failed(InvalidUsage.Cause.UNKNOWN_COMMAND));
CommandExecutorNotFoundEvent event = publisher.publish(new CommandExecutorNotFoundEvent(invocation, commandRoute, failedReasons));

return completedFuture(CommandExecuteResult.failed(event.getFailedReason().getReason()));
}

CommandExecutor<SENDER> executor = executors.next();

if (publisher.hasSubscribers(CandidateExecutorFoundEvent.class)) {
CandidateExecutorFoundEvent<SENDER> foundEvent = publisher.publish(new CandidateExecutorFoundEvent<>(invocation, executor));
if (foundEvent.getFlow() == ExecuteFlow.STOP) {
return completedFuture(CommandExecuteResult.failed(executor, foundEvent.getFlowResult()));
}

if (foundEvent.getFlow() == ExecuteFlow.SKIP) {
return this.execute(executors, invocation, matcher, commandRoute, FailedReason.max(foundEvent.getFlowResult(), last));
}
}

// Handle matching arguments
return this.requirementMatchService.match(executor, invocation, matcher.copy()).thenCompose(match -> {
if (publisher.hasSubscribers(CandidateExecutorMatchEvent.class)) {
CandidateExecutorMatchEvent<SENDER> matchEvent = publisher.publish(new CandidateExecutorMatchEvent<>(invocation, executor, match));
if (matchEvent.getFlow() == ExecuteFlow.STOP) {
return completedFuture(CommandExecuteResult.failed(executor, matchEvent.getFlowResult()));
}

if (matchEvent.getFlow() == ExecuteFlow.SKIP) {
return this.execute(executors, invocation, matcher, commandRoute, FailedReason.max(matchEvent.getFlowResult(), last));
}
CommandExecutorFoundEvent<SENDER> matchEvent = publisher.publish(new CommandExecutorFoundEvent<>(invocation, executor, match));
if (matchEvent.isCancelled()) {
return completedFuture(CommandExecuteResult.failed(executor, matchEvent.getCancelReason()));
}

if (match.isFailed()) {
FailedReason current = match.getFailedReason();

if (current.hasResult()) {
return this.execute(executors, invocation, matcher, commandRoute, FailedReason.max(current, last));
}

return this.execute(executors, invocation, matcher, commandRoute, last);
CommandExecutorMatchResult processedMatch = matchEvent.getResult();
if (processedMatch.isFailed()) {
failedReasons.add(processedMatch.getFailedReason());
return this.execute(executors, invocation, matcher, commandRoute, failedReasons);
}

if (publisher.hasSubscribers(CommandPreExecutionEvent.class)) {
CommandPreExecutionEvent<SENDER> executionEvent = publisher.publish(new CommandPreExecutionEvent<>(invocation, executor));
if (executionEvent.getFlow() == ExecuteFlow.STOP) {
return completedFuture(CommandExecuteResult.failed(executor, executionEvent.getFlowResult()));
}

if (executionEvent.getFlow() == ExecuteFlow.SKIP) {
return this.execute(executors, invocation, matcher, commandRoute, FailedReason.max(executionEvent.getFlowResult(), last));
}
CommandPreExecutionEvent<SENDER> executionEvent = publisher.publish(new CommandPreExecutionEvent<>(invocation, executor));
if (executionEvent.isCancelled()) {
return completedFuture(CommandExecuteResult.failed(executor, executionEvent.getCancelReason()));
}

// Execution
SchedulerPoll type = executor.metaCollector().findFirst(Meta.POLL_TYPE);

return scheduler.supply(type, () -> execute(match, executor));
return scheduler.supply(type, () -> execute(processedMatch, executor));
}).exceptionally(throwable -> toThrown(executor, throwable));
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
/**
* Represents an event fired when a command executor is executed.
* Flow:
* - {@link CandidateExecutorFoundEvent}
* - {@link CandidateExecutorMatchEvent}
* - {@link CommandExecutorFoundEvent}
* - {@link CommandExecutorFoundEvent}
* - {@link CommandPreExecutionEvent}
* - {@link CommandPostExecutionEvent}
*/
Expand Down
Loading

0 comments on commit fb9d43e

Please sign in to comment.