From ef8abe223b02d4167cb290c01e1f7205f970ace3 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Tue, 27 Nov 2018 16:05:04 +0100 Subject: [PATCH 1/3] Give player feedback when they submit an application. --- .../minecraftmanager/listeners/AsyncPlayerChatListener.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/xyz/etztech/minecraftmanager/listeners/AsyncPlayerChatListener.java b/src/main/java/xyz/etztech/minecraftmanager/listeners/AsyncPlayerChatListener.java index 1dc6295..8b05223 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/listeners/AsyncPlayerChatListener.java +++ b/src/main/java/xyz/etztech/minecraftmanager/listeners/AsyncPlayerChatListener.java @@ -141,6 +141,7 @@ public class AsyncPlayerChatListener implements Listener { public void invoke(String s) { MCMResponse response = new MCMResponse(s); if (response.getStatus()) { + player.sendMessage(ChatColor.GREEN + "Your application has been submitted! Hang tight while it gets reviewed by staff."); MinecraftManagerThread.application(player.getName(), response.getExtra().getAsString()); } else { player.sendMessage(ChatColor.RED + response.getMessage()); From 4cb519c3fc4265f2c5f0e1d015f38ff1b1adad3b Mon Sep 17 00:00:00 2001 From: Etzelia Date: Thu, 6 Dec 2018 23:20:39 +0100 Subject: [PATCH 2/3] Re-initialize MCMUtil on reload to update log file handlers. --- src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java b/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java index 695df12..dfa238d 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java +++ b/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java @@ -108,6 +108,7 @@ public class MinecraftManager extends JavaPlugin implements IMinecraftManager { } rules = new Rules(config); banOptions = config.getStringList("ban.options"); + MCMUtil.init(); } From cba8c226b620b02c87c8eee7d0782dbc6e4a2244 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Fri, 7 Dec 2018 17:07:54 -0600 Subject: [PATCH 3/3] Added log4j Fixes #11 --- pom.xml | 5 ++ .../xyz/etztech/minecraftmanager/MCMUtil.java | 54 ++++++++----------- .../minecraftmanager/MinecraftManager.java | 4 +- .../command/CommandApplication.java | 4 +- .../command/CommandTicket.java | 2 +- .../command/CommandWarning.java | 2 +- .../listeners/AsyncPlayerChatListener.java | 3 +- .../listeners/SessionListener.java | 4 +- src/main/resources/log4j.properties | 17 ++++++ 9 files changed, 52 insertions(+), 43 deletions(-) create mode 100644 src/main/resources/log4j.properties diff --git a/pom.xml b/pom.xml index 721dfc4..54e836c 100644 --- a/pom.xml +++ b/pom.xml @@ -39,6 +39,11 @@ commons-lang 2.6 + + log4j + log4j + 1.2.17 + xyz.etztech EtzCore diff --git a/src/main/java/xyz/etztech/minecraftmanager/MCMUtil.java b/src/main/java/xyz/etztech/minecraftmanager/MCMUtil.java index eb56a30..b525856 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/MCMUtil.java +++ b/src/main/java/xyz/etztech/minecraftmanager/MCMUtil.java @@ -1,46 +1,39 @@ package xyz.etztech.minecraftmanager; import com.google.gson.JsonArray; -import net.md_5.bungee.api.chat.*; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.ComponentBuilder; +import net.md_5.bungee.api.chat.HoverEvent; +import net.md_5.bungee.api.chat.TextComponent; import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.entity.Player; import xyz.etztech.core.web.CoreWeb; import xyz.etztech.minecraftmanager.objects.ModelResponse; -import javax.xml.soap.Text; -import java.io.FileWriter; -import java.io.PrintWriter; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.HashMap; -import java.util.List; import java.util.Map; public class MCMUtil { - private static FileWriter globalFile; - public static PrintWriter globalLog; - private static FileWriter staffFile; - public static PrintWriter staffLog; + public static Logger globalLogger; + public static Logger staffLogger; + public static void init() { MinecraftManager plugin = (MinecraftManager) Bukkit.getPluginManager().getPlugin("MinecraftManager"); String pluginPath = plugin.getDataFolder().getAbsolutePath(); - String globalPath = pluginPath + "/logs/global.log"; + System.setProperty("mcm.log", pluginPath + "/logs"); try { - globalFile = new FileWriter(globalPath, true); - globalLog = new PrintWriter(globalFile); + globalLogger = Logger.getLogger("mcm.global"); } catch (Exception e) { - plugin.log("Could not connect to global log (" + globalPath + ")."); + plugin.log("Could not connect to global log."); } if (plugin.getConfig().getBoolean("staff-chat.enabled")) { - String staffPath = pluginPath + "/logs/staff.log"; try { - staffFile = new FileWriter(staffPath, true); - staffLog = new PrintWriter(staffFile); + globalLogger = Logger.getLogger("mcm.staff"); } catch (Exception e) { - plugin.log("Could not connect to staff log (" + staffPath + ")."); + plugin.log("Could not connect to staff log."); } } } @@ -49,7 +42,7 @@ public class MCMUtil { staffMessage(message, false); } public static void staffMessage(String message, boolean mcm) { - log(staffLog, message); + log(staffLogger, message); String tag = mcm ? "MCM " : ""; HoverEvent hover = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(tag + "Staff Chat").create()); TextComponent text = new TextComponent(message); @@ -64,7 +57,7 @@ public class MCMUtil { @Deprecated public static void staffMessage(String message, String hoverCommand) { - log(staffLog, message); + log(staffLogger, message); HoverEvent hover = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Staff Chat").create()); TextComponent text = new TextComponent(message); text.setHoverEvent(hover); @@ -80,7 +73,7 @@ public class MCMUtil { } public static void staffMessage(TextComponent message) { - log(staffLog, message.toPlainText()); + log(staffLogger, message.toPlainText()); for (Player player : Bukkit.getServer().getOnlinePlayers()) { if (player.hasPermission("minecraftmanager.staff")) { player.spigot().sendMessage(message); @@ -90,14 +83,14 @@ public class MCMUtil { } public static void globalMessage(String message) { - log(globalLog, message); + log(globalLogger, message); for (Player player : Bukkit.getServer().getOnlinePlayers()) { player.sendMessage(message); } } public static void globalMessage(TextComponent message) { - log(globalLog, message.toPlainText()); + log(globalLogger, message.toPlainText()); for (Player player : Bukkit.getServer().getOnlinePlayers()) { player.spigot().sendMessage(message); } @@ -122,18 +115,13 @@ public class MCMUtil { return uuid; } - public static void log(PrintWriter logger, String message) { + public static void log(Logger logger, String message) { try { - //[4/21/17 7:12 PM] - //Bukkit.getServer().getConsoleSender().sendMessage(message); message = ChatColor.stripColor(message); - Date date = new Date(); - SimpleDateFormat sdf = new SimpleDateFormat("M/d/yy hh:mm a"); - String text = "[" + sdf.format(date) + "] " + message; - logger.append(text + "\n"); - logger.flush(); + logger.info(message); } catch (Exception ex) { Bukkit.getServer().getConsoleSender().sendMessage("Could not log message! Check your log files!"); } } + } diff --git a/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java b/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java index dfa238d..d542e8e 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java +++ b/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java @@ -103,12 +103,12 @@ public class MinecraftManager extends JavaPlugin implements IMinecraftManager { public void reloadConfig() { super.reloadConfig(); loadConfig(); + MCMUtil.init(); if (chatListener != null) { chatListener.reload(); } rules = new Rules(config); banOptions = config.getStringList("ban.options"); - MCMUtil.init(); } @@ -197,7 +197,7 @@ public class MinecraftManager extends JavaPlugin implements IMinecraftManager { @Override public void globalLog(Player player, String message) { - MCMUtil.log(MCMUtil.globalLog, player.getName() + " > " + message); + MCMUtil.log(MCMUtil.globalLogger, player.getName() + " > " + message); } } diff --git a/src/main/java/xyz/etztech/minecraftmanager/command/CommandApplication.java b/src/main/java/xyz/etztech/minecraftmanager/command/CommandApplication.java index 34dca75..3048ec9 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/command/CommandApplication.java +++ b/src/main/java/xyz/etztech/minecraftmanager/command/CommandApplication.java @@ -199,7 +199,7 @@ public class CommandApplication implements CommandExecutor { @Override public void invoke(String s) { MCMResponse response = new MCMResponse(s); - commandSender.sendMessage(response.getMCMessage()); + commandSender.spigot().sendMessage(response.getMCJSON()); } } @@ -236,7 +236,7 @@ public class CommandApplication implements CommandExecutor { @Override public void invoke(String s) { MCMResponse response = new MCMResponse(s); - commandSender.sendMessage(response.getMCMessage()); + commandSender.spigot().sendMessage(response.getMCJSON()); } } diff --git a/src/main/java/xyz/etztech/minecraftmanager/command/CommandTicket.java b/src/main/java/xyz/etztech/minecraftmanager/command/CommandTicket.java index df7ea3b..38b3172 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/command/CommandTicket.java +++ b/src/main/java/xyz/etztech/minecraftmanager/command/CommandTicket.java @@ -39,7 +39,7 @@ public class CommandTicket implements CommandExecutor { @Override public void invoke(String s) { MCMResponse response = new MCMResponse(s); - player.sendMessage(response.getMCMessage()); + player.spigot().sendMessage(response.getMCJSON()); try { JsonObject extra = (JsonObject) response.getExtra(); MinecraftManagerThread.ticket(player.getName(), extra.get("id").getAsString(), extra.get("link").getAsString()); diff --git a/src/main/java/xyz/etztech/minecraftmanager/command/CommandWarning.java b/src/main/java/xyz/etztech/minecraftmanager/command/CommandWarning.java index 48e9ab7..24f93a3 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/command/CommandWarning.java +++ b/src/main/java/xyz/etztech/minecraftmanager/command/CommandWarning.java @@ -34,7 +34,7 @@ public class CommandWarning implements CommandExecutor { @Override public void invoke(String s) { MCMResponse response = new MCMResponse(s); - player.sendMessage(response.getMCMessage()); + player.spigot().sendMessage(response.getMCJSON()); } } diff --git a/src/main/java/xyz/etztech/minecraftmanager/listeners/AsyncPlayerChatListener.java b/src/main/java/xyz/etztech/minecraftmanager/listeners/AsyncPlayerChatListener.java index 8b05223..366d451 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/listeners/AsyncPlayerChatListener.java +++ b/src/main/java/xyz/etztech/minecraftmanager/listeners/AsyncPlayerChatListener.java @@ -31,7 +31,6 @@ public class AsyncPlayerChatListener implements Listener { public AsyncPlayerChatListener(MinecraftManager minecraftManager) { this.plugin = minecraftManager; reload(); - MCMUtil.init(); } public void reload() { @@ -74,7 +73,7 @@ public class AsyncPlayerChatListener implements Listener { } else { message.append(chat); if (!MinecraftManager.getLogOverride()) { - MCMUtil.log(MCMUtil.globalLog, componentText(message.create())); + MCMUtil.log(MCMUtil.globalLogger, componentText(message.create())); } } } else { diff --git a/src/main/java/xyz/etztech/minecraftmanager/listeners/SessionListener.java b/src/main/java/xyz/etztech/minecraftmanager/listeners/SessionListener.java index ed5618d..f840450 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/listeners/SessionListener.java +++ b/src/main/java/xyz/etztech/minecraftmanager/listeners/SessionListener.java @@ -38,7 +38,7 @@ public class SessionListener implements Listener { @EventHandler public void onJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); - MCMUtil.log(MCMUtil.globalLog, player.getName() + " logged in."); + MCMUtil.log(MCMUtil.globalLogger, player.getName() + " logged in."); // Check for guests if (player.hasPermission("minecraftmanager.guest")) { @@ -72,7 +72,7 @@ public class SessionListener implements Listener { @EventHandler public void onLogout(PlayerQuitEvent event) { Player player = event.getPlayer(); - MCMUtil.log(MCMUtil.globalLog, player.getName() + " logged out."); + MCMUtil.log(MCMUtil.globalLogger, player.getName() + " logged out."); } diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties new file mode 100644 index 0000000..627810c --- /dev/null +++ b/src/main/resources/log4j.properties @@ -0,0 +1,17 @@ +# Root logger option +log4j.rootLogger=INFO, mcm.global, mcm.staff + +# Redirect log messages to a log file, support file rolling. +log4j.appender.mcm.global=org.apache.log4j.RollingFileAppender +log4j.appender.mcm.global.File=${mcm.log}/global.log +log4j.appender.mcm.global.MaxFileSize=5MB +log4j.appender.mcm.global.MaxBackupIndex=5 +log4j.appender.mcm.global.layout=org.apache.log4j.PatternLayout +log4j.appender.mcm.global.layout.ConversionPattern=[%d{M/d/yy hh:mm a}] %m%n + +log4j.appender.mcm.staff=org.apache.log4j.RollingFileAppender +log4j.appender.mcm.staff.File=${mcm.log}/staff.log +log4j.appender.mcm.staff.MaxFileSize=5MB +log4j.appender.mcm.staff.MaxBackupIndex=5 +log4j.appender.mcm.staff.layout=org.apache.log4j.PatternLayout +log4j.appender.mcm.staff.layout.ConversionPattern=[%d{M/d/yy hh:mm a}] %m%n \ No newline at end of file