Optimizations include changing the return of comparePermissionString.

This commit is contained in:
ElgarL 2011-11-25 19:33:32 +00:00
parent 0cb77d8a03
commit b25a8f059b
6 changed files with 282 additions and 260 deletions

View File

@ -521,10 +521,10 @@ public class NijikoPermissionsProxy extends PermissionHandler {
@Override
public boolean has(String world, String playerName, String permission) {
if (permission == null || permission.equals("")) {
if (permission == null || permission.isEmpty()) {
return false;
}
if (playerName == null || playerName == "") {
if (playerName == null || playerName.isEmpty()) {
GroupManager.logger.severe("A plugin is asking permission '" + permission + "' for a null player... Which plugin does that? Bastards!");
return false;
}

View File

@ -77,4 +77,5 @@ v 1.6:
- Prevent Group.equals tests throwing a NullPointerException for GlobalGroups.
- Stop throwing errors on an empty users file.
- Optimize sorting to speedup permission tests.
- Fix superperms to pass all tests http://dev.bukkit.org/server-mods/superpermstest/
- Fix superperms to pass all tests http://dev.bukkit.org/server-mods/superpermstest/
- Optimizations include changing the return of comparePermissionString.

View File

@ -10,209 +10,232 @@ import java.util.ArrayList;
import org.anjocaido.groupmanager.GroupManager;
import org.anjocaido.groupmanager.dataholder.WorldDataHolder;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
/**
*
*
* @author gabrielcouto
*/
public class User extends DataUnit implements Cloneable {
/**
/**
*
*/
private String group = null;
private ArrayList<String> subGroups = new ArrayList<String>();
/**
*This one holds the fields in INFO node.
* like prefix = 'c'
* or build = false
*/
private UserVariables variables = new UserVariables(this);
private String group = null;
private ArrayList<String> subGroups = new ArrayList<String>();
/**
* This one holds the fields in INFO node. like prefix = 'c' or build =
* false
*/
private UserVariables variables = new UserVariables(this);
private transient Player bukkitPlayer = null;
/**
*
* @param name
*/
public User(WorldDataHolder source, String name) {
super(source, name);
this.group = source.getDefaultGroup().getName();
}
/**
*
* @param name
*/
public User(WorldDataHolder source, String name) {
super(source, name);
this.group = source.getDefaultGroup().getName();
}
/**
*
* @return User clone
*/
@Override
public User clone() {
User clone = new User(getDataSource(), this.getName());
clone.group = this.group;
for (String perm : this.getPermissionList()) {
clone.addPermission(perm);
}
//clone.variables = this.variables.clone();
//clone.flagAsChanged();
return clone;
}
/**
*
* @return User clone
*/
@Override
public User clone() {
User clone = new User(getDataSource(), this.getName());
clone.group = this.group;
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.getName())) {
return null;
}
User clone = dataSource.createUser(this.getName());
if (dataSource.getGroup(group) == null) {
clone.setGroup(dataSource.getDefaultGroup());
} else {
clone.setGroup(dataSource.getGroup(this.getGroupName()));
}
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.getName())) {
return null;
}
User clone = dataSource.createUser(this.getName());
if (dataSource.getGroup(group) == null) {
clone.setGroup(dataSource.getDefaultGroup());
} else {
clone.setGroup(dataSource.getGroup(this.getGroupName()));
}
for (String perm : this.getPermissionList()) {
clone.addPermission(perm);
}
// clone.variables = this.variables.clone();
clone.flagAsChanged();
return clone;
}
public Group getGroup() {
Group result = getDataSource().getGroup(group);
if (result == null) {
this.setGroup(getDataSource().getDefaultGroup());
result = getDataSource().getDefaultGroup();
}
return result;
}
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;
}
/**
* @return the group
*/
public String getGroupName() {
Group result = getDataSource().getGroup(group);
if (result == null) {
group = getDataSource().getDefaultGroup().getName();
}
return group;
}
/**
* @param group the group to set
*/
@Deprecated
public void setGroup(String group) {
this.group = group;
flagAsChanged();
if (GroupManager.isLoaded())
if(GroupManager.BukkitPermissions.player_join = false)
GroupManager.BukkitPermissions.updateAllPlayers();
}
/**
* @param group
* the group to set
*/
@Deprecated
public void setGroup(String group) {
this.group = group;
flagAsChanged();
if (GroupManager.isLoaded())
if (GroupManager.BukkitPermissions.player_join = false)
GroupManager.BukkitPermissions.updateAllPlayers();
}
/**
* @param group the group to set
*/
public void setGroup(Group group) {
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.player_join = false)
GroupManager.BukkitPermissions.updateAllPlayers();
// 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.getName(), String.format(" moved to the group %s.", group.getName()));
}
}
/**
* @param group
* the group to set
*/
public void setGroup(Group group) {
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.player_join = false)
GroupManager.BukkitPermissions.updateAllPlayers();
public void addSubGroup(Group subGroup) {
if (this.group.equalsIgnoreCase(subGroup.getName())) {
return;
}
if (!this.getDataSource().groupExists(subGroup.getName())) {
getDataSource().addGroup(subGroup);
}
subGroup = getDataSource().getGroup(subGroup.getName());
removeSubGroup(subGroup);
subGroups.add(subGroup.getName());
flagAsChanged();
if (GroupManager.isLoaded())
if (GroupManager.BukkitPermissions.player_join = false)
GroupManager.BukkitPermissions.updateAllPlayers();
}
// 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)));
public int subGroupsSize() {
return subGroups.size();
}
if (notify)
GroupManager.notify(this.getName(), String.format(" moved to the group %s.", group.getName()));
}
}
public boolean isSubGroupsEmpty() {
return subGroups.isEmpty();
}
public void addSubGroup(Group subGroup) {
if (this.group.equalsIgnoreCase(subGroup.getName())) {
return;
}
if (!this.getDataSource().groupExists(subGroup.getName())) {
getDataSource().addGroup(subGroup);
}
subGroup = getDataSource().getGroup(subGroup.getName());
removeSubGroup(subGroup);
subGroups.add(subGroup.getName());
flagAsChanged();
if (GroupManager.isLoaded())
if (GroupManager.BukkitPermissions.player_join = false)
GroupManager.BukkitPermissions.updateAllPlayers();
}
public boolean containsSubGroup(Group subGroup) {
return subGroups.contains(subGroup.getName());
}
public int subGroupsSize() {
return subGroups.size();
}
public boolean removeSubGroup(Group subGroup) {
try {
if (subGroups.remove(subGroup.getName())) {
flagAsChanged();
if (GroupManager.isLoaded())
if (GroupManager.BukkitPermissions.player_join = false)
GroupManager.BukkitPermissions.updateAllPlayers();
return true;
}
} catch (Exception e) {
}
return false;
}
public boolean isSubGroupsEmpty() {
return subGroups.isEmpty();
}
public ArrayList<Group> subGroupListCopy() {
ArrayList<Group> val = new ArrayList<Group>();
for (String gstr : subGroups) {
Group g = getDataSource().getGroup(gstr);
if (g == null) {
removeSubGroup(g);
continue;
}
val.add(g);
}
return val;
}
public boolean containsSubGroup(Group subGroup) {
return subGroups.contains(subGroup.getName());
}
public ArrayList<String> subGroupListStringCopy() {
return new ArrayList<String>(subGroups);
}
public boolean removeSubGroup(Group subGroup) {
try {
if (subGroups.remove(subGroup.getName())) {
flagAsChanged();
if (GroupManager.isLoaded())
if (GroupManager.BukkitPermissions.player_join = false)
GroupManager.BukkitPermissions.updateAllPlayers();
return true;
}
} catch (Exception e) {
}
return false;
}
/**
* @return the variables
*/
public UserVariables getVariables() {
return variables;
}
public ArrayList<Group> subGroupListCopy() {
ArrayList<Group> val = new ArrayList<Group>();
for (String gstr : subGroups) {
Group g = getDataSource().getGroup(gstr);
if (g == null) {
removeSubGroup(g);
continue;
}
val.add(g);
}
return val;
}
public ArrayList<String> subGroupListStringCopy() {
return new ArrayList<String>(subGroups);
}
/**
* @return the variables
*/
public UserVariables getVariables() {
return variables;
}
/**
*
* @param varList
*/
public void setVariables(Map<String, Object> varList) {
UserVariables temp = new UserVariables(this, varList);
variables.clearVars();
for (String key : temp.getVarKeyList()) {
variables.addVar(key, temp.getVarObject(key));
}
flagAsChanged();
if (GroupManager.isLoaded())
if (GroupManager.BukkitPermissions.player_join = false)
GroupManager.BukkitPermissions.updateAllPlayers();
}
public User updatePlayer(Player player) {
if (player != null) {
bukkitPlayer = player;
}
return this;
}
public Player getBukkitPlayer() {
if (bukkitPlayer == null) {
bukkitPlayer = Bukkit.getPlayer(this.getName());
}
return bukkitPlayer;
}
/**
*
* @param varList
*/
public void setVariables(Map<String, Object> varList) {
UserVariables temp = new UserVariables(this, varList);
variables.clearVars();
for (String key : temp.getVarKeyList()) {
variables.addVar(key, temp.getVarObject(key));
}
flagAsChanged();
if (GroupManager.isLoaded())
if (GroupManager.BukkitPermissions.player_join = false)
GroupManager.BukkitPermissions.updateAllPlayers();
}
}

