Add convenience logging method. PErmisisonUtil was moved.

This commit is contained in:
asofold 2012-11-07 05:25:42 +01:00
parent 59d0ea2629
commit 9fc50e315b
2 changed files with 89 additions and 17 deletions

View File

@ -1,39 +1,98 @@
package fr.neatmonster.nocheatplus.permissions; 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 java.util.Set;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.SimpleCommandMap; import org.bukkit.command.SimpleCommandMap;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import fr.neatmonster.nocheatplus.command.CommandUtil;
public class PermissionUtil { 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<CommandProtectionEntry> protectCommands(Collection<String> commands, String permissionBase, boolean ops){
return protectCommands(permissionBase, commands, true, ops);
}
/**
*
* @param permissionBase * @param permissionBase
* @param ignoredCommands * @param ignoredCommands
* @param invertIgnored
* @param ops * @param ops
* @return
*/ */
public static void alterCommandPermissions(String permissionBase, Set<String> ignoredCommands, boolean invertIgnored, boolean ops){ public static Collection<CommandProtectionEntry> protectCommands(String permissionBase, Collection<String> ignoredCommands, boolean invertIgnored, boolean ops){
Set<String> checked = new HashSet<String>();
for (String label : ignoredCommands){
checked.add(CommandUtil.getCommandLabel(label, false));
}
PluginManager pm = Bukkit.getPluginManager(); PluginManager pm = Bukkit.getPluginManager();
Permission rootPerm = pm.getPermission(permissionBase); Permission rootPerm = pm.getPermission(permissionBase);
if (rootPerm == null){ if (rootPerm == null){
rootPerm = new Permission(permissionBase); rootPerm = new Permission(permissionBase);
pm.addPermission(rootPerm); pm.addPermission(rootPerm);
} }
SimpleCommandMap map = getCommandMap(); List<CommandProtectionEntry> changed = new LinkedList<CommandProtectionEntry>();
SimpleCommandMap map = CommandUtil.getCommandMap();
for (Command command : map.getCommands()){ for (Command command : map.getCommands()){
String lcLabel = command.getLabel().trim().toLowerCase(); String lcLabel = command.getLabel().trim().toLowerCase();
if (ignoredCommands != null){ if (checked != null){
if (ignoredCommands.contains(lcLabel)){ if (checked.contains(lcLabel)){
if (!invertIgnored) continue; if (!invertIgnored) continue;
} }
else if (invertIgnored) continue; else if (invertIgnored) continue;
@ -47,21 +106,25 @@ public class PermissionUtil {
command.setPermission(cmdPermName); command.setPermission(cmdPermName);
cmdHadPerm = false; cmdHadPerm = false;
} }
else cmdHadPerm = true; else{
cmdHadPerm = true;
}
// Set permission default behavior. // Set permission default behavior.
Permission cmdPerm = pm.getPermission(cmdPermName); Permission cmdPerm = pm.getPermission(cmdPermName);
if (cmdPerm == null){ if (cmdPerm == null){
if (!cmdHadPerm){ if (!cmdHadPerm){
cmdPerm = new Permission(cmdPermName); cmdPerm = new Permission(cmdPermName);
cmdPerm.addParent(rootPerm, true); cmdPerm.addParent(rootPerm, true);
cmdPerm.setDefault(ops ? PermissionDefault.OP : PermissionDefault.FALSE);
pm.addPermission(cmdPerm); pm.addPermission(cmdPerm);
} }
} }
else{ // Create change history entry.
// Change default of the permission. if (cmdHadPerm) changed.add(new CommandProtectionEntry(command, lcLabel, cmdPermName, cmdPerm.getDefault(), command.getPermissionMessage()));
cmdPerm.setDefault(ops ? PermissionDefault.OP : PermissionDefault.FALSE); 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;
} }
} }

View File

@ -255,10 +255,19 @@ public class CheckUtils {
return text; 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)); final PrintWriter pw = new PrintWriter(new StringWriter(340));
e.printStackTrace(pw); t.printStackTrace(pw);
scheduleOutput(pw.toString()); return pw.toString();
} }
/** /**