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;
}
if (plugin.al.isAdmin(playerSender)) {
if (plugin.al.isAdmin(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);
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);
} else {
}
else
{
String code = "";
Random random = new Random();
for (int i = 0; i < 5; i++) {
for (int i = 0; i < 5; i++)
{
code += random.nextInt(10);
}
Discord.LINK_CODES.put(code, admin);
@ -47,17 +53,22 @@ public class Command_linkdiscord extends FreedomCommand
else
{
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);
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);
} else {
}
else
{
String code = "";
Random random = new Random();
for (int i = 0; i < 5; i++) {
for (int i = 0; i < 5; i++)
{
code += random.nextInt(10);
}
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);
int cleared = 0;
for (String ip : data.getIPs())
for (String ip : data.getIps())
{
if (!ip.equals(Ips.getIp(playerSender)))
{
@ -30,6 +30,7 @@ public class Command_playerverify extends FreedomCommand
cleared++;
}
}
msg("Cleared all IP's except your current IP \"" + Ips.getIp(playerSender) + "\"");
msg("Cleared " + cleared + " IP's.");
plugin.pv.saveVerificationData(data);
@ -59,12 +60,12 @@ public class Command_playerverify extends FreedomCommand
return true;
}
VPlayer data = plugin.pv.getVerificationPlayer(playerSender);
if (data.isDiscordVerificationEnabled())
if (data.getDiscordEnabled())
{
msg("Discord verification is already enabled for you.", ChatColor.RED);
return true;
}
data.setDiscordVerificationEnabled(true);
data.setDiscordEnabled(true);
plugin.pv.saveVerificationData(data);
msg("Enabled discord verification. Please type /linkdiscord to link a discord account.", ChatColor.GREEN);
return true;
@ -79,12 +80,12 @@ public class Command_playerverify extends FreedomCommand
{
case "discord":
VPlayer data = plugin.pv.getVerificationPlayer(playerSender);
if (!data.isDiscordVerificationEnabled())
if (!data.getDiscordEnabled())
{
msg("Discord verification is already disabled for you.", ChatColor.RED);
return true;
}
data.setDiscordVerificationEnabled(false);
data.setDiscordEnabled(false);
plugin.pv.saveVerificationData(data);
msg("Disabled discord verification.", ChatColor.GREEN);
return true;
@ -99,10 +100,10 @@ public class Command_playerverify extends FreedomCommand
{
case "discord":
VPlayer data = plugin.pv.getVerificationPlayer(playerSender);
boolean enabled = data.isDiscordVerificationEnabled();
boolean specified = data.getDiscordID() != null;
boolean enabled = data.getDiscordEnabled();
boolean specified = data.getDiscordId() != null;
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;
case "forum":
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.CommandSender;
import org.bukkit.entity.Player;
import java.util.Date;
import java.util.Random;
@ -52,12 +53,12 @@ public class Command_verify extends FreedomCommand
{
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);
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 (playerSender!= null)
if (playerSender != null)
{
masterBuilder.setName(playerSender.getName());
masterBuilder.addIp(Ips.getIp(playerSender));

View File

@ -28,7 +28,7 @@ public class MessageListener extends ListenerAdapter
if (Discord.PLAYER_LINK_CODES.get(code) != null)
{
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);
Discord.PLAYER_LINK_CODES.remove(code);

View File

@ -21,11 +21,13 @@ public class PlayerVerification extends FreedomService
@Getter
public final Map<String, VPlayer> dataMap = Maps.newHashMap(); // username, data
private File configFolder;
@Getter
private final File configFolder;
public PlayerVerification(TotalFreedomMod plugin)
{
super(plugin);
this.configFolder = new File(plugin.getDataFolder(), "playerverification");
}
@ -35,27 +37,18 @@ public class PlayerVerification extends FreedomService
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);
}
save();
}
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));
VPlayer vPlayer = getVerificationPlayer(player);
return !plugin.al.isAdmin(player)
&& (vPlayer.getForumEnabled() || vPlayer.getDiscordEnabled())
&& !vPlayer.getIps().contains(Ips.getIp(player));
}
public void verifyPlayer(Player player)
@ -64,116 +57,124 @@ public class PlayerVerification extends FreedomService
{
return;
}
VPlayer vplayer = getVerificationPlayer(player.getName());
vplayer.addIp(Ips.getIp(player));
saveVerificationData(vplayer);
VPlayer vPlayer = getVerificationPlayer(player);
vPlayer.addIp(Ips.getIp(player));
dataMap.put(player.getName(), vPlayer);
YamlConfig config = getConfig(vPlayer);
vPlayer.saveTo(config);
config.save();
}
public void saveVerificationData(VPlayer player)
{
if (dataMap.containsKey(player.getName()))
{
dataMap.remove(player.getName());
}
YamlConfig config = getConfig(player);
player.saveTo(config);
config.save();
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)
{
VPlayer data = getVerificationPlayer(player.getName());
if (data != null)
// Check for existing data
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
VPlayer newEntry = new VPlayer(player.getName());
newEntry.addIp(Ips.getIp(player));
saveVerificationData(newEntry);
return newEntry;
// Load data
vPlayer = getVerificationPlayer(player.getName());
// Create new data if nonexistent
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)
{
if (dataMap.containsKey(username))
{
return dataMap.get(username);
}
VPlayer player = loadData(username);
if (player != null)
{
return player;
}
username = username.toLowerCase();
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));
final VPlayer vPlayer = new VPlayer(username);
vPlayer.loadFrom(getConfig(vPlayer));
if (!data.isValid())
if (!vPlayer.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())
for (Player players : Bukkit.getOnlinePlayers())
{
if (onlinePlayer.getName().equals(username))
if (players.getName().equals(username))
{
dataMap.put(username, data);
return data;
dataMap.put(username, vPlayer);
return vPlayer;
}
}
return data;
}
public void removeEntry(String username)
{
if (getVerificationPlayer(username) != null)
{
getConfigFile(username).delete();
if (dataMap.containsKey(username))
{
dataMap.remove(username);
}
}
return vPlayer;
}
@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());
}
dataMap.remove(event.getPlayer().getName());
}
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();
return config;
}

