mirror of
https://github.com/AtlasMediaGroup/TotalFreedomMod.git
synced 2025-06-11 21:43:54 +00:00
[UNTESTED] implement player verification system
This commit is contained in:
@ -0,0 +1,152 @@
|
||||
package me.totalfreedom.totalfreedommod.playerverification;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import lombok.Getter;
|
||||
import me.totalfreedom.totalfreedommod.FreedomService;
|
||||
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
||||
import me.totalfreedom.totalfreedommod.util.FLog;
|
||||
import net.pravian.aero.config.YamlConfig;
|
||||
import net.pravian.aero.util.Ips;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
public class PlayerVerification extends FreedomService {
|
||||
|
||||
@Getter
|
||||
public final Map<String, VPlayer> dataMap = Maps.newHashMap(); // username, data
|
||||
private File configFolder;
|
||||
|
||||
public PlayerVerification(TotalFreedomMod plugin) {
|
||||
super(plugin);
|
||||
this.configFolder = new File(plugin.getDataFolder(), "playerverification");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
dataMap.clear();
|
||||
}
|
||||
|
||||
public void save(VPlayer data) {
|
||||
YamlConfig config = getConfig(data);
|
||||
data.saveTo(config);
|
||||
config.save();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
//save all (should be saved in theory but to be safe)
|
||||
for (VPlayer player : dataMap.values()) {
|
||||
save(player);
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean isPlayerImpostor(Player player) {
|
||||
VPlayer vplayer = getVerificationPlayer(player.getName());
|
||||
return !plugin.al.isAdmin(player) && vplayer != null && (vplayer.getForumVerificationEnabled() || vplayer.getDiscordVerificationEnabled()) && !vplayer.getIPs().contains(Ips.getIp(player));
|
||||
}
|
||||
|
||||
public void verifyPlayer(Player player) {
|
||||
if (!isPlayerImpostor(player)) {
|
||||
return;
|
||||
}
|
||||
VPlayer vplayer = getVerificationPlayer(player.getName());
|
||||
vplayer.addIp(Ips.getIp(player));
|
||||
saveVerificationData(vplayer);
|
||||
}
|
||||
|
||||
public void saveVerificationData(VPlayer player) {
|
||||
if (dataMap.containsKey(player.getName())) {
|
||||
dataMap.remove(player.getName());
|
||||
}
|
||||
dataMap.put(player.getName(), player);
|
||||
save(player);
|
||||
}
|
||||
|
||||
//may not return null
|
||||
public VPlayer getVerificationPlayer(Player player) {
|
||||
VPlayer data = getVerificationPlayer(player.getName());
|
||||
if (data != null) {
|
||||
return data;
|
||||
}
|
||||
// Create new entry.
|
||||
FLog.info("Creating new player verification entry for " + player.getName());
|
||||
|
||||
// Create new player data
|
||||
VPlayer newEntry = new VPlayer(player.getName());
|
||||
newEntry.addIp(Ips.getIp(player));
|
||||
saveVerificationData(newEntry);
|
||||
return newEntry;
|
||||
}
|
||||
|
||||
//may return null
|
||||
public VPlayer getVerificationPlayer(String username) {
|
||||
if (dataMap.containsKey(username)) {
|
||||
return dataMap.get(username);
|
||||
}
|
||||
VPlayer player = loadData(username);
|
||||
if (player != null) {
|
||||
return player;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public VPlayer loadData(String username) {
|
||||
final File configFile = getConfigFile(username);
|
||||
if (!configFile.exists()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final VPlayer data = new VPlayer(username);
|
||||
data.loadFrom(getConfig(data));
|
||||
|
||||
if (!data.isValid()) {
|
||||
FLog.warning("Could not load player verification entry: " + username + ". Entry is not valid!");
|
||||
configFile.delete();
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
// Only store data in map if the player is online
|
||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||
if (onlinePlayer.getName().equals(username)) {
|
||||
dataMap.put(username, data);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public void removeEntry(String username) {
|
||||
if (getVerificationPlayer(username) != null) {
|
||||
getConfigFile(username).delete();
|
||||
if (dataMap.containsKey(username)) {
|
||||
dataMap.remove(username);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
if (dataMap.containsKey(event.getPlayer().getName())) {
|
||||
saveVerificationData(dataMap.get(event.getPlayer().getName()));
|
||||
dataMap.remove(event.getPlayer().getName());
|
||||
}
|
||||
}
|
||||
|
||||
protected File getConfigFile(String name) {
|
||||
return new File(configFolder, name + ".yml");
|
||||
}
|
||||
|
||||
protected YamlConfig getConfig(VPlayer data) {
|
||||
final YamlConfig config = new YamlConfig(plugin, getConfigFile(data.getName().toLowerCase()), false);
|
||||
config.load();
|
||||
return config;
|
||||
}
|
||||
}
|
@ -0,0 +1,134 @@
|
||||
package me.totalfreedom.totalfreedommod.playerverification;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
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.Validate;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class VPlayer implements ConfigLoadable, ConfigSavable, Validatable {
|
||||
|
||||
private final List<String> ips = Lists.newArrayList();
|
||||
@Getter
|
||||
@Setter
|
||||
private String name;
|
||||
@Getter
|
||||
@Setter
|
||||
private String forumUsername = null;
|
||||
@Getter
|
||||
@Setter
|
||||
private String discordID = null;
|
||||
@Getter
|
||||
@Setter
|
||||
private Boolean discordVerificationEnabled = false;
|
||||
@Getter
|
||||
@Setter
|
||||
private Boolean forumVerificationEnabled = false;
|
||||
|
||||
|
||||
public VPlayer(String username) {
|
||||
this.name = username;
|
||||
}
|
||||
|
||||
public void loadFrom(Player player) {
|
||||
name = player.getName();
|
||||
ips.clear();
|
||||
ips.add(Ips.getIp(player));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadFrom(ConfigurationSection cs) {
|
||||
name = cs.getString("username", null);
|
||||
ips.clear();
|
||||
ips.addAll(cs.getStringList("ips"));
|
||||
forumUsername = cs.getString("forum_username", null);
|
||||
discordID = cs.getString("discord_id", null);
|
||||
discordVerificationEnabled = cs.getBoolean("discord_verification_enabled", false);
|
||||
forumVerificationEnabled = cs.getBoolean("forum_verification_enabled", false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveTo(ConfigurationSection cs) {
|
||||
Validate.isTrue(isValid(), "Could not save player veirfication entry: " + name + ". Entry not valid!");
|
||||
cs.set("username", name);
|
||||
cs.set("forum_username", forumUsername);
|
||||
cs.set("discord_id", discordID);
|
||||
cs.set("ips", Lists.newArrayList(ips));
|
||||
cs.set("discord_verification_enabled", discordVerificationEnabled);
|
||||
cs.set("forum_verification_enabled", forumVerificationEnabled);
|
||||
}
|
||||
|
||||
// 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 List<String> getIPs() {
|
||||
return ips;
|
||||
}
|
||||
|
||||
public void clearIPs() {
|
||||
ips.clear();
|
||||
}
|
||||
|
||||
public Boolean isDiscordVerificationEnabled() {
|
||||
return discordVerificationEnabled;
|
||||
}
|
||||
|
||||
public Boolean isForumVerificationEnabled() {
|
||||
return forumVerificationEnabled;
|
||||
}
|
||||
|
||||
public void setDiscordVerificationEnabled(boolean enabled) {
|
||||
this.discordVerificationEnabled = enabled;
|
||||
}
|
||||
|
||||
public void setForumVerificationEnabled(boolean enabled) {
|
||||
this.forumVerificationEnabled = enabled;
|
||||
}
|
||||
|
||||
public String getDiscordID() {
|
||||
return discordID;
|
||||
}
|
||||
|
||||
public void setDiscordID(String discordID) {
|
||||
this.discordID = discordID;
|
||||
}
|
||||
|
||||
public String getForumUsername() {
|
||||
return forumUsername;
|
||||
}
|
||||
|
||||
public void setForumUsername(String forumUsername) {
|
||||
this.forumUsername = forumUsername;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return name != null
|
||||
&& !ips.isEmpty();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user