2016-03-01 03:15:02 +00:00
|
|
|
/*
|
|
|
|
* To change this template, choose Tools | Templates
|
|
|
|
* and open the template in the editor.
|
|
|
|
*/
|
|
|
|
package org.anjocaido.groupmanager.data;
|
|
|
|
|
|
|
|
// import com.sun.org.apache.bcel.internal.generic.AALOAD;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2016-03-01 03:19:54 +00:00
|
|
|
import java.util.Map;
|
2016-03-01 03:15:02 +00:00
|
|
|
|
|
|
|
import org.anjocaido.groupmanager.GroupManager;
|
|
|
|
import org.anjocaido.groupmanager.dataholder.WorldDataHolder;
|
|
|
|
import org.anjocaido.groupmanager.events.GMUserEvent.Action;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
|
|
|
/**
|
2016-03-01 03:19:54 +00:00
|
|
|
*
|
2016-03-01 03:15:02 +00:00
|
|
|
* @author gabrielcouto/ElgarL
|
|
|
|
*/
|
|
|
|
public class User extends DataUnit implements Cloneable {
|
|
|
|
|
|
|
|
private String group = null;
|
|
|
|
private final List<String> subGroups = Collections.synchronizedList(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;
|
|
|
|
|
|
|
|
/**
|
2016-03-01 03:19:54 +00:00
|
|
|
*
|
2016-03-01 03:15:02 +00:00
|
|
|
* @param name
|
|
|
|
*/
|
2016-03-01 03:19:54 +00:00
|
|
|
public User(final WorldDataHolder source, final String name) {
|
2016-03-01 03:15:02 +00:00
|
|
|
|
|
|
|
super(source, name);
|
|
|
|
this.group = source.getDefaultGroup().getName();
|
|
|
|
}
|
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2016-03-01 03:15:02 +00:00
|
|
|
/**
|
2016-03-01 03:19:54 +00:00
|
|
|
*
|
2016-03-01 03:15:02 +00:00
|
|
|
* @return User clone
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public User clone() {
|
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
final User clone = new User(getDataSource(), this.getLastName());
|
2016-03-01 03:15:02 +00:00
|
|
|
clone.group = this.group;
|
|
|
|
|
|
|
|
// Clone all subgroups.
|
|
|
|
clone.subGroups.addAll(this.subGroupListStringCopy());
|
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
for (final String perm : this.getPermissionList()) {
|
2016-03-01 03:15:02 +00:00
|
|
|
clone.addPermission(perm);
|
|
|
|
}
|
|
|
|
// clone.variables = this.variables.clone();
|
|
|
|
// clone.flagAsChanged();
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-01 03:15:02 +00:00
|
|
|
/**
|
|
|
|
* Use this to deliver a user from one WorldDataHolder to another
|
2016-03-01 03:19:54 +00:00
|
|
|
*
|
2016-03-01 03:15:02 +00:00
|
|
|
* @param dataSource
|
|
|
|
* @return null if given dataSource already contains the same user
|
|
|
|
*/
|
2016-03-01 03:19:54 +00:00
|
|
|
public User clone(final WorldDataHolder dataSource) {
|
2016-03-01 03:15:02 +00:00
|
|
|
|
|
|
|
if (dataSource.isUserDeclared(this.getUUID())) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
final User clone = dataSource.createUser(this.getUUID());
|
2016-03-01 03:15:02 +00:00
|
|
|
|
|
|
|
if (dataSource.getGroup(group) == null) {
|
|
|
|
clone.setGroup(dataSource.getDefaultGroup());
|
|
|
|
} else {
|
|
|
|
clone.setGroup(dataSource.getGroup(this.getGroupName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone all subgroups.
|
|
|
|
clone.subGroups.addAll(this.subGroupListStringCopy());
|
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
for (final String perm : this.getPermissionList()) {
|
2016-03-01 03:15:02 +00:00
|
|
|
clone.addPermission(perm);
|
|
|
|
}
|
|
|
|
|
|
|
|
clone.variables = this.variables.clone(this);
|
|
|
|
clone.flagAsChanged();
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
public boolean containsSubGroup(final Group subGroup) {
|
2016-03-01 03:15:02 +00:00
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
return subGroups.contains(subGroup.getName());
|
|
|
|
}
|
2016-03-01 03:15:02 +00:00
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
public Player getBukkitPlayer() {
|
2016-03-01 03:15:02 +00:00
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
if (bukkitPlayer == null) {
|
|
|
|
bukkitPlayer = Bukkit.getPlayer(this.getLastName());
|
2016-03-01 03:15:02 +00:00
|
|
|
}
|
2016-03-01 03:19:54 +00:00
|
|
|
return bukkitPlayer;
|
2016-03-01 03:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
final Group result = getDataSource().getGroup(group);
|
2016-03-01 03:15:02 +00:00
|
|
|
if (result == null) {
|
|
|
|
group = getDataSource().getDefaultGroup().getName();
|
|
|
|
}
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Place holder to let people know to stop using this method.
|
2016-03-01 03:19:54 +00:00
|
|
|
*
|
2016-03-01 03:15:02 +00:00
|
|
|
* @deprecated use {@link #getLastName()} and {@link #getUUID()}.
|
|
|
|
* @return a string containing the players last known name.
|
|
|
|
*/
|
|
|
|
@Deprecated
|
|
|
|
public String getName() {
|
|
|
|
|
|
|
|
return this.getLastName();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2016-03-01 03:15:02 +00:00
|
|
|
/**
|
|
|
|
* @param group
|
|
|
|
* the group to set
|
|
|
|
*/
|
2016-03-01 03:19:54 +00:00
|
|
|
public void setGroup(final Group group) {
|
2016-03-01 03:15:02 +00:00
|
|
|
|
|
|
|
setGroup(group, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param group
|
|
|
|
* the group to set
|
|
|
|
* @param updatePerms
|
|
|
|
* if we are to trigger a superperms update.
|
2016-03-01 03:19:54 +00:00
|
|
|
*
|
2016-03-01 03:15:02 +00:00
|
|
|
*/
|
2016-03-01 03:19:54 +00:00
|
|
|
public void setGroup(Group group, final Boolean updatePerms) {
|
2016-03-01 03:15:02 +00:00
|
|
|
|
|
|
|
if (!this.getDataSource().groupExists(group.getName())) {
|
|
|
|
getDataSource().addGroup(group);
|
|
|
|
}
|
|
|
|
group = getDataSource().getGroup(group.getName());
|
2016-03-01 03:19:54 +00:00
|
|
|
final String oldGroup = this.group;
|
2016-03-01 03:15:02 +00:00
|
|
|
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?
|
2016-03-01 03:19:54 +00:00
|
|
|
final String defaultGroupName = getDataSource().getDefaultGroup().getName();
|
2016-03-01 03:15:02 +00:00
|
|
|
// if we were not in the default group
|
|
|
|
// or we were in the default group and the move is to a different
|
|
|
|
// group.
|
2016-03-01 03:19:54 +00:00
|
|
|
final boolean notify = (!oldGroup.equalsIgnoreCase(defaultGroupName)) || ((oldGroup.equalsIgnoreCase(defaultGroupName)) && (!this.group.equalsIgnoreCase(defaultGroupName)));
|
2016-03-01 03:15:02 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param varList
|
|
|
|
*/
|
|
|
|
public void setVariables(final Map<String, Object> varList) {
|
2016-03-01 03:15:02 +00:00
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
// UserVariables temp = new UserVariables(this, varList);
|
|
|
|
variables.clearVars();
|
|
|
|
for (final String key : varList.keySet()) {
|
|
|
|
variables.addVar(key, varList.get(key));
|
2016-03-01 03:15:02 +00:00
|
|
|
}
|
|
|
|
flagAsChanged();
|
|
|
|
if (GroupManager.isLoaded()) {
|
2016-03-01 03:19:54 +00:00
|
|
|
// if (!GroupManager.BukkitPermissions.isPlayer_join())
|
|
|
|
// GroupManager.BukkitPermissions.updatePlayer(this.getName());
|
|
|
|
GroupManager.getGMEventHandler().callEvent(this, Action.USER_INFO_CHANGED);
|
2016-03-01 03:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<Group> subGroupListCopy() {
|
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
final ArrayList<Group> val = new ArrayList<Group>();
|
2016-03-01 03:15:02 +00:00
|
|
|
synchronized (subGroups) {
|
2016-03-01 03:19:54 +00:00
|
|
|
for (final String gstr : subGroups) {
|
|
|
|
final Group g = getDataSource().getGroup(gstr);
|
2016-03-01 03:15:02 +00:00
|
|
|
if (g == null) {
|
|
|
|
removeSubGroup(g);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
val.add(g);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<String> subGroupListStringCopy() {
|
|
|
|
synchronized (subGroups) {
|
|
|
|
return new ArrayList<String>(subGroups);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
public int subGroupsSize() {
|
2016-03-01 03:15:02 +00:00
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
return subGroups.size();
|
2016-03-01 03:15:02 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 03:19:54 +00:00
|
|
|
public User updatePlayer(final Player player) {
|
2016-03-01 03:15:02 +00:00
|
|
|
|
|
|
|
bukkitPlayer = player;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|