From 1e7c074217738f24afc70377c6d8433ec0c5f77e Mon Sep 17 00:00:00 2001 From: wizjany Date: Sat, 1 Jun 2019 07:41:19 -0400 Subject: [PATCH] Few misc command fixes. --- .../bukkit/BukkitCommandInspector.java | 5 +--- .../worldedit/bukkit/WorldEditListener.java | 5 +--- .../worldedit/command/GeneralCommands.java | 7 +++-- .../command/util/PermissionCondition.java | 1 - .../util/WorldEditAsyncCommandBuilder.java | 19 ++++++++++++ .../platform/PlatformCommandManager.java | 5 ++++ .../sk89q/worldedit/forge/CommandWrapper.java | 30 +++++++++---------- 7 files changed, 44 insertions(+), 28 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitCommandInspector.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitCommandInspector.java index 0cd9b1b9d..005d79259 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitCommandInspector.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitCommandInspector.java @@ -80,10 +80,7 @@ class BukkitCommandInspector implements CommandInspector { InjectedValueStore store = MapBackedValueStore.create(); store.injectValue(Key.of(Actor.class), context -> Optional.of(plugin.wrapCommandSender(sender))); - CommandParameters parameters = NoInputCommandParameters.builder() - .injectedValues(MemoizingValueAccess.wrap(store)) - .build(); - return mapping.get().getCondition().satisfied(parameters); + return mapping.get().getCondition().satisfied(store); } else { logger.warn("BukkitCommandInspector doesn't know how about the command '" + command + "'"); return false; diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java index 69c803880..8f19dc3a0 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java @@ -77,14 +77,11 @@ public class WorldEditListener implements Listener { InjectedValueStore store = MapBackedValueStore.create(); store.injectValue(Key.of(Actor.class), context -> Optional.of(plugin.wrapCommandSender(event.getPlayer()))); - CommandParameters parameters = NoInputCommandParameters.builder() - .injectedValues(MemoizingValueAccess.wrap(store)) - .build(); CommandManager commandManager = plugin.getWorldEdit().getPlatformManager().getPlatformCommandManager().getCommandManager(); event.getCommands().removeIf(name -> // remove if in the manager and not satisfied commandManager.getCommand(name) - .filter(command -> !command.getCondition().satisfied(parameters)) + .filter(command -> !command.getCondition().satisfied(store)) .isPresent() ); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/GeneralCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/GeneralCommands.java index 34a56b8e5..92ab5611d 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/GeneralCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/GeneralCommands.java @@ -219,15 +219,16 @@ public class GeneralCommands { aliases = {"/searchitem", "/l", "/search"}, desc = "Search for an item" ) + @CommandPermissions("worldedit.searchitem") public void searchItem(Actor actor, - @Arg(desc = "Search query", variable = true) - List query, @Switch(name = 'b', desc = "Only search for blocks") boolean blocksOnly, @Switch(name = 'i', desc = "Only search for items") boolean itemsOnly, @ArgFlag(name = 'p', desc = "Page of results to return", def = "1") - int page) { + int page, + @Arg(desc = "Search query", variable = true) + List query) { String search = String.join(" ", query); if (search.length() <= 2) { actor.printError("Enter a longer search string (len > 2)."); 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 a3ba83c6f..ac92b2fef 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 @@ -21,7 +21,6 @@ package com.sk89q.worldedit.command.util; import com.sk89q.worldedit.extension.platform.Actor; import org.enginehub.piston.Command; -import org.enginehub.piston.CommandParameters; import org.enginehub.piston.inject.InjectedValueAccess; import org.enginehub.piston.inject.Key; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/WorldEditAsyncCommandBuilder.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/WorldEditAsyncCommandBuilder.java index 903a12449..f21fb2b6d 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/WorldEditAsyncCommandBuilder.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/WorldEditAsyncCommandBuilder.java @@ -1,3 +1,22 @@ +/* + * 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 com.sk89q.worldedit.WorldEdit; 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 0b6c56361..9d2e53d8f 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 @@ -125,6 +125,7 @@ import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.function.Consumer; @@ -583,6 +584,10 @@ public final class PlatformCommandManager { original.getEnd() + 1 ); }).collect(Collectors.toList())); + } catch (ConditionFailedException e) { + if (e.getCondition() instanceof PermissionCondition) { + event.setSuggestions(new ArrayList<>()); + } } catch (CommandException e) { event.getActor().printError(e.getMessage()); } diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/CommandWrapper.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/CommandWrapper.java index 6a15e51dd..4c871e655 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/CommandWrapper.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/CommandWrapper.java @@ -31,17 +31,20 @@ import com.mojang.brigadier.suggestion.Suggestion; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import com.sk89q.worldedit.WorldEdit; -import com.sk89q.worldedit.command.util.PermissionCondition; import com.sk89q.worldedit.event.platform.CommandSuggestionEvent; +import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.internal.util.Substring; import net.minecraft.command.CommandSource; +import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayerMP; +import org.enginehub.piston.inject.InjectedValueStore; +import org.enginehub.piston.inject.Key; +import org.enginehub.piston.inject.MapBackedValueStore; import java.util.List; -import java.util.Set; +import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.function.Predicate; -import java.util.stream.Stream; import static net.minecraft.command.Commands.argument; import static net.minecraft.command.Commands.literal; @@ -58,8 +61,7 @@ public final class CommandWrapper { .then(argument("args", StringArgumentType.greedyString()) .suggests(CommandWrapper::suggest) .executes(FAKE_COMMAND)); - if (command.getCondition().as(PermissionCondition.class) - .filter(p -> p.getPermissions().size() > 0).isPresent()) { + if (command.getCondition() != org.enginehub.piston.Command.Condition.TRUE) { base.requires(requirementsFor(command)); } dispatcher.register(base); @@ -67,8 +69,7 @@ public final class CommandWrapper { } public static final Command FAKE_COMMAND = ctx -> { - EntityPlayerMP player = ctx.getSource().asPlayer(); - if (player.world.isRemote()) { + if (ctx.getSource().getWorld().isRemote) { return 0; } return 1; @@ -76,15 +77,12 @@ public final class CommandWrapper { private static Predicate requirementsFor(org.enginehub.piston.Command mapping) { return ctx -> { - ForgePermissionsProvider permsProvider = ForgeWorldEdit.inst.getPermissionsProvider(); - return ctx.getEntity() instanceof EntityPlayerMP && - mapping.getCondition().as(PermissionCondition.class) - .map(PermissionCondition::getPermissions) - .map(Set::stream) - .orElseGet(Stream::empty) - .allMatch(perm -> permsProvider.hasPermission( - (EntityPlayerMP) ctx.getEntity(), perm - )); + final Entity entity = ctx.getEntity(); + if (!(entity instanceof EntityPlayerMP)) return true; + final Actor actor = ForgeAdapter.adaptPlayer(((EntityPlayerMP) entity)); + InjectedValueStore store = MapBackedValueStore.create(); + store.injectValue(Key.of(Actor.class), context -> Optional.of(actor)); + return mapping.getCondition().satisfied(store); }; }