diff --git a/src/config.yml b/src/config.yml index db406de..2930f85 100644 --- a/src/config.yml +++ b/src/config.yml @@ -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 diff --git a/src/defaultconfig.yml b/src/defaultconfig.yml index db406de..2930f85 100644 --- a/src/defaultconfig.yml +++ b/src/defaultconfig.yml @@ -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 diff --git a/src/mineverse/Aust1n46/chat/MineverseChat.java b/src/mineverse/Aust1n46/chat/MineverseChat.java index 6ba2db9..ba9398e 100644 --- a/src/mineverse/Aust1n46/chat/MineverseChat.java +++ b/src/mineverse/Aust1n46/chat/MineverseChat.java @@ -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 players = new HashSet(); public static Set onlinePlayers = new HashSet(); public static List networkPlayerNames = new ArrayList(); - private boolean firstRun = true; + //private boolean firstRun = true; // Plugin Messaging Channel public static final String PLUGIN_MESSAGING_CHANNEL = "venturechat:data"; @@ -220,58 +219,9 @@ public class MineverseChat extends JavaPlugin implements PluginMessageListener { aaInfo = new AliasInfo(this); 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 ignores = new HashSet(); - StringTokenizer i = new StringTokenizer(PlayerData.getPlayerData().getConfigurationSection("players." + uuidString).getString("ignores"), ","); - while(i.hasMoreTokens()) { - ignores.add(UUID.fromString(i.nextToken())); - } - Set listening = new HashSet(); - 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 mutes = new HashMap(); - 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 blockedCommands = new HashSet(); - 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()); @@ -451,8 +401,7 @@ public class MineverseChat extends JavaPlugin implements PluginMessageListener { .sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&e - Updating Player Mutes")); } } - }, 0L, 1200L); // one minute interval - this.firstRun = false; + }, 0L, 1200L); // one minute interval } @SuppressWarnings("unchecked") diff --git a/src/mineverse/Aust1n46/chat/api/MineverseChatPlayer.java b/src/mineverse/Aust1n46/chat/api/MineverseChatPlayer.java index 9b7b0d0..4326f81 100644 --- a/src/mineverse/Aust1n46/chat/api/MineverseChatPlayer.java +++ b/src/mineverse/Aust1n46/chat/api/MineverseChatPlayer.java @@ -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 ignores, Set listening, HashMap mutes, Set blockedCommands, List 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(); - this.jsonFormat = jsonFormat; - this.cooldowns = new HashMap(); - this.spam = new HashMap>(); - 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 ignores, Set listening, HashMap mutes, Set 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 ignores, Set listening, HashMap mutes, Set 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>(); this.messageToggle = messageToggle; this.bungeeToggle = bungeeToggle; + this.tempData = tempData; + } + + public boolean isTempData() { + return tempData; } public boolean getBungeeToggle() { diff --git a/src/mineverse/Aust1n46/chat/database/PlayerData.java b/src/mineverse/Aust1n46/chat/database/PlayerData.java index cc6a721..7bd6ef9 100644 --- a/src/mineverse/Aust1n46/chat/database/PlayerData.java +++ b/src/mineverse/Aust1n46/chat/database/PlayerData.java @@ -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"); - } - cs = playerData.createSection("players." + p.getUUID().toString()); - } - cs.set("name", p.getName()); - cs.set("current", p.getCurrentChannel().getName()); - String ignores = ""; - for(UUID s : p.getIgnores()) { - ignores += s.toString() + ","; - } - cs.set("ignores", ignores); - String listening = ""; - for(String channel : p.getListening()) { - ChatChannel c = ChatChannel.getChannel(channel); - listening += c.getName() + ","; - } - String mutes = ""; - for(String channel : p.getMutes().keySet()) { - ChatChannel c = ChatChannel.getChannel(channel); - mutes += c.getName() + ":" + p.getMutes().get(c.getName()) + ","; - } - String blockedCommands = ""; - for(String s : p.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", 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()); - 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); + 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 ignores = new HashSet(); + StringTokenizer i = new StringTokenizer(playerData.getConfigurationSection("players." + uuidString).getString("ignores"), ","); + while(i.hasMoreTokens()) { + ignores.add(UUID.fromString(i.nextToken())); } + Set listening = new HashSet(); + StringTokenizer l = new StringTokenizer(playerData.getConfigurationSection("players." + uuidString).getString("listen"), ","); + while(l.hasMoreTokens()) { + String channel = l.nextToken(); + if(ChatChannel.isChannel(channel)) { + listening.add(channel); + } + } + HashMap mutes = new HashMap(); + 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 blockedCommands = new HashSet(); + 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); } - playerData.save(playerDataFile); } - catch(IOException e) { + 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(); } } -} \ No newline at end of file + + /** + * 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 ignores = new HashSet(); + StringTokenizer i = new StringTokenizer(playerDataFileYamlConfiguration.getString("ignores"), ","); + while(i.hasMoreTokens()) { + ignores.add(UUID.fromString(i.nextToken())); + } + Set listening = new HashSet(); + StringTokenizer l = new StringTokenizer(playerDataFileYamlConfiguration.getString("listen"), ","); + while(l.hasMoreTokens()) { + String channel = l.nextToken(); + if(ChatChannel.isChannel(channel)) { + listening.add(channel); + } + } + HashMap mutes = new HashMap(); + 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 blockedCommands = new HashSet(); + 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 : mcp.getIgnores()) { + ignores += s.toString() + ","; + } + playerDataFileYamlConfiguration.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); + } + playerDataFileYamlConfiguration.set("listen", listening); + if(mutes.length() > 0) { + mutes = mutes.substring(0, mutes.length() - 1); + } + playerDataFileYamlConfiguration.set("mutes", mutes); + if(blockedCommands.length() > 0) { + blockedCommands = blockedCommands.substring(0, blockedCommands.length() - 1); + } + 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()); + playerDataFileYamlConfiguration.set("date", dateNow); + mcp.setModified(false); + + playerDataFileYamlConfiguration.save(playerDataFile); + } + catch (IOException e) { + e.printStackTrace(); + } + } + + public static void savePlayerData() { + for(MineverseChatPlayer p : MineverseChat.players) { + savePlayerData(p); + } + } +} diff --git a/src/mineverse/Aust1n46/chat/listeners/LoginListener.java b/src/mineverse/Aust1n46/chat/listeners/LoginListener.java index 72c1294..eeef4da 100644 --- a/src/mineverse/Aust1n46/chat/listeners/LoginListener.java +++ b/src/mineverse/Aust1n46/chat/listeners/LoginListener.java @@ -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 ignores = new HashSet(); Set listening = new HashSet(); @@ -68,7 +63,7 @@ public class LoginListener implements Listener { HashMap mutes = new HashMap(); Set blockedCommands = new HashSet(); 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); - */ } } \ No newline at end of file diff --git a/src/mineverse/Aust1n46/chat/utilities/UUIDFetcher.java b/src/mineverse/Aust1n46/chat/utilities/UUIDFetcher.java index 27a3af8..bd433d8 100644 --- a/src/mineverse/Aust1n46/chat/utilities/UUIDFetcher.java +++ b/src/mineverse/Aust1n46/chat/utilities/UUIDFetcher.java @@ -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> { //unimplemented private static final double PROFILES_PER_REQUEST = 100; @@ -102,4 +110,18 @@ public class UUIDFetcher implements Callable> { //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; + } }