From a904ff9fb9acbbd6dfc067628cf095877f5454a5 Mon Sep 17 00:00:00 2001 From: wizjany Date: Sat, 1 Jun 2019 09:16:08 -0400 Subject: [PATCH] Check sub-command permissions. This ensures root commands aren't sent to the client/suggested unless a player has at least one subcommand available to them. --- .../command/util/PermissionCondition.java | 2 +- .../util/SubCommandPermissionCondition.java | 52 +++++++++++++++++++ .../platform/PlatformCommandManager.java | 6 ++- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/util/SubCommandPermissionCondition.java 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 ac92b2fef..6f6de7a0d 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 @@ -26,7 +26,7 @@ import org.enginehub.piston.inject.Key; import java.util.Set; -public final class PermissionCondition implements Command.Condition { +public class PermissionCondition implements Command.Condition { private static final Key ACTOR_KEY = Key.of(Actor.class); 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 new file mode 100644 index 000000000..12170980e --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/SubCommandPermissionCondition.java @@ -0,0 +1,52 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.command.util; + +import org.enginehub.piston.Command; +import org.enginehub.piston.inject.InjectedValueAccess; + +import java.util.Collection; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public 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); + } + + @Override + public boolean satisfied(InjectedValueAccess context) { + return aggregate.satisfied(context); + } +} 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 87d39d80b..c74f46367 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 @@ -80,6 +80,7 @@ import com.sk89q.worldedit.command.argument.RegistryConverter; import com.sk89q.worldedit.command.argument.VectorConverter; import com.sk89q.worldedit.command.argument.ZonedDateTimeConverter; import com.sk89q.worldedit.command.util.PermissionCondition; +import com.sk89q.worldedit.command.util.SubCommandPermissionCondition; import com.sk89q.worldedit.entity.Entity; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.event.platform.CommandEvent; @@ -266,11 +267,14 @@ public final class PlatformCommandManager { ); additionalConfig.accept(manager); + final List subCommands = manager.getAllCommands().collect(Collectors.toList()); cmd.addPart(SubCommandPart.builder(TranslatableComponent.of("worldedit.argument.action"), TextComponent.of("Sub-command to run.")) - .withCommands(manager.getAllCommands().collect(Collectors.toList())) + .withCommands(subCommands) .required() .build()); + + cmd.condition(new SubCommandPermissionCondition(subCommands)); }); }