From 6f41bad62408414a5ab6408e3f2755753eeb5f93 Mon Sep 17 00:00:00 2001 From: asofold Date: Fri, 9 Nov 2012 10:17:07 +0100 Subject: [PATCH] Refactor slightly to hide SimpleCommandMap (ongoing). --- .../nocheatplus/command/CommandUtil.java | 44 ++++++++++++++++--- .../permissions/PermissionUtil.java | 4 +- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/fr/neatmonster/nocheatplus/command/CommandUtil.java b/src/fr/neatmonster/nocheatplus/command/CommandUtil.java index 51b562b1..d7cd6786 100644 --- a/src/fr/neatmonster/nocheatplus/command/CommandUtil.java +++ b/src/fr/neatmonster/nocheatplus/command/CommandUtil.java @@ -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 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 */ diff --git a/src/fr/neatmonster/nocheatplus/permissions/PermissionUtil.java b/src/fr/neatmonster/nocheatplus/permissions/PermissionUtil.java index 005ecd64..cecc9dcd 100644 --- a/src/fr/neatmonster/nocheatplus/permissions/PermissionUtil.java +++ b/src/fr/neatmonster/nocheatplus/permissions/PermissionUtil.java @@ -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 changed = new LinkedList(); - 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)){