From be0d21e2a93073881573d9a2ebcb3e49516827d8 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Wed, 5 Dec 2018 16:32:20 +1000 Subject: [PATCH] Filter out commands that the player doesn't have permissions for. Workaround for a Spigot issue. --- .../worldedit/bukkit/WorldEditListener.java | 18 ++++++++++++++++++ .../util/command/composition/FlagParser.java | 1 - 2 files changed, 18 insertions(+), 1 deletion(-) 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 44dd56b44..05774b84c 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 @@ -21,10 +21,13 @@ package com.sk89q.worldedit.bukkit; +import com.sk89q.minecraft.util.commands.CommandLocals; import com.sk89q.util.StringUtil; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.entity.Player; +import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.util.Location; +import com.sk89q.worldedit.util.command.CommandMapping; import com.sk89q.worldedit.world.World; import org.bukkit.block.Block; import org.bukkit.event.Event.Result; @@ -33,10 +36,14 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import org.bukkit.event.player.PlayerCommandSendEvent; import org.bukkit.event.player.PlayerGameModeChangeEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.EquipmentSlot; +import java.util.Set; +import java.util.stream.Collectors; + /** * Handles all events thrown in relation to a Player */ @@ -99,6 +106,17 @@ public class WorldEditListener implements Listener { } } + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void onPlayerCommand(PlayerCommandSendEvent event) { + CommandLocals locals = new CommandLocals(); + locals.put(Actor.class, plugin.wrapCommandSender(event.getPlayer())); + Set toRemove = plugin.getWorldEdit().getPlatformManager().getCommandManager().getDispatcher().getCommands().stream() + .filter(commandMapping -> !commandMapping.getCallable().testPermission(locals)) + .map(CommandMapping::getPrimaryAlias) + .collect(Collectors.toSet()); + event.getCommands().removeIf(toRemove::contains); + } + /** * Called when a player interacts * diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/composition/FlagParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/composition/FlagParser.java index 479a62d6f..fd64dd82d 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/composition/FlagParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/composition/FlagParser.java @@ -183,7 +183,6 @@ public class FlagParser implements CommandExecutor { return (T) data.get(flag); } - @SuppressWarnings("unchecked") public T get(FlagData data, T fallback) { T value = get(data); if (value == null) {