2020-12-04 00:28:53 +00:00
|
|
|
package me.totalfreedom.totalfreedommod.admin;
|
2019-07-17 08:32:42 +00:00
|
|
|
|
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
import java.util.Map;
|
|
|
|
import me.totalfreedom.totalfreedommod.FreedomService;
|
2020-07-01 01:51:06 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.config.YamlConfig;
|
2019-07-17 08:32:42 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FLog;
|
2020-07-01 01:51:06 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FUtil;
|
2019-07-17 08:32:42 +00:00
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.player.PlayerJoinEvent;
|
|
|
|
import org.bukkit.event.player.PlayerQuitEvent;
|
|
|
|
|
|
|
|
public class ActivityLog extends FreedomService
|
|
|
|
{
|
|
|
|
|
|
|
|
public static final String FILENAME = "activitylog.yml";
|
|
|
|
|
|
|
|
private final Map<String, ActivityLogEntry> allActivityLogs = Maps.newHashMap();
|
|
|
|
private final Map<String, ActivityLogEntry> nameTable = Maps.newHashMap();
|
|
|
|
private final Map<String, ActivityLogEntry> ipTable = Maps.newHashMap();
|
|
|
|
|
|
|
|
private final YamlConfig config;
|
|
|
|
|
2020-07-01 01:51:06 +00:00
|
|
|
public ActivityLog()
|
2019-07-17 08:32:42 +00:00
|
|
|
{
|
|
|
|
this.config = new YamlConfig(plugin, FILENAME, true);
|
|
|
|
}
|
|
|
|
|
2020-12-25 19:46:43 +00:00
|
|
|
public static String getFILENAME()
|
|
|
|
{
|
|
|
|
return FILENAME;
|
|
|
|
}
|
|
|
|
|
2019-07-17 08:32:42 +00:00
|
|
|
@Override
|
2020-07-01 01:51:06 +00:00
|
|
|
public void onStart()
|
2019-07-17 08:32:42 +00:00
|
|
|
{
|
|
|
|
load();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-07-01 01:51:06 +00:00
|
|
|
public void onStop()
|
2019-07-17 08:32:42 +00:00
|
|
|
{
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void load()
|
|
|
|
{
|
|
|
|
config.load();
|
|
|
|
|
|
|
|
allActivityLogs.clear();
|
|
|
|
nameTable.clear();
|
|
|
|
ipTable.clear();
|
|
|
|
for (String key : config.getKeys(false))
|
|
|
|
{
|
|
|
|
ConfigurationSection section = config.getConfigurationSection(key);
|
|
|
|
if (section == null)
|
|
|
|
{
|
2020-12-25 19:46:43 +00:00
|
|
|
FLog.warning("Invalid activity log format: " + key);
|
2019-07-17 08:32:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActivityLogEntry activityLogEntry = new ActivityLogEntry(key);
|
|
|
|
activityLogEntry.loadFrom(section);
|
|
|
|
|
|
|
|
if (!activityLogEntry.isValid())
|
|
|
|
{
|
|
|
|
FLog.warning("Could not load activity log: " + key + ". Missing details!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
allActivityLogs.put(key, activityLogEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTables();
|
|
|
|
FLog.info("Loaded " + allActivityLogs.size() + " activity logs");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void save()
|
|
|
|
{
|
|
|
|
// Clear the config
|
|
|
|
for (String key : config.getKeys(false))
|
|
|
|
{
|
|
|
|
config.set(key, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ActivityLogEntry activityLog : allActivityLogs.values())
|
|
|
|
{
|
|
|
|
activityLog.saveTo(config.createSection(activityLog.getConfigKey()));
|
|
|
|
}
|
|
|
|
|
|
|
|
config.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ActivityLogEntry getActivityLog(CommandSender sender)
|
|
|
|
{
|
|
|
|
if (sender instanceof Player)
|
|
|
|
{
|
|
|
|
return getActivityLog((Player)sender);
|
|
|
|
}
|
|
|
|
|
|
|
|
return getEntryByName(sender.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
public ActivityLogEntry getActivityLog(Player player)
|
|
|
|
{
|
|
|
|
ActivityLogEntry activityLog = getEntryByName(player.getName());
|
|
|
|
if (activityLog == null)
|
|
|
|
{
|
2020-07-01 01:51:06 +00:00
|
|
|
String ip = FUtil.getIp(player);
|
2019-07-17 08:32:42 +00:00
|
|
|
activityLog = getEntryByIp(ip);
|
|
|
|
if (activityLog != null)
|
|
|
|
{
|
2020-12-04 00:28:53 +00:00
|
|
|
// Set the new username
|
|
|
|
activityLog.setName(player.getName());
|
|
|
|
save();
|
|
|
|
updateTables();
|
2019-07-17 08:32:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
activityLog = new ActivityLogEntry(player);
|
|
|
|
allActivityLogs.put(activityLog.getConfigKey(), activityLog);
|
|
|
|
updateTables();
|
|
|
|
|
|
|
|
activityLog.saveTo(config.createSection(activityLog.getConfigKey()));
|
|
|
|
config.save();
|
|
|
|
}
|
|
|
|
}
|
2020-07-01 01:51:06 +00:00
|
|
|
String ip = FUtil.getIp(player);
|
2019-07-17 08:32:42 +00:00
|
|
|
if (!activityLog.getIps().contains(ip))
|
|
|
|
{
|
|
|
|
activityLog.addIp(ip);
|
|
|
|
save();
|
|
|
|
updateTables();
|
|
|
|
}
|
|
|
|
return activityLog;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ActivityLogEntry getEntryByName(String name)
|
|
|
|
{
|
|
|
|
return nameTable.get(name.toLowerCase());
|
|
|
|
}
|
|
|
|
|
|
|
|
public ActivityLogEntry getEntryByIp(String ip)
|
|
|
|
{
|
|
|
|
return ipTable.get(ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateTables()
|
|
|
|
{
|
|
|
|
nameTable.clear();
|
|
|
|
ipTable.clear();
|
|
|
|
|
|
|
|
for (ActivityLogEntry activityLog : allActivityLogs.values())
|
|
|
|
{
|
|
|
|
nameTable.put(activityLog.getName().toLowerCase(), activityLog);
|
|
|
|
|
|
|
|
for (String ip : activityLog.getIps())
|
|
|
|
{
|
|
|
|
ipTable.put(ip, activityLog);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 05:43:02 +00:00
|
|
|
@EventHandler(priority = EventPriority.HIGH)
|
2019-07-17 08:32:42 +00:00
|
|
|
public void onPlayerJoin(PlayerJoinEvent event)
|
|
|
|
{
|
|
|
|
Player player = event.getPlayer();
|
2020-12-04 00:28:53 +00:00
|
|
|
if (plugin.al.isAdmin(player))
|
2019-07-17 08:32:42 +00:00
|
|
|
{
|
|
|
|
getActivityLog(event.getPlayer()).addLogin();
|
|
|
|
plugin.acl.save();
|
|
|
|
plugin.acl.updateTables();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 05:43:02 +00:00
|
|
|
@EventHandler(priority = EventPriority.HIGH)
|
2019-07-17 08:32:42 +00:00
|
|
|
public void onPlayerQuit(PlayerQuitEvent event)
|
|
|
|
{
|
|
|
|
Player player = event.getPlayer();
|
2020-12-04 00:28:53 +00:00
|
|
|
if (plugin.al.isAdmin(player))
|
2019-07-17 08:32:42 +00:00
|
|
|
{
|
|
|
|
getActivityLog(event.getPlayer()).addLogout();
|
|
|
|
plugin.acl.save();
|
|
|
|
plugin.acl.updateTables();
|
|
|
|
}
|
|
|
|
}
|
2020-12-25 19:46:43 +00:00
|
|
|
|
|
|
|
public Map<String, ActivityLogEntry> getAllActivityLogs()
|
|
|
|
{
|
|
|
|
return allActivityLogs;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, ActivityLogEntry> getNameTable()
|
|
|
|
{
|
|
|
|
return nameTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, ActivityLogEntry> getIpTable()
|
|
|
|
{
|
|
|
|
return ipTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
public YamlConfig getConfig()
|
|
|
|
{
|
|
|
|
return config;
|
|
|
|
}
|
2020-12-04 00:28:53 +00:00
|
|
|
}
|