-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathPermissionManager.java
More file actions
511 lines (436 loc) · 19.1 KB
/
PermissionManager.java
File metadata and controls
511 lines (436 loc) · 19.1 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package dev.esophose.playerparticles.manager;
import dev.esophose.playerparticles.config.Settings;
import dev.esophose.playerparticles.particles.OtherPPlayer;
import dev.esophose.playerparticles.particles.PPlayer;
import dev.esophose.playerparticles.particles.ParticleEffect;
import dev.esophose.playerparticles.styles.ParticleStyle;
import dev.rosewood.rosegarden.RosePlugin;
import dev.rosewood.rosegarden.manager.Manager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permissible;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.PluginManager;
public class PermissionManager extends Manager {
private static final String PERMISSION_PREFIX = "playerparticles.";
private enum PPermission {
EFFECT("effect"),
STYLE("style"),
FIXED("fixed"),
FIXED_MAX("fixed.max"),
FIXED_UNLIMITED("fixed.unlimited"),
FIXED_CLEAR("fixed.clear"),
FIXED_TELEPORT("fixed.teleport"),
RELOAD("reload"),
OVERRIDE("override"),
RESET_OTHERS("reset.others"),
GUI("gui"),
PARTICLES_MAX("particles.max"),
PARTICLES_UNLIMITED("particles.unlimited"),
GROUPS_MAX("groups.max"),
GROUPS_UNLIMITED("groups.unlimited"),
WORLDGUARD_BYPASS("worldguard.bypass");
private final String permissionString;
PPermission(String permissionString) {
this.permissionString = permissionString;
}
/**
* Checks if a Permissible has a PlayerParticles permission
*
* @param p The Permissible
* @return True if the Player has permission
*/
public boolean check(Permissible p) {
if (p == null) return false;
String permission = PERMISSION_PREFIX + this.permissionString;
return p.hasPermission(permission);
}
/**
* Checks if a Permissible has a PlayerParticles permission with a sub-permission
*
* @param p The Permissible
* @param subPermission The sub-permission
* @return True if the Player has permission
*/
public boolean check(Permissible p, String subPermission) {
String permission = PERMISSION_PREFIX + this.permissionString + '.' + subPermission;
return p.hasPermission(permission);
}
@Override
public String toString() {
return PERMISSION_PREFIX + this.permissionString;
}
}
public PermissionManager(RosePlugin playerParticles) {
super(playerParticles);
playerParticles.getScheduler().runTaskLater(() -> {
try {
// Register plugin permissions to Bukkit
PluginManager pluginManager = Bukkit.getPluginManager();
Set<Permission> allPermissions = new HashSet<>();
// Effects
Map<String, Boolean> effectPermissions = new HashMap<>();
for (ParticleEffect effect : ParticleEffect.values()) {
if (!effect.isSupported())
continue;
Permission permission = new Permission("playerparticles.effect." + effect.getInternalName());
pluginManager.addPermission(permission);
effectPermissions.put(permission.getName(), true);
}
// Effects Wildcard
allPermissions.add(new Permission("playerparticles.effect.*", effectPermissions));
// Styles
Map<String, Boolean> stylePermissions = new HashMap<>();
for (ParticleStyle style : playerParticles.getManager(ParticleStyleManager.class).getStylesWithDisabled()) {
Permission permission = new Permission("playerparticles.style." + style.getInternalName());
pluginManager.addPermission(permission);
stylePermissions.put(permission.getName(), true);
}
// Styles Wildcard
allPermissions.add(new Permission("playerparticles.style.*", stylePermissions));
// Fixed
pluginManager.addPermission(new Permission("playerparticles.fixed"));
pluginManager.addPermission(new Permission("playerparticles.fixed.max"));
pluginManager.addPermission(new Permission("playerparticles.fixed.unlimited"));
pluginManager.addPermission(new Permission("playerparticles.fixed.clear"));
pluginManager.addPermission(new Permission("playerparticles.fixed.teleport"));
// Misc
pluginManager.addPermission(new Permission("playerparticles.reload"));
pluginManager.addPermission(new Permission("playerparticles.override"));
pluginManager.addPermission(new Permission("playerparticles.reset.others"));
pluginManager.addPermission(new Permission("playerparticles.gui"));
pluginManager.addPermission(new Permission("playerparticles.particles.max"));
pluginManager.addPermission(new Permission("playerparticles.particles.unlimited"));
pluginManager.addPermission(new Permission("playerparticles.groups.max"));
pluginManager.addPermission(new Permission("playerparticles.groups.unlimited"));
pluginManager.addPermission(new Permission("playerparticles.worldguard.bypass"));
// Register all non-child permissions
Map<String, Boolean> childPermissions = new HashMap<>();
for (Permission permission : allPermissions) {
pluginManager.addPermission(permission);
childPermissions.put(permission.getName(), true);
}
// Register all permissions as a child to the global plugin permission
pluginManager.addPermission(new Permission("playerparticles.*", childPermissions));
} catch (Exception e) {
playerParticles.getLogger().warning("Failed to register permissions dynamically. Did you load PlayerParticles through means other than a restart or reload?");
}
}, 2L);
}
@Override
public void reload() {
}
@Override
public void disable() {
}
/**
* Checks if the given player has the given permission
*
* @param pplayer The player to check
* @param permission The permission to check
* @return true if the player has the permission, otherwise false
*/
public boolean hasPermission(PPlayer pplayer, String permission) {
return pplayer.getUnderlyingExecutor().hasPermission(permission);
}
/**
* Checks if the given player has reached the max number of particles in their active group
*
* @param pplayer The player to check
* @return If the player has reached the max number of particles in their active group
*/
public boolean hasPlayerReachedMaxParticles(PPlayer pplayer) {
if (PPermission.PARTICLES_UNLIMITED.check(pplayer.getUnderlyingExecutor()))
return false;
PPlayer executor = this.getUnderlyingExecutorAsPPlayer(pplayer);
if (executor != pplayer)
return false;
return pplayer.getActiveParticles().size() >= this.getPermissionAmount(pplayer.getUnderlyingExecutor(), PPermission.PARTICLES_MAX, Settings.MAX_PARTICLES.get());
}
/**
* Checks if the given player has reached the max number of saved particle groups
*
* @param pplayer The player to check
* @return If the player has reached the max number of saved particle groups
*/
public boolean hasPlayerReachedMaxGroups(PPlayer pplayer) {
if (PPermission.GROUPS_UNLIMITED.check(pplayer.getUnderlyingExecutor()))
return false;
PPlayer executor = this.getUnderlyingExecutorAsPPlayer(pplayer);
if (executor != pplayer)
return false;
return executor.getParticleGroups().size() - 1 >= this.getPermissionAmount(pplayer.getUnderlyingExecutor(), PPermission.GROUPS_MAX, Settings.MAX_GROUPS.get());
}
/**
* Checks if the given player is able to save groups
*
* @param pplayer The player to check
* @return If the player has permission to save groups
*/
public boolean canPlayerSaveGroups(PPlayer pplayer) {
if (PPermission.GROUPS_UNLIMITED.check(pplayer.getUnderlyingExecutor()))
return true;
return this.getPermissionAmount(pplayer.getUnderlyingExecutor(), PPermission.GROUPS_MAX, Settings.MAX_GROUPS.get()) != 0;
}
/**
* Checks if the given player has reached the max number of fixed effects
*
* @param pplayer The player to check
* @return If the player has reached the max number of fixed effects
*/
public boolean hasPlayerReachedMaxFixedEffects(PPlayer pplayer) {
if (PPermission.FIXED_UNLIMITED.check(pplayer.getUnderlyingExecutor()))
return false;
PPlayer executor = this.getUnderlyingExecutorAsPPlayer(pplayer);
if (executor != pplayer)
return false;
return pplayer.getFixedEffectIds().size() >= this.getPermissionAmount(pplayer.getUnderlyingExecutor(), PPermission.FIXED_MAX, Settings.MAX_FIXED_EFFECTS.get());
}
/**
* Gets the max distance a fixed effect can be created from the player
*
* @return The max distance a fixed effect can be created from the player
*/
public int getMaxFixedEffectCreationDistance() {
return Settings.MAX_FIXED_EFFECT_CREATION_DISTANCE.get();
}
/**
* Gets the maximum number of particles a player is allowed to use
*
* @param pplayer The pplayer to check
* @return The maximum number of particles based on the config.yml value, or unlimited
*/
public int getMaxParticlesAllowed(PPlayer pplayer) {
if (PPermission.PARTICLES_UNLIMITED.check(pplayer.getUnderlyingExecutor()))
return Integer.MAX_VALUE;
PPlayer executor = this.getUnderlyingExecutorAsPPlayer(pplayer);
if (executor != pplayer)
return Integer.MAX_VALUE;
return this.getPermissionAmount(pplayer.getUnderlyingExecutor(), PPermission.PARTICLES_MAX, Settings.MAX_PARTICLES.get());
}
/**
* Checks if a world is enabled for particles to spawn in
*
* @param world The world name to check
* @return True if the world is disabled
*/
public boolean isWorldEnabled(String world) {
return !this.getDisabledWorlds().contains(world);
}
/**
* Gets all the worlds that are disabled
*
* @return All world names that are disabled
*/
public List<String> getDisabledWorlds() {
return Settings.DISABLED_WORLDS.get();
}
/**
* Checks if a player can reset another offline player's particles
*
* @param player The player to check the permission for
* @return True if the player has permission, otherwise false
*/
public boolean canResetOthers(PPlayer player) {
return PPermission.RESET_OTHERS.check(player.getUnderlyingExecutor());
}
/**
* Checks if a player has permission to use an effect
*
* @param player The player to check the permission for
* @param effect The effect to check
* @return True if the player has permission to use the effect
*/
public boolean hasEffectPermission(PPlayer player, ParticleEffect effect) {
return PPermission.EFFECT.check(player.getUnderlyingExecutor(), effect.getInternalName());
}
/**
* Checks if a player has permission to use a style
* Always returns true for 'normal', a player needs at least one style to apply particles
*
* @param player The player to check the permission for
* @param style The style to check
* @return If the player has permission to use the style
*/
public boolean hasStylePermission(PPlayer player, ParticleStyle style) {
return PPermission.STYLE.check(player.getUnderlyingExecutor(), style.getInternalName());
}
/**
* Gets a String List of all effect names a player has permission for
*
* @param p The player to get effect names for
* @return A String List of all effect names the given player has permission for
*/
public List<String> getEffectNamesUserHasPermissionFor(PPlayer p) {
List<String> list = new ArrayList<>();
for (ParticleEffect pe : ParticleEffect.getEnabledEffects())
if (this.hasEffectPermission(p, pe))
list.add(pe.getName());
return list;
}
/**
* Gets a String List of all style names a player has permission for
*
* @param p The player to get style names for
* @return A String List of all style names the given player has permission for
*/
public List<String> getStyleNamesUserHasPermissionFor(PPlayer p) {
List<String> list = new ArrayList<>();
for (ParticleStyle ps : this.rosePlugin.getManager(ParticleStyleManager.class).getStyles())
if (this.hasStylePermission(p, ps))
list.add(ps.getName());
return list;
}
/**
* Gets a String List of all fixable style names a player has permission for
*
* @param p The player to get style names for
* @return A String List of all fixable style names the given player has permission for
*/
public List<String> getFixableStyleNamesUserHasPermissionFor(PPlayer p) {
List<String> list = new ArrayList<>();
for (ParticleStyle ps : this.rosePlugin.getManager(ParticleStyleManager.class).getStyles())
if (ps.canBeFixed() && this.hasStylePermission(p, ps))
list.add(ps.getName());
return list;
}
/**
* Gets a List of all effects a player has permission for
*
* @param p The player to get effects for
* @return A List of all effects the given player has permission for
*/
public List<ParticleEffect> getEffectsUserHasPermissionFor(PPlayer p) {
List<ParticleEffect> list = new ArrayList<>();
for (ParticleEffect pe : ParticleEffect.getEnabledEffects())
if (this.hasEffectPermission(p, pe))
list.add(pe);
return list;
}
/**
* Gets a List of all styles a player has permission for
*
* @param p The player to get styles for
* @return A List of all styles the given player has permission for
*/
public List<ParticleStyle> getStylesUserHasPermissionFor(PPlayer p) {
List<ParticleStyle> list = new ArrayList<>();
for (ParticleStyle ps : this.rosePlugin.getManager(ParticleStyleManager.class).getStyles())
if (this.hasStylePermission(p, ps))
list.add(ps);
return list;
}
/**
* Checks if a player has permission to created fixed effects
*
* @param player The player to check the permission for
* @return True if the player has permission
*/
public boolean canUseFixedEffects(PPlayer player) {
return PPermission.FIXED.check(player.getUnderlyingExecutor());
}
/**
* Checks if a player has permission to clear fixed effects
*
* @param player The player to check the permission for
* @return True if the player has permission to use /pp fixed clear
*/
public boolean canClearFixedEffects(PPlayer player) {
return PPermission.FIXED_CLEAR.check(player.getUnderlyingExecutor());
}
/**
* Checks if a player has permission to teleport to fixed effects
*
* @param player The player to check the permission for
* @return True if the player has permission to use /pp fixed teleport
*/
public boolean canTeleportToFixedEffects(PPlayer player) {
return PPermission.FIXED_TELEPORT.check(player.getUnderlyingExecutor());
}
/**
* Checks if a player has permission to open the GUI
*
* @param player The player to check the permission for
* @return True if the player has permission to open the GUI
*/
public boolean canOpenGui(PPlayer player) {
return !Settings.GUI_REQUIRE_PERMISSION.get() || PPermission.GUI.check(player.getUnderlyingExecutor());
}
/**
* Checks if a player has permission to open a specific GUI
*
* @param player The player to check the permission for
* @param gui The gui name
* @return True if the player has permission to open the GUI
*/
public boolean canOpenGui(PPlayer player, String gui) {
return !Settings.GUI_REQUIRE_PERMISSION.get() || PPermission.GUI.check(player.getUnderlyingExecutor(), gui);
}
/**
* Checks if a player has permission to use /pp reload
*
* @param sender The sender to check the permission for
* @return True if the sender has permission to reload the plugin's settings
*/
public boolean canReloadPlugin(CommandSender sender) {
return PPermission.RELOAD.check(sender);
}
/**
* Checks if a player can use /ppo
*
* @param sender The CommandSender to check
* @return If the sender can use /ppo
*/
public boolean canOverride(CommandSender sender) {
if (this.isConsole(sender))
return true;
return PPermission.OVERRIDE.check(sender);
}
/**
* Checks if a player has the WorldGuard bypass permission
*
* @param player The Player to check
* @return If the player has the WorldGuard bypass permission
*/
public boolean hasWorldGuardBypass(Player player) {
return PPermission.WORLDGUARD_BYPASS.check(player);
}
private boolean isConsole(PPlayer pplayer) {
return this.isConsole(pplayer.getUnderlyingExecutor());
}
private boolean isConsole(CommandSender sender) {
return sender instanceof ConsoleCommandSender;
}
private PPlayer getUnderlyingExecutorAsPPlayer(PPlayer pplayer) {
if (pplayer instanceof OtherPPlayer) {
OtherPPlayer other = (OtherPPlayer) pplayer;
CommandSender executor = other.getUnderlyingExecutor();
if (this.isConsole(executor))
return null;
Player executingPlayer = (Player) executor;
return this.rosePlugin.getManager(DataManager.class).getPPlayer(executingPlayer.getUniqueId());
}
return pplayer;
}
private int getPermissionAmount(Permissible permissible, PPermission permission, int lowerBound) {
int amount = lowerBound;
for (PermissionAttachmentInfo info : permissible.getEffectivePermissions()) {
String target = info.getPermission().toLowerCase();
if (target.startsWith(permission.toString()) && info.getValue()) {
try {
amount = Math.max(amount, Integer.parseInt(target.substring(target.lastIndexOf('.') + 1)));
} catch (NumberFormatException ignored) { }
}
}
return amount;
}
}