From 17f3a4ca3d8204f7233869ab1aacdc2efd7cca7c Mon Sep 17 00:00:00 2001 From: JeromSar Date: Tue, 12 May 2015 16:54:51 +0200 Subject: [PATCH] Remove blocked commands from the CommandMap. Resolves #622 Temporarily workaround: Remove blocked command from the CommandMap In Spigot 1.8.3, cancelling PlayerCommandPreprocessEvent will have no effect This results in TFM failing to block player commands: The player will get a message, but the command will still execute. Removing the command from the CommandMap is a temporary workaround untill the related Spigot issue has been fixed. https://hub.spigotmc.org/jira/browse/SPIGOT-879 --- buildnumber.properties | 4 +-- nbproject/project.properties | 4 +-- src/config.yml | 1 - .../TotalFreedomMod/TFM_CommandBlocker.java | 36 ++++++++++++++++++- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/buildnumber.properties b/buildnumber.properties index 6a1f7e7..8eb3d19 100644 --- a/buildnumber.properties +++ b/buildnumber.properties @@ -1,3 +1,3 @@ #Build Number for ANT. Do not edit! -#Tue May 12 16:41:20 CEST 2015 -build.number=1011 +#Tue May 12 16:51:12 CEST 2015 +build.number=1014 diff --git a/nbproject/project.properties b/nbproject/project.properties index cfd893d..2656919 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -57,8 +57,8 @@ javac.compilerargs=-Xlint:unchecked -Xlint:deprecation javac.deprecation=false javac.processorpath=\ ${javac.classpath} -javac.source=1.7 -javac.target=1.7 +javac.source=1.6 +javac.target=1.6 javac.test.classpath=\ ${javac.classpath}:\ ${build.classes.dir} diff --git a/src/config.yml b/src/config.yml index f4bec58..386b34c 100644 --- a/src/config.yml +++ b/src/config.yml @@ -125,7 +125,6 @@ blocked_commands: - 's:b:/reload:_' # Superadmin commands - Auto-eject - - 's:a:/stop:_' - 's:a:/save-all:_' - 's:a:/save-on:_' - 's:a:/save-off:_' diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_CommandBlocker.java b/src/me/StevenLawson/TotalFreedomMod/TFM_CommandBlocker.java index d165578..1681f13 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_CommandBlocker.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_CommandBlocker.java @@ -1,8 +1,11 @@ package me.StevenLawson.TotalFreedomMod; +import java.lang.reflect.Field; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader; import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry; import org.apache.commons.lang3.StringUtils; @@ -10,6 +13,7 @@ import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandMap; import org.bukkit.command.CommandSender; +import org.bukkit.command.SimpleCommandMap; import org.bukkit.entity.Player; public class TFM_CommandBlocker @@ -90,6 +94,35 @@ public class TFM_CommandBlocker if (command != null) { + // Temporarily workaround: Remove blocked command from the CommandMap + // In Spigot 1.8.3, cancelling PlayerCommandPreprocessEvent will have no effect + // This results in TFM failing to block player commands: The player will get a message, + // but the command will still execute. Removing the command from the CommandMap is a + // temporary workaround untill the related Spigot issue has been fixed. + // https://hub.spigotmc.org/jira/browse/SPIGOT-879 + try + { + Field field = SimpleCommandMap.class.getDeclaredField("knownCommands"); + field.setAccessible(true); + Map knownCommands = (Map) field.get(commandMap); + + Iterator it = knownCommands.entrySet().iterator(); + while (it.hasNext()) + { + final Object e = it.next(); + if (command.equals(((Entry) e).getValue())) + { + it.remove(); + } + } + } + catch (Exception ex) + { + TFM_Log.severe("Could not nullify command: " + command.getName()); + TFM_Log.severe(ex); + } + // End Temporary workaround + for (String alias : command.getAliases()) { BLOCKED_COMMANDS.put(alias.toLowerCase(), blockedCommandEntry); @@ -120,7 +153,8 @@ public class TFM_CommandBlocker return true; } - if (command.startsWith("/")) { + if (command.startsWith("/")) + { command = command.substring(1); }