Browse Source

清理代码...

Signed-off-by: 502647092 <jtb1@163.com>
master
502647092 6 years ago
parent
commit
d882d5c2f9
  1. 129
      src/main/java/org/anjocaido/groupmanager/data/Group.java
  2. 47
      src/main/java/org/anjocaido/groupmanager/data/GroupVariables.java
  3. 251
      src/main/java/org/anjocaido/groupmanager/data/User.java
  4. 35
      src/main/java/org/anjocaido/groupmanager/data/UserVariables.java
  5. 1371
      src/main/java/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java
  6. 1647
      src/main/java/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java
  7. 475
      src/main/java/org/anjocaido/groupmanager/permissions/BukkitPermissions.java

129
src/main/java/org/anjocaido/groupmanager/data/Group.java

@ -4,16 +4,17 @@
*/
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
*/
public class Group extends DataUnit implements Cloneable {
@ -29,47 +30,52 @@ 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);
}
}
}
/**
* Clone this group
*
*
* @return a clone of this group
*/
@Override
@ -84,33 +90,33 @@ 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;
}
/**
* Use this to deliver a group from a different dataSource to another
*
*
* @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);
@ -122,7 +128,7 @@ public class Group extends DataUnit implements Cloneable {
* an unmodifiable list of inherits list
* You can't manage the list by here
* Lol... version 0.6 had a problem because this.
*
*
* @return the inherits
*/
public List<String> getInherits() {
@ -130,33 +136,36 @@ 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);
}
}
return this.getUUID();
}
public boolean removeInherits(String inherit) {
/**
* @return the variables
*/
public GroupVariables getVariables() {
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())) {
List<String> clone = new ArrayList<String>(inherits);
final List<String> clone = new ArrayList<String>(inherits);
clone.remove(inherit.toLowerCase());
inherits = Collections.unmodifiableList(clone);
flagAsChanged();
@ -168,23 +177,15 @@ public class Group extends DataUnit implements Cloneable {
}
/**
* @return the variables
*/
public GroupVariables getVariables() {
return variables;
}
/**
*
*
* @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();

47
src/main/java/org/anjocaido/groupmanager/data/GroupVariables.java

@ -7,15 +7,14 @@ package org.anjocaido.groupmanager.data;
import java.util.Map;
/**
*
*
* @author gabrielcouto
*/
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,33 +47,25 @@ 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;
}
/**
* Remove a var from the list
*
*
* @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;
}
}

251
src/main/java/org/anjocaido/groupmanager/data/User.java

@ -8,23 +8,20 @@ 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;
/**
*
*
* @author gabrielcouto/ElgarL
*/
public class User extends DataUnit implements Cloneable {
/**
*
*/
private String group = null;
private final List<String> subGroups = Collections.synchronizedList(new ArrayList<String>());
/**
@ -35,29 +32,58 @@ public class User extends DataUnit implements Cloneable {
private transient Player bukkitPlayer = null;
/**
*
*
* @param name
*/
public User(WorldDataHolder source, String name) {
public User(final WorldDataHolder source, final String name) {
super(source, name);
this.group = source.getDefaultGroup().getName();
}
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())) {
return false;
}
// User already has this subgroup
if (containsSubGroup(subGroup))
return false;
// If the group doesn't exists add it
if (!this.getDataSource().groupExists(subGroup.getName())) {
getDataSource().addGroup(subGroup);
}
subGroups.add(subGroup.getName());
flagAsChanged();
if (GroupManager.isLoaded()) {
if (!GroupManager.BukkitPermissions.isPlayer_join())
GroupManager.BukkitPermissions.updatePlayer(getBukkitPlayer());
GroupManager.getGMEventHandler().callEvent(this, Action.USER_SUBGROUP_CHANGED);
}
return true;
// subGroup = getDataSource().getGroup(subGroup.getName());
// removeSubGroup(subGroup);
// subGroups.add(subGroup.getName());
}
/**
*
*
* @return User clone
*/
@Override
public User clone() {
User clone = new User(getDataSource(), this.getLastName());
final User clone = new User(getDataSource(), this.getLastName());
clone.group = this.group;
// Clone all subgroups.
clone.subGroups.addAll(this.subGroupListStringCopy());
for (String perm : this.getPermissionList()) {
for (final String perm : this.getPermissionList()) {
clone.addPermission(perm);
}
// clone.variables = this.variables.clone();
@ -65,19 +91,41 @@ public class User extends DataUnit implements Cloneable {
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(WorldDataHolder dataSource) {
public User clone(final WorldDataHolder dataSource) {
if (dataSource.isUserDeclared(this.getUUID())) {
return null;
}
User clone = dataSource.createUser(this.getUUID());
final User clone = dataSource.createUser(this.getUUID());
if (dataSource.getGroup(group) == null) {
clone.setGroup(dataSource.getDefaultGroup());
@ -88,7 +136,7 @@ public class User extends DataUnit implements Cloneable {
// Clone all subgroups.
clone.subGroups.addAll(this.subGroupListStringCopy());
for (String perm : this.getPermissionList()) {
for (final String perm : this.getPermissionList()) {
clone.addPermission(perm);
}
@ -97,26 +145,17 @@ public class User extends DataUnit implements Cloneable {
return clone;
}
public User clone(String uUID, String CurrentName) {
public boolean containsSubGroup(final Group subGroup) {
User clone = this.getDataSource().createUser(uUID);
clone.setLastName(CurrentName);
// Set the group silently.
clone.setGroup(this.getDataSource().getGroup(this.getGroupName()), false);
return subGroups.contains(subGroup.getName());
}
// Clone all subgroups.
clone.subGroups.addAll(this.subGroupListStringCopy());
public Player getBukkitPlayer() {
for (String perm : this.getPermissionList()) {
clone.addPermission(perm);
if (bukkitPlayer == null) {
bukkitPlayer = Bukkit.getPlayer(this.getLastName());
}
clone.variables = this.variables.clone(this);
clone.flagAsChanged();
return clone;
return bukkitPlayer;
}
public Group getGroup() {
@ -134,7 +173,7 @@ public class User extends DataUnit implements Cloneable {
*/
public String getGroupName() {
Group result = getDataSource().getGroup(group);
final Group result = getDataSource().getGroup(group);
if (result == null) {
group = getDataSource().getDefaultGroup().getName();
}
@ -143,7 +182,7 @@ public class User extends DataUnit implements Cloneable {
/**
* 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.
*/
@ -154,11 +193,40 @@ public class User extends DataUnit implements Cloneable {
}
/**
* @return the variables
*/
public UserVariables getVariables() {
return variables;
}
public boolean isSubGroupsEmpty() {
return subGroups.isEmpty();
}
public boolean removeSubGroup(final Group subGroup) {
try {
if (subGroups.remove(subGroup.getName())) {
flagAsChanged();
if (GroupManager.isLoaded())
if (!GroupManager.BukkitPermissions.isPlayer_join())
GroupManager.BukkitPermissions.updatePlayer(getBukkitPlayer());
GroupManager.getGMEventHandler().callEvent(this, Action.USER_SUBGROUP_CHANGED);
return true;
}
} catch (final Exception e) {
}
return false;
}
/**
* @param group
* the group to set
*/
public void setGroup(Group group) {
public void setGroup(final Group group) {
setGroup(group, true);
}
@ -168,15 +236,15 @@ public class User extends DataUnit implements Cloneable {
* the group to set
* @param updatePerms
* if we are to trigger a superperms update.
*
*
*/
public void setGroup(Group group, Boolean updatePerms) {
public void setGroup(Group group, final Boolean updatePerms) {
if (!this.getDataSource().groupExists(group.getName())) {
getDataSource().addGroup(group);
}
group = getDataSource().getGroup(group.getName());
String oldGroup = this.group;
final String oldGroup = this.group;
this.group = group.getName();
flagAsChanged();
if (GroupManager.isLoaded()) {
@ -184,11 +252,11 @@ public class User extends DataUnit implements Cloneable {
GroupManager.BukkitPermissions.updatePlayer(getBukkitPlayer());
// Do we notify of the group change?
String defaultGroupName = getDataSource().getDefaultGroup().getName();
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.
boolean notify = (!oldGroup.equalsIgnoreCase(defaultGroupName)) || ((oldGroup.equalsIgnoreCase(defaultGroupName)) && (!this.group.equalsIgnoreCase(defaultGroupName)));
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()));
@ -198,72 +266,31 @@ public class User extends DataUnit implements Cloneable {
}
}
public boolean addSubGroup(Group subGroup) {
// Don't allow adding a subgroup if it's already set as the primary.
if (this.group.equalsIgnoreCase(subGroup.getName())) {
return false;
}
// User already has this subgroup
if (containsSubGroup(subGroup))
return false;
/**
*
* @param varList
*/
public void setVariables(final Map<String, Object> varList) {
// If the group doesn't exists add it
if (!this.getDataSource().groupExists(subGroup.getName())) {
getDataSource().addGroup(subGroup);
// UserVariables temp = new UserVariables(this, varList);
variables.clearVars();
for (final String key : varList.keySet()) {
variables.addVar(key, varList.get(key));
}
subGroups.add(subGroup.getName());
flagAsChanged();
if (GroupManager.isLoaded()) {
if (!GroupManager.BukkitPermissions.isPlayer_join())
GroupManager.BukkitPermissions.updatePlayer(getBukkitPlayer());
GroupManager.getGMEventHandler().callEvent(this, Action.USER_SUBGROUP_CHANGED);
}
return true;
// subGroup = getDataSource().getGroup(subGroup.getName());
// removeSubGroup(subGroup);
// subGroups.add(subGroup.getName());
}
public int subGroupsSize() {
return subGroups.size();
}
public boolean isSubGroupsEmpty() {
return subGroups.isEmpty();
}
public boolean containsSubGroup(Group subGroup) {
return subGroups.contains(subGroup.getName());
}
public boolean removeSubGroup(Group subGroup) {
try {
if (subGroups.remove(subGroup.getName())) {
flagAsChanged();
if (GroupManager.isLoaded())
if (!GroupManager.BukkitPermissions.isPlayer_join())
GroupManager.BukkitPermissions.updatePlayer(getBukkitPlayer());
GroupManager.getGMEventHandler().callEvent(this, Action.USER_SUBGROUP_CHANGED);
return true;
}
} catch (Exception e) {
// if (!GroupManager.BukkitPermissions.isPlayer_join())
// GroupManager.BukkitPermissions.updatePlayer(this.getName());
GroupManager.getGMEventHandler().callEvent(this, Action.USER_INFO_CHANGED);
}
return false;
}
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() {
return variables;
}
/**
*
* @param varList
*/
public void setVariables(Map<String, Object> varList) {
public int subGroupsSize() {
// 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);
}
return subGroups.size();
}
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;
}
}

35
src/main/java/org/anjocaido/groupmanager/data/UserVariables.java

@ -7,50 +7,49 @@ package org.anjocaido.groupmanager.data;
import java.util.Map;
/**
*
*
* @author gabrielcouto
*/
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;
}
/**
* @return the owner
*/
@Override
public User getOwner() {
return owner;
}
/**
* A clone of all vars here.
*
*
* @return UserVariables clone
*/
protected UserVariables clone(User newOwner) {
protected UserVariables clone(final User newOwner) {
UserVariables clone = new UserVariables(newOwner);
final UserVariables clone = new UserVariables(newOwner);
synchronized (variables) {
for (String key : variables.keySet()) {
for (final String key : variables.keySet()) {
clone.variables.put(key, variables.get(key));
}
}
newOwner.flagAsChanged();
return clone;
}
/**
* @return the owner
*/
@Override
public User getOwner() {
return owner;
}
}

1371
src/main/java/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java

File diff suppressed because it is too large

1647
src/main/java/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java

File diff suppressed because it is too large

475
src/main/java/org/anjocaido/groupmanager/permissions/BukkitPermissions.java

@ -3,12 +3,12 @@
* 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,
@ -47,20 +47,139 @@ import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.PluginManager;
/**
*
*
* BukkitPermissions overrides to force GM reponses to Superperms
*
*
* @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) {
this.plugin = plugin;
this.reset();
this.registerEvents();
/**
* Remove all attachments in case of a restart or reload.
*/
public void removeAllAttachments() {
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() {
PluginManager manager = plugin.getServer().getPluginManager();
/**
* @param player_join
* the player_join to set
*/
public void setPlayer_join(final boolean player_join) {
manager.registerEvents(new PlayerEvents(), plugin);
manager.registerEvents(new BukkitEvents(), plugin);
this.player_join = player_join;
}
public void collectPermissions() {
registeredPermissions.clear();
/**
* force Bukkit to update every OnlinePlayers permissions.
*/
public void updateAllPlayers() {
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);
}
@ -137,20 +267,20 @@ public class BukkitPermissions {
/**
* 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) {
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,178 +421,45 @@ 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) {
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);
}
}
}
}
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) {
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))) {
for (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;
}
/**
* 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;
}
protected class BukkitEvents implements Listener {
/**
* force Bukkit to update every OnlinePlayers permissions.
*/
public void updateAllPlayers() {
@EventHandler(priority = EventPriority.NORMAL)
public void onPluginDisable(final PluginDisableEvent event) {
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
updatePermissions(player);
collectPermissions();
// updateAllPlayers();
}
}
/**
* 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);
}
}
@EventHandler(priority = EventPriority.NORMAL)
public void onPluginEnable(final PluginEnableEvent event) {
/**
* Remove all attachments in case of a restart or reload.
*/
public void removeAllAttachments() {
if (!GroupManager.isLoaded())
return;
/*
* Remove all attachments.
*/
for (String key : attachments.keySet()) {
attachments.get(key).remove();
collectPermissions();
updateAllPlayers();
}
attachments.clear();
}
/**
* Player events tracked to cause Superperms updates
*
*
* @author ElgarL
*
*
*/
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();
}
}
}

Loading…
Cancel
Save