diff --git a/pom.xml b/pom.xml index 87b2e77..6923593 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ eu.mclive ChatLog - 1.13.6-release + 1.14.1-release Chatlog Chatlog generator to report players @@ -26,7 +26,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.10.1 + 3.14.0 ${java.version} ${java.version} @@ -35,7 +35,7 @@ org.apache.maven.plugins maven-shade-plugin - 3.4.0 + 3.6.0 @@ -73,8 +73,7 @@ org.spigotmc spigot-api - - 1.19.4-R0.1-SNAPSHOT + 1.21.4-R0.1-SNAPSHOT provided jar @@ -87,7 +86,7 @@ org.apache.commons commons-lang3 - 3.12.0 + 3.17.0 diff --git a/resources/config.yml b/resources/config.yml index 6e67a4d..8aa79a1 100644 --- a/resources/config.yml +++ b/resources/config.yml @@ -57,4 +57,6 @@ Cleanup: # How old messages need to be before they are deleted. since: 14 -update-check: true \ No newline at end of file +update-check: true +# How often to check for updates in days, defaults to 1 day +update-interval: 1 \ No newline at end of file diff --git a/resources/plugin.yml b/resources/plugin.yml index 10b63bd..5121f50 100644 --- a/resources/plugin.yml +++ b/resources/plugin.yml @@ -1,6 +1,6 @@ name: ChatLog descriptipn: Chatlog generator to report players -version: 1.13.6 +version: 1.14.1 api-version: 1.13 author: McLive website: www.freecraft.eu @@ -8,10 +8,14 @@ website: www.freecraft.eu main: eu.mclive.ChatLog.ChatLog commands: - chatreport: - aliases: [chatlog] + chatlog: + aliases: + - chatreport description: Generate a chatlog to report players permissions: chatlog.command: - description: Allows /chatreport \ No newline at end of file + description: Allows /chatlog + + chatlog.reload: + description: Allows reloading the plugin with /chatlog reload \ No newline at end of file diff --git a/src/eu/mclive/ChatLog/AsyncChatListener.java b/src/eu/mclive/ChatLog/AsyncChatListener.java index b104007..e9fc197 100644 --- a/src/eu/mclive/ChatLog/AsyncChatListener.java +++ b/src/eu/mclive/ChatLog/AsyncChatListener.java @@ -17,6 +17,7 @@ public void onAsyncPlayerChat(final AsyncPlayerChatEvent e) { if (e.isCancelled()) { return; } - plugin.getUtils().logMessage(e.getPlayer(), e.getMessage()); + String worldName = e.getPlayer().getWorld().getName(); + plugin.getUtils().logMessage(e.getPlayer(), worldName, e.getMessage()); } } diff --git a/src/eu/mclive/ChatLog/ChatListener.java b/src/eu/mclive/ChatLog/ChatListener.java index 6c87883..c6d9e57 100644 --- a/src/eu/mclive/ChatLog/ChatListener.java +++ b/src/eu/mclive/ChatLog/ChatListener.java @@ -17,6 +17,7 @@ public void onPlayerChat(final PlayerChatEvent e) { if (e.isCancelled()) { return; } - plugin.getUtils().logMessage(e.getPlayer(), e.getMessage()); + String worldName = e.getPlayer().getWorld().getName(); + plugin.getUtils().logMessage(e.getPlayer(), worldName, e.getMessage()); } } diff --git a/src/eu/mclive/ChatLog/ChatLog.java b/src/eu/mclive/ChatLog/ChatLog.java index 8fbe66a..8e1802b 100644 --- a/src/eu/mclive/ChatLog/ChatLog.java +++ b/src/eu/mclive/ChatLog/ChatLog.java @@ -1,9 +1,9 @@ package eu.mclive.ChatLog; -import java.io.IOException; import java.util.Calendar; import java.util.Date; import java.util.concurrent.Callable; +import java.util.logging.Logger; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -11,7 +11,7 @@ import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; -import eu.mclive.ChatLog.Commands.Chatreport; +import eu.mclive.ChatLog.Commands.Chatlog; import eu.mclive.ChatLog.MySQL.MySQL; import eu.mclive.ChatLog.MySQL.MySQLHandler; import eu.mclive.ChatLog.update.UpdateListener; @@ -20,6 +20,7 @@ public class ChatLog extends JavaPlugin implements Listener { public UUIDHandler UUIDHandler; + public Logger logger = getLogger(); public MySQL sql; public Messages messages; public MySQLHandler sqlHandler; @@ -27,14 +28,8 @@ public class ChatLog extends JavaPlugin implements Listener { private eu.mclive.ChatLog.bstats.Metrics bstats; private Utils utils; - /** - * Issued ChatLogs since last submit. - */ private int issuedChatLogs = 0; - /** - * Logged Messages since last submit. - */ private int loggedMessages = 0; public void onEnable() { @@ -68,15 +63,18 @@ public void onEnable() { Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "[ChatLog] " + ChatColor.RED + "Failed to load bStats."); } } - + UpdateUtil.scheduleUpdateChecker(this); this.cleanup(); - this.registerEvents(); this.registerCommands(); - this.checkUpdates(); - - Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "[ChatLog] " + ChatColor.GREEN + "Plugin started."); - } + boolean performUpdateCheck = getConfig().getBoolean("update-check"); + if (performUpdateCheck) { + Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "[ChatLog] " + ChatColor.YELLOW + "Checking for updates..."); + UpdateUtil.checkForUpdates(this); + } + + Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "[ChatLog] " + ChatColor.GREEN + "Plugin started."); + } public void onDisable() { if (sql != null) { @@ -91,7 +89,7 @@ private void setupConfig() { } private void registerCommands() { - getCommand("chatreport").setExecutor(new Chatreport(this)); + getCommand("chatlog").setExecutor(new Chatlog(this)); } private void registerEvents() { @@ -141,9 +139,9 @@ public void run() { String server = getConfig().getString("Server-Name"); String bypassCharacter = getConfig().getString("bypass-character"); String bypassPermission = getConfig().getString("bypass-permission"); - //System.out.println(server + p + msg + timestamp); + String worldName = p.getWorld().getName(); if (bypassCharacter.isEmpty() || (msg.startsWith(bypassCharacter) && !p.hasPermission(bypassPermission)) || !msg.startsWith(bypassCharacter)) { - sqlHandler.addMessage(server, p, msg, timestamp); + sqlHandler.addMessage(server, p, msg, timestamp, worldName); } } }); diff --git a/src/eu/mclive/ChatLog/Commands/Chatreport.java b/src/eu/mclive/ChatLog/Commands/Chatlog.java similarity index 61% rename from src/eu/mclive/ChatLog/Commands/Chatreport.java rename to src/eu/mclive/ChatLog/Commands/Chatlog.java index 5b2da13..64c9dce 100644 --- a/src/eu/mclive/ChatLog/Commands/Chatreport.java +++ b/src/eu/mclive/ChatLog/Commands/Chatlog.java @@ -1,94 +1,139 @@ -package eu.mclive.ChatLog.Commands; - -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.UUID; - -import org.bukkit.Bukkit; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.ChatColor; - -import eu.mclive.ChatLog.ChatLog; - -public class Chatreport implements CommandExecutor { - private ChatLog plugin; - - public Chatreport(ChatLog plugin) { - this.plugin = plugin; - } - - private HashMap lastReport = new HashMap<>(); - - public boolean onCommand(final CommandSender sender, Command cmd, String commandLabel, final String[] args) { - final String player = sender.getName(); - - if (cmd.getName().equalsIgnoreCase("chatreport")) - if (sender.hasPermission("chatlog.command")) - { - Long last = this.lastReport.get(player); - Long cooldown = plugin.getConfig().getLong("Cooldown") * 1000; - if (last != null && cooldown > 0) { - Long now = System.currentTimeMillis(); - Long until = last + cooldown; - if (System.currentTimeMillis() <= until) { - Long left = (until - now) / 1000; - sender.sendMessage(plugin.messages.prefix + plugin.messages.command_cooldown.replace("%seconds%", left.toString())); - return true; - } - } - if (args.length == 0) { - sender.sendMessage(plugin.messages.help_above); - sender.sendMessage(plugin.messages.help.replace("%cmd%", "/" + commandLabel)); - sender.sendMessage(plugin.messages.help_below); - } - if (args.length >= 1) { - final Date now = new Date(); - final Long timestamp = now.getTime() / 1000; - final String server = plugin.getConfig().getString("Server-Name"); - boolean mode = plugin.getConfig().getBoolean("minigames-mode"); - int ChatHistory = plugin.getConfig().getInt("Chat-History"); - if (!mode) { //disabled minigame mode? Only get messages from last 15 minutes! - Calendar cal = Calendar.getInstance(); - cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) - ChatHistory); //15 minutes before - plugin.pluginstart = cal.getTimeInMillis() / 1000L; - } - Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { - public void run() { - List users = new ArrayList<>(); - for (int i = 0; i < args.length; i++) { - String user = args[i]; - int messagesSent = plugin.sqlHandler.checkMessage(server, user, plugin.pluginstart, timestamp); - if (messagesSent >= 1) { - users.add(user); - } else { - sender.sendMessage(plugin.messages.prefix + plugin.messages.no_messages_found.replace("%name%", user)); - } - } - String reportid = UUID.randomUUID().toString().replace("-", ""); - if (users != null && users.size() > 0) { - plugin.sqlHandler.setReport(server, users, plugin.pluginstart, timestamp, reportid); - String URL = plugin.getConfig().getString("URL"); - sender.sendMessage(plugin.messages.prefix + plugin.messages.url.replace("%url%", URL + reportid)); - lastReport.put(player, System.currentTimeMillis()); - plugin.incrementIssuedChatLogs(); - } else { - sender.sendMessage(plugin.messages.prefix + plugin.messages.no_report_saved); - } - } - }); - } - } - else - { - sender.sendMessage(plugin.messages.prefix + plugin.messages.no_permission); - return true; - } - return false; - } - -} +package eu.mclive.ChatLog.Commands; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.ChatColor; + +import eu.mclive.ChatLog.ChatLog; +import eu.mclive.ChatLog.Messages; + +public class Chatlog implements CommandExecutor { + private ChatLog plugin; + private Messages messages; + + public Chatlog(ChatLog plugin) { + this.plugin = plugin; + this.messages = plugin.messages; + } + + private HashMap lastReport = new HashMap<>(); + + public boolean onCommand(final CommandSender sender, Command cmd, String commandLabel, final String[] args) { + final String player = sender.getName(); + plugin.UUIDHandler.resetMessage(); + if (cmd.getName().equalsIgnoreCase("chatlog")) + if (sender.hasPermission("chatlog.command")) + { + Long last = this.lastReport.get(player); + Long cooldown = plugin.getConfig().getLong("Cooldown") * 1000; + if (last != null && cooldown > 0) { + Long now = System.currentTimeMillis(); + Long until = last + cooldown; + if (System.currentTimeMillis() <= until) { + Long left = (until - now) / 1000; + if (messages.no_messages_found != null && !messages.no_messages_found.isEmpty()) { + sender.sendMessage(plugin.messages.prefix + plugin.messages.command_cooldown.replace("%seconds%", left.toString())); + } + return true; + } + } + if (args.length == 0) { + if (messages.no_messages_found != null && !messages.no_messages_found.isEmpty()) { + sender.sendMessage(plugin.messages.help_above); + } + if (messages.no_messages_found != null && !messages.no_messages_found.isEmpty()) { + sender.sendMessage(plugin.messages.help.replace("%cmd%", "/" + commandLabel)); + } + if (messages.no_messages_found != null && !messages.no_messages_found.isEmpty()) { + sender.sendMessage(plugin.messages.help_below); + return true; + } + } + if(args.length == 1 && args[0].equalsIgnoreCase("reload")) + { + if (sender.hasPermission("chatlog.reload")) + { + sender.sendMessage(ChatColor.DARK_GREEN + "[ChatLog] " + ChatColor.YELLOW + "Plugin reloading..."); + try { + plugin.reloadConfig(); + messages.MessagesReload(); + plugin.saveConfig(); + sender.sendMessage(ChatColor.DARK_GREEN + "[ChatLog] " + ChatColor.GREEN + "Plugin successfully reloaded."); + sender.sendMessage(ChatColor.DARK_GREEN + "[ChatLog] " + ChatColor.GREEN + "You may need to restart the server for certain changes to take affect."); + } catch (Exception e) { + sender.sendMessage(ChatColor.DARK_GREEN + "[ChatLog] " + ChatColor.RED + "An error occurred while trying to reload the plugin."); + e.printStackTrace(); + } + return true; + } + else + { + if (messages.no_messages_found != null && !messages.no_messages_found.isEmpty()) { + sender.sendMessage(plugin.messages.prefix + plugin.messages.no_permission); + } + return true; + } + } + if (args.length >= 1) { + final Date now = new Date(); + final Long timestamp = now.getTime() / 1000; + final String server = plugin.getConfig().getString("Server-Name"); + boolean mode = plugin.getConfig().getBoolean("minigames-mode"); + int ChatHistory = plugin.getConfig().getInt("Chat-History"); + if (!mode) { + Calendar cal = Calendar.getInstance(); + cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) - ChatHistory); + plugin.pluginstart = cal.getTimeInMillis() / 1000L; + } + Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + public void run() { + List users = new ArrayList<>(); + for (int i = 0; i < args.length; i++) { + String user = args[i]; + int messagesSent = plugin.sqlHandler.checkMessage(server, user, plugin.pluginstart, timestamp); + if (messagesSent >= 1) { + users.add(user); + } else { + if (messages.no_messages_found != null && !messages.no_messages_found.isEmpty()) { + sender.sendMessage(plugin.messages.prefix + plugin.messages.no_messages_found.replace("%name%", user)); + } + } + } + String reportid = UUID.randomUUID().toString().replace("-", ""); + if (users != null && users.size() > 0) { + plugin.sqlHandler.setReport(server, users, plugin.pluginstart, timestamp, reportid); + String URL = plugin.getConfig().getString("URL"); + if (messages.no_messages_found != null && !messages.no_messages_found.isEmpty()) { + sender.sendMessage(plugin.messages.prefix + plugin.messages.url.replace("%url%", URL + reportid)); + } + lastReport.put(player, System.currentTimeMillis()); + plugin.incrementIssuedChatLogs(); + } else { + if (messages.no_messages_found != null && !messages.no_messages_found.isEmpty()) { + sender.sendMessage(plugin.messages.prefix + plugin.messages.no_report_saved); + } + } + } + }); + } + } + else + { + if (messages.no_messages_found != null && !messages.no_messages_found.isEmpty()) { + sender.sendMessage(plugin.messages.prefix + plugin.messages.no_permission); + } + return true; + } + return false; + } + +} diff --git a/src/eu/mclive/ChatLog/Messages.java b/src/eu/mclive/ChatLog/Messages.java index 9d0e590..7ba00b3 100644 --- a/src/eu/mclive/ChatLog/Messages.java +++ b/src/eu/mclive/ChatLog/Messages.java @@ -47,25 +47,38 @@ public Messages(ChatLog plugin) { e.printStackTrace(); } - prefix = addcolors(cfg.getString("Prefix")); - url = addcolors(cfg.getString("URL")); - help_above = addcolors(cfg.getString("Help-Above")); - help = addcolors(cfg.getString("Help")); - help_below = addcolors(cfg.getString("Help-Below")); - command_cooldown = addcolors(cfg.getString("Command-Cooldown")); - no_permission = addcolors(cfg.getString("No-Permission")); - no_messages_found = addcolors(cfg.getString("No-Messages-Found")); - no_report_saved = addcolors(cfg.getString("No-Report-Saved")); + prefix = addColors(cfg.getString("Prefix")); + url = addColors(cfg.getString("URL")); + help_above = addColors(cfg.getString("Help-Above")); + help = addColors(cfg.getString("Help")); + help_below = addColors(cfg.getString("Help-Below")); + command_cooldown = addColors(cfg.getString("Command-Cooldown")); + no_permission = addColors(cfg.getString("No-Permission")); + no_messages_found = addColors(cfg.getString("No-Messages-Found")); + no_report_saved = addColors(cfg.getString("No-Report-Saved")); } + + public void MessagesReload() { + cfg = YamlConfiguration.loadConfiguration(file); + prefix = addColors(cfg.getString("Prefix")); + url = addColors(cfg.getString("URL")); + help_above = addColors(cfg.getString("Help-Above")); + help = addColors(cfg.getString("Help")); + help_below = addColors(cfg.getString("Help-Below")); + command_cooldown = addColors(cfg.getString("Command-Cooldown")); + no_permission = addColors(cfg.getString("No-Permission")); + no_messages_found = addColors(cfg.getString("No-Messages-Found")); + no_report_saved = addColors(cfg.getString("No-Report-Saved")); + } - private String addcolors(String msg) { + private String addColors(String msg) { msg = ChatColor.translateAlternateColorCodes('&', msg); return msg; } public String help() { - return addcolors(cfg.getString("help")); + return addColors(cfg.getString("help")); } } diff --git a/src/eu/mclive/ChatLog/MySQL/MySQL.java b/src/eu/mclive/ChatLog/MySQL/MySQL.java index d5c44f9..a0311b0 100644 --- a/src/eu/mclive/ChatLog/MySQL/MySQL.java +++ b/src/eu/mclive/ChatLog/MySQL/MySQL.java @@ -132,7 +132,7 @@ public void closeConnection() { try { if (conn != null && !conn.isClosed()) { conn.close(); - Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "[ChatLog] " + ChatColor.GREEN + "MySQL successfully closed."); + Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "[ChatLog] " + ChatColor.GREEN + "MySQL successfully unloaded."); } } catch (SQLException e) { e.printStackTrace(); diff --git a/src/eu/mclive/ChatLog/MySQL/MySQLHandler.java b/src/eu/mclive/ChatLog/MySQL/MySQLHandler.java index 839e5cf..a0312eb 100644 --- a/src/eu/mclive/ChatLog/MySQL/MySQLHandler.java +++ b/src/eu/mclive/ChatLog/MySQL/MySQLHandler.java @@ -7,6 +7,11 @@ import java.util.List; import java.util.UUID; +import java.util.Map; +import java.util.HashMap; +import java.util.Arrays; + + import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; @@ -20,14 +25,83 @@ public class MySQLHandler { private ChatLog plugin; private MySQL sql; + private static final Map> EXPECTED_COLUMNS = new HashMap<>(); + + static { + EXPECTED_COLUMNS.put("messages", Arrays.asList("id", "server", "world", "name", "message", "timestamp")); + EXPECTED_COLUMNS.put("reportmessages", Arrays.asList("id", "server", "world", "name", "message", "timestamp", "reportid")); + } + public MySQLHandler(MySQL mysql, ChatLog plugin) { sql = mysql; - sql.queryUpdate("CREATE TABLE IF NOT EXISTS messages (id int NOT NULL AUTO_INCREMENT, server varchar(100), name varchar(100), message varchar(400), timestamp varchar(50), PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci"); - sql.queryUpdate("CREATE TABLE IF NOT EXISTS reportmessages (id int NOT NULL AUTO_INCREMENT, server varchar(100), name varchar(100), message varchar(400), timestamp varchar(50), reportid text, PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci"); this.plugin = plugin; + createTablesIfNeeded(); + sanityCheckDatabaseSchema(); + } + + private void createTablesIfNeeded() { + sql.queryUpdate("CREATE TABLE IF NOT EXISTS messages (" + + "id INT NOT NULL AUTO_INCREMENT, " + + "server VARCHAR(100), " + + "world VARCHAR(100), " + + "name VARCHAR(100), " + + "message VARCHAR(400), " + + "timestamp VARCHAR(50), " + + "PRIMARY KEY (id)) " + + "DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci"); + + sql.queryUpdate("CREATE TABLE IF NOT EXISTS reportmessages (" + + "id INT NOT NULL AUTO_INCREMENT, " + + "server VARCHAR(100), " + + "world VARCHAR(100), " + + "name VARCHAR(100), " + + "message VARCHAR(400), " + + "timestamp VARCHAR(50), " + + "reportid TEXT, " + + "PRIMARY KEY (id)) " + + "DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci"); } - public void addMessage(String server, Player p, String msg, Long timestamp) { + private void sanityCheckDatabaseSchema() { + Connection conn = sql.getConnection(); + try { + for (Map.Entry> entry : EXPECTED_COLUMNS.entrySet()) { + String tableName = entry.getKey(); + List expectedColumns = entry.getValue(); + + for (String column : expectedColumns) { + if (!columnExists(conn, tableName, column)) { + addColumn(tableName, column); + } + } + } + } catch (SQLException e) { + e.printStackTrace(); + Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "[ChatLog] " + ChatColor.RED + "Failed to update database schema: " + e.getMessage()); + } + } + + private boolean columnExists(Connection conn, String tableName, String columnName) throws SQLException { + String query = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND COLUMN_NAME = ?"; + try (PreparedStatement stmt = conn.prepareStatement(query)) { + stmt.setString(1, tableName); + stmt.setString(2, columnName); + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + return rs.getInt(1) > 0; + } + } + } + return false; + } + + private void addColumn(String tableName, String columnName) { + String alterTableQuery = "ALTER TABLE " + tableName + " ADD COLUMN " + columnName + " VARCHAR(100);"; + sql.queryUpdate(alterTableQuery); + Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "[ChatLog] " + ChatColor.YELLOW + "Column '" + columnName + "' added to table '" + tableName + "'."); + } + + public void addMessage(String server, Player p, String msg, Long timestamp, String worldName) { String name; if (plugin.getConfig().getBoolean("use-UUIDs")) { @@ -38,11 +112,12 @@ public void addMessage(String server, Player p, String msg, Long timestamp) { } Connection conn = sql.getConnection(); - try (PreparedStatement st = conn.prepareStatement("INSERT INTO messages (server, name, message, timestamp) VALUES (?,?,?,?);")) { + try (PreparedStatement st = conn.prepareStatement("INSERT INTO messages (server, world, name, message, timestamp) VALUES (?,?,?,?,?);")) { st.setString(1, server); - st.setString(2, name); - st.setString(3, msg); - st.setLong(4, timestamp); + st.setString(2, worldName); + st.setString(3, name); + st.setString(4, msg); + st.setLong(5, timestamp); st.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); @@ -53,7 +128,6 @@ public int checkMessage(String server, String p2, Long pluginstart, Long timesta String name = null; if (plugin.getConfig().getBoolean("use-UUIDs")) { - //player could be offline name = plugin.UUIDHandler.getUUID(p2); } else { name = p2; @@ -68,7 +142,6 @@ public int checkMessage(String server, String p2, Long pluginstart, Long timesta st.setLong(4, timestamp); rs = st.executeQuery(); rs.next(); - //System.out.println("Von " + p2 + " gesendete Nachrichten seit Pluginstart: " + rs.getInt("count") ); return rs.getInt("count"); } catch (SQLException e) { e.printStackTrace(); @@ -82,7 +155,6 @@ public void setReport(String server, List users, Long pluginstart, Long Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "[ChatLog] " + ChatColor.GREEN + "ReportID: " + ChatColor.YELLOW + reportid); for (String user : users) { if (plugin.getConfig().getBoolean("use-UUIDs")) { - //player could be offline user = plugin.UUIDHandler.getUUID(user); } try (PreparedStatement st = conn.prepareStatement("SELECT * FROM messages WHERE server = ? && name = ? && timestamp >= ? && timestamp <= ?;")) { @@ -92,12 +164,14 @@ public void setReport(String server, List users, Long pluginstart, Long st.setLong(4, timestamp); rs = st.executeQuery(); while (rs.next()) { - try (PreparedStatement st2 = conn.prepareStatement("INSERT INTO reportmessages (server, name, message, timestamp, reportid) VALUES (?,?,?,?,?);")) { + String world = rs.getString("world"); + try (PreparedStatement st2 = conn.prepareStatement("INSERT INTO reportmessages (server, name, world, message, timestamp, reportid) VALUES (?,?,?,?,?,?);")) { st2.setString(1, server); st2.setString(2, user); - st2.setString(3, rs.getString("message")); - st2.setLong(4, rs.getLong("timestamp")); - st2.setString(5, reportid); + st2.setString(3, world); + st2.setString(4, rs.getString("message")); + st2.setLong(5, rs.getLong("timestamp")); + st2.setString(6, reportid); st2.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); diff --git a/src/eu/mclive/ChatLog/Permission.java b/src/eu/mclive/ChatLog/Permission.java index 293754a..93df9c1 100644 --- a/src/eu/mclive/ChatLog/Permission.java +++ b/src/eu/mclive/ChatLog/Permission.java @@ -1,8 +1,5 @@ package eu.mclive.ChatLog; -/** - * Created by McLive on 28.10.2016. - */ public class Permission { public static final String UPDATE = "chatlog.update"; } diff --git a/src/eu/mclive/ChatLog/UUIDHandler.java b/src/eu/mclive/ChatLog/UUIDHandler.java index a4cde74..c287ef1 100644 --- a/src/eu/mclive/ChatLog/UUIDHandler.java +++ b/src/eu/mclive/ChatLog/UUIDHandler.java @@ -7,8 +7,6 @@ import java.util.UUID; import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; -import org.bukkit.command.ConsoleCommandSender; import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.Listener; @@ -18,19 +16,27 @@ public class UUIDHandler implements Listener { private ChatLog plugin; + private boolean messageSent; public UUIDHandler(ChatLog plugin) { this.plugin = plugin; + this.messageSent = false; } public String getUUID(String player) { Player p = Bukkit.getServer().getPlayer(player); if (p != null) { UUID uuid = Bukkit.getServer().getPlayer(player).getUniqueId(); + if (!messageSent) { Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "[ChatLog] " + ChatColor.AQUA + p.getName() + ChatColor.GREEN + " is online! UUID: " + ChatColor.YELLOW + uuid.toString().replace("-", "")); + messageSent = true; + } return uuid.toString().replace("-", ""); } else { + if (!messageSent) { Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "[ChatLog] " + ChatColor.AQUA + player + ChatColor.RED + " is offline! Fetching UUID from" + ChatColor.YELLOW +" https://api.minetools.eu/"); + messageSent = true; + } final JSONParser jsonParser = new JSONParser(); try { HttpURLConnection connection = (HttpURLConnection) new URL("https://api.minetools.eu/uuid/" + player).openConnection(); @@ -57,4 +63,8 @@ public String getUUID(String player) { } } + + public void resetMessage() { + messageSent = false; + } } \ No newline at end of file diff --git a/src/eu/mclive/ChatLog/Utils.java b/src/eu/mclive/ChatLog/Utils.java index b9f70b4..14d9406 100644 --- a/src/eu/mclive/ChatLog/Utils.java +++ b/src/eu/mclive/ChatLog/Utils.java @@ -3,9 +3,6 @@ import org.bukkit.ChatColor; import org.bukkit.entity.Player; -/** - * Created by McLive on 08.10.2016. - */ public class Utils { private ChatLog plugin; @@ -13,7 +10,7 @@ public Utils(ChatLog plugin) { this.plugin = plugin; } - public void logMessage(Player p, String msg) { + public void logMessage(Player p, String worldName, String msg) { plugin.addMessage(p, ChatColor.stripColor(msg)); plugin.incrementLoggedMessages(); } diff --git a/src/eu/mclive/ChatLog/update/UpdateUtil.java b/src/eu/mclive/ChatLog/update/UpdateUtil.java index 1e7a08e..6e8c57f 100644 --- a/src/eu/mclive/ChatLog/update/UpdateUtil.java +++ b/src/eu/mclive/ChatLog/update/UpdateUtil.java @@ -28,6 +28,47 @@ public class UpdateUtil { private final static int PLUGIN = 1128; private final static String LATEST_VERSION = "/versions/latest"; + public static void scheduleUpdateChecker(final Plugin plugin) { + boolean updateCheckEnabled = plugin.getConfig().getBoolean("update-check"); + if (!updateCheckEnabled) { + Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "[ChatLog] " + ChatColor.GREEN + "Update check: " + ChatColor.AQUA + "disabled."); + return; + } + + long updateInterval = plugin.getConfig().getLong("update-interval"); + if (updateInterval <= 0) { + updateInterval = 1; + } + long delay = 5 * 20; + long interval = updateInterval * 24 * 60 * 60 * 20; + new BukkitRunnable() { + @Override + public void run() { + checkForUpdates(plugin); + } + }.runTaskTimer(plugin, delay, interval); + Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "[ChatLog] " + ChatColor.GREEN + "Update check: " + ChatColor.AQUA + "enabled."); +} + + public static void checkForUpdates(final Plugin plugin) { + Bukkit.getScheduler().runTask(plugin, new Runnable() { + @Override + public void run() { + final String message = getUpdateMessage(true, plugin); + if (message != null) { + Bukkit.getScheduler().runTask(plugin, new Runnable() { + @Override + public void run() { + plugin.getLogger().warning(message); + } + }); + } else { + Bukkit.getLogger().info("No update available."); + } + } + }); +} + public static void sendUpdateMessage(final UUID uuid, final Plugin plugin) { if (plugin.getConfig().getBoolean("update-check")) new BukkitRunnable() {