mirror of
https://github.com/NoCheatPlus/NoCheatPlus.git
synced 2024-09-18 18:01:17 +02:00
Partly stitch up command protection.
- Never skip undoing changes. This is necessary to also process commands that are not registered or that are fall-back aliases. - Check adding all plugin commands always. - Check for aliases as well for decision if to match a command.
This commit is contained in:
parent
547dfc890b
commit
b8fd9da08f
@ -25,7 +25,6 @@ public class CommandUtil {
|
||||
* @return Returns null if not CraftBukkit or CommandMap not available.
|
||||
*/
|
||||
public static CommandMap getCommandMap() {
|
||||
// TODO: compat / null
|
||||
try {
|
||||
return NCPAPIProvider.getNoCheatPlusAPI().getMCAccess().getCommandMap();
|
||||
}
|
||||
@ -36,16 +35,20 @@ public class CommandUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Fails with an exception if SimpleCommandMap is not found, currently.
|
||||
* Get all Command instances, that NCP can get hold of. Attempt to get a SimpleCommandMap instance from the server to get the actually registered commands, but also get commands from JavaPlugin instances.
|
||||
* @return
|
||||
*/
|
||||
public static Collection<Command> getCommands() {
|
||||
CommandMap commandMap = getCommandMap();
|
||||
final Collection<Command> commands = new LinkedHashSet<Command>(500);
|
||||
|
||||
// All (?) commands from the SimpleCommandMap of the server, if available.
|
||||
final CommandMap commandMap = getCommandMap();
|
||||
if (commandMap != null && commandMap instanceof SimpleCommandMap) {
|
||||
return ((SimpleCommandMap) commandMap).getCommands();
|
||||
commands.addAll(((SimpleCommandMap) commandMap).getCommands());
|
||||
}
|
||||
else{
|
||||
final Collection<Command> commands = new LinkedHashSet<Command>(100);
|
||||
// TODO: Fall-back for Vanilla / CB commands? [Fall-back should be altering permission defaults, though negating permissions is the right way.]
|
||||
|
||||
// Fall-back: plugin commands.
|
||||
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
|
||||
if (plugin instanceof JavaPlugin) {
|
||||
final JavaPlugin javaPlugin = (JavaPlugin) plugin;
|
||||
@ -53,28 +56,31 @@ public class CommandUtil {
|
||||
if (map != null) {
|
||||
for (String label : map.keySet()) {
|
||||
Command command = javaPlugin.getCommand(label);
|
||||
if (command != null) commands.add(command);
|
||||
if (command != null) {
|
||||
commands.add(command);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO: Vanilla / CB commands !?
|
||||
}
|
||||
|
||||
return commands;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return The command label, if possible to find, or the alias itself (+ trim + lower-case).
|
||||
*/
|
||||
public static String getCommandLabel(final String alias, final boolean strict) {
|
||||
final Command command = getCommand(alias);
|
||||
if (command == null) {
|
||||
return strict ? null : alias.trim().toLowerCase();
|
||||
}
|
||||
else return command.getLabel().trim().toLowerCase();
|
||||
else {
|
||||
return command.getLabel().trim().toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -103,13 +109,17 @@ public class CommandUtil {
|
||||
final List<String> res = new ArrayList<String>();
|
||||
for (final CheckType checkType : CheckType.values()) {
|
||||
final String name = checkType.name();
|
||||
if (name.startsWith(ref)) res.add(name);
|
||||
if (name.startsWith(ref)) {
|
||||
res.add(name);
|
||||
}
|
||||
}
|
||||
if (ref.indexOf('_') == -1) {
|
||||
for (final CheckType checkType : CheckType.values()) {
|
||||
final String name = checkType.name();
|
||||
final String[] split = name.split("_", 2);
|
||||
if (split.length > 1 && split[1].startsWith(ref)) res.add(name);
|
||||
if (split.length > 1 && split[1].startsWith(ref)) {
|
||||
res.add(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!res.isEmpty()) {
|
||||
|
@ -47,13 +47,18 @@ public class PermissionUtil {
|
||||
}
|
||||
|
||||
public void restore() {
|
||||
Command registered = CommandUtil.getCommand(label);
|
||||
if (registered == null || registered != command) return;
|
||||
if (!label.equalsIgnoreCase(command.getLabel().trim().toLowerCase())) command.setLabel(label);
|
||||
// (Don't skip resetting, as there could be fall-back aliases.)
|
||||
// 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);
|
||||
if (perm != null && perm.getDefault() != permissionDefault) {
|
||||
perm.setDefault(permissionDefault);
|
||||
}
|
||||
}
|
||||
command.setPermissionMessage(permissionMessage);
|
||||
}
|
||||
@ -61,7 +66,7 @@ public class PermissionUtil {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param commands
|
||||
* @param commands Command white-list.
|
||||
* @param permissionBase
|
||||
* @param ops
|
||||
* @return
|
||||
@ -91,25 +96,28 @@ public class PermissionUtil {
|
||||
* @param permissionMessage
|
||||
* @return
|
||||
*/
|
||||
public static List<CommandProtectionEntry> protectCommands(String permissionBase, Collection<String> ignoredCommands, boolean invertIgnored, boolean ops, String permissionMessage){
|
||||
Set<String> checked = new HashSet<String>();
|
||||
public static List<CommandProtectionEntry> protectCommands(final String permissionBase, final Collection<String> ignoredCommands, final boolean invertIgnored, final boolean ops, final String permissionMessage) {
|
||||
final Set<String> checked = new HashSet<String>();
|
||||
for (String label : ignoredCommands) {
|
||||
checked.add(CommandUtil.getCommandLabel(label, false));
|
||||
}
|
||||
PluginManager pm = Bukkit.getPluginManager();
|
||||
final PluginManager pm = Bukkit.getPluginManager();
|
||||
Permission rootPerm = pm.getPermission(permissionBase);
|
||||
if (rootPerm == null) {
|
||||
rootPerm = new Permission(permissionBase);
|
||||
pm.addPermission(rootPerm);
|
||||
}
|
||||
List<CommandProtectionEntry> changed = new LinkedList<CommandProtectionEntry>();
|
||||
for (Command command : CommandUtil.getCommands()){
|
||||
String lcLabel = command.getLabel().trim().toLowerCase();
|
||||
if (checked != null){
|
||||
if (checked.contains(lcLabel)){
|
||||
if (!invertIgnored) continue;
|
||||
final List<CommandProtectionEntry> changed = new LinkedList<CommandProtectionEntry>();
|
||||
// Apply protection based on white-list or black-list.
|
||||
for (final Command command : CommandUtil.getCommands()) {
|
||||
final String lcLabel = command.getLabel().trim().toLowerCase();
|
||||
if (checked.contains(lcLabel) || containsAnyAliases(checked, command)) {
|
||||
if (!invertIgnored) {
|
||||
continue;
|
||||
}
|
||||
else if (invertIgnored) continue;
|
||||
}
|
||||
else if (invertIgnored) {
|
||||
continue;
|
||||
}
|
||||
// Set the permission for the command.
|
||||
String cmdPermName = command.getPermission();
|
||||
@ -133,8 +141,12 @@ public class PermissionUtil {
|
||||
}
|
||||
}
|
||||
// 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()));
|
||||
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(permissionMessage);
|
||||
@ -142,6 +154,24 @@ public class PermissionUtil {
|
||||
return changed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the checked set contains any trim+lower-case alias of the command.
|
||||
* @param checked
|
||||
* @param command
|
||||
* @return
|
||||
*/
|
||||
private static final boolean containsAnyAliases(final Set<String> checked, final Command command) {
|
||||
final Collection<String> aliases = command.getAliases();
|
||||
if (aliases != null) {
|
||||
for (final String alias : aliases) {
|
||||
if (checked.contains(alias.trim().toLowerCase())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a permission as child for all the other permissions given in a Collection.
|
||||
* @param permissions Not expected to exist.
|
||||
|
Loading…
Reference in New Issue
Block a user