Mirror von
https://github.com/IntellectualSites/FastAsyncWorldEdit.git
synchronisiert 2024-09-06 16:12:51 +02:00
Handle sub-commands without permissions.
Dieser Commit ist enthalten in:
Ursprung
a904ff9fb9
Commit
d7c11cbb69
@ -42,7 +42,8 @@ public class PermissionCondition implements Command.Condition {
|
||||
|
||||
@Override
|
||||
public boolean satisfied(InjectedValueAccess context) {
|
||||
return context.injectedValue(ACTOR_KEY)
|
||||
return permissions.isEmpty() ||
|
||||
context.injectedValue(ACTOR_KEY)
|
||||
.map(actor -> permissions.stream().anyMatch(actor::hasPermission))
|
||||
.orElse(false);
|
||||
}
|
||||
|
@ -19,34 +19,49 @@
|
||||
|
||||
package com.sk89q.worldedit.command.util;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import org.enginehub.piston.Command;
|
||||
import org.enginehub.piston.inject.InjectedValueAccess;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class SubCommandPermissionCondition extends PermissionCondition {
|
||||
public final class SubCommandPermissionCondition extends PermissionCondition {
|
||||
|
||||
private final Command.Condition aggregate;
|
||||
|
||||
public SubCommandPermissionCondition(Collection<Command> commands) {
|
||||
super(commands.stream().map(Command::getCondition)
|
||||
.map(c -> c.as(PermissionCondition.class))
|
||||
.flatMap(optCon -> optCon.map(permCon -> permCon.getPermissions().stream()).orElse(Stream.empty()))
|
||||
.collect(Collectors.toSet()));
|
||||
this.aggregate = commands.stream().map(Command::getCondition)
|
||||
.map(c -> c.as(PermissionCondition.class))
|
||||
.filter(Optional::isPresent)
|
||||
.map(Optional::get)
|
||||
.map(c -> c.as(Command.Condition.class))
|
||||
.map(Optional::get)
|
||||
.reduce(Command.Condition::or).orElse(Command.Condition.TRUE);
|
||||
private SubCommandPermissionCondition(Set<String> perms, Command.Condition aggregate) {
|
||||
super(perms);
|
||||
this.aggregate = aggregate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean satisfied(InjectedValueAccess context) {
|
||||
return aggregate.satisfied(context);
|
||||
}
|
||||
|
||||
public static class Generator {
|
||||
private final List<Command> subCommands;
|
||||
|
||||
public Generator(List<Command> subCommands) {
|
||||
this.subCommands = subCommands;
|
||||
}
|
||||
|
||||
public Command.Condition build() {
|
||||
final List<Command.Condition> conditions = subCommands.stream().map(Command::getCondition).collect(Collectors.toList());
|
||||
final List<Optional<PermissionCondition>> permConds = conditions.stream().map(c -> c.as(PermissionCondition.class)).collect(Collectors.toList());
|
||||
if (permConds.stream().anyMatch(o -> !o.isPresent())) {
|
||||
// if any sub-command doesn't require permissions, then this command doesn't require permissions
|
||||
return new PermissionCondition(ImmutableSet.of());
|
||||
}
|
||||
// otherwise, this command requires any one subcommand to be available
|
||||
final Set<String> perms = permConds.stream().map(Optional::get).flatMap(cond -> cond.getPermissions().stream()).collect(Collectors.toSet());
|
||||
final Command.Condition aggregate = permConds.stream().map(Optional::get)
|
||||
.map(c -> (Command.Condition) c)
|
||||
.reduce(Command.Condition::or).orElse(TRUE);
|
||||
return new SubCommandPermissionCondition(perms, aggregate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -274,7 +274,7 @@ public final class PlatformCommandManager {
|
||||
.required()
|
||||
.build());
|
||||
|
||||
cmd.condition(new SubCommandPermissionCondition(subCommands));
|
||||
cmd.condition(new SubCommandPermissionCondition.Generator(subCommands).build());
|
||||
});
|
||||
}
|
||||
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren