Resolves TFPatches/TotalFreedomMod#57 (#66)

After a bit of recreating the PlayerVerification system, it will work now.
This commit is contained in:
Nathan Curran 2018-06-02 00:13:40 +10:00 committed by Lemon
parent 45bda95a75
commit ee7dbd56da
6 changed files with 140 additions and 191 deletions

View File

@ -25,19 +25,25 @@ public class Command_linkdiscord extends FreedomCommand
return true; return true;
} }
if (plugin.al.isAdmin(playerSender)) { if (plugin.al.isAdmin(playerSender))
{
Admin admin = plugin.al.getAdmin(playerSender); Admin admin = plugin.al.getAdmin(playerSender);
if (admin.getDiscordID() != null) { if (admin.getDiscordID() != null)
{
msg("Your minecraft account is already linked to a discord account.", ChatColor.RED); msg("Your minecraft account is already linked to a discord account.", ChatColor.RED);
return true; return true;
} }
if (Discord.LINK_CODES.containsValue(admin)) { if (Discord.LINK_CODES.containsValue(admin))
{
msg("Your linking code is " + ChatColor.GREEN + Discord.getCodeForAdmin(admin), ChatColor.AQUA); msg("Your linking code is " + ChatColor.GREEN + Discord.getCodeForAdmin(admin), ChatColor.AQUA);
} else { }
else
{
String code = ""; String code = "";
Random random = new Random(); Random random = new Random();
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++)
{
code += random.nextInt(10); code += random.nextInt(10);
} }
Discord.LINK_CODES.put(code, admin); Discord.LINK_CODES.put(code, admin);
@ -47,17 +53,22 @@ public class Command_linkdiscord extends FreedomCommand
else else
{ {
VPlayer data = plugin.pv.getVerificationPlayer(playerSender); VPlayer data = plugin.pv.getVerificationPlayer(playerSender);
if (data.getDiscordID() != null) { if (data.getDiscordId() != null)
{
msg("Your minecraft account is already linked to a discord account.", ChatColor.RED); msg("Your minecraft account is already linked to a discord account.", ChatColor.RED);
return true; return true;
} }
if (Discord.PLAYER_LINK_CODES.containsValue(data)) { if (Discord.PLAYER_LINK_CODES.containsValue(data))
{
msg("Your linking code is " + ChatColor.GREEN + Discord.getCodeForPlayer(data), ChatColor.AQUA); msg("Your linking code is " + ChatColor.GREEN + Discord.getCodeForPlayer(data), ChatColor.AQUA);
} else { }
else
{
String code = ""; String code = "";
Random random = new Random(); Random random = new Random();
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++)
{
code += random.nextInt(10); code += random.nextInt(10);
} }
Discord.PLAYER_LINK_CODES.put(code, data); Discord.PLAYER_LINK_CODES.put(code, data);

View File

@ -22,7 +22,7 @@ public class Command_playerverify extends FreedomCommand
{ {
VPlayer data = plugin.pv.getVerificationPlayer(playerSender); VPlayer data = plugin.pv.getVerificationPlayer(playerSender);
int cleared = 0; int cleared = 0;
for (String ip : data.getIPs()) for (String ip : data.getIps())
{ {
if (!ip.equals(Ips.getIp(playerSender))) if (!ip.equals(Ips.getIp(playerSender)))
{ {
@ -30,6 +30,7 @@ public class Command_playerverify extends FreedomCommand
cleared++; cleared++;
} }
} }
msg("Cleared all IP's except your current IP \"" + Ips.getIp(playerSender) + "\""); msg("Cleared all IP's except your current IP \"" + Ips.getIp(playerSender) + "\"");
msg("Cleared " + cleared + " IP's."); msg("Cleared " + cleared + " IP's.");
plugin.pv.saveVerificationData(data); plugin.pv.saveVerificationData(data);
@ -59,12 +60,12 @@ public class Command_playerverify extends FreedomCommand
return true; return true;
} }
VPlayer data = plugin.pv.getVerificationPlayer(playerSender); VPlayer data = plugin.pv.getVerificationPlayer(playerSender);
if (data.isDiscordVerificationEnabled()) if (data.getDiscordEnabled())
{ {
msg("Discord verification is already enabled for you.", ChatColor.RED); msg("Discord verification is already enabled for you.", ChatColor.RED);
return true; return true;
} }
data.setDiscordVerificationEnabled(true); data.setDiscordEnabled(true);
plugin.pv.saveVerificationData(data); plugin.pv.saveVerificationData(data);
msg("Enabled discord verification. Please type /linkdiscord to link a discord account.", ChatColor.GREEN); msg("Enabled discord verification. Please type /linkdiscord to link a discord account.", ChatColor.GREEN);
return true; return true;
@ -79,12 +80,12 @@ public class Command_playerverify extends FreedomCommand
{ {
case "discord": case "discord":
VPlayer data = plugin.pv.getVerificationPlayer(playerSender); VPlayer data = plugin.pv.getVerificationPlayer(playerSender);
if (!data.isDiscordVerificationEnabled()) if (!data.getDiscordEnabled())
{ {
msg("Discord verification is already disabled for you.", ChatColor.RED); msg("Discord verification is already disabled for you.", ChatColor.RED);
return true; return true;
} }
data.setDiscordVerificationEnabled(false); data.setDiscordEnabled(false);
plugin.pv.saveVerificationData(data); plugin.pv.saveVerificationData(data);
msg("Disabled discord verification.", ChatColor.GREEN); msg("Disabled discord verification.", ChatColor.GREEN);
return true; return true;
@ -99,10 +100,10 @@ public class Command_playerverify extends FreedomCommand
{ {
case "discord": case "discord":
VPlayer data = plugin.pv.getVerificationPlayer(playerSender); VPlayer data = plugin.pv.getVerificationPlayer(playerSender);
boolean enabled = data.isDiscordVerificationEnabled(); boolean enabled = data.getDiscordEnabled();
boolean specified = data.getDiscordID() != null; boolean specified = data.getDiscordId() != null;
msg(ChatColor.GRAY + "Discord Verification Enabled: " + (enabled ? ChatColor.GREEN + "true" : ChatColor.RED + "false")); msg(ChatColor.GRAY + "Discord Verification Enabled: " + (enabled ? ChatColor.GREEN + "true" : ChatColor.RED + "false"));
msg(ChatColor.GRAY + "Discord ID: " + (specified ? ChatColor.GREEN + data.getDiscordID() : ChatColor.RED + "not set")); msg(ChatColor.GRAY + "Discord ID: " + (specified ? ChatColor.GREEN + data.getDiscordId() : ChatColor.RED + "not set"));
return true; return true;
case "forum": case "forum":
msg("TODO. Forum verification will be enabled in a later update."); msg("TODO. Forum verification will be enabled in a later update.");

View File

@ -12,6 +12,7 @@ import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.Date; import java.util.Date;
import java.util.Random; import java.util.Random;
@ -52,12 +53,12 @@ public class Command_verify extends FreedomCommand
{ {
if (plugin.pv.isPlayerImpostor(playerSender)) if (plugin.pv.isPlayerImpostor(playerSender))
{ {
if (plugin.pv.getVerificationPlayer(playerSender).getDiscordID() == null) if (plugin.pv.getVerificationPlayer(playerSender).getDiscordId() == null)
{ {
msg("You do not have a discord account linked to your minecraft account, please verify the manual way.", ChatColor.RED); msg("You do not have a discord account linked to your minecraft account, please verify the manual way.", ChatColor.RED);
return true; return true;
} }
discordId = plugin.pv.getVerificationPlayer(playerSender).getDiscordID(); discordId = plugin.pv.getVerificationPlayer(playerSender).getDiscordId();
} }
} }
@ -109,7 +110,7 @@ public class Command_verify extends FreedomCommand
if (masterBuilder != null) if (masterBuilder != null)
{ {
if (playerSender!= null) if (playerSender != null)
{ {
masterBuilder.setName(playerSender.getName()); masterBuilder.setName(playerSender.getName());
masterBuilder.addIp(Ips.getIp(playerSender)); masterBuilder.addIp(Ips.getIp(playerSender));

View File

@ -28,7 +28,7 @@ public class MessageListener extends ListenerAdapter
if (Discord.PLAYER_LINK_CODES.get(code) != null) if (Discord.PLAYER_LINK_CODES.get(code) != null)
{ {
VPlayer player = Discord.PLAYER_LINK_CODES.get(code); VPlayer player = Discord.PLAYER_LINK_CODES.get(code);
player.setDiscordID(event.getMessage().getAuthor().getId()); player.setDiscordId(event.getMessage().getAuthor().getId());
TotalFreedomMod.plugin().pv.saveVerificationData(player); TotalFreedomMod.plugin().pv.saveVerificationData(player);
Discord.PLAYER_LINK_CODES.remove(code); Discord.PLAYER_LINK_CODES.remove(code);

View File

@ -21,11 +21,13 @@ public class PlayerVerification extends FreedomService
@Getter @Getter
public final Map<String, VPlayer> dataMap = Maps.newHashMap(); // username, data public final Map<String, VPlayer> dataMap = Maps.newHashMap(); // username, data
private File configFolder; @Getter
private final File configFolder;
public PlayerVerification(TotalFreedomMod plugin) public PlayerVerification(TotalFreedomMod plugin)
{ {
super(plugin); super(plugin);
this.configFolder = new File(plugin.getDataFolder(), "playerverification"); this.configFolder = new File(plugin.getDataFolder(), "playerverification");
} }
@ -35,27 +37,18 @@ public class PlayerVerification extends FreedomService
dataMap.clear(); dataMap.clear();
} }
public void save(VPlayer data)
{
YamlConfig config = getConfig(data);
data.saveTo(config);
config.save();
}
@Override @Override
protected void onStop() protected void onStop()
{ {
//save all (should be saved in theory but to be safe) save();
for (VPlayer player : dataMap.values())
{
save(player);
}
} }
public Boolean isPlayerImpostor(Player player) public Boolean isPlayerImpostor(Player player)
{ {
VPlayer vplayer = getVerificationPlayer(player.getName()); VPlayer vPlayer = getVerificationPlayer(player);
return !plugin.al.isAdmin(player) && vplayer != null && (vplayer.getForumVerificationEnabled() || vplayer.getDiscordVerificationEnabled()) && !vplayer.getIPs().contains(Ips.getIp(player)); return !plugin.al.isAdmin(player)
&& (vPlayer.getForumEnabled() || vPlayer.getDiscordEnabled())
&& !vPlayer.getIps().contains(Ips.getIp(player));
} }
public void verifyPlayer(Player player) public void verifyPlayer(Player player)
@ -64,116 +57,124 @@ public class PlayerVerification extends FreedomService
{ {
return; return;
} }
VPlayer vplayer = getVerificationPlayer(player.getName());
vplayer.addIp(Ips.getIp(player)); VPlayer vPlayer = getVerificationPlayer(player);
saveVerificationData(vplayer); vPlayer.addIp(Ips.getIp(player));
dataMap.put(player.getName(), vPlayer);
YamlConfig config = getConfig(vPlayer);
vPlayer.saveTo(config);
config.save();
} }
public void saveVerificationData(VPlayer player) public void saveVerificationData(VPlayer player)
{ {
if (dataMap.containsKey(player.getName())) YamlConfig config = getConfig(player);
{ player.saveTo(config);
dataMap.remove(player.getName()); config.save();
}
dataMap.put(player.getName(), player); dataMap.put(player.getName(), player);
save(player);
} }
//may not return null public void removeEntry(String name)
{
if (getVerificationPlayer(name) != null)
{
getConfigFile(name).delete();
dataMap.remove(name);
}
}
public void save()
{
for (VPlayer vPlayer : dataMap.values())
{
YamlConfig config = getConfig(vPlayer);
vPlayer.saveTo(config);
config.save();
}
}
// May not return null
public VPlayer getVerificationPlayer(Player player) public VPlayer getVerificationPlayer(Player player)
{ {
VPlayer data = getVerificationPlayer(player.getName()); // Check for existing data
if (data != null) VPlayer vPlayer = dataMap.get(player.getName());
if (vPlayer != null)
{ {
return data; return vPlayer;
} }
// Create new entry.
FLog.info("Creating new player verification entry for " + player.getName());
// Create new player data // Load data
VPlayer newEntry = new VPlayer(player.getName()); vPlayer = getVerificationPlayer(player.getName());
newEntry.addIp(Ips.getIp(player));
saveVerificationData(newEntry); // Create new data if nonexistent
return newEntry; if (vPlayer == null)
{
FLog.info("Creating new player verification entry for " + player.getName());
// Create new player
vPlayer = new VPlayer(player);
vPlayer.addIp(Ips.getIp(player));
// Store player
dataMap.put(player.getName(), vPlayer);
// Save player
YamlConfig config = getConfig(vPlayer);
vPlayer.saveTo(config);
config.save();
}
return vPlayer;
} }
//may return null // May return null
public VPlayer getVerificationPlayer(String username) public VPlayer getVerificationPlayer(String username)
{ {
if (dataMap.containsKey(username)) username = username.toLowerCase();
{
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); final File configFile = getConfigFile(username);
if (!configFile.exists()) if (!configFile.exists())
{ {
return null; return null;
} }
final VPlayer data = new VPlayer(username); final VPlayer vPlayer = new VPlayer(username);
data.loadFrom(getConfig(data)); vPlayer.loadFrom(getConfig(vPlayer));
if (!data.isValid()) if (!vPlayer.isValid())
{ {
FLog.warning("Could not load player verification entry: " + username + ". Entry is not valid!"); FLog.warning("Could not load player verification entry: " + username + ". Entry is not valid!");
configFile.delete(); configFile.delete();
return null; return null;
} }
// Only store data in map if the player is online // Only store data in map if the player is online
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) for (Player players : Bukkit.getOnlinePlayers())
{ {
if (onlinePlayer.getName().equals(username)) if (players.getName().equals(username))
{ {
dataMap.put(username, data); dataMap.put(username, vPlayer);
return data; return vPlayer;
} }
} }
return data;
}
public void removeEntry(String username) return vPlayer;
{
if (getVerificationPlayer(username) != null)
{
getConfigFile(username).delete();
if (dataMap.containsKey(username))
{
dataMap.remove(username);
}
}
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerQuit(PlayerQuitEvent event) public void onPlayerQuit(PlayerQuitEvent event)
{ {
if (dataMap.containsKey(event.getPlayer().getName())) dataMap.remove(event.getPlayer().getName());
{
saveVerificationData(dataMap.get(event.getPlayer().getName()));
dataMap.remove(event.getPlayer().getName());
}
} }
protected File getConfigFile(String name) protected File getConfigFile(String name)
{ {
return new File(configFolder, name + ".yml"); return new File(getConfigFolder(), name + ".yml");
} }
protected YamlConfig getConfig(VPlayer data) protected YamlConfig getConfig(VPlayer player)
{ {
final YamlConfig config = new YamlConfig(plugin, getConfigFile(data.getName().toLowerCase()), false); final YamlConfig config = new YamlConfig(plugin, getConfigFile(player.getName().toLowerCase()), false);
config.load(); config.load();
return config; return config;
} }

View File

@ -6,11 +6,11 @@ import lombok.Setter;
import net.pravian.aero.base.ConfigLoadable; import net.pravian.aero.base.ConfigLoadable;
import net.pravian.aero.base.ConfigSavable; import net.pravian.aero.base.ConfigSavable;
import net.pravian.aero.base.Validatable; import net.pravian.aero.base.Validatable;
import net.pravian.aero.util.Ips;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.Collections;
import java.util.List; import java.util.List;
public class VPlayer implements ConfigLoadable, ConfigSavable, Validatable public class VPlayer implements ConfigLoadable, ConfigSavable, Validatable
@ -22,135 +22,70 @@ public class VPlayer implements ConfigLoadable, ConfigSavable, Validatable
private String name; private String name;
@Getter @Getter
@Setter @Setter
private String discordId = null;
@Getter
@Setter
private String forumUsername = null; private String forumUsername = null;
@Getter @Getter
@Setter @Setter
private String discordID = null; private Boolean discordEnabled = false;
@Getter @Getter
@Setter @Setter
private Boolean discordVerificationEnabled = false; private Boolean forumEnabled = false;
@Getter
@Setter
private Boolean forumVerificationEnabled = false;
public VPlayer(String name)
public VPlayer(String username)
{ {
this.name = username; this.name = name;
} }
public void loadFrom(Player player) public VPlayer(Player player)
{ {
name = player.getName(); this(player.getName());
ips.clear();
ips.add(Ips.getIp(player));
} }
@Override @Override
public void loadFrom(ConfigurationSection cs) public void loadFrom(ConfigurationSection cs)
{ {
name = cs.getString("username", null); name = cs.getString("username", name);
ips.clear(); ips.clear();
ips.addAll(cs.getStringList("ips")); ips.addAll(cs.getStringList("ips"));
forumUsername = cs.getString("forum_username", null); discordId = cs.getString("discordId", null);
discordID = cs.getString("discord_id", null); forumUsername = cs.getString("forumUsername", null);
discordVerificationEnabled = cs.getBoolean("discord_verification_enabled", false); discordEnabled = cs.getBoolean("discordEnabled", false);
forumVerificationEnabled = cs.getBoolean("forum_verification_enabled", false); forumEnabled = cs.getBoolean("forumEnabled", false);
} }
@Override @Override
public void saveTo(ConfigurationSection cs) public void saveTo(ConfigurationSection cs)
{ {
Validate.isTrue(isValid(), "Could not save player veirfication entry: " + name + ". Entry not valid!"); Validate.isTrue(isValid(), "Could not save player verification entry: " + name + ". Entry not valid!");
cs.set("username", name); cs.set("name", name);
cs.set("forum_username", forumUsername); cs.set("discordId", discordId);
cs.set("discord_id", discordID); cs.set("forumUsername", forumUsername);
cs.set("discordEnabled", discordEnabled);
cs.set("forumEnabled", forumEnabled);
cs.set("ips", Lists.newArrayList(ips)); cs.set("ips", Lists.newArrayList(ips));
cs.set("discord_verification_enabled", discordVerificationEnabled);
cs.set("forum_verification_enabled", forumVerificationEnabled);
} }
// Util IP methods public List<String> getIps()
public void addIp(String ip)
{ {
if (!ips.contains(ip)) return Collections.unmodifiableList(ips);
{
ips.add(ip);
}
} }
public void addIps(List<String> ips) public boolean addIp(String ip)
{ {
for (String ip : ips) return ips.contains(ip) ? false : ips.add(ip);
{
addIp(ip);
}
} }
public boolean removeIp(String ip)
public void removeIp(String ip)
{ {
if (ips.contains(ip)) return ips.remove(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 @Override
public boolean isValid() public boolean isValid()
{ {
return name != null return name != null
&& !ips.isEmpty(); && !ips.isEmpty();
} }
} }