TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerList.java

223 lines
5.5 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.player;
import com.google.common.collect.Maps;
import java.io.File;
import java.util.Collection;
import java.util.Map;
import lombok.Getter;
import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import net.pravian.aero.config.YamlConfig;
import net.pravian.aero.util.Ips;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerQuitEvent;
public class PlayerList extends FreedomService
{
public static final long AUTO_PURGE_TICKS = 20L * 60L * 5L;
//
@Getter
public final Map<String, FPlayer> playerMap = Maps.newHashMap(); // ip,dataMap
@Getter
public final Map<String, PlayerData> dataMap = Maps.newHashMap(); // ip,dataMap
@Getter
private final File configFolder;
public PlayerList(TotalFreedomMod plugin)
{
super(plugin);
this.configFolder = new File(plugin.getDataFolder(), "players");
}
@Override
protected void onStart()
{
playerMap.clear();
dataMap.clear();
// Preload online players
for (Player player : server.getOnlinePlayers())
{
getPlayer(player);
}
}
@Override
protected void onStop()
{
save();
}
public void save()
{
for (PlayerData data : dataMap.values())
{
YamlConfig config = getConfig(data);
data.saveTo(config);
config.save();
}
}
public FPlayer getPlayerSync(Player player)
{
synchronized (playerMap)
{
return getPlayer(player);
}
}
public String getIp(OfflinePlayer player)
{
if (player.isOnline())
{
return Ips.getIp(player.getPlayer());
}
final PlayerData entry = getData(player.getName());
return (entry == null ? null : entry.getIps().iterator().next());
}
// May not return null
public FPlayer getPlayer(Player player)
{
FPlayer tPlayer = playerMap.get(Ips.getIp(player));
if (tPlayer != null)
{
return tPlayer;
}
TotalFreedomMod Electrum Version 5.0 This TotalFreedomMod release implements many changes. Most notably, the internals have been completely revamped. TotalFreedomMod now relies on the Aero library for core mechanics such as command handling and services. Another important change is the UUID system. In TotalFreedomMod Electrum, it has been completely removed. The core reason for this is that the system as a whole was very bugged. Additionally, it did not solve the primary reason for its conception: preserving player data when the player changes their username. This is because TotalFreedomMod servers usually run in offline-mode. This meaning that some of the players joining do not have a registerd Mojang UUID whatsoever. All in all, the UUID system was buggy, and it did not fix the reason it was implemented, so it has been completely removed. The admin list and the ban list now use usernames and IPs again. Lastly, many smaller changes have been implemented. Due to the amount of changes, they have not been named individualy. Please refer to the issues below for more details. Fixes #342 Fixes #350 Fixes #380 Fixes #684 Fixes #704 Fixes #716 Fixes #735 Fixes #745 Fixes #784 Fixes #765 Fixes #791 Fixes #805 Fixes #826 Fixes #883 Fixes #1524 Fixes #1534 Fixes #1536 Fixes #1538 Fixes #1545 Fixes #1546 Fixes #1568 Fixes #1627 Resolves #403 Resolves #435 Resolves #597 Resolves #603 Resolves #628 Resolves #690 Resolves #708 Resolves #747 Resolves #748 Resolves #749 Resolves #764 Resolves #767 Resolves #782 Resolves #809 Resolves #803 Resolves #811 Resolves #813 Resolves #830 Resolves #848 Resolves #856 Resolves #876 Resolves #908 Resolves #992 Resolves #1018 Resolves #1432 Resolves #1446 Resolves #1494 Resolves #1501 Resolves #1526 Resolves #1540 Resolves #1550 Resolves #1560 Resolves #1561 Resolves #1578 Resolves #1613
2016-05-12 19:40:39 +00:00
tPlayer = new FPlayer(plugin, player);
playerMap.put(Ips.getIp(player), tPlayer);
return tPlayer;
}
// May not return null
public PlayerData getData(Player player)
{
// Check already loaded
PlayerData data = dataMap.get(Ips.getIp(player));
if (data != null)
{
return data;
}
// Load data
data = getData(player.getName());
// Create data if nonexistent
if (data == null)
{
FLog.info("Creating new player data entry for " + player.getName());
// Create new player
final long unix = FUtil.getUnixTime();
data = new PlayerData(player);
data.setFirstJoinUnix(unix);
data.setLastJoinUnix(unix);
data.addIp(Ips.getIp(player));
// Store player
dataMap.put(player.getName().toLowerCase(), data);
// Save player
YamlConfig config = getConfig(data);
data.saveTo(config);
config.save();
}
return data;
}
// May return null
public PlayerData getData(String username)
{
username = username.toLowerCase();
// Check if the player is a known player
final File configFile = getConfigFile(username);
if (!configFile.exists())
{
return null;
}
// Create and load entry
final PlayerData data = new PlayerData(username);
data.loadFrom(getConfig(data));
if (!data.isValid())
{
FLog.warning("Could not load player data entry: " + username + ". Entry is not valid!");
configFile.delete();
return null;
}
// Only store data if the player is online
for (String ip : data.getIps())
{
for (Player onlinePlayer : Bukkit.getOnlinePlayers())
{
if (Ips.getIp(onlinePlayer).equals(ip))
{
dataMap.put(ip, data);
return data;
}
}
}
return data;
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerQuit(PlayerQuitEvent event)
{
final Player player = event.getPlayer();
final String ip = Ips.getIp(player);
playerMap.remove(ip);
dataMap.remove(ip);
}
public Collection<FPlayer> getLoadedPlayers()
{
return playerMap.values();
}
public Collection<PlayerData> getLoadedData()
{
return dataMap.values();
}
public int purgeAllData()
{
int deleted = 0;
for (File file : getConfigFolder().listFiles())
{
deleted += file.delete() ? 1 : 0;
}
dataMap.clear();
return deleted;
}
protected File getConfigFile(String name)
{
return new File(getConfigFolder(), name + ".yml");
}
protected YamlConfig getConfig(PlayerData data)
{
TotalFreedomMod Electrum Version 5.0 This TotalFreedomMod release implements many changes. Most notably, the internals have been completely revamped. TotalFreedomMod now relies on the Aero library for core mechanics such as command handling and services. Another important change is the UUID system. In TotalFreedomMod Electrum, it has been completely removed. The core reason for this is that the system as a whole was very bugged. Additionally, it did not solve the primary reason for its conception: preserving player data when the player changes their username. This is because TotalFreedomMod servers usually run in offline-mode. This meaning that some of the players joining do not have a registerd Mojang UUID whatsoever. All in all, the UUID system was buggy, and it did not fix the reason it was implemented, so it has been completely removed. The admin list and the ban list now use usernames and IPs again. Lastly, many smaller changes have been implemented. Due to the amount of changes, they have not been named individualy. Please refer to the issues below for more details. Fixes #342 Fixes #350 Fixes #380 Fixes #684 Fixes #704 Fixes #716 Fixes #735 Fixes #745 Fixes #784 Fixes #765 Fixes #791 Fixes #805 Fixes #826 Fixes #883 Fixes #1524 Fixes #1534 Fixes #1536 Fixes #1538 Fixes #1545 Fixes #1546 Fixes #1568 Fixes #1627 Resolves #403 Resolves #435 Resolves #597 Resolves #603 Resolves #628 Resolves #690 Resolves #708 Resolves #747 Resolves #748 Resolves #749 Resolves #764 Resolves #767 Resolves #782 Resolves #809 Resolves #803 Resolves #811 Resolves #813 Resolves #830 Resolves #848 Resolves #856 Resolves #876 Resolves #908 Resolves #992 Resolves #1018 Resolves #1432 Resolves #1446 Resolves #1494 Resolves #1501 Resolves #1526 Resolves #1540 Resolves #1550 Resolves #1560 Resolves #1561 Resolves #1578 Resolves #1613
2016-05-12 19:40:39 +00:00
final YamlConfig config = new YamlConfig(plugin, getConfigFile(data.getUsername().toLowerCase()), false);
config.load();
return config;
}
}