1.15.2 Permission Arguments Checker

Discussion in 'Spigot Plugin Development' started by DarkKnights22, Mar 7, 2020.

  1. How would I do permission-based stuff like party.limit.X would limit party members to X for that player?

    E.g: party.limit.3 would limit the party members for the player DarkKnights22 to 3 members.
     
  2. Maybe something like this:
    Code (Java):
    for (PermissionAttachmentInfo effectivePermission : Bukkit.getPlayer("notch").getEffectivePermissions()) {
        String permission = effectivePermission.getPermission();

        if (permission.startsWith("party.limit.")) {
            int length = "party.limit.".length();
         
            if (permission.length() > length) {
                try {
                    int members = Integer.parseInt(permission.substring(length + 1));

                    /* Do the logic... */
                } catch (NumberFormatException exception) {
                    /* Handle the exception... */
                }
            }

            break;
        }
    }
     
    #2 patri9ck, Mar 7, 2020
    Last edited: Mar 8, 2020
  3. Small nitpick, but you have an extra e in there.

    Also wouldn't indexOf("party.limit.") return a value of 0? Think you want lastIndexOf(".") instead.
     
    • Agree Agree x 1
  4. Thanks, I fixed it.
     
  5. I got this:

    java.lang.NumberFormatException: For input string: ""

    On this line: if (permission.length() > length)
     
  6. That is not possible. You get the exception on this line:
    Code (Java):
    int members = Integer.parseInt(permission.substring(length + 1));
    That is because the passed String object is not an integer. You can for example send a message to the console that the String set in the configuration is not valid.
     
  7. Woops, yeh that line. And it is a number, I set it myself.
     
  8. Print permission and permission.substring(length + 1) to see whether its correct.
     
  9. Empty
     
  10. Both? Are you sure you added the permission to the player?
     
  11. Strahan

    Benefactor

    I had to do this once and searched around the forum. I used this code and saved it in my Bukkit notes. Alas, I did not save the author with which to credit though, but here it is:
    Code (Text):
    public int getMax(String permissionRoot, Permissible permissible) {
      if (permissible.isOp()) return -1;

      final AtomicInteger max = new AtomicInteger();
      permissible.getEffectivePermissions().stream().map(PermissionAttachmentInfo::getPermission).map(String::toLowerCase).filter(value -> value.startsWith(permissionRoot)).map(value -> value.replace(permissionRoot, "")).forEach(value -> {
        if (value.equalsIgnoreCase("*")) {
          max.set(-1);
          return;
        }

        if (max.get() == -1) return;

        try {
          int amount = Integer.parseInt(value);
          if (amount > max.get()) max.set(amount);
        } catch (NumberFormatException ignored) {
        }
      });

      return max.get();
    }
    Pass the full path of the permission root, including last period to the function. So to get 5 from myplugin.warps.5, do getMax("myplugin.warps.", object).
     
    • Winner Winner x 1
  12. Another options would be to modify the idea: so that the permission gives u the right to have or create a party with x players.
    If you want to check if a player can join the party you just have to check if the owner has the permission party.limit.(partySize + 1) if he hasnt you can deny the join. The counter part is every player needs X permissions but if they only should have a party of 1-6 its fine i guess. Another pro: u can disable parties of specific sizes.

    Second atempt: try to implement your feaature without plugins and safe your max. party sizes in a .yaml file.