Updated Spigot player data saving system.

This commit is contained in:
Aust1n46 2021-02-13 09:53:40 -06:00
parent ffc54bebaf
commit 9df128c9a6
7 changed files with 309 additions and 262 deletions

View File

@ -92,6 +92,13 @@ loglevel: info
# saveinterval is in minutes
saveinterval: 30
# If you're running a "cracked" server, player data might not be stored properly, and thus, you are on your own.
# If you run your server in offline mode, you might have to reset your player data when switching to online mode!
# If you see this warning by accident and you are using BungeeCord, make sure you have properly setup IP Forwarding.
# https://www.spigotmc.org/wiki/bungeecord-ip-forwarding/
# No player data will be saved in offline mode unless you set this acknowledgement to 'true'
offline_server_acknowledgement: false
# The time in seconds between each check to remove timed mutes
unmuteinterval: 60

View File

@ -92,6 +92,13 @@ loglevel: info
# saveinterval is in minutes
saveinterval: 30
# If you're running a "cracked" server, player data might not be stored properly, and thus, you are on your own.
# If you run your server in offline mode, you might have to reset your player data when switching to online mode!
# If you see this warning by accident and you are using BungeeCord, make sure you have properly setup IP Forwarding.
# https://www.spigotmc.org/wiki/bungeecord-ip-forwarding/
# No player data will be saved in offline mode unless you set this acknowledgement to 'true'
offline_server_acknowledgement: false
# The time in seconds between each check to remove timed mutes
unmuteinterval: 60

View File

