Skip to content

Commit

Permalink
More changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi committed Mar 2, 2025
1 parent d4a26fc commit 3e9e375
Show file tree
Hide file tree
Showing 93 changed files with 822 additions and 666 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import dev.rollczi.example.fabric.server.command.ExampleCommand;
import dev.rollczi.litecommands.fabric.LiteFabricFactory;
import dev.rollczi.litecommands.luckperms.LuckPermsResolver;
import dev.rollczi.litecommands.luckperms.LuckPermsPermissionFactory;
import net.fabricmc.api.DedicatedServerModInitializer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
Expand All @@ -13,11 +13,11 @@ public void onInitializeServer() {
LiteFabricFactory.server()
.settings(settings ->
settings.permissionResolver(
new LuckPermsResolver<>(source -> {
new LuckPermsPermissionFactory<>(source -> {
if (source instanceof ServerCommandSource && ((ServerCommandSource) source).isExecutedByPlayer()) {
return ((ServerCommandSource) source).getPlayer().getUuid();
} else if (source instanceof ServerCommandSource && ((ServerCommandSource) source).output instanceof MinecraftServer) {
return LuckPermsResolver.CONSOLE;
return LuckPermsPermissionFactory.CONSOLE;
}
return null;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,4 @@ String thread2() {
return Thread.currentThread().getName();
}

@Execute(name = "testPermission")
@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "3.12.0")
String testPermission(@Sender PlatformSender sender, @Arg String permission) {
return permission + ": " + sender.hasPermission(permission);
}
}
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 @@ -50,7 +50,7 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String alias, Str
ParseableInput<?> input = ParseableInput.raw(args);
PlatformSender platformSender = new BukkitPlatformSender(sender);

this.invocationHook.execute(new Invocation<>(sender, platformSender, commandRoute.getName(), alias, input), input);
this.invocationHook.execute(new Invocation<>(platformSender, commandRoute.getName(), alias, input), input);
return true;
}

Expand Down Expand Up @@ -83,7 +83,7 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String alias, Str
public CompletableFuture<Set<Suggestion>> suggest(CommandSender sender, String alias, String[] args) {
SuggestionInput<?> input = SuggestionInput.raw(args);
PlatformSender platformSender = new BukkitPlatformSender(sender);
Invocation<CommandSender> invocation = new Invocation<>(sender, platformSender, commandRoute.getName(), alias, input);
Invocation<CommandSender> invocation = new Invocation<>(platformSender, commandRoute.getName(), alias, input);

return CompletableFuture.completedFuture(this.suggestionHook.suggest(invocation, input).getSuggestions()); // TODO Run suggestion asynchronously inside LiteCommands platform
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ public BukkitPlatformSender(CommandSender handle) {
this.handle = handle;
}

@Override
public boolean hasPermission(String permission) {
return this.handle.hasPermission(permission);
}

@Override
public String getName() {
return this.handle.getName();
Expand Down Expand Up @@ -49,9 +44,13 @@ public Identifier getIdentifier() {
}

@Override
public Comparable<Void> sendMessage(String message) {
public Object getHandle() {
return this.handle;
}

@Override
public void sendMessage(String message) {
this.handle.sendMessage(message);
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public BungeeCommand(LiteBungeeSettings settings, CommandRoute<CommandSender> co
public void execute(CommandSender sender, String[] args) {
ParseableInput<?> input = ParseableInput.raw(args);
BungeeSender platformSender = new BungeeSender(sender);
Invocation<CommandSender> invocation = new Invocation<>(sender, platformSender, this.commandSection.getName(), this.label, input);
Invocation<CommandSender> invocation = new Invocation<>(platformSender, this.commandSection.getName(), this.label, input);

this.executeListener.execute(invocation, input);
}
Expand All @@ -44,7 +44,7 @@ public void execute(CommandSender sender, String[] args) {
public Iterable<String> onTabComplete(CommandSender sender, String[] args) {
SuggestionInput<?> input = SuggestionInput.raw(args);
BungeeSender platformSender = new BungeeSender(sender);
Invocation<CommandSender> invocation = new Invocation<>(sender, platformSender, this.commandSection.getName(), this.label, input);
Invocation<CommandSender> invocation = new Invocation<>(platformSender, this.commandSection.getName(), this.label, input);

return this.suggestionListener.suggest(invocation, input)
.asMultiLevelList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public Identifier getIdentifier() {
}

@Override
public boolean hasPermission(String permission) {
return this.handle.hasPermission(permission);
public Object getHandle() {
return this.handle;
}

}
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
Loading

0 comments on commit 3e9e375

Please sign in to comment.