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
This commit is contained in:
JeromSar 2015-05-12 16:54:51 +02:00
parent e2d0e9e754
commit 17f3a4ca3d
4 changed files with 39 additions and 6 deletions

View file

@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit! #Build Number for ANT. Do not edit!
#Tue May 12 16:41:20 CEST 2015 #Tue May 12 16:51:12 CEST 2015
build.number=1011 build.number=1014

View file

@ -57,8 +57,8 @@ javac.compilerargs=-Xlint:unchecked -Xlint:deprecation
javac.deprecation=false javac.deprecation=false
javac.processorpath=\ javac.processorpath=\
${javac.classpath} ${javac.classpath}
javac.source=1.7 javac.source=1.6
javac.target=1.7 javac.target=1.6
javac.test.classpath=\ javac.test.classpath=\
${javac.classpath}:\ ${javac.classpath}:\
${build.classes.dir} ${build.classes.dir}

View file

@ -125,7 +125,6 @@ blocked_commands:
- 's:b:/reload:_' - 's:b:/reload:_'
# Superadmin commands - Auto-eject # Superadmin commands - Auto-eject
- 's:a:/stop:_'
- 's:a:/save-all:_' - 's:a:/save-all:_'
- 's:a:/save-on:_' - 's:a:/save-on:_'
- 's:a:/save-off:_' - 's:a:/save-off:_'

View file

@ -1,8 +1,11 @@
package me.StevenLawson.TotalFreedomMod; package me.StevenLawson.TotalFreedomMod;
import java.lang.reflect.Field;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader; import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader;
import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry; import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -10,6 +13,7 @@ import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandMap; import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class TFM_CommandBlocker public class TFM_CommandBlocker
@ -90,6 +94,35 @@ public class TFM_CommandBlocker
if (command != null) 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()) for (String alias : command.getAliases())
{ {
BLOCKED_COMMANDS.put(alias.toLowerCase(), blockedCommandEntry); BLOCKED_COMMANDS.put(alias.toLowerCase(), blockedCommandEntry);
@ -120,7 +153,8 @@ public class TFM_CommandBlocker
return true; return true;
} }
if (command.startsWith("/")) { if (command.startsWith("/"))
{
command = command.substring(1); command = command.substring(1);
} }