This plugin logs all player messages including commands.
To install this plugin, copy the plugin JAR file to your server's plugins folder.
I currently run a Rainbow server, but I want greater functionality than the Rainbow API provides, so I've been learning Spigot. This is the first serious plugin that I wrote for Spigot that I'll need when I migrate my server from Rainbow to Spigot.
Code (Text):
package FredashaySpigotChatLogger;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.command.CommandExecutor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
public class MyPlugin extends JavaPlugin implements Listener, CommandExecutor {
private static Logger logger = null;
private static PluginDescriptionFile pdfFile = null;
@Override
public void onEnable() {
logger = Logger.getLogger("Minecraft");
pdfFile = getDescription();
getServer().getPluginManager().registerEvents(this,this);
}
@EventHandler
public void onPlayerCommand(PlayerCommandPreprocessEvent event) {
logMessage(event.getPlayer(), event.getMessage());
}
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent event) {
logMessage(event.getPlayer(), event.getMessage());
}
private void logMessage(Player player, String message) {
long currentUnixTime = 0;
SimpleDateFormat mySdfDate = null;
SimpleDateFormat mySdfTime = null;
Date messageDateTime = null;
String messagePrettyDate = null;
String messagePrettyTime = null;
boolean logExists = false;
Location whereAt = null;
String locString = null;
currentUnixTime = System.currentTimeMillis();
mySdfDate = new SimpleDateFormat("yyyy-MM-dd");
mySdfTime = new SimpleDateFormat("HH:mm:ss");
messageDateTime = new java.sql.Date(currentUnixTime);
messagePrettyDate = mySdfDate.format(messageDateTime);
messagePrettyTime = mySdfTime.format(messageDateTime);
String logFilename = null;
File logFile = null;
String logFileName = "FredashayChatLog-" + messagePrettyDate + ".csv";
FileOutputStream logStream = null;
logFile = new File(logFileName);
try {
logExists = false;
if (logFile.exists()) {
logExists = true;
}
logStream = new FileOutputStream(logFile, true);
if (!logExists) {
nyanCat(logStream, "Timestamp, Date, Time, Player Name, Player IP, Game World Location, Chat Message, ; ");
}
whereAt = player.getLocation();
locString = whereAt.getWorld().getName() + " (" + Math.round(whereAt.getBlockX()) + " / " + Math.round(whereAt.getBlockY()) + " / " + Math.round(whereAt.getBlockZ()) + ")";
nyanCat(logStream, currentUnixTime + ", " + messagePrettyDate + ", " + messagePrettyTime + ", " + player.getName() + ", " + player.getAddress() + ", " + locString + ",\"" + message.replaceAll("\"", "''") + "\",; ");
logStream.close();
}
catch (IOException oops) {
logger.info("[" + pdfFile.getName() + "] Error writing Chat Log file '" + logFilename + "'. ");
oops.printStackTrace(System.err);
}
}
private void nyanCat(FileOutputStream fileStream, String output) throws IOException {
try {
String newline = System.getProperty("line.separator");
output = output + newline;
byte[] data = output.getBytes();
fileStream.write(data, 0, data.length);
}
catch (IOException oops) {
logger.info("[" + pdfFile.getName() + "] Error writing file. ");
oops.printStackTrace(System.err);
}
}
// private void doNothing() { }
}