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.
This commit is contained in:
wizjany 2019-06-01 09:16:08 -04:00
parent bae2a0b244
commit a904ff9fb9
3 changed files with 58 additions and 2 deletions

View File

@ -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> ACTOR_KEY = Key.of(Actor.class);

View File

@ -0,0 +1,52 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <http://www.gnu.org/licenses/>.
*/
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<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);
}
@Override
public boolean satisfied(InjectedValueAccess context) {
return aggregate.satisfied(context);
}
}

View File

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