Handle sub-commands without permissions.

Dieser Commit ist enthalten in:
wizjany 2019-06-02 02:11:36 -04:00
Ursprung a904ff9fb9
Commit d7c11cbb69
3 geänderte Dateien mit 33 neuen und 17 gelöschten Zeilen

Datei anzeigen

@ -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);
}

Datei anzeigen

@ -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);
}
}
}

Datei anzeigen

@ -274,7 +274,7 @@ public final class PlatformCommandManager {
.required()
.build());
cmd.condition(new SubCommandPermissionCondition(subCommands));
cmd.condition(new SubCommandPermissionCondition.Generator(subCommands).build());
});
}