-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathLimitUtil.java
More file actions
218 lines (189 loc) · 9.64 KB
/
Copy pathLimitUtil.java
File metadata and controls
218 lines (189 loc) · 9.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package dev.espi.protectionstones.utils;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
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 {
// warning: group regions should be split into merged regions first
public static String checkAddOwner(PSPlayer psp, List<PSProtectBlock> blocksAdded) {
HashMap<PSProtectBlock, Integer> regionLimits = psp.getRegionLimits();
int maxPS = psp.getGlobalRegionLimits();
ProtectionStones.getInstance().debug(String.format("Player's global limit is %d.", maxPS));
ProtectionStones.getInstance().debug(String.format("Player has limits on %d region types.", regionLimits.size()));
if (maxPS != -1 || !regionLimits.isEmpty()) { // only check if limit was found
// count player's protection blocks
int total = 0;
HashMap<PSProtectBlock, Integer> playerRegionCounts = getOwnedRegionTypeCounts(psp);
// add the blocks
for (PSProtectBlock b : blocksAdded) {
ProtectionStones.getInstance().debug(String.format("Adding region type %s.", b.alias));
if (playerRegionCounts.containsKey(b)) {
playerRegionCounts.put(b, playerRegionCounts.get(b)+1);
} else {
playerRegionCounts.put(b, 1);
}
}
// check each limit
for (PSProtectBlock type : playerRegionCounts.keySet()) {
if (regionLimits.containsKey(type)) {
ProtectionStones.getInstance().debug(String.format("Of type %s: player will have %d regions - Player's limit is %d regions.", type.alias, playerRegionCounts.get(type), regionLimits.get(type)));
if (playerRegionCounts.get(type) > regionLimits.get(type)) {
return PSL.ADDREMOVE_PLAYER_REACHED_LIMIT.msg();
}
}
total += playerRegionCounts.get(type);
}
// check if player has passed region limit
ProtectionStones.getInstance().debug(String.format("The player will have %d regions in total. Their limit is %d.", total, maxPS));
if (total > maxPS && maxPS != -1) {
return PSL.ADDREMOVE_PLAYER_REACHED_LIMIT.msg();
}
}
return "";
}
public static boolean check(Player p, PSProtectBlock b) {
if (!p.hasPermission("protectionstones.admin")) {
// check if player has limit on protection stones
String msg = LimitUtil.hasPlayerPassedRegionLimit(PSPlayer.fromPlayer(p), b);
if (!msg.isEmpty()) {
PSL.msg(p, msg);
return false;
}
}
return true;
}
public static boolean hasPassedOrEqualsRentLimit(Player p) {
int lim = MiscUtil.getPermissionNumber(p, "protectionstones.rent.limit.", -1);
if (lim != -1) {
int total = 0;
// find total number of rented regions
HashMap<World, RegionManager> m = WGUtils.getAllRegionManagers();
for (World w : m.keySet()) {
RegionManager rgm = m.get(w);
for (ProtectedRegion r : rgm.getRegions().values()) {
if (ProtectionStones.isPSRegion(r) && r.getOwners().contains(WorldGuardPlugin.inst().wrapPlayer(p))) {
PSRegion psr = PSRegion.fromWGRegion(p.getWorld(), r);
if (psr != null && psr.getTenant() != null && psr.getTenant().equals(p.getUniqueId())) total++;
}
}
}
return total >= lim;
}
return false;
}
/**
* Returns the region counts of a player (for all worlds).
* @param psp player
* @return map of region types to the counts
*/
private static HashMap<PSProtectBlock, Integer> getOwnedRegionTypeCounts(PSPlayer psp) {
if (ProtectionStones.getInstance().isDebug()) { // psp.getName may incur a performance penalty
ProtectionStones.getInstance().debug(String.format("Debug limits for: %s", psp.getName()));
}
HashMap<PSProtectBlock, Integer> counts = new HashMap<>();
HashMap<World, RegionManager> m = WGUtils.getAllRegionManagers();
for (World w : m.keySet()) {
psp.getPSRegions(w, false).forEach(r -> {
if (r instanceof PSGroupRegion) {
ProtectionStones.getInstance().debug(String.format("Checking group region %s's (world %s) (type %s) regions:", r.getId(), w.getName(), r.getTypeOptions().alias));
for (PSMergedRegion psmr : ((PSGroupRegion) r).getMergedRegions()) {
if (psmr.getTypeOptions() == null) continue;
if (!counts.containsKey(psmr.getTypeOptions())) {
counts.put(psmr.getTypeOptions(), 1);
} else {
counts.put(psmr.getTypeOptions(), counts.get(psmr.getTypeOptions())+1);
}
ProtectionStones.getInstance().debug(String.format("Merged region %s (world %s) (type %s)", psmr.getId(), w.getName(), psmr.getTypeOptions().alias));
}
} else {
if (r.getTypeOptions() == null) return;
if (!counts.containsKey(r.getTypeOptions())) {
counts.put(r.getTypeOptions(), 1);
} else {
counts.put(r.getTypeOptions(), counts.get(r.getTypeOptions())+1);
}
ProtectionStones.getInstance().debug(String.format("Region %s (world %s) (type %s)", r.getId(), w.getName(), r.getTypeOptions().alias));
}
});
}
return counts;
}
private static String hasPlayerPassedRegionLimit(PSPlayer psp, PSProtectBlock b) {
HashMap<PSProtectBlock, Integer> regionLimits = psp.getRegionLimits();
int maxPS = psp.getGlobalRegionLimits();
if (maxPS != -1 || !regionLimits.isEmpty()) { // only check if limit was found
// count player's protection stones
int total = 0, bFound = 0;
HashMap<PSProtectBlock, Integer> playerRegionCounts = getOwnedRegionTypeCounts(psp);
for (PSProtectBlock type : playerRegionCounts.keySet()) {
ProtectionStones.getInstance().debug(String.format("Adding region type %s.", b.alias));
if (type.equals(b)) {
bFound = playerRegionCounts.get(type);
}
total += playerRegionCounts.get(type);
}
// check if player has passed region limit
ProtectionStones.getInstance().debug(String.format("The player will have %d regions in total. Their limit is %d.", total, maxPS));
if (total >= maxPS && maxPS != -1) {
return PSL.REACHED_REGION_LIMIT.msg().replace("%limit%", ""+maxPS);
}
// check if player has passed per block limit
ProtectionStones.getInstance().debug(String.format("Of type %s: player will have %d regions - Player's limit is %d regions.", b.alias, bFound, regionLimits.get(b) == null ? -1 : regionLimits.get(b)));
if (regionLimits.get(b) != null && bFound >= regionLimits.get(b)) {
return PSL.REACHED_PER_BLOCK_REGION_LIMIT.msg().replace("%limit%", ""+regionLimits.get(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;
}
}