Refactor slightly to hide SimpleCommandMap (ongoing).

This commit is contained in:
asofold 2012-11-09 10:17:07 +01:00
parent d4e171b9c9
commit 6f41bad624
2 changed files with 40 additions and 8 deletions

View File

@ -1,24 +1,53 @@
package fr.neatmonster.nocheatplus.command;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.craftbukkit.CraftServer;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.utilities.LogUtil;
public class CommandUtil {
public static SimpleCommandMap getCommandMap(){
return (((CraftServer) Bukkit.getServer()).getCommandMap());
/**
* Return plugin + server commands [Subject to change].
* @return Returns null if not CraftBukkit or CommandMap not available.
*/
public static CommandMap getCommandMap(){
// TODO: compat / null
try{
return (((CraftServer) Bukkit.getServer()).getCommandMap());
}
catch(Throwable t){
LogUtil.logSevere(t);
return null;
}
}
/**
* Get the command label (trim + lower case).
* Fails with an exception if SimpleCommandMap is not found, currently.
* @return
*/
public static Collection<Command> getCommands(){
CommandMap commandMap = getCommandMap();
if (commandMap != null && commandMap instanceof SimpleCommandMap){
return ((SimpleCommandMap) commandMap).getCommands();
}
else{
// TODO: Find a way to also secure server commands.
throw new RuntimeException("Can not handle other than SimpleCommandMap.");
}
}
/**
* Get the command label (trim + lower case), include server commands [subject to change].
* @param alias
* @param strict If to return null if no command is found.
* @return
@ -31,14 +60,19 @@ public class CommandUtil {
else return command.getLabel().trim().toLowerCase();
}
/**
* Get a command, include server commands [subject to change].
* @param alias
* @return
*/
public static Command getCommand(final String alias) {
final SimpleCommandMap map = getCommandMap();
final CommandMap map = getCommandMap();
final String lcAlias = alias.trim().toLowerCase();
return map.getCommand(lcAlias);
}
/**
*
* Match for CheckType, some smart method, to also match after first "_" for convenience of input.
* @param input
* @return
*/

View File

@ -8,7 +8,6 @@ import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.PluginManager;
@ -88,8 +87,7 @@ public class PermissionUtil {
pm.addPermission(rootPerm);
}
List<CommandProtectionEntry> changed = new LinkedList<CommandProtectionEntry>();
SimpleCommandMap map = CommandUtil.getCommandMap();
for (Command command : map.getCommands()){
for (Command command : CommandUtil.getCommands()){
String lcLabel = command.getLabel().trim().toLowerCase();
if (checked != null){
if (checked.contains(lcLabel)){