3
0
Mirror von https://github.com/IntellectualSites/FastAsyncWorldEdit.git synchronisiert 2024-12-26 19:02:36 +01:00

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 @Override
public boolean satisfied(InjectedValueAccess context) { public boolean satisfied(InjectedValueAccess context) {
return context.injectedValue(ACTOR_KEY) return permissions.isEmpty() ||
context.injectedValue(ACTOR_KEY)
.map(actor -> permissions.stream().anyMatch(actor::hasPermission)) .map(actor -> permissions.stream().anyMatch(actor::hasPermission))
.orElse(false); .orElse(false);
} }

Datei anzeigen

@ -19,34 +19,49 @@
package com.sk89q.worldedit.command.util; package com.sk89q.worldedit.command.util;
import com.google.common.collect.ImmutableSet;
import org.enginehub.piston.Command; import org.enginehub.piston.Command;
import org.enginehub.piston.inject.InjectedValueAccess; import org.enginehub.piston.inject.InjectedValueAccess;
import java.util.Collection; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors; 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; private final Command.Condition aggregate;
public SubCommandPermissionCondition(Collection<Command> commands) { private SubCommandPermissionCondition(Set<String> perms, Command.Condition aggregate) {
super(commands.stream().map(Command::getCondition) super(perms);
.map(c -> c.as(PermissionCondition.class)) this.aggregate = aggregate;
.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);
} }
@Override @Override
public boolean satisfied(InjectedValueAccess context) { public boolean satisfied(InjectedValueAccess context) {
return aggregate.satisfied(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() .required()
.build()); .build());
cmd.condition(new SubCommandPermissionCondition(subCommands)); cmd.condition(new SubCommandPermissionCondition.Generator(subCommands).build());
}); });
} }