|
3 | 3 | import dev.rollczi.litecommands.LiteCommandsException;
|
4 | 4 | import dev.rollczi.litecommands.argument.parser.input.ParseableInputMatcher;
|
5 | 5 | import dev.rollczi.litecommands.command.CommandRoute;
|
6 |
| -import dev.rollczi.litecommands.command.executor.event.CandidateExecutorFoundEvent; |
7 |
| -import dev.rollczi.litecommands.command.executor.event.CandidateExecutorMatchEvent; |
| 6 | +import dev.rollczi.litecommands.command.executor.event.CommandExecutorFoundEvent; |
| 7 | +import dev.rollczi.litecommands.command.executor.event.CommandExecutorNotFoundEvent; |
8 | 8 | import dev.rollczi.litecommands.command.executor.event.CommandPostExecutionEvent;
|
9 | 9 | import dev.rollczi.litecommands.command.executor.event.CommandPreExecutionEvent;
|
10 |
| -import dev.rollczi.litecommands.command.executor.flow.ExecuteFlow; |
| 10 | +import dev.rollczi.litecommands.invalidusage.InvalidUsage.Cause; |
11 | 11 | import dev.rollczi.litecommands.invalidusage.InvalidUsageException;
|
12 | 12 | import dev.rollczi.litecommands.event.EventPublisher;
|
13 | 13 | import dev.rollczi.litecommands.handler.result.ResultHandleService;
|
| 14 | +import dev.rollczi.litecommands.priority.MutablePrioritizedList; |
| 15 | +import dev.rollczi.litecommands.priority.PriorityLevel; |
14 | 16 | import dev.rollczi.litecommands.requirement.RequirementMatchService;
|
15 | 17 | import dev.rollczi.litecommands.shared.FailedReason;
|
16 |
| -import dev.rollczi.litecommands.flow.Flow; |
17 |
| -import dev.rollczi.litecommands.invalidusage.InvalidUsage; |
18 | 18 | import dev.rollczi.litecommands.invocation.Invocation;
|
19 | 19 | import dev.rollczi.litecommands.meta.Meta;
|
20 | 20 | import dev.rollczi.litecommands.scheduler.Scheduler;
|
21 | 21 | import dev.rollczi.litecommands.scheduler.SchedulerPoll;
|
22 |
| -import dev.rollczi.litecommands.validator.ValidatorService; |
23 | 22 | import java.util.Iterator;
|
24 |
| -import org.jetbrains.annotations.Nullable; |
25 | 23 |
|
26 | 24 | import java.util.concurrent.CompletableFuture;
|
27 | 25 | import java.util.concurrent.CompletionException;
|
|
30 | 28 |
|
31 | 29 | public class CommandExecuteService<SENDER> {
|
32 | 30 |
|
33 |
| - private final ValidatorService<SENDER> validatorService; |
34 | 31 | private final ResultHandleService<SENDER> resultResolver;
|
35 | 32 | private final Scheduler scheduler;
|
36 | 33 | private final RequirementMatchService<SENDER> requirementMatchService;
|
37 | 34 | private final EventPublisher publisher;
|
38 | 35 |
|
39 | 36 | public CommandExecuteService(
|
40 |
| - ValidatorService<SENDER> validatorService, |
41 | 37 | ResultHandleService<SENDER> resultResolver,
|
42 | 38 | Scheduler scheduler,
|
43 | 39 | RequirementMatchService<SENDER> requirementMatchService,
|
44 | 40 | EventPublisher publisher
|
45 | 41 | ) {
|
46 |
| - this.validatorService = validatorService; |
47 | 42 | this.resultResolver = resultResolver;
|
48 | 43 | this.scheduler = scheduler;
|
49 | 44 | this.requirementMatchService = requirementMatchService;
|
@@ -92,86 +87,51 @@ private <MATCHER extends ParseableInputMatcher<MATCHER>> CompletableFuture<Comma
|
92 | 87 | ParseableInputMatcher<MATCHER> matcher,
|
93 | 88 | CommandRoute<SENDER> commandRoute
|
94 | 89 | ) {
|
95 |
| - return this.execute(commandRoute.getExecutors().iterator(), invocation, matcher, commandRoute, null); |
| 90 | + return this.execute(commandRoute.getExecutors().iterator(), invocation, matcher, commandRoute, new MutablePrioritizedList<>()); |
96 | 91 | }
|
97 | 92 |
|
98 | 93 | private <MATCHER extends ParseableInputMatcher<MATCHER>> CompletableFuture<CommandExecuteResult> execute(
|
99 | 94 | Iterator<CommandExecutor<SENDER>> executors,
|
100 | 95 | Invocation<SENDER> invocation,
|
101 | 96 | ParseableInputMatcher<MATCHER> matcher,
|
102 | 97 | CommandRoute<SENDER> commandRoute,
|
103 |
| - @Nullable FailedReason last |
| 98 | + MutablePrioritizedList<FailedReason> failedReasons |
104 | 99 | ) {
|
105 | 100 | // Handle failed
|
106 | 101 | if (!executors.hasNext()) {
|
107 |
| - // Route valid |
108 |
| - Flow validate = validatorService.validate(invocation, commandRoute); |
109 |
| - if (validate.isTerminate() || validate.isStopCurrent()) { |
110 |
| - return completedFuture(CommandExecuteResult.failed(validate.getReason())); |
111 |
| - } // TODO: event (CommandExcutorNotFoundEvent) |
112 |
| - |
113 |
| - CommandExecutor<SENDER> executor = commandRoute.getExecutors().isEmpty() |
114 |
| - ? null : |
115 |
| - commandRoute.getExecutors().last(); |
116 |
| - |
117 |
| - if (last != null && last.hasResult()) { |
118 |
| - return completedFuture(CommandExecuteResult.failed(executor, last)); |
| 102 | + if (commandRoute.getExecutors().isEmpty()) { |
| 103 | + failedReasons.add(FailedReason.of(Cause.UNKNOWN_COMMAND, PriorityLevel.LOW)); |
119 | 104 | }
|
120 | 105 |
|
121 |
| - return completedFuture(CommandExecuteResult.failed(InvalidUsage.Cause.UNKNOWN_COMMAND)); |
| 106 | + CommandExecutorNotFoundEvent event = publisher.publish(new CommandExecutorNotFoundEvent(invocation, commandRoute, failedReasons)); |
| 107 | + |
| 108 | + return completedFuture(CommandExecuteResult.failed(event.getFailedReason().getReason())); |
122 | 109 | }
|
123 | 110 |
|
124 | 111 | CommandExecutor<SENDER> executor = executors.next();
|
125 | 112 |
|
126 |
| - if (publisher.hasSubscribers(CandidateExecutorFoundEvent.class)) { |
127 |
| - CandidateExecutorFoundEvent<SENDER> foundEvent = publisher.publish(new CandidateExecutorFoundEvent<>(invocation, executor)); |
128 |
| - if (foundEvent.getFlow() == ExecuteFlow.STOP) { |
129 |
| - return completedFuture(CommandExecuteResult.failed(executor, foundEvent.getFlowResult())); |
130 |
| - } |
131 |
| - |
132 |
| - if (foundEvent.getFlow() == ExecuteFlow.SKIP) { |
133 |
| - return this.execute(executors, invocation, matcher, commandRoute, FailedReason.max(foundEvent.getFlowResult(), last)); |
134 |
| - } |
135 |
| - } |
136 |
| - |
137 | 113 | // Handle matching arguments
|
138 | 114 | return this.requirementMatchService.match(executor, invocation, matcher.copy()).thenCompose(match -> {
|
139 |
| - if (publisher.hasSubscribers(CandidateExecutorMatchEvent.class)) { |
140 |
| - CandidateExecutorMatchEvent<SENDER> matchEvent = publisher.publish(new CandidateExecutorMatchEvent<>(invocation, executor, match)); |
141 |
| - if (matchEvent.getFlow() == ExecuteFlow.STOP) { |
142 |
| - return completedFuture(CommandExecuteResult.failed(executor, matchEvent.getFlowResult())); |
143 |
| - } |
144 |
| - |
145 |
| - if (matchEvent.getFlow() == ExecuteFlow.SKIP) { |
146 |
| - return this.execute(executors, invocation, matcher, commandRoute, FailedReason.max(matchEvent.getFlowResult(), last)); |
147 |
| - } |
| 115 | + CommandExecutorFoundEvent<SENDER> matchEvent = publisher.publish(new CommandExecutorFoundEvent<>(invocation, executor, match)); |
| 116 | + if (matchEvent.isCancelled()) { |
| 117 | + return completedFuture(CommandExecuteResult.failed(executor, matchEvent.getCancelReason())); |
148 | 118 | }
|
149 | 119 |
|
150 |
| - if (match.isFailed()) { |
151 |
| - FailedReason current = match.getFailedReason(); |
152 |
| - |
153 |
| - if (current.hasResult()) { |
154 |
| - return this.execute(executors, invocation, matcher, commandRoute, FailedReason.max(current, last)); |
155 |
| - } |
156 |
| - |
157 |
| - return this.execute(executors, invocation, matcher, commandRoute, last); |
| 120 | + CommandExecutorMatchResult processedMatch = matchEvent.getResult(); |
| 121 | + if (processedMatch.isFailed()) { |
| 122 | + failedReasons.add(processedMatch.getFailedReason()); |
| 123 | + return this.execute(executors, invocation, matcher, commandRoute, failedReasons); |
158 | 124 | }
|
159 | 125 |
|
160 |
| - if (publisher.hasSubscribers(CommandPreExecutionEvent.class)) { |
161 |
| - CommandPreExecutionEvent<SENDER> executionEvent = publisher.publish(new CommandPreExecutionEvent<>(invocation, executor)); |
162 |
| - if (executionEvent.getFlow() == ExecuteFlow.STOP) { |
163 |
| - return completedFuture(CommandExecuteResult.failed(executor, executionEvent.getFlowResult())); |
164 |
| - } |
165 |
| - |
166 |
| - if (executionEvent.getFlow() == ExecuteFlow.SKIP) { |
167 |
| - return this.execute(executors, invocation, matcher, commandRoute, FailedReason.max(executionEvent.getFlowResult(), last)); |
168 |
| - } |
| 126 | + CommandPreExecutionEvent<SENDER> executionEvent = publisher.publish(new CommandPreExecutionEvent<>(invocation, executor)); |
| 127 | + if (executionEvent.isCancelled()) { |
| 128 | + return completedFuture(CommandExecuteResult.failed(executor, executionEvent.getCancelReason())); |
169 | 129 | }
|
170 | 130 |
|
171 | 131 | // Execution
|
172 | 132 | SchedulerPoll type = executor.metaCollector().findFirst(Meta.POLL_TYPE);
|
173 | 133 |
|
174 |
| - return scheduler.supply(type, () -> execute(match, executor)); |
| 134 | + return scheduler.supply(type, () -> execute(processedMatch, executor)); |
175 | 135 | }).exceptionally(throwable -> toThrown(executor, throwable));
|
176 | 136 | }
|
177 | 137 |
|
|
0 commit comments