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;
|
2017-08-01 19:59:24 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.LogViewer.LogsRegistrationMode;
|
|
|
|
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
2016-03-06 15:56:15 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.rank.Rank;
|
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
|
2016-03-06 15:56:15 +00:00
|
|
|
private boolean active = true;
|
2015-10-19 17:43:46 +00:00
|
|
|
@Getter
|
|
|
|
@Setter
|
2016-03-06 15:56:15 +00:00
|
|
|
private Rank rank = Rank.SUPER_ADMIN;
|
2015-10-19 17:43:46 +00:00
|
|
|
@Getter
|
|
|
|
private final List<String> ips = Lists.newArrayList();
|
|
|
|
@Getter
|
|
|
|
@Setter
|
|
|
|
private Date lastLogin = new Date();
|
|
|
|
@Getter
|
|
|
|
@Setter
|
|
|
|
private String loginMessage = null;
|
2017-12-29 18:12:47 +00:00
|
|
|
@Getter
|
|
|
|
@Setter
|
|
|
|
private String discordID = null;
|
2018-02-22 01:22:13 +00:00
|
|
|
@Getter
|
|
|
|
@Setter
|
|
|
|
private String tag = null;
|
2018-04-19 14:29:19 +00:00
|
|
|
@Getter
|
|
|
|
@Setter
|
|
|
|
private Boolean commandSpy = false;
|
|
|
|
@Getter
|
|
|
|
@Setter
|
2018-05-22 23:01:24 +00:00
|
|
|
private Boolean potionSpy = false;
|
|
|
|
@Getter
|
|
|
|
@Setter
|
2018-04-19 14:29:19 +00:00
|
|
|
private Boolean oldAdminMode = false;
|
2015-10-19 17:43:46 +00:00
|
|
|
|
2017-10-13 18:35:11 +00:00
|
|
|
public static final String CONFIG_FILENAME = "admins.yml";
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
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("- 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")
|
2017-12-29 18:12:47 +00:00
|
|
|
.append("- Is Active: ").append(active).append("\n")
|
2018-02-22 01:22:13 +00:00
|
|
|
.append("- Discord ID: ").append(discordID).append("\n")
|
|
|
|
.append("- Tag: ").append(tag);
|
2015-10-19 17:43:46 +00:00
|
|
|
|
|
|
|
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);
|
2016-03-06 15:56:15 +00:00
|
|
|
active = cs.getBoolean("active", true);
|
|
|
|
rank = Rank.findRank(cs.getString("rank"));
|
2015-10-19 17:43:46 +00:00
|
|
|
ips.clear();
|
|
|
|
ips.addAll(cs.getStringList("ips"));
|
|
|
|
lastLogin = FUtil.stringToDate(cs.getString("last_login"));
|
|
|
|
loginMessage = cs.getString("login_message", null);
|
2017-12-29 18:12:47 +00:00
|
|
|
discordID = cs.getString("discord_id", null);
|
2018-02-22 01:22:13 +00:00
|
|
|
tag = cs.getString("tag", null);
|
2018-04-19 14:29:19 +00:00
|
|
|
commandSpy = cs.getBoolean("command_spy", false);
|
2018-05-22 23:01:24 +00:00
|
|
|
potionSpy = cs.getBoolean("potion_spy", false);
|
2018-04-19 14:29:19 +00:00
|
|
|
oldAdminMode = cs.getBoolean("old_admin_mode", false);
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void saveTo(ConfigurationSection cs)
|
|
|
|
{
|
|
|
|
Validate.isTrue(isValid(), "Could not save admin entry: " + name + ". Entry not valid!");
|
|
|
|
cs.set("username", name);
|
2016-03-06 15:56:15 +00:00
|
|
|
cs.set("active", active);
|
2015-10-19 17:43:46 +00:00
|
|
|
cs.set("rank", rank.toString());
|
2015-11-15 23:32:04 +00:00
|
|
|
cs.set("ips", Lists.newArrayList(ips));
|
2016-03-02 19:28:01 +00:00
|
|
|
cs.set("last_login", FUtil.dateToString(lastLogin));
|
|
|
|
cs.set("login_message", loginMessage);
|
2017-12-29 18:12:47 +00:00
|
|
|
cs.set("discord_id", discordID);
|
2018-02-22 01:22:13 +00:00
|
|
|
cs.set("tag", tag);
|
2018-04-19 14:29:19 +00:00
|
|
|
cs.set("command_spy", commandSpy);
|
2018-05-22 23:01:24 +00:00
|
|
|
cs.set("potion_spy", potionSpy);
|
2018-04-19 14:29:19 +00:00
|
|
|
cs.set("old_admin_mode", oldAdminMode);
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
public boolean isAtLeast(Rank pRank)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2016-02-29 20:48:17 +00:00
|
|
|
return rank.isAtLeast(pRank);
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
public boolean hasLoginMessage()
|
|
|
|
{
|
|
|
|
return loginMessage != null && !loginMessage.isEmpty();
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 18:35:11 +00:00
|
|
|
public String getLoginMessage()
|
|
|
|
{
|
|
|
|
return this.loginMessage;
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public void removeIp(String ip)
|
|
|
|
{
|
|
|
|
if (ips.contains(ip))
|
|
|
|
{
|
|
|
|
ips.remove(ip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearIPs()
|
|
|
|
{
|
|
|
|
ips.clear();
|
|
|
|
}
|
|
|
|
|
2017-08-01 19:59:24 +00:00
|
|
|
public void setActive(boolean active)
|
|
|
|
{
|
|
|
|
this.active = active;
|
|
|
|
|
|
|
|
final TotalFreedomMod plugin = TotalFreedomMod.plugin();
|
|
|
|
|
|
|
|
if (!active)
|
|
|
|
{
|
|
|
|
if (getRank().isAtLeast(Rank.TELNET_ADMIN))
|
|
|
|
{
|
|
|
|
if (plugin.btb != null)
|
|
|
|
{
|
|
|
|
plugin.btb.killTelnetSessions(getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin.lv.updateLogsRegistration(null, getName(), LogsRegistrationMode.DELETE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
@Override
|
|
|
|
public boolean isValid()
|
|
|
|
{
|
|
|
|
return configKey != null
|
|
|
|
&& name != null
|
|
|
|
&& rank != null
|
|
|
|
&& !ips.isEmpty()
|
|
|
|
&& lastLogin != null;
|
|
|
|
}
|
2017-10-13 18:35:11 +00:00
|
|
|
|
|
|
|
public boolean isActive()
|
|
|
|
{
|
|
|
|
return this.active;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getConfigKey()
|
|
|
|
{
|
|
|
|
return this.configKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName()
|
|
|
|
{
|
|
|
|
return this.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setName(final String name)
|
|
|
|
{
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Rank getRank()
|
|
|
|
{
|
|
|
|
return this.rank;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRank(final Rank rank)
|
|
|
|
{
|
|
|
|
this.rank = rank;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> getIps()
|
|
|
|
{
|
|
|
|
return this.ips;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Date getLastLogin()
|
|
|
|
{
|
|
|
|
return this.lastLogin;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLastLogin(final Date lastLogin)
|
|
|
|
{
|
|
|
|
this.lastLogin = lastLogin;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLoginMessage(final String loginMessage)
|
|
|
|
{
|
|
|
|
this.loginMessage = loginMessage;
|
|
|
|
}
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|