@ -21,7 +21,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -148,7 +147,7 @@ public class MineverseChat extends JavaPlugin implements PluginMessageListener {
public static Set<MineverseChatPlayer> players = new HashSet<MineverseChatPlayer>();
public static Set<MineverseChatPlayer> onlinePlayers = new HashSet<MineverseChatPlayer>();
public static List<String> networkPlayerNames = new ArrayList<String>();
private boolean firstRun = true;
//private boolean firstRun = true;
// Plugin Messaging Channel
public static final String PLUGIN_MESSAGING_CHANNEL = "venturechat:data";
@ -221,57 +220,8 @@ public class MineverseChat extends JavaPlugin implements PluginMessageListener {
jfInfo = new JsonFormatInfo(this);
gsInfo = new GuiSlotInfo();
PlayerData.initialize();
if(this.firstRun) {
for(String uuidString : PlayerData.getPlayerData().getConfigurationSection("players").getKeys(false)) {
UUID uuid = UUID.fromString(uuidString);
String name = PlayerData.getPlayerData().getConfigurationSection("players." + uuid).getString("name");
String currentChannelName = PlayerData.getPlayerData().getConfigurationSection("players." + uuid).getString("current");
ChatChannel currentChannel = ChatChannel.isChannel(currentChannelName) ? ChatChannel.getChannel(currentChannelName) : ChatChannel.getDefaultChannel();
Set<UUID> ignores = new HashSet<UUID>();
StringTokenizer i = new StringTokenizer(PlayerData.getPlayerData().getConfigurationSection("players." + uuidString).getString("ignores"), ",");
while(i.hasMoreTokens()) {
ignores.add(UUID.fromString(i.nextToken()));
}
Set<String> listening = new HashSet<String>();
StringTokenizer l = new StringTokenizer(PlayerData.getPlayerData().getConfigurationSection("players." + uuidString).getString("listen"), ",");
while(l.hasMoreTokens()) {
String channel = l.nextToken();
if(ChatChannel.isChannel(channel)) {
listening.add(channel);
}
}
HashMap<String, Integer> mutes = new HashMap<String, Integer>();
StringTokenizer m = new StringTokenizer(PlayerData.getPlayerData().getConfigurationSection("players." + uuidString).getString("mutes"), ",");
while(m.hasMoreTokens()) {
String[] parts = m.nextToken().split(":");
if(ChatChannel.isChannel(parts[0])) {
if(parts[1].equals("null")) {
log.info("[VentureChat] Null Mute Time: " + parts[0] + " " + name);
continue;
}
mutes.put(ChatChannel.getChannel(parts[0]).getName(), Integer.parseInt(parts[1]));
}
}
Set<String> blockedCommands = new HashSet<String>();
StringTokenizer b = new StringTokenizer(PlayerData.getPlayerData().getConfigurationSection("players." + uuidString).getString("blockedcommands"), ",");
while(b.hasMoreTokens()) {
blockedCommands.add(b.nextToken());
}
boolean host = PlayerData.getPlayerData().getConfigurationSection("players." + uuidString).getBoolean("host");
UUID party = PlayerData.getPlayerData().getConfigurationSection("players." + uuidString).getString("party").length() > 0 ? UUID.fromString(PlayerData.getPlayerData().getConfigurationSection("players." + uuidString).getString("party")) : null;
boolean filter = PlayerData.getPlayerData().getConfigurationSection("players." + uuidString).getBoolean("filter");
boolean notifications = PlayerData.getPlayerData().getConfigurationSection("players." + uuidString).getBoolean("notifications");
String nickname = PlayerData.getPlayerData().getConfigurationSection("players." + uuidString).getString("nickname");
String jsonFormat = "Default";
boolean spy = PlayerData.getPlayerData().getConfigurationSection("players." + uuidString).getBoolean("spy", false);
boolean commandSpy = PlayerData.getPlayerData().getConfigurationSection("players." + uuidString).getBoolean("commandspy", false);
boolean rangedSpy = PlayerData.getPlayerData().getConfigurationSection("players." + uuidString).getBoolean("rangedspy", false);
boolean messageToggle = PlayerData.getPlayerData().getConfigurationSection("players." + uuidString).getBoolean("messagetoggle", true);
boolean bungeeToggle = PlayerData.getPlayerData().getConfigurationSection("players." + uuidString).getBoolean("bungeetoggle", true);
players.add(new MineverseChatPlayer(uuid, name, currentChannel, ignores, listening, mutes, blockedCommands, host, party, filter, notifications, nickname, jsonFormat, spy, commandSpy, rangedSpy, messageToggle, bungeeToggle));
}
}
PlayerData.loadLegacyPlayerData();
PlayerData.loadPlayerData();
for(Player p : this.getServer().getOnlinePlayers()) {
MineverseChatPlayer mcp = MineverseChatAPI.getMineverseChatPlayer(p);
mcp.setName(p.getName());
@ -452,7 +402,6 @@ public class MineverseChat extends JavaPlugin implements PluginMessageListener {
}
}
}, 0L, 1200L); // one minute interval
this.firstRun = false;
}
@SuppressWarnings("unchecked")

View File

