From d7c11cbb690fada46d14f34ec5cb9231ff03d2ed Mon Sep 17 00:00:00 2001 From: wizjany Date: Sun, 2 Jun 2019 02:11:36 -0400 Subject: [PATCH] Handle sub-commands without permissions. --- .../command/util/PermissionCondition.java | 3 +- .../util/SubCommandPermissionCondition.java | 45 ++++++++++++------- .../platform/PlatformCommandManager.java | 2 +- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/PermissionCondition.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/PermissionCondition.java index 6f6de7a0d..99a4622d6 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/PermissionCondition.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/PermissionCondition.java @@ -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); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/SubCommandPermissionCondition.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/SubCommandPermissionCondition.java index 12170980e..c501bbbeb 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/SubCommandPermissionCondition.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/SubCommandPermissionCondition.java @@ -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 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 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 subCommands; + + public Generator(List subCommands) { + this.subCommands = subCommands; + } + + public Command.Condition build() { + final List conditions = subCommands.stream().map(Command::getCondition).collect(Collectors.toList()); + final List> 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 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); + } + } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java index c74f46367..2de667d08 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java @@ -274,7 +274,7 @@ public final class PlatformCommandManager { .required() .build()); - cmd.condition(new SubCommandPermissionCondition(subCommands)); + cmd.condition(new SubCommandPermissionCondition.Generator(subCommands).build()); }); }