2015-10-19 17:43:46 +00:00
|
|
|
package me.totalfreedom.totalfreedommod.admin;
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.Setter;
|
2015-11-15 23:32:04 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.rank.PlayerRank;
|
2015-10-19 17:43:46 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FUtil;
|
|
|
|
import net.pravian.aero.base.ConfigLoadable;
|
|
|
|
import net.pravian.aero.base.ConfigSavable;
|
|
|
|
import net.pravian.aero.base.Validatable;
|
|
|
|
import net.pravian.aero.util.Ips;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.commons.lang3.Validate;
|
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
|
|
|
public class Admin implements ConfigLoadable, ConfigSavable, Validatable
|
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
@Getter
|
|
|
|
private String configKey;
|
|
|
|
@Getter
|
|
|
|
@Setter
|
|
|
|
private String name;
|
|
|
|
@Getter
|
|
|
|
@Setter
|
|
|
|
private boolean activated = true;
|
|
|
|
@Getter
|
|
|
|
@Setter
|
|
|
|
private PlayerRank rank = PlayerRank.SUPER_ADMIN;
|
|
|
|
@Getter
|
|
|
|
private final List<String> ips = Lists.newArrayList();
|
|
|
|
@Getter
|
|
|
|
@Setter
|
|
|
|
private Date lastLogin = new Date();
|
|
|
|
@Getter
|
|
|
|
@Setter
|
|
|
|
private String loginMessage = null;
|
|
|
|
|
|
|
|
public Admin(Player player)
|
|
|
|
{
|
|
|
|
this.configKey = player.getName().toLowerCase();
|
|
|
|
this.name = player.getName();
|
|
|
|
this.ips.add(Ips.getIp(player));
|
|
|
|
}
|
|
|
|
|
|
|
|
public Admin(String configKey)
|
|
|
|
{
|
|
|
|
this.configKey = configKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString()
|
|
|
|
{
|
|
|
|
final StringBuilder output = new StringBuilder();
|
|
|
|
|
|
|
|
output.append("Admin: ").append(name).append("\n")
|
|
|
|
.append("- Last Login Name: ").append(name).append("\n")
|
|
|
|
.append("- IPs: ").append(StringUtils.join(ips, ", ")).append("\n")
|
|
|
|
.append("- Last Login: ").append(FUtil.dateToString(lastLogin)).append("\n")
|
|
|
|
.append("- Custom Login Message: ").append(loginMessage).append("\n")
|
|
|
|
.append("- Rank: ").append(rank.getName()).append("\n")
|
|
|
|
.append("- Is Activated: ").append(activated);
|
|
|
|
|
|
|
|
return output.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadFrom(Player player)
|
|
|
|
{
|
|
|
|
configKey = player.getName().toLowerCase();
|
|
|
|
name = player.getName();
|
|
|
|
ips.clear();
|
|
|
|
ips.add(Ips.getIp(player));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void loadFrom(ConfigurationSection cs)
|
|
|
|
{
|
|
|
|
name = cs.getString("username", configKey);
|
|
|
|
activated = cs.getBoolean("active", true);
|
|
|
|
rank = PlayerRank.forString(cs.getString("rank"));
|
|
|
|
ips.clear();
|
|
|
|
ips.addAll(cs.getStringList("ips"));
|
|
|
|
lastLogin = FUtil.stringToDate(cs.getString("last_login"));
|
|
|
|
loginMessage = cs.getString("login_message", null);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void saveTo(ConfigurationSection cs)
|
|
|
|
{
|
|
|
|
Validate.isTrue(isValid(), "Could not save admin entry: " + name + ". Entry not valid!");
|
|
|
|
cs.set("username", name);
|
|
|
|
cs.set("active", activated);
|
|
|
|
cs.set("rank", rank.toString());
|
2015-11-15 23:32:04 +00:00
|
|
|
cs.set("ips", Lists.newArrayList(ips));
|
2015-10-19 17:43:46 +00:00
|
|
|
cs.set("last_login", lastLogin);
|
|
|
|
cs.set("login_message", null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isMinimum(PlayerRank pRank)
|
|
|
|
{
|
|
|
|
return rank.ordinal() >= pRank.ordinal();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Util IP methods
|
|
|
|
public void addIp(String ip)
|
|
|
|
{
|
|
|
|
if (!ips.contains(ip))
|
|
|
|
{
|
|
|
|
ips.add(ip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addIps(List<String> ips)
|
|
|
|
{
|
|
|
|
for (String ip : ips)
|
|
|
|
{
|
|
|
|
addIp(ip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeIp(String ip)
|
|
|
|
{
|
|
|
|
if (ips.contains(ip))
|
|
|
|
{
|
|
|
|
ips.remove(ip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearIPs()
|
|
|
|
{
|
|
|
|
ips.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isValid()
|
|
|
|
{
|
|
|
|
return configKey != null
|
|
|
|
&& name != null
|
|
|
|
&& rank != null
|
|
|
|
&& !ips.isEmpty()
|
|
|
|
&& lastLogin != null;
|
|
|
|
}
|
|
|
|
}
|