@ -49,44 +49,20 @@ public class MineverseChatPlayer {
private boolean rangedSpy;
private boolean messageToggle;
private boolean bungeeToggle;
private boolean tempData;
//buttons variable no longer used
//mail variable no longer used
@Deprecated
public MineverseChatPlayer(UUID uuid, String name, ChatChannel currentChannel, Set<UUID> ignores, Set<String> listening, HashMap<String, Integer> mutes, Set<String> blockedCommands, List<String> mail, boolean host, UUID party, boolean filter, boolean notifications, String nickname, String jsonFormat, boolean spy, boolean commandSpy, boolean rangedSpy, boolean buttons, boolean messageToggle, boolean bungeeToggle) {
this.uuid = uuid;
this.name = name;
this.currentChannel = currentChannel;
this.ignores = ignores;
this.listening = listening;
this.mutes = mutes;
this.blockedCommands = blockedCommands;
this.host = host;
this.party = party;
this.filter = filter;
this.notifications = notifications;
this.nickname = nickname;
this.online = false;
this.player = null;
this.hasPlayed = false;
this.conversation = null;
this.spy = spy;
this.rangedSpy = rangedSpy;
this.commandSpy = commandSpy;
this.quickChat = false;
this.quickChannel = null;
this.replyPlayer = null;
this.partyChat = false;
this.modified = false;
this.messages = new ArrayList<ChatMessage>();
this.jsonFormat = jsonFormat;
this.cooldowns = new HashMap<ChatChannel, Integer>();
this.spam = new HashMap<ChatChannel, List<Integer>>();
this.messageToggle = messageToggle;
this.bungeeToggle = bungeeToggle;
this(uuid, name, currentChannel, ignores, listening, mutes, blockedCommands, host, party, filter, notifications, nickname, jsonFormat, spy, commandSpy, rangedSpy, messageToggle, bungeeToggle);
}
public MineverseChatPlayer(UUID uuid, String name, ChatChannel currentChannel, Set<UUID> ignores, Set<String> listening, HashMap<String, Integer> mutes, Set<String> blockedCommands, boolean host, UUID party, boolean filter, boolean notifications, String nickname, String jsonFormat, boolean spy, boolean commandSpy, boolean rangedSpy, boolean messageToggle, boolean bungeeToggle) {
this(uuid, name, currentChannel, ignores, listening, mutes, blockedCommands, host, party, filter, notifications, nickname, jsonFormat, spy, commandSpy, rangedSpy, messageToggle, bungeeToggle, false);
}
public MineverseChatPlayer(UUID uuid, String name, ChatChannel currentChannel, Set<UUID> ignores, Set<String> listening, HashMap<String, Integer> mutes, Set<String> blockedCommands, boolean host, UUID party, boolean filter, boolean notifications, String nickname, String jsonFormat, boolean spy, boolean commandSpy, boolean rangedSpy, boolean messageToggle, boolean bungeeToggle, boolean tempData) {
this.uuid = uuid;
this.name = name;
this.currentChannel = currentChannel;
@ -117,6 +93,11 @@ public class MineverseChatPlayer {
this.spam = new HashMap<ChatChannel, List<Integer>>();
this.messageToggle = messageToggle;
this.bungeeToggle = bungeeToggle;
this.tempData = tempData;
}
public boolean isTempData() {
return tempData;
}
public boolean getBungeeToggle() {

View File

@ -2,104 +2,261 @@ package mineverse.Aust1n46.chat.database;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import mineverse.Aust1n46.chat.MineverseChat;
import mineverse.Aust1n46.chat.api.MineverseChatPlayer;
import mineverse.Aust1n46.chat.channel.ChatChannel;
import mineverse.Aust1n46.chat.utilities.Format;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
//This class writes player data to the PlayerData.yml file in preparation for saving and shutting down the server.
/**
* Class for reading and writing player data.
*
* @author Aust1n46
*/
public class PlayerData {
private static FileConfiguration playerData;
private static File playerDataFile;
private static MineverseChat plugin;
private static MineverseChat plugin = MineverseChat.getInstance();
private static final String PLAYER_DATA_DIRECTORY_PATH = plugin.getDataFolder().getAbsolutePath() + "/PlayerData";
public static void initialize() {
plugin = MineverseChat.getInstance();
playerDataFile = new File(plugin.getDataFolder().getAbsolutePath(), "Players.yml");
if(!playerDataFile.isFile()) {
plugin.saveResource("Players.yml", true);
public static void loadLegacyPlayerData() {
File legacyPlayerDataFile = new File(plugin.getDataFolder().getAbsolutePath(), "Players.yml");
if(!legacyPlayerDataFile.isFile()) {
return;
}
playerData = YamlConfiguration.loadConfiguration(playerDataFile);
}
public static FileConfiguration getPlayerData() {
return playerData;
}
public static void savePlayerData() {
try {
for(MineverseChatPlayer p : MineverseChat.players) {
if(p.wasModified() || p.isOnline()) {
ConfigurationSection cs = playerData.getConfigurationSection("players." + p.getUUID().toString());
String nickname = p.getNickname();
if(cs == null) {
ConfigurationSection ps = playerData.getConfigurationSection("players");
if(ps == null) {
cs = playerData.createSection("players");
Bukkit.getConsoleSender().sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&c - Detected Legacy Player Data!"));
Bukkit.getConsoleSender().sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&c - Converting to new structure and deleting old Players.yml file!"));
FileConfiguration playerData = YamlConfiguration.loadConfiguration(legacyPlayerDataFile);
for(String uuidString : playerData.getConfigurationSection("players").getKeys(false)) {
UUID uuid = UUID.fromString(uuidString);
String name = playerData.getConfigurationSection("players." + uuid).getString("name");
String currentChannelName = playerData.getConfigurationSection("players." + uuid).getString("current");
ChatChannel currentChannel = ChatChannel.isChannel(currentChannelName) ? ChatChannel.getChannel(currentChannelName) : ChatChannel.getDefaultChannel();
Set<UUID> ignores = new HashSet<UUID>();
StringTokenizer i = new StringTokenizer(playerData.getConfigurationSection("players." + uuidString).getString("ignores"), ",");
while(i.hasMoreTokens()) {
ignores.add(UUID.fromString(i.nextToken()));
}
cs = playerData.createSection("players." + p.getUUID().toString());
Set<String> listening = new HashSet<String>();
StringTokenizer l = new StringTokenizer(playerData.getConfigurationSection("players." + uuidString).getString("listen"), ",");
while(l.hasMoreTokens()) {
String channel = l.nextToken();
if(ChatChannel.isChannel(channel)) {
listening.add(channel);
}
cs.set("name", p.getName());
cs.set("current", p.getCurrentChannel().getName());
}
HashMap<String, Integer> mutes = new HashMap<String, Integer>();
StringTokenizer m = new StringTokenizer(playerData.getConfigurationSection("players." + uuidString).getString("mutes"), ",");
while(m.hasMoreTokens()) {
String[] parts = m.nextToken().split(":");
if(ChatChannel.isChannel(parts[0])) {
if(parts[1].equals("null")) {
Bukkit.getConsoleSender().sendMessage("[VentureChat] Null Mute Time: " + parts[0] + " " + name);
continue;
}
mutes.put(ChatChannel.getChannel(parts[0]).getName(), Integer.parseInt(parts[1]));
}
}
Set<String> blockedCommands = new HashSet<String>();
StringTokenizer b = new StringTokenizer(playerData.getConfigurationSection("players." + uuidString).getString("blockedcommands"), ",");
while(b.hasMoreTokens()) {
blockedCommands.add(b.nextToken());
}
boolean host = playerData.getConfigurationSection("players." + uuidString).getBoolean("host");
UUID party = playerData.getConfigurationSection("players." + uuidString).getString("party").length() > 0 ? UUID.fromString(playerData.getConfigurationSection("players." + uuidString).getString("party")) : null;
boolean filter = playerData.getConfigurationSection("players." + uuidString).getBoolean("filter");
boolean notifications = playerData.getConfigurationSection("players." + uuidString).getBoolean("notifications");
String nickname = playerData.getConfigurationSection("players." + uuidString).getString("nickname");
String jsonFormat = "Default";
boolean spy = playerData.getConfigurationSection("players." + uuidString).getBoolean("spy", false);
boolean commandSpy = playerData.getConfigurationSection("players." + uuidString).getBoolean("commandspy", false);
boolean rangedSpy = playerData.getConfigurationSection("players." + uuidString).getBoolean("rangedspy", false);
boolean messageToggle = playerData.getConfigurationSection("players." + uuidString).getBoolean("messagetoggle", true);
boolean bungeeToggle = playerData.getConfigurationSection("players." + uuidString).getBoolean("bungeetoggle", true);
MineverseChatPlayer mcp = new MineverseChatPlayer(uuid, name, currentChannel, ignores, listening, mutes, blockedCommands, host, party, filter, notifications, nickname, jsonFormat, spy, commandSpy, rangedSpy, messageToggle, bungeeToggle);
mcp.setModified(true);
MineverseChat.players.add(mcp);
}
}
catch (Exception e) {
MineverseChat.players.clear();
Bukkit.getConsoleSender().sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&c - Error Loading Legacy Player Data!"));
Bukkit.getConsoleSender().sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&c - Deleted Players.yml file!"));
}
finally {
legacyPlayerDataFile.delete();
}
}
public static void loadPlayerData() {
try {
File playerDataDirectory = new File(PLAYER_DATA_DIRECTORY_PATH);
if(!playerDataDirectory.exists()) {
playerDataDirectory.mkdirs();
}
Files.walk(Paths.get(PLAYER_DATA_DIRECTORY_PATH))
.filter(Files::isRegularFile)
.forEach((path) -> readPlayerDataFile(path));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Loads the player data file for a specific player. Corrupt/invalid data files are skipped and deleted.
*
* @param path
*/
private static void readPlayerDataFile(Path path) {
MineverseChatPlayer mcp;
File playerDataFile = path.toFile();
if(!playerDataFile.exists()) {
return;
}
try {
FileConfiguration playerDataFileYamlConfiguration = YamlConfiguration.loadConfiguration(playerDataFile);
String uuidString = playerDataFile.getName().replace(".yml", "");
UUID uuid = UUID.fromString(uuidString);
String name = playerDataFileYamlConfiguration.getString("name");
String currentChannelName = playerDataFileYamlConfiguration.getString("current");
ChatChannel currentChannel = ChatChannel.isChannel(currentChannelName) ? ChatChannel.getChannel(currentChannelName) : ChatChannel.getDefaultChannel();
Set<UUID> ignores = new HashSet<UUID>();
StringTokenizer i = new StringTokenizer(playerDataFileYamlConfiguration.getString("ignores"), ",");
while(i.hasMoreTokens()) {
ignores.add(UUID.fromString(i.nextToken()));
}
Set<String> listening = new HashSet<String>();
StringTokenizer l = new StringTokenizer(playerDataFileYamlConfiguration.getString("listen"), ",");
while(l.hasMoreTokens()) {
String channel = l.nextToken();
if(ChatChannel.isChannel(channel)) {
listening.add(channel);
}
}
HashMap<String, Integer> mutes = new HashMap<String, Integer>();
StringTokenizer m = new StringTokenizer(playerDataFileYamlConfiguration.getString("mutes"), ",");
while(m.hasMoreTokens()) {
String[] parts = m.nextToken().split(":");
if(ChatChannel.isChannel(parts[0])) {
if(parts[1].equals("null")) {
Bukkit.getConsoleSender().sendMessage("[VentureChat] Null Mute Time: " + parts[0] + " " + name);
continue;
}
mutes.put(ChatChannel.getChannel(parts[0]).getName(), Integer.parseInt(parts[1]));
}
}
Set<String> blockedCommands = new HashSet<String>();
StringTokenizer b = new StringTokenizer(playerDataFileYamlConfiguration.getString("blockedcommands"), ",");
while(b.hasMoreTokens()) {
blockedCommands.add(b.nextToken());
}
boolean host = playerDataFileYamlConfiguration.getBoolean("host");
UUID party = playerDataFileYamlConfiguration.getString("party").length() > 0 ? UUID.fromString(playerDataFileYamlConfiguration.getString("party")) : null;
boolean filter = playerDataFileYamlConfiguration.getBoolean("filter");
boolean notifications = playerDataFileYamlConfiguration.getBoolean("notifications");
String nickname = playerDataFileYamlConfiguration.getString("nickname");
String jsonFormat = "Default";
boolean spy = playerDataFileYamlConfiguration.getBoolean("spy", false);
boolean commandSpy = playerDataFileYamlConfiguration.getBoolean("commandspy", false);
boolean rangedSpy = playerDataFileYamlConfiguration.getBoolean("rangedspy", false);
boolean messageToggle = playerDataFileYamlConfiguration.getBoolean("messagetoggle", true);
boolean bungeeToggle = playerDataFileYamlConfiguration.getBoolean("bungeetoggle", true);
mcp = new MineverseChatPlayer(uuid, name, currentChannel, ignores, listening, mutes, blockedCommands, host, party, filter, notifications, nickname, jsonFormat, spy, commandSpy, rangedSpy, messageToggle, bungeeToggle);
}
catch (Exception e) {
Bukkit.getConsoleSender().sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&c - Error Loading Data File: " + playerDataFile.getName()));
Bukkit.getConsoleSender().sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&c - File will be skipped and deleted."));
playerDataFile.delete();
return;
}
if(mcp != null) {
MineverseChat.players.add(mcp);
}
}
public static void savePlayerData(MineverseChatPlayer mcp) {
if(mcp == null || mcp.isTempData() || (!mcp.isOnline() && !mcp.wasModified())) {
return;
}
try {
File playerDataFile = new File(PLAYER_DATA_DIRECTORY_PATH, mcp.getUUID() + ".yml");
FileConfiguration playerDataFileYamlConfiguration = YamlConfiguration.loadConfiguration(playerDataFile);
if(!playerDataFile.exists()) {
playerDataFileYamlConfiguration.save(playerDataFile);
}
playerDataFileYamlConfiguration.set("name", mcp.getName());
playerDataFileYamlConfiguration.set("current", mcp.getCurrentChannel().getName());
String ignores = "";
for(UUID s : p.getIgnores()) {
for(UUID s : mcp.getIgnores()) {
ignores += s.toString() + ",";
}
cs.set("ignores", ignores);
playerDataFileYamlConfiguration.set("ignores", ignores);
String listening = "";
for(String channel : p.getListening()) {
for(String channel : mcp.getListening()) {
ChatChannel c = ChatChannel.getChannel(channel);
listening += c.getName() + ",";
}
String mutes = "";
for(String channel : p.getMutes().keySet()) {
for(String channel : mcp.getMutes().keySet()) {
ChatChannel c = ChatChannel.getChannel(channel);
mutes += c.getName() + ":" + p.getMutes().get(c.getName()) + ",";
mutes += c.getName() + ":" + mcp.getMutes().get(c.getName()) + ",";
}
String blockedCommands = "";
for(String s : p.getBlockedCommands()) {
for(String s : mcp.getBlockedCommands()) {
blockedCommands += s + ",";
}
if(listening.length() > 0) {
listening = listening.substring(0, listening.length() - 1);
}
cs.set("listen", listening);
playerDataFileYamlConfiguration.set("listen", listening);
if(mutes.length() > 0) {
mutes = mutes.substring(0, mutes.length() - 1);
}
cs.set("mutes", mutes);
playerDataFileYamlConfiguration.set("mutes", mutes);
if(blockedCommands.length() > 0) {
blockedCommands = blockedCommands.substring(0, blockedCommands.length() - 1);
}
cs.set("blockedcommands", blockedCommands);
cs.set("host", p.isHost());
cs.set("party", p.hasParty() ? p.getParty().toString() : "");
cs.set("filter", p.hasFilter());
cs.set("notifications", p.hasNotifications());
cs.set("nickname", nickname);
cs.set("spy", p.isSpy());
cs.set("commandspy", p.hasCommandSpy());
cs.set("rangedspy", p.getRangedSpy());
cs.set("messagetoggle", p.getMessageToggle());
cs.set("bungeetoggle", p.getBungeeToggle());
playerDataFileYamlConfiguration.set("blockedcommands", blockedCommands);
playerDataFileYamlConfiguration.set("host", mcp.isHost());
playerDataFileYamlConfiguration.set("party", mcp.hasParty() ? mcp.getParty().toString() : "");
playerDataFileYamlConfiguration.set("filter", mcp.hasFilter());
playerDataFileYamlConfiguration.set("notifications", mcp.hasNotifications());
playerDataFileYamlConfiguration.set("nickname", mcp.getNickname());
playerDataFileYamlConfiguration.set("spy", mcp.isSpy());
playerDataFileYamlConfiguration.set("commandspy", mcp.hasCommandSpy());
playerDataFileYamlConfiguration.set("rangedspy", mcp.getRangedSpy());
playerDataFileYamlConfiguration.set("messagetoggle", mcp.getMessageToggle());
playerDataFileYamlConfiguration.set("bungeetoggle", mcp.getBungeeToggle());
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
String dateNow = formatter.format(currentDate.getTime());
cs.set("date", dateNow);
p.setModified(false);
playerDataFileYamlConfiguration.set("date", dateNow);
mcp.setModified(false);
playerDataFileYamlConfiguration.save(playerDataFile);
}
}
playerData.save(playerDataFile);
}
catch(IOException e) {
catch (IOException e) {
e.printStackTrace();
}
}
public static void savePlayerData() {
for(MineverseChatPlayer p : MineverseChat.players) {
savePlayerData(p);
}
}
}

View File

@ -1,21 +1,10 @@
package mineverse.Aust1n46.chat.listeners;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import mineverse.Aust1n46.chat.MineverseChat;
import mineverse.Aust1n46.chat.api.MineverseChatAPI;
import mineverse.Aust1n46.chat.api.MineverseChatPlayer;
import mineverse.Aust1n46.chat.channel.ChatChannel;
import mineverse.Aust1n46.chat.database.PlayerData;
import mineverse.Aust1n46.chat.utilities.Format;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -24,11 +13,18 @@ import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import mineverse.Aust1n46.chat.MineverseChat;
import mineverse.Aust1n46.chat.api.MineverseChatAPI;
import mineverse.Aust1n46.chat.api.MineverseChatPlayer;
import mineverse.Aust1n46.chat.channel.ChatChannel;
import mineverse.Aust1n46.chat.database.PlayerData;
import mineverse.Aust1n46.chat.utilities.Format;
import mineverse.Aust1n46.chat.utilities.UUIDFetcher;
//This class listens for when Players login or logout and manages their wrapped MineverseChatPlayer
//and it's data.
public class LoginListener implements Listener {
private MineverseChat plugin = MineverseChat.getInstance();
private FileConfiguration playerData = PlayerData.getPlayerData();
@EventHandler(priority = EventPriority.LOW)
public void onPlayerKick(PlayerKickEvent plog) {
@ -58,9 +54,8 @@ public class LoginListener implements Listener {
if(mcp == null) {
Player player = event.getPlayer();
String name = player.getName();
// Disabling Mojang UUID Query
//UUID uuid = UUIDFetcher.getUUIDOf(name);
UUID uuid = player.getUniqueId();
UUID uuid = UUIDFetcher.getUUIDFromPlayer(player);
boolean tempData = UUIDFetcher.uuidIsOffline(uuid);
ChatChannel current = ChatChannel.getDefaultChannel();
Set<UUID> ignores = new HashSet<UUID>();
Set<String> listening = new HashSet<String>();
@ -68,7 +63,7 @@ public class LoginListener implements Listener {
HashMap<String, Integer> mutes = new HashMap<String, Integer>();
Set<String> blockedCommands = new HashSet<String>();
String jsonFormat = "Default";
mcp = new MineverseChatPlayer(uuid, name, current, ignores, listening, mutes, blockedCommands, false, null, true, true, name, jsonFormat, false, false, false, true, true);
mcp = new MineverseChatPlayer(uuid, name, current, ignores, listening, mutes, blockedCommands, false, null, true, true, name, jsonFormat, false, false, false, true, true, tempData);
MineverseChat.players.add(mcp);
}
mcp.setName(event.getPlayer().getName());
@ -109,84 +104,13 @@ public class LoginListener implements Listener {
if(!plugin.getConfig().getConfigurationSection("login").getBoolean("enabled", true)) {
event.setJoinMessage("");
}
/*
* if(MineverseChat.onlinePlayers.size() == 1) {
* plugin.updatePlayerList(sync, true); }
* plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new
* Runnable() { public void run() {
* MineverseChat.networkPlayers.put(sync.getName(),
* sync.getPlayer().getServer().getServerName());
* plugin.updatePlayerList(sync, false); } }, 1L);
*/
}
private void playerLeaving(Player player) {
MineverseChatPlayer mcp = MineverseChatAPI.getOnlineMineverseChatPlayer(player);
ConfigurationSection cs = playerData.getConfigurationSection("players." + mcp.getUUID().toString());
if(mcp.getPlayer() == null) {
return;
}
if(cs == null) {
ConfigurationSection ps = playerData.getConfigurationSection("players");
if(ps == null) {
cs = playerData.createSection("players");
}
cs = playerData.createSection("players." + mcp.getUUID().toString());
}
cs.set("name", mcp.getName());
cs.set("current", mcp.getCurrentChannel().getName());
String ignores = "";
for(UUID s : mcp.getIgnores()) {
ignores += s.toString() + ",";
}
cs.set("ignores", ignores);
String listening = "";
for(String channel : mcp.getListening()) {
ChatChannel c = ChatChannel.getChannel(channel);
listening += c.getName() + ",";
}
String mutes = "";
for(String channel : mcp.getMutes().keySet()) {
ChatChannel c = ChatChannel.getChannel(channel);
mutes += c.getName() + ":" + mcp.getMutes().get(c.getName()) + ",";
}
String blockedCommands = "";
for(String s : mcp.getBlockedCommands()) {
blockedCommands += s + ",";
}
if(listening.length() > 0) {
listening = listening.substring(0, listening.length() - 1);
}
cs.set("listen", listening);
if(mutes.length() > 0) {
mutes = mutes.substring(0, mutes.length() - 1);
}
cs.set("mutes", mutes);
if(blockedCommands.length() > 0) {
blockedCommands = blockedCommands.substring(0, blockedCommands.length() - 1);
}
cs.set("blockedcommands", blockedCommands);
cs.set("host", mcp.isHost());
cs.set("party", mcp.hasParty() ? mcp.getParty().toString() : "");
cs.set("filter", mcp.hasFilter());
cs.set("notifications", mcp.hasNotifications());
cs.set("nickname", mcp.getPlayer().getDisplayName());
mcp.setNickname(mcp.getPlayer().getDisplayName());
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
String dateNow = formatter.format(currentDate.getTime());
cs.set("date", dateNow);
PlayerData.savePlayerData(mcp);
mcp.clearMessages();
mcp.setOnline(false);
MineverseChat.onlinePlayers.remove(mcp);
// System.out.println(mcp.getName() + " logged off.");
/*
* final MineverseChatPlayer sync = mcp;
* plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new
* Runnable() { public void run() {
* MineverseChat.networkPlayers.remove(sync.getName());
* plugin.updatePlayerList(sync, false); sync.setOnline(false);
* MineverseChat.onlinePlayers.remove(sync); } }, 1L);
*/
}
}

View File

@ -1,17 +1,25 @@
package mineverse.Aust1n46.chat.utilities;
import com.google.common.collect.ImmutableList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import com.google.common.collect.ImmutableList;
//This class is used to query the Mojang servers to verify UUID's.
public class UUIDFetcher implements Callable<Map<String, UUID>> { //unimplemented
private static final double PROFILES_PER_REQUEST = 100;
@ -102,4 +110,18 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> { //unimplemente
public static boolean uuidIsOffline(UUID uuid) {
return uuid.version() == 3;
}
public static UUID getUUIDFromPlayer(Player player) {
UUID uuid = player.getUniqueId();
if(uuidIsOffline(uuid)) {
Bukkit.getConsoleSender().sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&c - Detected Offline UUID!"));
Bukkit.getConsoleSender().sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&c - If you are using BungeeCord, make sure you have properly setup IP Forwarding."));
Bukkit.getConsoleSender().sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&c - https://www.spigotmc.org/wiki/bungeecord-ip-forwarding/"));
Bukkit.getConsoleSender().sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&c - You can access this wiki page from the log file or just Google it."));
Bukkit.getConsoleSender().sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&c - If you're running a \"cracked\" server, player data might not be stored properly, and thus, you are on your own."));
Bukkit.getConsoleSender().sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&c - If you run your server in offline mode, you might have to reset your player data when switching to online mode!"));
Bukkit.getConsoleSender().sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&c - No player data will be saved in offline mode unless you set the \"cracked\" server acknowledgement in the config!"));
}
return uuid;
}
}