View File

@ -44,12 +44,13 @@ public class OverloadedWorldHolder extends WorldDataHolder {
@Override
public User getUser(String userName) {
//OVERLOADED CODE
if (overloadedUsers.containsKey(userName.toLowerCase())) {
return overloadedUsers.get(userName.toLowerCase());
String userNameLowered = userName.toLowerCase();
if (overloadedUsers.containsKey(userNameLowered)) {
return overloadedUsers.get(userNameLowered);
}
//END CODE
if (users.containsKey(userName.toLowerCase())) {
return users.get(userName.toLowerCase());
if (users.containsKey(userNameLowered)) {
return users.get(userNameLowered);
}
User newUser = createUser(userName);
haveUsersChanged = true;

View File

@ -268,9 +268,10 @@ public class WorldsHolder {
* @return OverloadedWorldHolder
*/
public OverloadedWorldHolder getWorldData(String worldName) {
OverloadedWorldHolder data = worldsData.get(worldName.toLowerCase());
if (mirrors.containsKey(worldName.toLowerCase())) {
String realOne = mirrors.get(worldName.toLowerCase());
String worldNameLowered = worldName.toLowerCase();
OverloadedWorldHolder data = worldsData.get(worldNameLowered);
if (mirrors.containsKey(worldNameLowered)) {
String realOne = mirrors.get(worldNameLowered);
data = worldsData.get(realOne.toLowerCase());
}
if (data == null) {

View File

@ -17,6 +17,7 @@ import org.anjocaido.groupmanager.utils.PermissionCheckResult;
import org.anjocaido.groupmanager.utils.PermissionCheckResult.Type;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
/**
* Everything here maintains the model created by Nijikokun
@ -62,7 +63,7 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface {
*/
@Override
public boolean permission(Player player, String permission) {
return checkUserPermission(ph.getUser(player.getName()), permission);
return checkUserPermission(ph.getUser(player.getName()).updatePlayer(player), permission);
}
/**
@ -98,13 +99,13 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface {
public List<String> getAllPlayersPermissions(String userName) {
List<String> playerPermArray = new ArrayList<String>();
for (String perm : ph.getUser(userName).getPermissionList()) {
if ((!playerPermArray.contains(perm)) && (!playerPermArray.contains("-" + perm))) {
playerPermArray.add(perm);
Map<String, Boolean> children = GroupManager.BukkitPermissions.getAllChildren(perm, playerPermArray);
if (children != null) {
for (String child : children.keySet()) {
if (children.get(child))
@ -135,7 +136,7 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface {
for (String perm : ph.getGroup(group).getPermissionList()) {
if ((!playerPermArray.contains(perm)) && (!playerPermArray.contains("-" + perm))) {
playerPermArray.add(perm);
Map<String, Boolean> children = GroupManager.BukkitPermissions.getAllChildren(perm, playerPermArray);
if (children != null) {
for (String child : children.keySet()) {
@ -149,7 +150,8 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface {
}
}
}
//Collections.sort(playerPermArray, StringPermissionComparator.getInstance());
// Collections.sort(playerPermArray,
// StringPermissionComparator.getInstance());
return playerPermArray;
}
@ -243,7 +245,8 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface {
/**
* Check if user can build. Checks inheritance and subgroups.
*
* @param userName Player's name
* @param userName
* Player's name
* @return true if the user can build
*/
public boolean canUserBuild(String userName) {
@ -283,8 +286,8 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface {
}
/**
* Checks the specified group for the Info Build node.
* Does NOT check inheritance
* Checks the specified group for the Info Build node. Does NOT check
* inheritance
*
* @param groupName
* @return true if can build
@ -615,15 +618,8 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface {
result.askedPermission = permission;
result.owner = user;
for (String access : user.getPermissionList()) {
if (comparePermissionString(access, permission)) {
result.accessLevel = access;
if (access.charAt(0) == '-') {
result.resultType = PermissionCheckResult.Type.NEGATION;
} else if (access.charAt(0) == '+') {
result.resultType = PermissionCheckResult.Type.EXCEPTION;
} else {
result.resultType = PermissionCheckResult.Type.FOUND;
}
result.resultType = comparePermissionString(access, permission);
if (result.resultType != PermissionCheckResult.Type.NOTFOUND) {
return result;
}
}
@ -645,15 +641,8 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface {
result.owner = group;
result.askedPermission = permission;
for (String access : group.getPermissionList()) {
if (comparePermissionString(access, permission)) {
result.accessLevel = access;
if (access.charAt(0) == '-') {
result.resultType = PermissionCheckResult.Type.NEGATION;
} else if (access.charAt(0) == '+') {
result.resultType = PermissionCheckResult.Type.EXCEPTION;
} else {
result.resultType = PermissionCheckResult.Type.FOUND;
}
result.resultType = comparePermissionString(access, permission);
if (result.resultType != PermissionCheckResult.Type.NOTFOUND) {
return result;
}
}
@ -670,7 +659,7 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface {
*/
public boolean checkUserPermission(User user, String permission) {
PermissionCheckResult result = checkFullGMPermission(user, permission, true);
if (result.resultType.equals(PermissionCheckResult.Type.EXCEPTION) || result.resultType.equals(PermissionCheckResult.Type.FOUND)) {
if (result.resultType == PermissionCheckResult.Type.EXCEPTION || result.resultType == PermissionCheckResult.Type.FOUND) {
return true;
}
@ -686,13 +675,13 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface {
* @return PermissionCheckResult
*/
public PermissionCheckResult checkFullUserPermission(User user, String targetPermission) {
return checkFullGMPermission(user, targetPermission, true);
}
/**
* Check user and groups with inheritance and Bukkit if bukkit = true
* return a PermissionCheckResult.
* Check user and groups with inheritance and Bukkit if bukkit = true return
* a PermissionCheckResult.
*
* @param user
* @param targetPermission
@ -708,31 +697,33 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface {
return result;
}
if (checkBukkit == true) {
// Check Bukkit perms to support plugins which add perms via code (Heroes).
final Player player = Bukkit.getPlayer(user.getName());
if ((player != null) && (player.hasPermission(targetPermission))) {
result.resultType = PermissionCheckResult.Type.FOUND;
if (checkBukkit) {
// Check Bukkit perms to support plugins which add perms via code
// (Heroes).
final Player player = user.getBukkitPlayer();
final Permission bukkitPerm = Bukkit.getPluginManager().getPermission(targetPermission);
if (player != null && bukkitPerm != null) {
result.resultType = player.hasPermission(bukkitPerm) ? PermissionCheckResult.Type.FOUND : PermissionCheckResult.Type.NEGATION;
result.owner = user;
return result;
}
}
PermissionCheckResult resultUser = checkUserOnlyPermission(user, targetPermission);
if (!resultUser.resultType.equals(PermissionCheckResult.Type.NOTFOUND)) {
if (resultUser.resultType != PermissionCheckResult.Type.NOTFOUND) {
return resultUser;
}
// IT ONLY CHECKS GROUPS PERMISSIONS IF RESULT FOR USER IS NOT FOUND
PermissionCheckResult resultGroup = checkGroupPermissionWithInheritance(user.getGroup(), targetPermission);
if (!resultGroup.resultType.equals(PermissionCheckResult.Type.NOTFOUND)) {
if (resultGroup.resultType != PermissionCheckResult.Type.NOTFOUND) {
return resultGroup;
}
// SUBGROUPS CHECK
for (Group subGroup : user.subGroupListCopy()) {
PermissionCheckResult resultSubGroup = checkGroupPermissionWithInheritance(subGroup, targetPermission);
if (!resultSubGroup.resultType.equals(PermissionCheckResult.Type.NOTFOUND)) {
if (resultSubGroup.resultType != PermissionCheckResult.Type.NOTFOUND) {
return resultSubGroup;
}
}
@ -994,37 +985,42 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface {
*
* @param userAccessLevel
* @param fullPermissionName
* @return true if found a matching token. false if not.
* @return PermissionCheckResult.Type
*/
public boolean comparePermissionString(String userAccessLevel, String fullPermissionName) {
int userAccessLevelLength;
if (userAccessLevel == null || fullPermissionName == null
|| fullPermissionName.length() == 0 || (userAccessLevelLength = userAccessLevel.length()) == 0) {
return false;
}
public PermissionCheckResult.Type comparePermissionString(String userAccessLevel, String fullPermissionName) {
int userAccessLevelLength;
if (userAccessLevel == null || fullPermissionName == null || fullPermissionName.length() == 0 || (userAccessLevelLength = userAccessLevel.length()) == 0) {
return PermissionCheckResult.Type.NOTFOUND;
}
int userAccessLevelOffset = 0;
if (userAccessLevel.charAt(0) == '+' || userAccessLevel.charAt(0) == '-') {
userAccessLevelOffset = 1;
}
if ("*".regionMatches(0, userAccessLevel, userAccessLevelOffset, userAccessLevelLength - userAccessLevelOffset)) {
return true;
}
int fullPermissionNameOffset;
if (fullPermissionName.charAt(0) == '+' || fullPermissionName.charAt(0) == '-') {
fullPermissionNameOffset = 1;
} else {
fullPermissionNameOffset = 0;
}
if (userAccessLevel.charAt(userAccessLevel.length() - 1) == '*') {
return userAccessLevel.regionMatches(true, userAccessLevelOffset, fullPermissionName, fullPermissionNameOffset, userAccessLevelLength - userAccessLevelOffset - 1);
} else {
return userAccessLevel.regionMatches(true, userAccessLevelOffset, fullPermissionName, fullPermissionNameOffset,
Math.max(userAccessLevelLength - userAccessLevelOffset, fullPermissionName.length() - fullPermissionNameOffset));
}
}
PermissionCheckResult.Type result = PermissionCheckResult.Type.FOUND;
int userAccessLevelOffset = 0;
if (userAccessLevel.charAt(0) == '+') {
userAccessLevelOffset = 1;
result = PermissionCheckResult.Type.EXCEPTION;
} else if (userAccessLevel.charAt(0) == '-') {
userAccessLevelOffset = 1;
result = PermissionCheckResult.Type.NEGATION;
}
if ("*".regionMatches(0, userAccessLevel, userAccessLevelOffset, userAccessLevelLength - userAccessLevelOffset)) {
return result;
}
int fullPermissionNameOffset;
if (fullPermissionName.charAt(0) == '+' || fullPermissionName.charAt(0) == '-') {
fullPermissionNameOffset = 1;
} else {
fullPermissionNameOffset = 0;
}
if (userAccessLevel.charAt(userAccessLevel.length() - 1) == '*') {
return userAccessLevel.regionMatches(true, userAccessLevelOffset, fullPermissionName, fullPermissionNameOffset, userAccessLevelLength - userAccessLevelOffset - 1) ?
result : PermissionCheckResult.Type.NOTFOUND;
} else {
return userAccessLevel.regionMatches(true, userAccessLevelOffset, fullPermissionName, fullPermissionNameOffset,
Math.max(userAccessLevelLength - userAccessLevelOffset, fullPermissionName.length() - fullPermissionNameOffset)) ?
result : PermissionCheckResult.Type.NOTFOUND;
}
}
/**
* Returns a list of all groups.