清理代码...

Signed-off-by: 502647092 <jtb1@163.com>
master
502647092 2016-03-01 11:19:54 +08:00
parent ae31156f72
commit d882d5c2f9
7 changed files with 2404 additions and 2417 deletions

View File

@ -4,14 +4,15 @@
*/
package org.anjocaido.groupmanager.data;
import org.anjocaido.groupmanager.GroupManager;
import org.anjocaido.groupmanager.dataholder.WorldDataHolder;
import org.anjocaido.groupmanager.events.GMGroupEvent.Action;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.anjocaido.groupmanager.GroupManager;
import org.anjocaido.groupmanager.dataholder.WorldDataHolder;
import org.anjocaido.groupmanager.events.GMGroupEvent.Action;
/**
*
* @author gabrielcouto/ElgarL
@ -29,42 +30,47 @@ public class Group extends DataUnit implements Cloneable {
*/
private GroupVariables variables = new GroupVariables(this);
/**
* Constructor for individual World Groups.
*
* @param name
*/
public Group(WorldDataHolder source, String name) {
super(source, name);
}
/**
* Constructor for Global Groups.
*
* @param name
*/
public Group(String name) {
public Group(final String name) {
super(name);
}
/**
* @return the name
* Constructor for individual World Groups.
*
* @param name
*/
public String getName() {
public Group(final WorldDataHolder source, final String name) {
return this.getUUID();
super(source, name);
}
/**
* Is this a GlobalGroup
*
* @return true if this is a global group
* @param inherit
* the inherits to set
*/
public boolean isGlobal() {
public void addInherits(final Group inherit) {
return (getDataSource() == null);
if (!isGlobal()) {
if (!this.getDataSource().groupExists(inherit.getName())) {
getDataSource().addGroup(inherit);
}
if (!inherits.contains(inherit.getName().toLowerCase())) {
final List<String> clone = new ArrayList<String>(inherits);
clone.add(inherit.getName().toLowerCase());
inherits = Collections.unmodifiableList(clone);
}
flagAsChanged();
if (GroupManager.isLoaded()) {
GroupManager.BukkitPermissions.updateAllPlayers();
GroupManager.getGMEventHandler().callEvent(this, Action.GROUP_INHERITANCE_CHANGED);
}
}
}
/**
@ -84,10 +90,10 @@ public class Group extends DataUnit implements Cloneable {
clone.inherits = this.getInherits().isEmpty() ? Collections.unmodifiableList(Collections.<String> emptyList()) : Collections.unmodifiableList(new ArrayList<String>(this.getInherits()));
}
for (String perm : this.getPermissionList()) {
for (final String perm : this.getPermissionList()) {
clone.addPermission(perm);
}
clone.variables = ((GroupVariables) variables).clone(clone);
clone.variables = variables.clone(clone);
// clone.flagAsChanged();
return clone;
}
@ -98,19 +104,19 @@ public class Group extends DataUnit implements Cloneable {
* @param dataSource
* @return Null or Clone
*/
public Group clone(WorldDataHolder dataSource) {
public Group clone(final WorldDataHolder dataSource) {
if (dataSource.groupExists(this.getName())) {
return null;
}
Group clone = dataSource.createGroup(this.getName());
final Group clone = dataSource.createGroup(this.getName());
// Don't add inheritance for GlobalGroups
if (!isGlobal()) {
clone.inherits = this.getInherits().isEmpty() ? Collections.unmodifiableList(Collections.<String> emptyList()) : Collections.unmodifiableList(new ArrayList<String>(this.getInherits()));
}
for (String perm : this.getPermissionList()) {
for (final String perm : this.getPermissionList()) {
clone.addPermission(perm);
}
clone.variables = variables.clone(clone);
@ -130,41 +136,11 @@ public class Group extends DataUnit implements Cloneable {
}
/**
* @param inherit
* the inherits to set
* @return the name
*/
public void addInherits(Group inherit) {
public String getName() {
if (!isGlobal()) {
if (!this.getDataSource().groupExists(inherit.getName())) {
getDataSource().addGroup(inherit);
}
if (!inherits.contains(inherit.getName().toLowerCase())) {
List<String> clone = new ArrayList<String>(inherits);
clone.add(inherit.getName().toLowerCase());
inherits = Collections.unmodifiableList(clone);
}
flagAsChanged();
if (GroupManager.isLoaded()) {
GroupManager.BukkitPermissions.updateAllPlayers();
GroupManager.getGMEventHandler().callEvent(this, Action.GROUP_INHERITANCE_CHANGED);
}
}
}
public boolean removeInherits(String inherit) {
if (!isGlobal()) {
if (this.inherits.contains(inherit.toLowerCase())) {
List<String> clone = new ArrayList<String>(inherits);
clone.remove(inherit.toLowerCase());
inherits = Collections.unmodifiableList(clone);
flagAsChanged();
GroupManager.getGMEventHandler().callEvent(this, Action.GROUP_INHERITANCE_CHANGED);
return true;
}
}
return false;
return this.getUUID();
}
/**
@ -175,16 +151,41 @@ public class Group extends DataUnit implements Cloneable {
return variables;
}
/**
* Is this a GlobalGroup
*
* @return true if this is a global group
*/
public boolean isGlobal() {
return (getDataSource() == null);
}
public boolean removeInherits(final String inherit) {
if (!isGlobal()) {
if (this.inherits.contains(inherit.toLowerCase())) {
final List<String> clone = new ArrayList<String>(inherits);
clone.remove(inherit.toLowerCase());
inherits = Collections.unmodifiableList(clone);
flagAsChanged();
GroupManager.getGMEventHandler().callEvent(this, Action.GROUP_INHERITANCE_CHANGED);
return true;
}
}
return false;
}
/**
*
* @param varList
*/
public void setVariables(Map<String, Object> varList) {
public void setVariables(final Map<String, Object> varList) {
if (!isGlobal()) {
GroupVariables temp = new GroupVariables(this, varList);
final GroupVariables temp = new GroupVariables(this, varList);
variables.clearVars();
for (String key : temp.getVarKeyList()) {
for (final String key : temp.getVarKeyList()) {
variables.addVar(key, temp.getVarObject(key));
}
flagAsChanged();

View File

@ -12,10 +12,9 @@ import java.util.Map;
*/
public class GroupVariables extends Variables implements Cloneable {
private Group owner;
public GroupVariables(Group owner) {
private final Group owner;
public GroupVariables(final Group owner) {
super(owner);
this.owner = owner;
addVar("prefix", "");
@ -23,7 +22,7 @@ public class GroupVariables extends Variables implements Cloneable {
addVar("build", false);
}
public GroupVariables(Group owner, Map<String, Object> varList) {
public GroupVariables(final Group owner, final Map<String, Object> varList) {
super(owner);
variables.clear();
@ -48,20 +47,12 @@ public class GroupVariables extends Variables implements Cloneable {
}
/**
* A clone of all vars here.
*
* @return GroupVariables clone
* @return the owner
*/
protected GroupVariables clone(Group newOwner) {
@Override
public Group getOwner() {
GroupVariables clone = new GroupVariables(newOwner);
synchronized (variables) {
for (String key : variables.keySet()) {
clone.variables.put(key, variables.get(key));
}
}
newOwner.flagAsChanged();
return clone;
return owner;
}
/**
@ -70,11 +61,11 @@ public class GroupVariables extends Variables implements Cloneable {
* @param name
*/
@Override
public void removeVar(String name) {
public void removeVar(final String name) {
try {
this.variables.remove(name);
} catch (Exception e) {
} catch (final Exception e) {
}
if (name.equals("prefix")) {
addVar("prefix", "");
@ -87,11 +78,19 @@ public class GroupVariables extends Variables implements Cloneable {
}
/**
* @return the owner
* A clone of all vars here.
*
* @return GroupVariables clone
*/
@Override
public Group getOwner() {
protected GroupVariables clone(final Group newOwner) {
return owner;
final GroupVariables clone = new GroupVariables(newOwner);
synchronized (variables) {
for (final String key : variables.keySet()) {
clone.variables.put(key, variables.get(key));
}
}
newOwner.flagAsChanged();
return clone;
}
}

View File

@ -8,11 +8,11 @@ package org.anjocaido.groupmanager.data;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.anjocaido.groupmanager.GroupManager;
import org.anjocaido.groupmanager.dataholder.WorldDataHolder;
import org.anjocaido.groupmanager.events.GMUserEvent.Action;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -22,9 +22,6 @@ import org.bukkit.entity.Player;
*/
public class User extends DataUnit implements Cloneable {
/**
*
*/
private String group = null;
private final List<String> subGroups = Collections.synchronizedList(new ArrayList<String>());
/**
@ -38,167 +35,13 @@ public class User extends DataUnit implements Cloneable {
*
* @param name
*/
public User(WorldDataHolder source, String name) {
public User(final WorldDataHolder source, final String name) {
super(source, name);
this.group = source.getDefaultGroup().getName();
}
/**
*
* @return User clone
*/
@Override
public User clone() {
User clone = new User(getDataSource(), this.getLastName());
clone.group = this.group;
// Clone all subgroups.
clone.subGroups.addAll(this.subGroupListStringCopy());
for (String perm : this.getPermissionList()) {
clone.addPermission(perm);
}
// clone.variables = this.variables.clone();
// clone.flagAsChanged();
return clone;
}
/**
* Use this to deliver a user from one WorldDataHolder to another
*
* @param dataSource
* @return null if given dataSource already contains the same user
*/
public User clone(WorldDataHolder dataSource) {
if (dataSource.isUserDeclared(this.getUUID())) {
return null;
}
User clone = dataSource.createUser(this.getUUID());
if (dataSource.getGroup(group) == null) {
clone.setGroup(dataSource.getDefaultGroup());
} else {
clone.setGroup(dataSource.getGroup(this.getGroupName()));
}
// Clone all subgroups.
clone.subGroups.addAll(this.subGroupListStringCopy());
for (String perm : this.getPermissionList()) {
clone.addPermission(perm);
}
clone.variables = this.variables.clone(this);
clone.flagAsChanged();
return clone;
}
public User clone(String uUID, String CurrentName) {
User clone = this.getDataSource().createUser(uUID);
clone.setLastName(CurrentName);
// Set the group silently.
clone.setGroup(this.getDataSource().getGroup(this.getGroupName()), false);
// Clone all subgroups.
clone.subGroups.addAll(this.subGroupListStringCopy());
for (String perm : this.getPermissionList()) {
clone.addPermission(perm);
}
clone.variables = this.variables.clone(this);
clone.flagAsChanged();
return clone;
}
public Group getGroup() {
Group result = getDataSource().getGroup(group);
if (result == null) {
this.setGroup(getDataSource().getDefaultGroup());
result = getDataSource().getDefaultGroup();
}
return result;
}
/**
* @return the group
*/
public String getGroupName() {
Group result = getDataSource().getGroup(group);
if (result == null) {
group = getDataSource().getDefaultGroup().getName();
}
return group;
}
/**
* Place holder to let people know to stop using this method.
*
* @deprecated use {@link #getLastName()} and {@link #getUUID()}.
* @return a string containing the players last known name.
*/
@Deprecated
public String getName() {
return this.getLastName();
}
/**
* @param group
* the group to set
*/
public void setGroup(Group group) {
setGroup(group, true);
}
/**
* @param group
* the group to set
* @param updatePerms
* if we are to trigger a superperms update.
*
*/
public void setGroup(Group group, Boolean updatePerms) {
if (!this.getDataSource().groupExists(group.getName())) {
getDataSource().addGroup(group);
}
group = getDataSource().getGroup(group.getName());
String oldGroup = this.group;
this.group = group.getName();
flagAsChanged();
if (GroupManager.isLoaded()) {
if (!GroupManager.BukkitPermissions.isPlayer_join() && (updatePerms))
GroupManager.BukkitPermissions.updatePlayer(getBukkitPlayer());
// Do we notify of the group change?
String defaultGroupName = getDataSource().getDefaultGroup().getName();
// if we were not in the default group
// or we were in the default group and the move is to a different
// group.
boolean notify = (!oldGroup.equalsIgnoreCase(defaultGroupName)) || ((oldGroup.equalsIgnoreCase(defaultGroupName)) && (!this.group.equalsIgnoreCase(defaultGroupName)));
if (notify)
GroupManager.notify(this.getLastName(), String.format(" moved to the group %s in %s.", group.getName(), this.getDataSource().getName()));
if (updatePerms)
GroupManager.getGMEventHandler().callEvent(this, Action.USER_GROUP_CHANGED);
}
}
public boolean addSubGroup(Group subGroup) {
public boolean addSubGroup(final Group subGroup) {
// Don't allow adding a subgroup if it's already set as the primary.
if (this.group.equalsIgnoreCase(subGroup.getName())) {
@ -227,9 +70,135 @@ public class User extends DataUnit implements Cloneable {
// subGroups.add(subGroup.getName());
}
public int subGroupsSize() {
/**
*
* @return User clone
*/
@Override
public User clone() {
return subGroups.size();
final User clone = new User(getDataSource(), this.getLastName());
clone.group = this.group;
// Clone all subgroups.
clone.subGroups.addAll(this.subGroupListStringCopy());
for (final String perm : this.getPermissionList()) {
clone.addPermission(perm);
}
// clone.variables = this.variables.clone();
// clone.flagAsChanged();
return clone;
}
public User clone(final String uUID, final String CurrentName) {
final User clone = this.getDataSource().createUser(uUID);
clone.setLastName(CurrentName);
// Set the group silently.
clone.setGroup(this.getDataSource().getGroup(this.getGroupName()), false);
// Clone all subgroups.
clone.subGroups.addAll(this.subGroupListStringCopy());
for (final String perm : this.getPermissionList()) {
clone.addPermission(perm);
}
clone.variables = this.variables.clone(this);
clone.flagAsChanged();
return clone;
}
/**
* Use this to deliver a user from one WorldDataHolder to another
*
* @param dataSource
* @return null if given dataSource already contains the same user
*/
public User clone(final WorldDataHolder dataSource) {
if (dataSource.isUserDeclared(this.getUUID())) {
return null;
}
final User clone = dataSource.createUser(this.getUUID());
if (dataSource.getGroup(group) == null) {
clone.setGroup(dataSource.getDefaultGroup());
} else {
clone.setGroup(dataSource.getGroup(this.getGroupName()));
}
// Clone all subgroups.
clone.subGroups.addAll(this.subGroupListStringCopy());
for (final String perm : this.getPermissionList()) {
clone.addPermission(perm);
}
clone.variables = this.variables.clone(this);
clone.flagAsChanged();
return clone;
}
public boolean containsSubGroup(final Group subGroup) {
return subGroups.contains(subGroup.getName());
}
public Player getBukkitPlayer() {
if (bukkitPlayer == null) {
bukkitPlayer = Bukkit.getPlayer(this.getLastName());
}
return bukkitPlayer;
}
public Group getGroup() {
Group result = getDataSource().getGroup(group);
if (result == null) {
this.setGroup(getDataSource().getDefaultGroup());
result = getDataSource().getDefaultGroup();
}
return result;
}
/**
* @return the group
*/
public String getGroupName() {
final Group result = getDataSource().getGroup(group);
if (result == null) {
group = getDataSource().getDefaultGroup().getName();
}
return group;
}
/**
* Place holder to let people know to stop using this method.
*
* @deprecated use {@link #getLastName()} and {@link #getUUID()}.
* @return a string containing the players last known name.
*/
@Deprecated
public String getName() {
return this.getLastName();
}
/**
* @return the variables
*/
public UserVariables getVariables() {
return variables;
}
public boolean isSubGroupsEmpty() {
@ -237,12 +206,7 @@ public class User extends DataUnit implements Cloneable {
return subGroups.isEmpty();
}
public boolean containsSubGroup(Group subGroup) {
return subGroups.contains(subGroup.getName());
}
public boolean removeSubGroup(Group subGroup) {
public boolean removeSubGroup(final Group subGroup) {
try {
if (subGroups.remove(subGroup.getName())) {
@ -253,17 +217,80 @@ public class User extends DataUnit implements Cloneable {
GroupManager.getGMEventHandler().callEvent(this, Action.USER_SUBGROUP_CHANGED);
return true;
}
} catch (Exception e) {
} catch (final Exception e) {
}
return false;
}
/**
* @param group
* the group to set
*/
public void setGroup(final Group group) {
setGroup(group, true);
}
/**
* @param group
* the group to set
* @param updatePerms
* if we are to trigger a superperms update.
*
*/
public void setGroup(Group group, final Boolean updatePerms) {
if (!this.getDataSource().groupExists(group.getName())) {
getDataSource().addGroup(group);
}
group = getDataSource().getGroup(group.getName());
final String oldGroup = this.group;
this.group = group.getName();
flagAsChanged();
if (GroupManager.isLoaded()) {
if (!GroupManager.BukkitPermissions.isPlayer_join() && (updatePerms))
GroupManager.BukkitPermissions.updatePlayer(getBukkitPlayer());
// Do we notify of the group change?
final String defaultGroupName = getDataSource().getDefaultGroup().getName();
// if we were not in the default group
// or we were in the default group and the move is to a different
// group.
final boolean notify = (!oldGroup.equalsIgnoreCase(defaultGroupName)) || ((oldGroup.equalsIgnoreCase(defaultGroupName)) && (!this.group.equalsIgnoreCase(defaultGroupName)));
if (notify)
GroupManager.notify(this.getLastName(), String.format(" moved to the group %s in %s.", group.getName(), this.getDataSource().getName()));
if (updatePerms)
GroupManager.getGMEventHandler().callEvent(this, Action.USER_GROUP_CHANGED);
}
}
/**
*
* @param varList
*/
public void setVariables(final Map<String, Object> varList) {
// UserVariables temp = new UserVariables(this, varList);
variables.clearVars();
for (final String key : varList.keySet()) {
variables.addVar(key, varList.get(key));
}
flagAsChanged();
if (GroupManager.isLoaded()) {
// if (!GroupManager.BukkitPermissions.isPlayer_join())
// GroupManager.BukkitPermissions.updatePlayer(this.getName());
GroupManager.getGMEventHandler().callEvent(this, Action.USER_INFO_CHANGED);
}
}
public ArrayList<Group> subGroupListCopy() {
ArrayList<Group> val = new ArrayList<Group>();
final ArrayList<Group> val = new ArrayList<Group>();
synchronized (subGroups) {
for (String gstr : subGroups) {
Group g = getDataSource().getGroup(gstr);
for (final String gstr : subGroups) {
final Group g = getDataSource().getGroup(gstr);
if (g == null) {
removeSubGroup(g);
continue;
@ -280,44 +307,14 @@ public class User extends DataUnit implements Cloneable {
}
}
/**
* @return the variables
*/
public UserVariables getVariables() {
public int subGroupsSize() {
return variables;
return subGroups.size();
}
/**
*
* @param varList
*/
public void setVariables(Map<String, Object> varList) {
// UserVariables temp = new UserVariables(this, varList);
variables.clearVars();
for (String key : varList.keySet()) {
variables.addVar(key, varList.get(key));
}
flagAsChanged();
if (GroupManager.isLoaded()) {
// if (!GroupManager.BukkitPermissions.isPlayer_join())
// GroupManager.BukkitPermissions.updatePlayer(this.getName());
GroupManager.getGMEventHandler().callEvent(this, Action.USER_INFO_CHANGED);
}
}
public User updatePlayer(Player player) {
public User updatePlayer(final Player player) {
bukkitPlayer = player;
return this;
}
public Player getBukkitPlayer() {
if (bukkitPlayer == null) {
bukkitPlayer = Bukkit.getPlayer(this.getLastName());
}
return bukkitPlayer;
}
}

View File

@ -12,39 +12,21 @@ import java.util.Map;
*/
public class UserVariables extends Variables {
private User owner;
private final User owner;
public UserVariables(User owner) {
public UserVariables(final User owner) {
super(owner);
this.owner = owner;
}
public UserVariables(User owner, Map<String, Object> varList) {
public UserVariables(final User owner, final Map<String, Object> varList) {
super(owner);
this.variables.clear();
this.variables.putAll(varList);
this.owner = owner;
}
/**
* A clone of all vars here.
*
* @return UserVariables clone
*/
protected UserVariables clone(User newOwner) {
UserVariables clone = new UserVariables(newOwner);
synchronized (variables) {
for (String key : variables.keySet()) {
clone.variables.put(key, variables.get(key));
}
}
newOwner.flagAsChanged();
return clone;
}
/**
* @return the owner
*/
@ -53,4 +35,21 @@ public class UserVariables extends Variables {
return owner;
}
/**
* A clone of all vars here.
*
* @return UserVariables clone
*/
protected UserVariables clone(final User newOwner) {
final UserVariables clone = new UserVariables(newOwner);
synchronized (variables) {
for (final String key : variables.keySet()) {
clone.variables.put(key, variables.get(key));
}
}
newOwner.flagAsChanged();
return clone;
}
}

View File

@ -53,14 +53,133 @@ import org.bukkit.plugin.PluginManager;
* @author ElgarL
*/
public class BukkitPermissions {
private static Field permissions;
// Setup reflection (Thanks to Codename_B for the reflection source)
static {
try {
permissions = PermissionAttachment.class.getDeclaredField("permissions");
permissions.setAccessible(true);
} catch (final SecurityException e) {
e.printStackTrace();
} catch (final NoSuchFieldException e) {
e.printStackTrace();
}
}
protected WeakHashMap<String, PermissionAttachment> attachments = new WeakHashMap<String, PermissionAttachment>();
protected LinkedHashMap<String, Permission> registeredPermissions = new LinkedHashMap<String, Permission>();
protected GroupManager plugin;
protected boolean dumpAllPermissions = true;
protected boolean dumpMatchedPermissions = true;
private boolean player_join = false;
public BukkitPermissions(final GroupManager plugin) {
this.plugin = plugin;
this.reset();
this.registerEvents();
GroupManager.logger.info("Superperms support enabled.");
}
public void collectPermissions() {
registeredPermissions.clear();
for (final Permission perm : Bukkit.getPluginManager().getPermissions()) {
registeredPermissions.put(perm.getName().toLowerCase(), perm);
}
}
/**
* 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
* @return Map of child permissions
*/
public Map<String, Boolean> getAllChildren(final String node, final Set<String> playerPermArray) {
final LinkedList<String> stack = new LinkedList<String>();
final Map<String, Boolean> alreadyVisited = new HashMap<String, Boolean>();
stack.push(node);
alreadyVisited.put(node, true);
while (!stack.isEmpty()) {
final String now = stack.pop();
final Map<String, Boolean> children = getChildren(now);
if ((children != null) && (!playerPermArray.contains("-" + now))) {
for (final String childName : children.keySet()) {
if (!alreadyVisited.containsKey(childName)) {
stack.push(childName);
alreadyVisited.put(childName, children.get(childName));
}
}
}
}
alreadyVisited.remove(node);
if (!alreadyVisited.isEmpty())
return alreadyVisited;
return null;
}
/**
* Fetch all permissions which are registered with superperms.
* {can include child nodes)
*
* @param includeChildren
* @return List of all permission nodes
*/
public List<String> getAllRegisteredPermissions(final boolean includeChildren) {
final List<String> perms = new ArrayList<String>();
for (final String key : registeredPermissions.keySet()) {
if (!perms.contains(key)) {
perms.add(key);
if (includeChildren) {
final Map<String, Boolean> children = getAllChildren(key, new HashSet<String>());
if (children != null) {
for (final String node : children.keySet())
if (!perms.contains(node))
perms.add(node);
}
}
}
}
return perms;
}
/**
* 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(final String node) {
final Permission perm = registeredPermissions.get(node.toLowerCase());
if (perm == null)
return null;
return perm.getChildren();
}
/**
* @return the player_join
*/
@ -70,35 +189,45 @@ public class BukkitPermissions {
}
/**
* @param player_join
* the player_join to set
* List all effective permissions for this player.
*
* @param player
* @return List<String> of permissions
*/
public void setPlayer_join(boolean player_join) {
public List<String> listPerms(final Player player) {
this.player_join = player_join;
}
final List<String> perms = new ArrayList<String>();
private static Field permissions;
/*
* // All permissions registered with Bukkit for this player
* PermissionAttachment attachment = this.attachments.get(player);
*
* // List perms for this player perms.add("Attachment Permissions:");
* for(Map.Entry<String, Boolean> entry :
* attachment.getPermissions().entrySet()){ perms.add(" " +
* entry.getKey() + " = " + entry.getValue()); }
*/
// Setup reflection (Thanks to Codename_B for the reflection source)
static {
try {
permissions = PermissionAttachment.class.getDeclaredField("permissions");
permissions.setAccessible(true);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
perms.add("Effective Permissions:");
for (final PermissionAttachmentInfo info : player.getEffectivePermissions()) {
if (info.getValue() == true)
perms.add(" " + info.getPermission() + " = " + info.getValue());
}
return perms;
}
public BukkitPermissions(GroupManager plugin) {
/**
* Remove all attachments in case of a restart or reload.
*/
public void removeAllAttachments() {
this.plugin = plugin;
this.reset();
this.registerEvents();
GroupManager.logger.info("Superperms support enabled.");
/*
* Remove all attachments.
*/
for (final String key : attachments.keySet()) {
attachments.get(key).remove();
}
attachments.clear();
}
public void reset() {
@ -111,25 +240,26 @@ public class BukkitPermissions {
this.updateAllPlayers();
}
private void registerEvents() {
/**
* @param player_join
* the player_join to set
*/
public void setPlayer_join(final boolean player_join) {
PluginManager manager = plugin.getServer().getPluginManager();
manager.registerEvents(new PlayerEvents(), plugin);
manager.registerEvents(new BukkitEvents(), plugin);
this.player_join = player_join;
}
public void collectPermissions() {
/**
* force Bukkit to update every OnlinePlayers permissions.
*/
public void updateAllPlayers() {
registeredPermissions.clear();
for (Permission perm : Bukkit.getPluginManager().getPermissions()) {
registeredPermissions.put(perm.getName().toLowerCase(), perm);
for (final Player player : Bukkit.getServer().getOnlinePlayers()) {
updatePermissions(player);
}
}
public void updatePermissions(Player player) {
public void updatePermissions(final Player player) {
this.updatePermissions(player, null);
}
@ -141,16 +271,16 @@ public class BukkitPermissions {
* @param player
* @param world
*/
public void updatePermissions(Player player, String world) {
public void updatePermissions(final Player player, String world) {
if (player == null || !GroupManager.isLoaded()) {
return;
}
String name = player.getName();
final String name = player.getName();
// Reset the User objects player reference.
User user = plugin.getWorldsHolder().getWorldData(player.getWorld().getName()).getUser(name);
final User user = plugin.getWorldsHolder().getWorldData(player.getWorld().getName()).getUser(name);
if (user != null)
user.updatePlayer(player);
@ -171,14 +301,14 @@ public class BukkitPermissions {
// Add all permissions for this player (GM only)
// child nodes will be calculated by Bukkit.
List<String> playerPermArray = new ArrayList<String>(plugin.getWorldsHolder().getWorldData(world).getPermissionsHandler().getAllPlayersPermissions(name, false));
LinkedHashMap<String, Boolean> newPerms = new LinkedHashMap<String, Boolean>();
final LinkedHashMap<String, Boolean> newPerms = new LinkedHashMap<String, Boolean>();
// 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 (final String permission : playerPermArray) {
value = (!permission.startsWith("-"));
newPerms.put((value ? permission : permission.substring(1)), value);
}
@ -201,7 +331,7 @@ public class BukkitPermissions {
synchronized (attachment.getPermissible()) {
@SuppressWarnings("unchecked")
Map<String, Boolean> orig = (Map<String, Boolean>) permissions.get(attachment);
final Map<String, Boolean> orig = (Map<String, Boolean>) permissions.get(attachment);
// Clear the map (faster than removing the attachment and
// recalculating)
orig.clear();
@ -211,39 +341,69 @@ public class BukkitPermissions {
attachment.getPermissible().recalculatePermissions();
}
} catch (IllegalArgumentException e) {
} catch (final IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
} catch (final IllegalAccessException e) {
e.printStackTrace();
}
GroupManager.logger.finest("Attachment updated for: " + name);
}
/**
* force Bukkit to update this Players permissions.
*/
public void updatePlayer(final Player player) {
if (player != null)
this.updatePermissions(player, null);
}
private void registerEvents() {
final PluginManager manager = plugin.getServer().getPluginManager();
manager.registerEvents(new PlayerEvents(), plugin);
manager.registerEvents(new BukkitEvents(), plugin);
}
/**
* Force remove any attachments
*
* @param player
*/
private void removeAttachment(final String playerName) {
if (attachments.containsKey(playerName)) {
attachments.get(playerName).remove();
attachments.remove(playerName);
}
}
/**
* Sort a permission node list by parent/child
*
* @param permList
* @return List sorted for priority
*/
private List<String> sort(List<String> permList) {
private List<String> sort(final List<String> permList) {
List<String> result = new ArrayList<String>();
final List<String> result = new ArrayList<String>();
for (String key : permList) {
for (final String key : permList) {
/*
* Ignore stupid plugins which add empty permission nodes.
*/
if (!key.isEmpty()) {
String a = key.charAt(0) == '-' ? key.substring(1) : key;
Map<String, Boolean> allchildren = GroupManager.BukkitPermissions.getAllChildren(a, new HashSet<String>());
final String a = key.charAt(0) == '-' ? key.substring(1) : key;
final Map<String, Boolean> allchildren = GroupManager.BukkitPermissions.getAllChildren(a, new HashSet<String>());
if (allchildren != null) {
ListIterator<String> itr = result.listIterator();
final ListIterator<String> itr = result.listIterator();
while (itr.hasNext()) {
String node = (String) itr.next();
String b = node.charAt(0) == '-' ? node.substring(1) : node;
final String node = itr.next();
final String b = node.charAt(0) == '-' ? node.substring(1) : node;
// Insert the parent node before the child
if (allchildren.containsKey(b)) {
@ -261,163 +421,24 @@ public class BukkitPermissions {
return result;
}
/**
* Fetch all permissions which are registered with superperms.
* {can include child nodes)
*
* @param includeChildren
* @return List of all permission nodes
*/
public List<String> getAllRegisteredPermissions(boolean includeChildren) {
protected class BukkitEvents implements Listener {
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) {
for (String node : children.keySet())
if (!perms.contains(node))
perms.add(node);
}
}
}
@EventHandler(priority = EventPriority.NORMAL)
public void onPluginDisable(final PluginDisableEvent event) {
collectPermissions();
// updateAllPlayers();
}
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
* @return Map of child permissions
*/
public Map<String, Boolean> getAllChildren(String node, Set<String> playerPermArray) {
@EventHandler(priority = EventPriority.NORMAL)
public void onPluginEnable(final PluginEnableEvent event) {
LinkedList<String> stack = new LinkedList<String>();
Map<String, Boolean> alreadyVisited = new HashMap<String, Boolean>();
stack.push(node);
alreadyVisited.put(node, true);
if (!GroupManager.isLoaded())
return;
while (!stack.isEmpty()) {
String now = stack.pop();
Map<String, Boolean> children = getChildren(now);
if ((children != null) && (!playerPermArray.contains("-" + now))) {
for (String childName : children.keySet()) {
if (!alreadyVisited.containsKey(childName)) {
stack.push(childName);
alreadyVisited.put(childName, children.get(childName));
}
}
}
collectPermissions();
updateAllPlayers();
}
alreadyVisited.remove(node);
if (!alreadyVisited.isEmpty())
return alreadyVisited;
return null;
}
/**
* 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();
}
/**
* List all effective permissions for this player.
*
* @param player
* @return List<String> of permissions
*/
public List<String> listPerms(Player player) {
List<String> perms = new ArrayList<String>();
/*
* // All permissions registered with Bukkit for this player
* PermissionAttachment attachment = this.attachments.get(player);
*
* // List perms for this player perms.add("Attachment Permissions:");
* for(Map.Entry<String, Boolean> entry :
* attachment.getPermissions().entrySet()){ perms.add(" " +
* entry.getKey() + " = " + entry.getValue()); }
*/
perms.add("Effective Permissions:");
for (PermissionAttachmentInfo info : player.getEffectivePermissions()) {
if (info.getValue() == true)
perms.add(" " + info.getPermission() + " = " + info.getValue());
}
return perms;
}
/**
* 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(String playerName) {
if (attachments.containsKey(playerName)) {
attachments.get(playerName).remove();
attachments.remove(playerName);
}
}
/**
* Remove all attachments in case of a restart or reload.
*/
public void removeAllAttachments() {
/*
* Remove all attachments.
*/
for (String key : attachments.keySet()) {
attachments.get(key).remove();
}
attachments.clear();
}
/**
@ -429,10 +450,16 @@ public class BukkitPermissions {
protected class PlayerEvents implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerJoin(PlayerJoinEvent event) {
public void onPlayerChangeWorld(final PlayerChangedWorldEvent event) { // has changed worlds
updatePermissions(event.getPlayer(), event.getPlayer().getWorld().getName());
}
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerJoin(final PlayerJoinEvent event) {
setPlayer_join(true);
Player player = event.getPlayer();
final Player player = event.getPlayer();
GroupManager.logger.finest("Player Join event: " + player.getName());
@ -450,22 +477,16 @@ 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());
}
/*
* Trigger at highest so we tidy up last.
*/
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerQuit(PlayerQuitEvent event) {
public void onPlayerQuit(final PlayerQuitEvent event) {
if (!GroupManager.isLoaded())
return;
Player player = event.getPlayer();
final Player player = event.getPlayer();
/*
* force remove any attachments as bukkit may not
@ -474,24 +495,4 @@ public class BukkitPermissions {
}
}
protected class BukkitEvents implements Listener {
@EventHandler(priority = EventPriority.NORMAL)
public void onPluginEnable(PluginEnableEvent event) {
if (!GroupManager.isLoaded())
return;
collectPermissions();
updateAllPlayers();
}
@EventHandler(priority = EventPriority.NORMAL)
public void onPluginDisable(PluginDisableEvent event) {
collectPermissions();
// updateAllPlayers();
}
}
}