From 9fc50e315b2b83ac6e7097f7dbf05d6d98c32887 Mon Sep 17 00:00:00 2001 From: asofold Date: Wed, 7 Nov 2012 05:25:42 +0100 Subject: [PATCH] Add convenience logging method. PErmisisonUtil was moved. --- .../permissions/PermissionUtil.java | 91 ++++++++++++++++--- .../nocheatplus/utilities/CheckUtils.java | 15 ++- 2 files changed, 89 insertions(+), 17 deletions(-) diff --git a/src/fr/neatmonster/nocheatplus/permissions/PermissionUtil.java b/src/fr/neatmonster/nocheatplus/permissions/PermissionUtil.java index bb655d0f..537bbb52 100644 --- a/src/fr/neatmonster/nocheatplus/permissions/PermissionUtil.java +++ b/src/fr/neatmonster/nocheatplus/permissions/PermissionUtil.java @@ -1,39 +1,98 @@ package fr.neatmonster.nocheatplus.permissions; +import java.util.Collection; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.SimpleCommandMap; -import org.bukkit.craftbukkit.CraftServer; import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; import org.bukkit.plugin.PluginManager; +import fr.neatmonster.nocheatplus.command.CommandUtil; + public class PermissionUtil { - public static SimpleCommandMap getCommandMap(){ - return (((CraftServer) Bukkit.getServer()).getCommandMap()); + /** + * Entry for what the old state of a command was. + * @author mc_dev + * + */ + public static class CommandProtectionEntry{ + public final Command command; + public final String label; + public final String permission; + public final PermissionDefault permissionDefault; + public final String permissionMessage; + /** + * + * @param command + * @param label trim + lower case. + * @param permission + * @param permissionDefault + * @param permissionMessage + */ + public CommandProtectionEntry(Command command, String label, String permission, PermissionDefault permissionDefault, String permissionMessage){ + this.command = command; + this.label = label; + this.permission = permission; + this.permissionDefault = permissionDefault; + this.permissionMessage = permissionMessage; + } + + public void restore(){ + Command registered = CommandUtil.getCommand(label); + if (registered == null || registered != command) return; + if (!label.equalsIgnoreCase(command.getLabel().trim().toLowerCase())) command.setLabel(label); + command.setPermission(permission); + if (permission != null && permissionDefault != null){ + Permission perm = Bukkit.getPluginManager().getPermission(permission); + if (perm != null) perm.setDefault(permissionDefault); + } + command.setPermissionMessage(permissionMessage); + } } /** - * TODO: Return undo info. + * + * @param commands + * @param permissionBase + * @param ops + * @return + */ + public static Collection protectCommands(Collection commands, String permissionBase, boolean ops){ + return protectCommands(permissionBase, commands, true, ops); + } + + /** + * * @param permissionBase * @param ignoredCommands + * @param invertIgnored * @param ops + * @return */ - public static void alterCommandPermissions(String permissionBase, Set ignoredCommands, boolean invertIgnored, boolean ops){ + public static Collection protectCommands(String permissionBase, Collection ignoredCommands, boolean invertIgnored, boolean ops){ + Set checked = new HashSet(); + for (String label : ignoredCommands){ + checked.add(CommandUtil.getCommandLabel(label, false)); + } PluginManager pm = Bukkit.getPluginManager(); Permission rootPerm = pm.getPermission(permissionBase); if (rootPerm == null){ rootPerm = new Permission(permissionBase); pm.addPermission(rootPerm); } - SimpleCommandMap map = getCommandMap(); + List changed = new LinkedList(); + SimpleCommandMap map = CommandUtil.getCommandMap(); for (Command command : map.getCommands()){ String lcLabel = command.getLabel().trim().toLowerCase(); - if (ignoredCommands != null){ - if (ignoredCommands.contains(lcLabel)){ + if (checked != null){ + if (checked.contains(lcLabel)){ if (!invertIgnored) continue; } else if (invertIgnored) continue; @@ -47,21 +106,25 @@ public class PermissionUtil { command.setPermission(cmdPermName); cmdHadPerm = false; } - else cmdHadPerm = true; + else{ + cmdHadPerm = true; + } // Set permission default behavior. Permission cmdPerm = pm.getPermission(cmdPermName); if (cmdPerm == null){ if (!cmdHadPerm){ cmdPerm = new Permission(cmdPermName); cmdPerm.addParent(rootPerm, true); - cmdPerm.setDefault(ops ? PermissionDefault.OP : PermissionDefault.FALSE); pm.addPermission(cmdPerm); } } - else{ - // Change default of the permission. - cmdPerm.setDefault(ops ? PermissionDefault.OP : PermissionDefault.FALSE); - } + // Create change history entry. + if (cmdHadPerm) changed.add(new CommandProtectionEntry(command, lcLabel, cmdPermName, cmdPerm.getDefault(), command.getPermissionMessage())); + else changed.add(new CommandProtectionEntry(command, lcLabel, null, null, command.getPermissionMessage())); + // Change + cmdPerm.setDefault(ops ? PermissionDefault.OP : PermissionDefault.FALSE); + command.setPermissionMessage("Unknown command. Type \"help\" for help."); } + return changed; } } diff --git a/src/fr/neatmonster/nocheatplus/utilities/CheckUtils.java b/src/fr/neatmonster/nocheatplus/utilities/CheckUtils.java index d2157b73..918c1bb4 100644 --- a/src/fr/neatmonster/nocheatplus/utilities/CheckUtils.java +++ b/src/fr/neatmonster/nocheatplus/utilities/CheckUtils.java @@ -255,10 +255,19 @@ public class CheckUtils { return text; } - public static void scheduleOutput(final Exception e) { + public static void scheduleOutput(final Throwable t) { + scheduleOutput(toString(t)); + } + + public static void logSevere(final Throwable t) { + Bukkit.getLogger().severe(toString(t)); + } + + public static String toString(final Throwable t){ + // TODO: Find the fastest way. final PrintWriter pw = new PrintWriter(new StringWriter(340)); - e.printStackTrace(pw); - scheduleOutput(pw.toString()); + t.printStackTrace(pw); + return pw.toString(); } /**