mirror of
https://github.com/PaperMC/Paper.git
synced 2025-08-15 20:23:53 -07:00
Made Permissions mutable, added methods to view which Permissibles are subscribed to which Permission
By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import java.util.Set;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.Event.Priority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.permissions.Permissible;
|
||||
import org.bukkit.permissions.Permission;
|
||||
|
||||
/**
|
||||
@@ -152,10 +153,93 @@ public interface PluginManager {
|
||||
*/
|
||||
public void addPermission(Permission perm);
|
||||
|
||||
/**
|
||||
* Removes a {@link Permission} registration from this plugin manager.
|
||||
*
|
||||
* If the specified permission does not exist in this plugin manager, nothing will happen.
|
||||
*
|
||||
* Removing a permission registration will <b>not</b> remove the permission from any {@link Permissible}s that have it.
|
||||
*
|
||||
* @param perm Permission to remove
|
||||
*/
|
||||
public void removePermission(Permission perm);
|
||||
|
||||
/**
|
||||
* Removes a {@link Permission} registration from this plugin manager.
|
||||
*
|
||||
* If the specified permission does not exist in this plugin manager, nothing will happen.
|
||||
*
|
||||
* Removing a permission registration will <b>not</b> remove the permission from any {@link Permissible}s that have it.
|
||||
*
|
||||
* @param name Permission to remove
|
||||
*/
|
||||
public void removePermission(String name);
|
||||
|
||||
/**
|
||||
* Gets the default permissions for the given op status
|
||||
*
|
||||
* @param op Which set of default permissions to get
|
||||
*/
|
||||
public Set<Permission> getDefaultPermissions(boolean op);
|
||||
|
||||
/**
|
||||
* Recalculates the defaults for the given {@link Permission}.
|
||||
*
|
||||
* This will have no effect if the specified permission is not registered here.
|
||||
*
|
||||
* @param perm Permission to recalculate
|
||||
*/
|
||||
public void recalculatePermissionDefaults(Permission perm);
|
||||
|
||||
/**
|
||||
* Subscribes the given Permissible for information about the requested Permission, by name.
|
||||
*
|
||||
* If the specified Permission changes in any form, the Permissible will be asked to recalculate.
|
||||
*
|
||||
* @param permission Permission to subscribe to
|
||||
* @param permissible Permissible subscribing
|
||||
*/
|
||||
public void subscribeToPermission(String permission, Permissible permissible);
|
||||
|
||||
/**
|
||||
* Unsubscribes the given Permissible for information about the requested Permission, by name.
|
||||
*
|
||||
* @param permission Permission to unsubscribe from
|
||||
* @param permissible Permissible subscribing
|
||||
*/
|
||||
public void unsubscribeFromPermission(String permission, Permissible permissible);
|
||||
|
||||
/**
|
||||
* Gets a set containing all subscribed {@link Permissible}s to the given permission, by name
|
||||
*
|
||||
* @param permission Permission to query for
|
||||
* @return Set containing all subscribed permissions
|
||||
*/
|
||||
public Set<Permissible> getPermissionSubscriptions(String permission);
|
||||
|
||||
/**
|
||||
* Subscribes to the given Default permissions by operator status
|
||||
*
|
||||
* If the specified defaults change in any form, the Permissible will be asked to recalculate.
|
||||
*
|
||||
* @param op Default list to subscribe to
|
||||
* @param permissible Permissible subscribing
|
||||
*/
|
||||
public void subscribeToDefaultPerms(boolean op, Permissible permissible);
|
||||
|
||||
/**
|
||||
* Unsubscribes from the given Default permissions by operator status
|
||||
*
|
||||
* @param op Default list to unsubscribe from
|
||||
* @param permissible Permissible subscribing
|
||||
*/
|
||||
public void unsubscribeFromDefaultPerms(boolean op, Permissible permissible);
|
||||
|
||||
/**
|
||||
* Gets a set containing all subscribed {@link Permissible}s to the given default list, by op status
|
||||
*
|
||||
* @param op Default list to query for
|
||||
* @return Set containing all subscribed permissions
|
||||
*/
|
||||
public Set<Permissible> getDefaultPermSubscriptions(boolean op);
|
||||
}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package org.bukkit.plugin;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.MapMaker;
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.ArrayList;
|
||||
@@ -27,7 +29,9 @@ import org.bukkit.command.SimpleCommandMap;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.Event.Priority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.permissions.Permissible;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.bukkit.permissions.PermissionDefault;
|
||||
|
||||
import org.bukkit.util.FileUtil;
|
||||
|
||||
@@ -44,6 +48,8 @@ public final class SimplePluginManager implements PluginManager {
|
||||
private final SimpleCommandMap commandMap;
|
||||
private final Map<String, Permission> permissions = new HashMap<String, Permission>();
|
||||
private final Map<Boolean, Set<Permission>> defaultPerms = new LinkedHashMap<Boolean, Set<Permission>>();
|
||||
private final Map<String, Map<Permissible, Boolean>> permSubs = new HashMap<String, Map<Permissible, Boolean>>();
|
||||
private final Map<Boolean, Map<Permissible, Boolean>> defSubs = new HashMap<Boolean, Map<Permissible, Boolean>>();
|
||||
private final Comparator<RegisteredListener> comparer = new Comparator<RegisteredListener>() {
|
||||
public int compare(RegisteredListener i, RegisteredListener j) {
|
||||
int result = i.getPriority().compareTo(j.getPriority());
|
||||
@@ -418,24 +424,117 @@ public final class SimplePluginManager implements PluginManager {
|
||||
}
|
||||
|
||||
permissions.put(name, perm);
|
||||
calculatePermissionDefault(perm);
|
||||
}
|
||||
|
||||
public Set<Permission> getDefaultPermissions(boolean op) {
|
||||
return ImmutableSet.copyOf(defaultPerms.get(op));
|
||||
}
|
||||
|
||||
public void removePermission(Permission perm) {
|
||||
removePermission(perm.getName().toLowerCase());
|
||||
}
|
||||
|
||||
public void removePermission(String name) {
|
||||
permissions.remove(name);
|
||||
}
|
||||
|
||||
public void recalculatePermissionDefaults(Permission perm) {
|
||||
if (permissions.containsValue(perm)) {
|
||||
defaultPerms.get(true).remove(perm);
|
||||
defaultPerms.get(false).remove(perm);
|
||||
|
||||
calculatePermissionDefault(perm);
|
||||
}
|
||||
}
|
||||
|
||||
private void calculatePermissionDefault(Permission perm) {
|
||||
if (!perm.getChildren().isEmpty()) {
|
||||
switch (perm.getDefault()) {
|
||||
case TRUE:
|
||||
defaultPerms.get(true).add(perm);
|
||||
defaultPerms.get(false).add(perm);
|
||||
break;
|
||||
case OP:
|
||||
defaultPerms.get(true).add(perm);
|
||||
break;
|
||||
case NOT_OP:
|
||||
defaultPerms.get(false).add(perm);
|
||||
break;
|
||||
if ((perm.getDefault() == PermissionDefault.OP) || (perm.getDefault() == PermissionDefault.TRUE)) {
|
||||
defaultPerms.get(true).add(perm);
|
||||
dirtyPermissibles(true);
|
||||
}
|
||||
if ((perm.getDefault() == PermissionDefault.NOT_OP) || (perm.getDefault() == PermissionDefault.TRUE)) {
|
||||
defaultPerms.get(false).add(perm);
|
||||
dirtyPermissibles(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Set<Permission> getDefaultPermissions(boolean op) {
|
||||
return defaultPerms.get(op);
|
||||
private void dirtyPermissibles(boolean op) {
|
||||
Set<Permissible> permissibles = getDefaultPermSubscriptions(op);
|
||||
|
||||
for (Permissible p : permissibles) {
|
||||
p.recalculatePermissions();
|
||||
}
|
||||
}
|
||||
|
||||
public void subscribeToPermission(String permission, Permissible permissible) {
|
||||
String name = permission.toLowerCase();
|
||||
Map<Permissible, Boolean> map = permSubs.get(name);
|
||||
|
||||
if (map == null) {
|
||||
map = new MapMaker().weakKeys().makeMap();
|
||||
permSubs.put(name, map);
|
||||
}
|
||||
|
||||
map.put(permissible, true);
|
||||
}
|
||||
|
||||
public void unsubscribeFromPermission(String permission, Permissible permissible) {
|
||||
String name = permission.toLowerCase();
|
||||
Map<Permissible, Boolean> map = permSubs.get(name);
|
||||
|
||||
if (map != null) {
|
||||
map.remove(permissible);
|
||||
|
||||
if (map.isEmpty()) {
|
||||
permSubs.remove(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Set<Permissible> getPermissionSubscriptions(String permission) {
|
||||
String name = permission.toLowerCase();
|
||||
Map<Permissible, Boolean> map = permSubs.get(name);
|
||||
|
||||
if (map == null) {
|
||||
return ImmutableSet.of();
|
||||
} else {
|
||||
return ImmutableSet.copyOf(map.keySet());
|
||||
}
|
||||
}
|
||||
|
||||
public void subscribeToDefaultPerms(boolean op, Permissible permissible) {
|
||||
Map<Permissible, Boolean> map = defSubs.get(op);
|
||||
|
||||
if (map == null) {
|
||||
map = new MapMaker().weakKeys().makeMap();
|
||||
defSubs.put(op, map);
|
||||
}
|
||||
|
||||
map.put(permissible, true);
|
||||
}
|
||||
|
||||
public void unsubscribeFromDefaultPerms(boolean op, Permissible permissible) {
|
||||
Map<Permissible, Boolean> map = defSubs.get(op);
|
||||
|
||||
if (map != null) {
|
||||
map.remove(permissible);
|
||||
|
||||
if (map.isEmpty()) {
|
||||
defSubs.remove(op);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Set<Permissible> getDefaultPermSubscriptions(boolean op) {
|
||||
Map<Permissible, Boolean> map = defSubs.get(op);
|
||||
|
||||
if (map == null) {
|
||||
return ImmutableSet.of();
|
||||
} else {
|
||||
return ImmutableSet.copyOf(map.keySet());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user