Skip to content

Commit

Permalink
limit region owners and members by new semi generic perm:
Browse files Browse the repository at this point in the history
protectionstones.members-limit.<integer> default 0
protectionstones.owners-limit.<integer> default 1

perm is necessary to add any member/owner espidev#383
  • Loading branch information
Galajus committed Jun 21, 2024
1 parent 66b6b3e commit ad42c21
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/main/java/dev/espi/protectionstones/PSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -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."),
Expand Down
39 changes: 35 additions & 4 deletions src/main/java/dev/espi/protectionstones/commands/ArgAddRemove.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
package dev.espi.protectionstones.commands;

import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import dev.espi.protectionstones.*;
import dev.espi.protectionstones.PSGroupRegion;
import dev.espi.protectionstones.PSL;
import dev.espi.protectionstones.PSPlayer;
import dev.espi.protectionstones.PSRegion;
import dev.espi.protectionstones.ProtectionStones;
import dev.espi.protectionstones.utils.LimitUtil;
import dev.espi.protectionstones.utils.UUIDCache;
import dev.espi.protectionstones.utils.WGUtils;
Expand All @@ -25,7 +29,13 @@
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -119,12 +129,33 @@ public boolean executeArgument(CommandSender s, String[] args, HashMap<String, S
// apply operation to regions
for (PSRegion r : regions) {

String regionName = r.getName() == null ? r.getId() : r.getName() + " (" + r.getId() + ")";
if (operationType.equals("add") || operationType.equals("addowner")) {

// check limit members and owners for a region -> 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));
}

Expand All @@ -137,7 +168,7 @@ public boolean executeArgument(CommandSender s, String[] args, HashMap<String, S
if (flags.containsKey("-a")) {
PSL.msg(p, PSL.REMOVED_FROM_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.REMOVED_FROM_REGION.msg().replace("%player%", addPlayerName));
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/dev/espi/protectionstones/utils/LimitUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import dev.espi.protectionstones.*;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachmentInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;

public class LimitUtil {

Expand Down Expand Up @@ -180,4 +183,36 @@ private static String hasPlayerPassedRegionLimit(PSPlayer psp, PSProtectBlock b)
return "";
}

/**
* Getting number variable on end of perm
* @param player Bukkit player
* @param searchingPerm permission without number variable on end like "protectionstones.owners-limit."
* @param varPos position of variable in permission
* @param defaultValue default return value if perm not exist
* @return perm int variable
*/
public static int getPermissionIntVariable(Player player, String searchingPerm, int varPos, int defaultValue) {
//admin check
if (player.hasPermission("protectionstones.admin") || player.isOp()) {
return 9999;
}
//getting perm to check
Optional<PermissionAttachmentInfo> 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;
}

}

0 comments on commit ad42c21

Please sign in to comment.