From c2f4a0dcb7e52c556bff1266a1a2c0b168b16670 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Galajus=20Kwiatkowski?= Date: Fri, 21 Jun 2024 11:46:52 +0200 Subject: [PATCH 1/2] limit region owners and members by new semi generic perm: protectionstones.members-limit. default 0 protectionstones.owners-limit. default 1 perm is necessary to add any member/owner --- .../java/dev/espi/protectionstones/PSL.java | 2 ++ .../commands/ArgAddRemove.java | 25 +++++++++++-- .../protectionstones/utils/LimitUtil.java | 35 +++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/main/java/dev/espi/protectionstones/PSL.java b/src/main/java/dev/espi/protectionstones/PSL.java index 534d11f..afecfdf 100644 --- a/src/main/java/dev/espi/protectionstones/PSL.java +++ b/src/main/java/dev/espi/protectionstones/PSL.java @@ -90,6 +90,8 @@ public enum PSL { REMOVED_FROM_REGION("psregion.removed_from_region", ChatColor.AQUA + "%player%" + ChatColor.GRAY + " has been removed from region."), REMOVED_FROM_REGION_SPECIFIC("psregion.removed_from_region_specific", ChatColor.AQUA + "%player%" + ChatColor.GRAY + " has been removed from region %region%."), NOT_IN_REGION("psregion.not_in_region", ChatColor.RED + "You are not in a protection stones region!"), + MEMBERS_LIMIT("psregion.members_limit_reached", ChatColor.RED + "Members limit reached (%limit%) for region: %region%"), + OWNERS_LIMIT("psregion.owners_limit_reached", ChatColor.RED + "Owners limit reached (%limit%) for region: %region%"), PLAYER_NOT_FOUND("psregion.player_not_found", ChatColor.RED + "Player not found."), NOT_PS_REGION("psregion.not_ps_region", ChatColor.RED + "Not a protection stones region."), REGION_DOES_NOT_EXIST("psregion.region_does_not_exist", ChatColor.RED + "Region does not exist."), diff --git a/src/main/java/dev/espi/protectionstones/commands/ArgAddRemove.java b/src/main/java/dev/espi/protectionstones/commands/ArgAddRemove.java index b6f167e..d5aea63 100644 --- a/src/main/java/dev/espi/protectionstones/commands/ArgAddRemove.java +++ b/src/main/java/dev/espi/protectionstones/commands/ArgAddRemove.java @@ -119,11 +119,32 @@ public boolean executeArgument(CommandSender s, String[] args, HashMap atm if more than one owner + // then, if a second owner has perm with more members/owners, he can add them - possible abuse? + int membersLimit = LimitUtil.getPermissionIntVariable(p, "protectionstones.members-limit.", 2, 0); + int ownersLimit = LimitUtil.getPermissionIntVariable(p, "protectionstones.owners-limit.", 2, 1); + + if (operationType.equals("add") && r.getMembers().size() >= membersLimit) { + PSL.msg(p, PSL.MEMBERS_LIMIT.msg() + .replace("%limit%", String.valueOf(membersLimit)) + .replace("%region%", regionName)); + continue; + } + if (operationType.equals("addowner") && r.getOwners().size() >= ownersLimit) { + PSL.msg(p, PSL.OWNERS_LIMIT.msg() + + .replace("%limit%", String.valueOf(ownersLimit)) + .replace("%region%", regionName)); + continue; + } + if (flags.containsKey("-a")) { PSL.msg(p, PSL.ADDED_TO_REGION_SPECIFIC.msg() .replace("%player%", addPlayerName) - .replace("%region%", r.getName() == null ? r.getId() : r.getName() + " (" + r.getId() + ")")); + .replace("%region%", regionName)); } else { PSL.msg(p, PSL.ADDED_TO_REGION.msg().replace("%player%", addPlayerName)); } @@ -137,7 +158,7 @@ public boolean executeArgument(CommandSender s, String[] args, HashMap perm = player.getEffectivePermissions().stream() + .filter(p -> p.getPermission().startsWith(searchingPerm)).findFirst(); + if (perm.isEmpty()) { + return defaultValue; + } + //getting var perm + String[] split = perm.get().getPermission().split("\\."); + if (split.length < varPos + 1) { + return defaultValue; + } + String limitString = split[varPos]; + + if (NumberUtils.isNumber(limitString)) { + return Integer.parseInt(limitString); + } + return defaultValue; + } + } From a9c9cc546b29a4df0439f40d11ecc6facd2cd287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Galajus=20Kwiatkowski?= Date: Fri, 21 Jun 2024 11:48:55 +0200 Subject: [PATCH 2/2] fix docs --- src/main/java/dev/espi/protectionstones/utils/LimitUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/dev/espi/protectionstones/utils/LimitUtil.java b/src/main/java/dev/espi/protectionstones/utils/LimitUtil.java index a96da33..9877f28 100644 --- a/src/main/java/dev/espi/protectionstones/utils/LimitUtil.java +++ b/src/main/java/dev/espi/protectionstones/utils/LimitUtil.java @@ -184,7 +184,7 @@ private static String hasPlayerPassedRegionLimit(PSPlayer psp, PSProtectBlock b) } /** - * Getting number variable on end of perm + * Getting number variable from permission * @param player Bukkit player * @param searchingPerm permission without number variable on end like "protectionstones.owners-limit." * @param varPos position of variable in permission