mirror of
https://github.com/NoCheatPlus/NoCheatPlus.git
synced 2024-12-29 03:48:50 +01:00
Add convenience logging method. PErmisisonUtil was moved.
This commit is contained in:
parent
59d0ea2629
commit
9fc50e315b
@ -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<CommandProtectionEntry> protectCommands(Collection<String> 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<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();
|
||||
Permission rootPerm = pm.getPermission(permissionBase);
|
||||
if (rootPerm == null){
|
||||
rootPerm = new Permission(permissionBase);
|
||||
pm.addPermission(rootPerm);
|
||||
}
|
||||
SimpleCommandMap map = getCommandMap();
|
||||
List<CommandProtectionEntry> changed = new LinkedList<CommandProtectionEntry>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user