summaryrefslogtreecommitdiffstats
path: root/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java
diff options
context:
space:
mode:
Diffstat (limited to 'EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java')
-rw-r--r--EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java153
1 files changed, 86 insertions, 67 deletions
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java
index 516679544..c366b65bf 100644
--- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java
+++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java
@@ -1,18 +1,19 @@
/*
-* 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 2
-* 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, write to the Free Software
-* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-*/
+ * 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
+ */
package org.anjocaido.groupmanager.permissions;
@@ -47,7 +48,6 @@ import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.PluginManager;
-
/**
*
* BukkitPermissions overrides to force GM reponses to Superperms
@@ -62,11 +62,12 @@ public class BukkitPermissions {
protected boolean dumpAllPermissions = true;
protected boolean dumpMatchedPermissions = true;
private boolean player_join = false;
-
+
/**
* @return the player_join
*/
public boolean isPlayer_join() {
+
return player_join;
}
@@ -74,6 +75,7 @@ public class BukkitPermissions {
* @param player_join the player_join to set
*/
public void setPlayer_join(boolean player_join) {
+
this.player_join = player_join;
}
@@ -92,6 +94,7 @@ public class BukkitPermissions {
}
public BukkitPermissions(GroupManager plugin) {
+
this.plugin = plugin;
this.collectPermissions();
this.registerEvents();
@@ -101,35 +104,38 @@ public class BukkitPermissions {
}
private void registerEvents() {
+
PluginManager manager = plugin.getServer().getPluginManager();
manager.registerEvents(new PlayerEvents(), plugin);
manager.registerEvents(new BukkitEvents(), plugin);
}
-
public void collectPermissions() {
+
registeredPermissions.clear();
for (Permission perm : Bukkit.getPluginManager().getPermissions()) {
- registeredPermissions.put(perm.getName().toLowerCase(), perm);
+ registeredPermissions.put(perm.getName().toLowerCase(), perm);
}
-
+
}
public void updatePermissions(Player player) {
+
this.updatePermissions(player, null);
}
-
/**
- * Push all permissions which are registered with GM for this player, on this world to Bukkit
+ * Push all permissions which are registered with GM for this player, on
+ * this world to Bukkit
* and make it update for the child nodes.
*
* @param player
* @param world
*/
public void updatePermissions(Player player, String world) {
+
if (player == null || !GroupManager.isLoaded()) {
return;
}
@@ -155,17 +161,18 @@ public class BukkitPermissions {
// Sort the perm list by parent/child, so it will push to superperms correctly.
playerPermArray = sort(playerPermArray);
-
+
Boolean value = false;
- for (String permission : playerPermArray) {
+ for (String permission : playerPermArray) {
value = (!permission.startsWith("-"));
- newPerms.put((value? permission : permission.substring(1)), value);
+ newPerms.put((value ? permission : permission.substring(1)), value);
}
/**
- * This is put in place until such a time as Bukkit pull 466 is implemented
- * https://github.com/Bukkit/Bukkit/pull/466
- */
+ * This is put in place until such a time as Bukkit pull 466 is
+ * implemented
+ * https://github.com/Bukkit/Bukkit/pull/466
+ */
try { // Codename_B source
@SuppressWarnings("unchecked")
Map<String, Boolean> orig = (Map<String, Boolean>) permissions.get(attachment);
@@ -182,7 +189,7 @@ public class BukkitPermissions {
e.printStackTrace();
}
}
-
+
/**
* Sort a permission node list by parent/child
*
@@ -190,20 +197,20 @@ public class BukkitPermissions {
* @return List sorted for priority
*/
private List<String> sort(List<String> permList) {
-
+
List<String> result = new ArrayList<String>();
-
+
for (String key : permList) {
- String a = key.charAt(0) == '-'? key.substring(1):key;
+ String a = key.charAt(0) == '-' ? key.substring(1) : key;
Map<String, Boolean> allchildren = GroupManager.BukkitPermissions.getAllChildren(a, new HashSet<String>());
if (allchildren != null) {
ListIterator<String> itr = result.listIterator();
-
- while (itr.hasNext()){
+
+ while (itr.hasNext()) {
String node = (String) itr.next();
- String b = node.charAt(0) == '-'? node.substring(1):node;
-
+ String b = node.charAt(0) == '-' ? node.substring(1) : node;
+
// Insert the parent node before the child
if (allchildren.containsKey(b)) {
itr.set(key);
@@ -215,11 +222,10 @@ public class BukkitPermissions {
if (!result.contains(key))
result.add(key);
}
-
+
return result;
}
-
/**
* Fetch all permissions which are registered with superperms.
* {can include child nodes)
@@ -228,13 +234,13 @@ public class BukkitPermissions {
* @return List of all permission nodes
*/
public List<String> getAllRegisteredPermissions(boolean includeChildren) {
-
+
List<String> perms = new ArrayList<String>();
-
+
for (String key : registeredPermissions.keySet()) {
if (!perms.contains(key)) {
perms.add(key);
-
+
if (includeChildren) {
Map<String, Boolean> children = getAllChildren(key, new HashSet<String>());
if (children != null) {
@@ -244,32 +250,33 @@ public class BukkitPermissions {
}
}
}
-
+
}
return perms;
}
-
+
/**
* Returns a map of ALL child permissions registered with bukkit
* null is empty
*
* @param node
- * @param playerPermArray current list of perms to check against for negations
+ * @param playerPermArray current list of perms to check against for
+ * negations
* @return Map of child permissions
*/
public Map<String, Boolean> getAllChildren(String node, Set<String> playerPermArray) {
-
+
LinkedList<String> stack = new LinkedList<String>();
Map<String, Boolean> alreadyVisited = new HashMap<String, Boolean>();
stack.push(node);
alreadyVisited.put(node, true);
-
+
while (!stack.isEmpty()) {
String now = stack.pop();
-
+
Map<String, Boolean> children = getChildren(now);
-
- if ((children != null) && (!playerPermArray.contains("-"+now))) {
+
+ if ((children != null) && (!playerPermArray.contains("-" + now))) {
for (String childName : children.keySet()) {
if (!alreadyVisited.containsKey(childName)) {
stack.push(childName);
@@ -279,24 +286,26 @@ public class BukkitPermissions {
}
}
alreadyVisited.remove(node);
- if (!alreadyVisited.isEmpty()) return alreadyVisited;
-
+ if (!alreadyVisited.isEmpty())
+ return alreadyVisited;
+
return null;
}
-
+
/**
- * Returns a map of the child permissions (1 node deep) as registered with Bukkit.
+ * Returns a map of the child permissions (1 node deep) as registered with
+ * Bukkit.
* null is empty
*
* @param node
* @return Map of child permissions
*/
public Map<String, Boolean> getChildren(String node) {
-
+
Permission perm = registeredPermissions.get(node.toLowerCase());
if (perm == null)
return null;
-
+
return perm.getChildren();
}
@@ -308,6 +317,7 @@ public class BukkitPermissions {
* @return List<String> of permissions
*/
public List<String> listPerms(Player player) {
+
List<String> perms = new ArrayList<String>();
/*
@@ -332,25 +342,28 @@ public class BukkitPermissions {
* force Bukkit to update every OnlinePlayers permissions.
*/
public void updateAllPlayers() {
+
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
updatePermissions(player);
}
}
-
+
/**
* force Bukkit to update this Players permissions.
*/
public void updatePlayer(Player player) {
+
if (player != null)
this.updatePermissions(player, null);
}
-
+
/**
* Force remove any attachments
*
* @param player
*/
private void removeAttachment(Player player) {
+
if (attachments.containsKey(player)) {
try {
player.removeAttachment(attachments.get(player));
@@ -363,15 +376,15 @@ public class BukkitPermissions {
attachments.remove(player);
}
}
-
+
/**
* Remove all attachments in case of a restart or reload.
*/
public void removeAllAttachments() {
-
+
Iterator<Player> itr = attachments.keySet().iterator();
-
- while (itr.hasNext()){
+
+ while (itr.hasNext()) {
Player player = itr.next();
try {
player.removeAttachment(attachments.get(player));
@@ -389,20 +402,21 @@ public class BukkitPermissions {
* Player events tracked to cause Superperms updates
*
* @author ElgarL
- *
+ *
*/
protected class PlayerEvents implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerJoin(PlayerJoinEvent event) {
+
setPlayer_join(true);
Player player = event.getPlayer();
-
+
/*
* Tidy up any lose ends
*/
removeAttachment(player);
-
+
// force GM to create the player if they are not already listed.
if (plugin.getWorldsHolder().getWorldData(player.getWorld().getName()).getUser(player.getName()) != null) {
setPlayer_join(false);
@@ -410,29 +424,32 @@ public class BukkitPermissions {
}
setPlayer_join(false);
}
-
+
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerChangeWorld(PlayerChangedWorldEvent event) { // has changed worlds
+
updatePermissions(event.getPlayer(), event.getPlayer().getWorld().getName());
}
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerKick(PlayerKickEvent event) {
+
Player player = event.getPlayer();
-
+
/*
* force remove any attachments as bukkit may not
*/
removeAttachment(player);
}
-
+
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerQuit(PlayerQuitEvent event) {
+
if (!GroupManager.isLoaded())
return;
Player player = event.getPlayer();
-
+
/*
* force remove any attachments as bukkit may not
*/
@@ -444,6 +461,7 @@ public class BukkitPermissions {
@EventHandler(priority = EventPriority.NORMAL)
public void onPluginEnable(PluginEnableEvent event) {
+
if (!GroupManager.isLoaded())
return;
@@ -453,6 +471,7 @@ public class BukkitPermissions {
@EventHandler(priority = EventPriority.NORMAL)
public void onPluginDisable(PluginDisableEvent event) {
+
collectPermissions();
// updateAllPlayers();
}