Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 23 additions & 2 deletions src/main/java/dev/espi/protectionstones/commands/ArgAddRemove.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,32 @@ 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 +158,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 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
* @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;
}

}