2018-03-24 15:41:45 +00:00
|
|
|
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;
|
|
|
|
|
2018-03-25 01:41:51 +00:00
|
|
|
public class PlayerVerification extends FreedomService
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
|
|
|
|
@Getter
|
|
|
|
public final Map<String, VPlayer> dataMap = Maps.newHashMap(); // username, data
|
|
|
|
private File configFolder;
|
|
|
|
|
2018-03-25 01:41:51 +00:00
|
|
|
public PlayerVerification(TotalFreedomMod plugin)
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
super(plugin);
|
|
|
|
this.configFolder = new File(plugin.getDataFolder(), "playerverification");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-25 01:41:51 +00:00
|
|
|
protected void onStart()
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
dataMap.clear();
|
|
|
|
}
|
|
|
|
|
2018-03-25 01:41:51 +00:00
|
|
|
public void save(VPlayer data)
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
YamlConfig config = getConfig(data);
|
|
|
|
data.saveTo(config);
|
|
|
|
config.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-25 01:41:51 +00:00
|
|
|
protected void onStop()
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
//save all (should be saved in theory but to be safe)
|
2018-03-25 01:41:51 +00:00
|
|
|
for (VPlayer player : dataMap.values())
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
save(player);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-25 01:41:51 +00:00
|
|
|
public Boolean isPlayerImpostor(Player player)
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
VPlayer vplayer = getVerificationPlayer(player.getName());
|
|
|
|
return !plugin.al.isAdmin(player) && vplayer != null && (vplayer.getForumVerificationEnabled() || vplayer.getDiscordVerificationEnabled()) && !vplayer.getIPs().contains(Ips.getIp(player));
|
|
|
|
}
|
|
|
|
|
2018-03-25 01:41:51 +00:00
|
|
|
public void verifyPlayer(Player player)
|
|
|
|
{
|
|
|
|
if (!isPlayerImpostor(player))
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
VPlayer vplayer = getVerificationPlayer(player.getName());
|
|
|
|
vplayer.addIp(Ips.getIp(player));
|
|
|
|
saveVerificationData(vplayer);
|
|
|
|
}
|
|
|
|
|
2018-03-25 01:41:51 +00:00
|
|
|
public void saveVerificationData(VPlayer player)
|
|
|
|
{
|
|
|
|
if (dataMap.containsKey(player.getName()))
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
dataMap.remove(player.getName());
|
|
|
|
}
|
|
|
|
dataMap.put(player.getName(), player);
|
|
|
|
save(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
//may not return null
|
2018-03-25 01:41:51 +00:00
|
|
|
public VPlayer getVerificationPlayer(Player player)
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
VPlayer data = getVerificationPlayer(player.getName());
|
2018-03-25 01:41:51 +00:00
|
|
|
if (data != null)
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
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
|
2018-03-25 01:41:51 +00:00
|
|
|
public VPlayer getVerificationPlayer(String username)
|
|
|
|
{
|
|
|
|
if (dataMap.containsKey(username))
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
return dataMap.get(username);
|
|
|
|
}
|
|
|
|
VPlayer player = loadData(username);
|
2018-03-25 01:41:51 +00:00
|
|
|
if (player != null)
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
return player;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-03-25 01:41:51 +00:00
|
|
|
public VPlayer loadData(String username)
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
final File configFile = getConfigFile(username);
|
2018-03-25 01:41:51 +00:00
|
|
|
if (!configFile.exists())
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final VPlayer data = new VPlayer(username);
|
|
|
|
data.loadFrom(getConfig(data));
|
|
|
|
|
2018-03-25 01:41:51 +00:00
|
|
|
if (!data.isValid())
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
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
|
2018-03-25 01:41:51 +00:00
|
|
|
for (Player onlinePlayer : Bukkit.getOnlinePlayers())
|
|
|
|
{
|
|
|
|
if (onlinePlayer.getName().equals(username))
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
dataMap.put(username, data);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2018-03-25 01:41:51 +00:00
|
|
|
public void removeEntry(String username)
|
|
|
|
{
|
|
|
|
if (getVerificationPlayer(username) != null)
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
getConfigFile(username).delete();
|
2018-03-25 01:41:51 +00:00
|
|
|
if (dataMap.containsKey(username))
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
dataMap.remove(username);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
2018-03-25 01:41:51 +00:00
|
|
|
public void onPlayerQuit(PlayerQuitEvent event)
|
|
|
|
{
|
|
|
|
if (dataMap.containsKey(event.getPlayer().getName()))
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
saveVerificationData(dataMap.get(event.getPlayer().getName()));
|
|
|
|
dataMap.remove(event.getPlayer().getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-25 01:41:51 +00:00
|
|
|
protected File getConfigFile(String name)
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
return new File(configFolder, name + ".yml");
|
|
|
|
}
|
|
|
|
|
2018-03-25 01:41:51 +00:00
|
|
|
protected YamlConfig getConfig(VPlayer data)
|
|
|
|
{
|
2018-03-24 15:41:45 +00:00
|
|
|
final YamlConfig config = new YamlConfig(plugin, getConfigFile(data.getName().toLowerCase()), false);
|
|
|
|
config.load();
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
}
|