View File

@ -6,11 +6,11 @@ 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.Collections;
import java.util.List;
public class VPlayer implements ConfigLoadable, ConfigSavable, Validatable
@ -22,131 +22,66 @@ public class VPlayer implements ConfigLoadable, ConfigSavable, Validatable
private String name;
@Getter
@Setter
private String discordId = null;
@Getter
@Setter
private String forumUsername = null;
@Getter
@Setter
private String discordID = null;
private Boolean discordEnabled = false;
@Getter
@Setter
private Boolean discordVerificationEnabled = false;
@Getter
@Setter
private Boolean forumVerificationEnabled = false;
private Boolean forumEnabled = false;
public VPlayer(String username)
public VPlayer(String name)
{
this.name = username;
this.name = name;
}
public void loadFrom(Player player)
public VPlayer(Player player)
{
name = player.getName();
ips.clear();
ips.add(Ips.getIp(player));
this(player.getName());
}
@Override
public void loadFrom(ConfigurationSection cs)
{
name = cs.getString("username", null);
name = cs.getString("username", name);
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);
discordId = cs.getString("discordId", null);
forumUsername = cs.getString("forumUsername", null);
discordEnabled = cs.getBoolean("discordEnabled", false);
forumEnabled = cs.getBoolean("forumEnabled", 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);
Validate.isTrue(isValid(), "Could not save player verification entry: " + name + ". Entry not valid!");
cs.set("name", name);
cs.set("discordId", discordId);
cs.set("forumUsername", forumUsername);
cs.set("discordEnabled", discordEnabled);
cs.set("forumEnabled", forumEnabled);
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)
public List<String> getIps()
{
if (!ips.contains(ip))
{
ips.add(ip);
}
return Collections.unmodifiableList(ips);
}
public void addIps(List<String> ips)
public boolean addIp(String ip)
{
for (String ip : ips)
{
addIp(ip);
}
return ips.contains(ip) ? false : ips.add(ip);
}
public void removeIp(String ip)
public boolean removeIp(String ip)
{
if (ips.contains(ip))
{
ips.remove(ip);
}
return 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()
{