清理代码...

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,16 +4,17 @@
*/ */
package org.anjocaido.groupmanager.data; 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.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; 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 * @author gabrielcouto/ElgarL
*/ */
public class Group extends DataUnit implements Cloneable { public class Group extends DataUnit implements Cloneable {
@ -29,47 +30,52 @@ public class Group extends DataUnit implements Cloneable {
*/ */
private GroupVariables variables = new GroupVariables(this); 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. * Constructor for Global Groups.
* *
* @param name * @param name
*/ */
public Group(String name) { public Group(final String name) {
super(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 * @param inherit
* * the inherits to set
* @return true if this is a global group
*/ */
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 * Clone this group
* *
* @return a clone of this group * @return a clone of this group
*/ */
@Override @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())); 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.addPermission(perm);
} }
clone.variables = ((GroupVariables) variables).clone(clone); clone.variables = variables.clone(clone);
// clone.flagAsChanged(); // clone.flagAsChanged();
return clone; return clone;
} }
/** /**
* Use this to deliver a group from a different dataSource to another * Use this to deliver a group from a different dataSource to another
* *
* @param dataSource * @param dataSource
* @return Null or Clone * @return Null or Clone
*/ */
public Group clone(WorldDataHolder dataSource) { public Group clone(final WorldDataHolder dataSource) {
if (dataSource.groupExists(this.getName())) { if (dataSource.groupExists(this.getName())) {
return null; return null;
} }
Group clone = dataSource.createGroup(this.getName()); final Group clone = dataSource.createGroup(this.getName());
// Don't add inheritance for GlobalGroups // Don't add inheritance for GlobalGroups
if (!isGlobal()) { if (!isGlobal()) {
clone.inherits = this.getInherits().isEmpty() ? Collections.unmodifiableList(Collections.<String> emptyList()) : Collections.unmodifiableList(new ArrayList<String>(this.getInherits())); 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.addPermission(perm);
} }
clone.variables = variables.clone(clone); clone.variables = variables.clone(clone);
@ -122,7 +128,7 @@ public class Group extends DataUnit implements Cloneable {
* an unmodifiable list of inherits list * an unmodifiable list of inherits list
* You can't manage the list by here * You can't manage the list by here
* Lol... version 0.6 had a problem because this. * Lol... version 0.6 had a problem because this.
* *
* @return the inherits * @return the inherits
*/ */
public List<String> getInherits() { public List<String> getInherits() {
@ -130,41 +136,11 @@ public class Group extends DataUnit implements Cloneable {
} }
/** /**
* @param inherit * @return the name
* the inherits to set
*/ */
public void addInherits(Group inherit) { public String getName() {
if (!isGlobal()) { return this.getUUID();
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;
} }
/** /**
@ -176,15 +152,40 @@ public class Group extends DataUnit implements Cloneable {
} }
/** /**
* * Is this a GlobalGroup
* @param varList *
* @return true if this is a global group
*/ */
public void setVariables(Map<String, Object> varList) { public boolean isGlobal() {
return (getDataSource() == null);
}
public boolean removeInherits(final String inherit) {
if (!isGlobal()) { if (!isGlobal()) {
GroupVariables temp = new GroupVariables(this, varList); 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(final Map<String, Object> varList) {
if (!isGlobal()) {
final GroupVariables temp = new GroupVariables(this, varList);
variables.clearVars(); variables.clearVars();
for (String key : temp.getVarKeyList()) { for (final String key : temp.getVarKeyList()) {
variables.addVar(key, temp.getVarObject(key)); variables.addVar(key, temp.getVarObject(key));
} }
flagAsChanged(); flagAsChanged();

View File

@ -7,15 +7,14 @@ package org.anjocaido.groupmanager.data;
import java.util.Map; import java.util.Map;
/** /**
* *
* @author gabrielcouto * @author gabrielcouto
*/ */
public class GroupVariables extends Variables implements Cloneable { public class GroupVariables extends Variables implements Cloneable {
private Group owner; private final Group owner;
public GroupVariables(Group owner) {
public GroupVariables(final Group owner) {
super(owner); super(owner);
this.owner = owner; this.owner = owner;
addVar("prefix", ""); addVar("prefix", "");
@ -23,7 +22,7 @@ public class GroupVariables extends Variables implements Cloneable {
addVar("build", false); addVar("build", false);
} }
public GroupVariables(Group owner, Map<String, Object> varList) { public GroupVariables(final Group owner, final Map<String, Object> varList) {
super(owner); super(owner);
variables.clear(); variables.clear();
@ -48,33 +47,25 @@ public class GroupVariables extends Variables implements Cloneable {
} }
/** /**
* A clone of all vars here. * @return the owner
*
* @return GroupVariables clone
*/ */
protected GroupVariables clone(Group newOwner) { @Override
public Group getOwner() {
GroupVariables clone = new GroupVariables(newOwner); return owner;
synchronized (variables) {
for (String key : variables.keySet()) {
clone.variables.put(key, variables.get(key));
}
}
newOwner.flagAsChanged();
return clone;
} }
/** /**
* Remove a var from the list * Remove a var from the list
* *
* @param name * @param name
*/ */
@Override @Override
public void removeVar(String name) { public void removeVar(final String name) {
try { try {
this.variables.remove(name); this.variables.remove(name);
} catch (Exception e) { } catch (final Exception e) {
} }
if (name.equals("prefix")) { if (name.equals("prefix")) {
addVar("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 protected GroupVariables clone(final Group newOwner) {
public Group getOwner() {
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,23 +8,20 @@ package org.anjocaido.groupmanager.data;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import org.anjocaido.groupmanager.GroupManager; import org.anjocaido.groupmanager.GroupManager;
import org.anjocaido.groupmanager.dataholder.WorldDataHolder; import org.anjocaido.groupmanager.dataholder.WorldDataHolder;
import org.anjocaido.groupmanager.events.GMUserEvent.Action; import org.anjocaido.groupmanager.events.GMUserEvent.Action;
import java.util.Map;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
/** /**
* *
* @author gabrielcouto/ElgarL * @author gabrielcouto/ElgarL
*/ */
public class User extends DataUnit implements Cloneable { public class User extends DataUnit implements Cloneable {
/**
*
*/
private String group = null; private String group = null;
private final List<String> subGroups = Collections.synchronizedList(new ArrayList<String>()); private final List<String> subGroups = Collections.synchronizedList(new ArrayList<String>());
/** /**
@ -35,170 +32,16 @@ public class User extends DataUnit implements Cloneable {
private transient Player bukkitPlayer = null; private transient Player bukkitPlayer = null;
/** /**
* *
* @param name * @param name
*/ */
public User(WorldDataHolder source, String name) { public User(final WorldDataHolder source, final String name) {
super(source, name); super(source, name);
this.group = source.getDefaultGroup().getName(); this.group = source.getDefaultGroup().getName();
} }
/** public boolean addSubGroup(final Group subGroup) {
*
* @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) {
// Don't allow adding a subgroup if it's already set as the primary. // Don't allow adding a subgroup if it's already set as the primary.
if (this.group.equalsIgnoreCase(subGroup.getName())) { if (this.group.equalsIgnoreCase(subGroup.getName())) {
@ -227,9 +70,135 @@ public class User extends DataUnit implements Cloneable {
// subGroups.add(subGroup.getName()); // 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() { public boolean isSubGroupsEmpty() {
@ -237,12 +206,7 @@ public class User extends DataUnit implements Cloneable {
return subGroups.isEmpty(); return subGroups.isEmpty();
} }
public boolean containsSubGroup(Group subGroup) { public boolean removeSubGroup(final Group subGroup) {
return subGroups.contains(subGroup.getName());
}
public boolean removeSubGroup(Group subGroup) {
try { try {
if (subGroups.remove(subGroup.getName())) { if (subGroups.remove(subGroup.getName())) {
@ -253,17 +217,80 @@ public class User extends DataUnit implements Cloneable {
GroupManager.getGMEventHandler().callEvent(this, Action.USER_SUBGROUP_CHANGED); GroupManager.getGMEventHandler().callEvent(this, Action.USER_SUBGROUP_CHANGED);
return true; return true;
} }
} catch (Exception e) { } catch (final Exception e) {
} }
return false; 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() { public ArrayList<Group> subGroupListCopy() {
ArrayList<Group> val = new ArrayList<Group>(); final ArrayList<Group> val = new ArrayList<Group>();
synchronized (subGroups) { synchronized (subGroups) {
for (String gstr : subGroups) { for (final String gstr : subGroups) {
Group g = getDataSource().getGroup(gstr); final Group g = getDataSource().getGroup(gstr);
if (g == null) { if (g == null) {
removeSubGroup(g); removeSubGroup(g);
continue; continue;
@ -280,44 +307,14 @@ public class User extends DataUnit implements Cloneable {
} }
} }
/** public int subGroupsSize() {
* @return the variables
*/
public UserVariables getVariables() {
return variables; return subGroups.size();
} }
/** public User updatePlayer(final Player player) {
*
* @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) {
bukkitPlayer = player; bukkitPlayer = player;
return this; return this;
} }
public Player getBukkitPlayer() {
if (bukkitPlayer == null) {
bukkitPlayer = Bukkit.getPlayer(this.getLastName());
}
return bukkitPlayer;
}
} }

View File

@ -7,44 +7,26 @@ package org.anjocaido.groupmanager.data;
import java.util.Map; import java.util.Map;
/** /**
* *
* @author gabrielcouto * @author gabrielcouto
*/ */
public class UserVariables extends Variables { public class UserVariables extends Variables {
private User owner; private final User owner;
public UserVariables(User owner) { public UserVariables(final User owner) {
super(owner); super(owner);
this.owner = owner; this.owner = owner;
} }
public UserVariables(User owner, Map<String, Object> varList) { public UserVariables(final User owner, final Map<String, Object> varList) {
super(owner); super(owner);
this.variables.clear(); this.variables.clear();
this.variables.putAll(varList); this.variables.putAll(varList);
this.owner = owner; 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 * @return the owner
*/ */
@ -53,4 +35,21 @@ public class UserVariables extends Variables {
return owner; 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

@ -3,12 +3,12 @@
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 * as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version. * of the License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * 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; import org.bukkit.plugin.PluginManager;
/** /**
* *
* BukkitPermissions overrides to force GM reponses to Superperms * BukkitPermissions overrides to force GM reponses to Superperms
* *
* @author ElgarL * @author ElgarL
*/ */
public class BukkitPermissions { 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 WeakHashMap<String, PermissionAttachment> attachments = new WeakHashMap<String, PermissionAttachment>();
protected LinkedHashMap<String, Permission> registeredPermissions = new LinkedHashMap<String, Permission>(); protected LinkedHashMap<String, Permission> registeredPermissions = new LinkedHashMap<String, Permission>();
protected GroupManager plugin; protected GroupManager plugin;
protected boolean dumpAllPermissions = true; protected boolean dumpAllPermissions = true;
protected boolean dumpMatchedPermissions = true; protected boolean dumpMatchedPermissions = true;
private boolean player_join = false; 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 * @return the player_join
*/ */
@ -70,35 +189,45 @@ public class BukkitPermissions {
} }
/** /**
* @param player_join * List all effective permissions for this player.
* the player_join to set *
* @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) perms.add("Effective Permissions:");
static { for (final PermissionAttachmentInfo info : player.getEffectivePermissions()) {
try { if (info.getValue() == true)
permissions = PermissionAttachment.class.getDeclaredField("permissions"); perms.add(" " + info.getPermission() + " = " + info.getValue());
permissions.setAccessible(true);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} }
return perms;
} }
public BukkitPermissions(GroupManager plugin) { /**
* Remove all attachments in case of a restart or reload.
*/
public void removeAllAttachments() {
this.plugin = plugin; /*
this.reset(); * Remove all attachments.
this.registerEvents(); */
for (final String key : attachments.keySet()) {
GroupManager.logger.info("Superperms support enabled."); attachments.get(key).remove();
}
attachments.clear();
} }
public void reset() { public void reset() {
@ -111,25 +240,26 @@ public class BukkitPermissions {
this.updateAllPlayers(); 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(); this.player_join = player_join;
manager.registerEvents(new PlayerEvents(), plugin);
manager.registerEvents(new BukkitEvents(), plugin);
} }
public void collectPermissions() { /**
* force Bukkit to update every OnlinePlayers permissions.
*/
public void updateAllPlayers() {
registeredPermissions.clear(); for (final Player player : Bukkit.getServer().getOnlinePlayers()) {
updatePermissions(player);
for (Permission perm : Bukkit.getPluginManager().getPermissions()) {
registeredPermissions.put(perm.getName().toLowerCase(), perm);
} }
} }
public void updatePermissions(Player player) { public void updatePermissions(final Player player) {
this.updatePermissions(player, null); this.updatePermissions(player, null);
} }
@ -137,20 +267,20 @@ public class BukkitPermissions {
/** /**
* Push all permissions which are registered with GM for this player, on * Push all permissions which are registered with GM for this player, on
* this world to Bukkit and make it update for the child nodes. * this world to Bukkit and make it update for the child nodes.
* *
* @param player * @param player
* @param world * @param world
*/ */
public void updatePermissions(Player player, String world) { public void updatePermissions(final Player player, String world) {
if (player == null || !GroupManager.isLoaded()) { if (player == null || !GroupManager.isLoaded()) {
return; return;
} }
String name = player.getName(); final String name = player.getName();
// Reset the User objects player reference. // 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) if (user != null)
user.updatePlayer(player); user.updatePlayer(player);
@ -171,14 +301,14 @@ public class BukkitPermissions {
// Add all permissions for this player (GM only) // Add all permissions for this player (GM only)
// child nodes will be calculated by Bukkit. // child nodes will be calculated by Bukkit.
List<String> playerPermArray = new ArrayList<String>(plugin.getWorldsHolder().getWorldData(world).getPermissionsHandler().getAllPlayersPermissions(name, false)); 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 // Sort the perm list by parent/child, so it will push to superperms
// correctly. // correctly.
playerPermArray = sort(playerPermArray); playerPermArray = sort(playerPermArray);
Boolean value = false; Boolean value = false;
for (String permission : playerPermArray) { for (final String permission : playerPermArray) {
value = (!permission.startsWith("-")); value = (!permission.startsWith("-"));
newPerms.put((value ? permission : permission.substring(1)), value); newPerms.put((value ? permission : permission.substring(1)), value);
} }
@ -201,7 +331,7 @@ public class BukkitPermissions {
synchronized (attachment.getPermissible()) { synchronized (attachment.getPermissible()) {
@SuppressWarnings("unchecked") @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 // Clear the map (faster than removing the attachment and
// recalculating) // recalculating)
orig.clear(); orig.clear();
@ -211,39 +341,69 @@ public class BukkitPermissions {
attachment.getPermissible().recalculatePermissions(); attachment.getPermissible().recalculatePermissions();
} }
} catch (IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IllegalAccessException e) { } catch (final IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }
GroupManager.logger.finest("Attachment updated for: " + name); 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 * Sort a permission node list by parent/child
* *
* @param permList * @param permList
* @return List sorted for priority * @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. * Ignore stupid plugins which add empty permission nodes.
*/ */
if (!key.isEmpty()) { if (!key.isEmpty()) {
String a = key.charAt(0) == '-' ? key.substring(1) : key; final String a = key.charAt(0) == '-' ? key.substring(1) : key;
Map<String, Boolean> allchildren = GroupManager.BukkitPermissions.getAllChildren(a, new HashSet<String>()); final Map<String, Boolean> allchildren = GroupManager.BukkitPermissions.getAllChildren(a, new HashSet<String>());
if (allchildren != null) { if (allchildren != null) {
ListIterator<String> itr = result.listIterator(); final ListIterator<String> itr = result.listIterator();
while (itr.hasNext()) { while (itr.hasNext()) {
String node = (String) itr.next(); final String node = itr.next();
String b = node.charAt(0) == '-' ? node.substring(1) : node; final String b = node.charAt(0) == '-' ? node.substring(1) : node;
// Insert the parent node before the child // Insert the parent node before the child
if (allchildren.containsKey(b)) { if (allchildren.containsKey(b)) {
@ -261,178 +421,45 @@ public class BukkitPermissions {
return result; return result;
} }
/** protected class BukkitEvents implements Listener {
* 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>(); @EventHandler(priority = EventPriority.NORMAL)
public void onPluginDisable(final PluginDisableEvent event) {
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);
}
}
}
collectPermissions();
// updateAllPlayers();
} }
return perms;
}
/** @EventHandler(priority = EventPriority.NORMAL)
* Returns a map of ALL child permissions registered with bukkit public void onPluginEnable(final PluginEnableEvent event) {
* 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>(); if (!GroupManager.isLoaded())
Map<String, Boolean> alreadyVisited = new HashMap<String, Boolean>(); return;
stack.push(node);
alreadyVisited.put(node, true);
while (!stack.isEmpty()) { collectPermissions();
String now = stack.pop(); updateAllPlayers();
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;
}
/**
* 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();
} }
/** /**
* Player events tracked to cause Superperms updates * Player events tracked to cause Superperms updates
* *
* @author ElgarL * @author ElgarL
* *
*/ */
protected class PlayerEvents implements Listener { protected class PlayerEvents implements Listener {
@EventHandler(priority = EventPriority.LOWEST) @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); setPlayer_join(true);
Player player = event.getPlayer(); final Player player = event.getPlayer();
GroupManager.logger.finest("Player Join event: " + player.getName()); GroupManager.logger.finest("Player Join event: " + player.getName());
@ -450,22 +477,16 @@ public class BukkitPermissions {
setPlayer_join(false); 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. * Trigger at highest so we tidy up last.
*/ */
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerQuit(PlayerQuitEvent event) { public void onPlayerQuit(final PlayerQuitEvent event) {
if (!GroupManager.isLoaded()) if (!GroupManager.isLoaded())
return; return;
Player player = event.getPlayer(); final Player player = event.getPlayer();
/* /*
* force remove any attachments as bukkit may not * 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();
}
}
} }