Solved Need some help with a logic function

Discussion in 'Plugin Development' started by bergerkiller, Nov 28, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    bergerkiller

    I am going to implement my own permission handling system in BKCommonLib to reduce code duplication and to automatically support PEX and Permissions 3.0. Not sure about GroupManager, I think that one uses super perms, if it doesn't, tell me.

    Anyhow, I allow the use of 'variable' permissions. These permission nodes can contain a given argument appended to the node 'root'. More info here:

    http://dev.bukkit.org/server-mods/bkcommonlib/pages/general-features-for-bkcommon-lib-plugins/

    I now have to write a generic function to handle all sorts of argument/* combinations. I only managed to write 1 and 2 arguments in the past, beyond that it becomes too 'bad code':

    Code:
        private boolean check(CommandSender sender, StringBuilder buffer, String... args) {
            buffer.setLength(0);
            buffer.append(this.node);
            for (String arg : args) {
                buffer.append('.').append(arg);
            }
            return CommonUtil.hasPermission(sender, buffer.toString());
        }
     
        public boolean has(CommandSender sender, String... args) {
            if (args.length == 0) {
                // No-argument version
                return CommonUtil.hasPermission(sender, this.node);
            } else {
                StringBuilder buffer = new StringBuilder();
                // Check all possible *-combinations
                // TODO: This needs to be made generic and not hardcoded per amount of arguments!
                // =========================================
                if (args.length == 1) {
                    return check(sender, buffer, args[0]) || check(sender, buffer, "*");
                } else if (args.length == 2) {
                    return check(sender, buffer, args[0], args[1]) || check(sender, buffer, args[0], "*")
                            || check(sender, buffer, "*", args[1]) || check(sender, buffer, "*", "*");
                } else {
                    throw new RuntimeException("No support for more than two permission arguments!");
                }
                // =========================================
            }
        }
    Anyone knows how to do this generic type of checking? So, switch all of the arguments into * and come up with all possible combinations? I have no idea how to do this, because a for loop is also hardcoded to the amount of arguments...

    Help greatly appreciated.
     
  2. First off: Why? What's wrong with Vault?
    Second: PEX is accessible through SuperPerms, too, like all modern permission plugins.
    Third: AFAIK Permissions 3 is death (doesn't even work with current CB builds) so why support it at all?

    Sorry that I couldn't answer your question, but atm I can't see a solution for your problem, too. :(

    BTW: This isn't meaned to be rude in any way, just some things that came in my mind while reading you post.
     
  3. Offline

    raGan.

    Maybe this would work
    Code:java
    1. has(CommandSender sender, String... args) {
    2. // all permissions from player.getEffectivePermissions() must be parsed to String[]
    3. List<String[]> parsedPerms = parsingMethod(player.getEffectivePermissions());
    4. List<int> toRemove = new ArrayList<int>(); // list of indexes to remove from perm list
    5. for (int i=0; i<agrs.length; i++) {
    6. toRemove.clear();
    7. // now to compare i-th argument in all perms
    8. for(int j=0; j < parsedPerms.size()) {
    9. if(i < parsedPerms.get(j).size()) {
    10. String currentArg = parsedPerms.get(j)[i];
    11. if(!currentArg.equals("*") && !currentArg.equalsIgnoreCase(args[i])) {
    12. // if agruments are not equal and player's one is not "*", it's wrong perm
    13. toRemove.add(j);
    14. }
    15. } else {
    16. if(!parsedPerms.get(parsedPerms.size() - 1).equals("*")) {
    17. // perm is shorter than needed, but
    18. // if last argument in player's permission is *, then it matches
    19. toRemove.add(j);
    20. }
    21. }
    22. }
    23. // now to remove not matching perms
    24. for(int k=(toRemove.size()-1); k >= 0; k--) {
    25. parsedPerms.remove(k);
    26. }
    27. }
    28. return !parsedPerms.isEmpty();
    29. }[/i][/i]
     
  4. Offline

    bergerkiller

    V10lator
    I know it's dead, but my poll over at MyWorlds told me otherwise. Unless everybody thought it was Bukkit permissions. Are you sure about Pex though? I had several tickets that requested support, so clearly it wasn't working...

    raGan.
    That is actually the 'handler' method, instead of using the internal hasPermission, you parse the permissions yourself. I know how to do that, but I am stuck with other permission handlers. As a result, I can not access those nodes.

    But I'll keep it in mind, maybe I can get all 'effective' permissions from the plugins too.
     
  5. Offline

    bergerkiller

    I decided to use Vault for permission support, so that is no longer open. Problem is, however, that Vault doesn't support the 'getActivePermissions' function that super perms does offer. This forces me to do the *-logic in the first post...

    Anyone knows an efficient way to do this?

    Ok no longer needed, I devised a nice recursive function to do it :D

    Code:
        private static boolean check(CommandSender sender, String root, String[] args, int argIndex) {
            if (argIndex >= args.length) {
                return CommonUtil.hasPermission(sender, root);
            } else {
                return check(sender, root + "." + args[argIndex], args, argIndex + 1) || check(sender, root + ".*", args, argIndex + 1);
            }
        }
    Test code:

    Code:
        public static void main(String[] args) {
            check("test.node", new String[] {"world1", "tool", "pickaxe"}, 0);
        }
     
        private static boolean check(String root, String[] args, int argIndex) {
            if (argIndex >= args.length) {
                System.out.println(root);
                return false;
            } else {
                return check(root + "." + args[argIndex], args, argIndex + 1) || check(root + ".*", args, argIndex + 1);
            }
        }
    Output:
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
Thread Status:
Not open for further replies.

Share This Page