/stats/src/Stats.java

https://github.com/ricin/minecraft-hey0-plugins · Java · 251 lines · 160 code · 85 blank · 6 comment · 3 complexity · 96a05af7adad5f972dd6443f4f3e1bda MD5 · raw file

  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.logging.MemoryHandler;
  4. import java.util.logging.FileHandler;
  5. import java.util.logging.Handler;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import java.util.logging.LogRecord;
  9. import java.util.logging.Formatter;
  10. import java.util.Date.*;
  11. import java.util.Calendar;
  12. import java.text.SimpleDateFormat;
  13. import java.util.Arrays;
  14. public class Stats extends Plugin {
  15. public PropertiesFile propertiesFile;
  16. public Date date;
  17. public Date oldDate;
  18. public Calendar cal;
  19. public String[] lineArray;
  20. public SimpleDateFormat dateFormatLogEntry = new SimpleDateFormat("yyMMdd-HH.mm.ss");
  21. public SimpleDateFormat dateFormatLogFile = new SimpleDateFormat("yyMMdd");
  22. protected static final Logger log = Logger.getLogger("Minecraft");
  23. protected static final Logger statLogger = Logger.getLogger("Stats");
  24. private final String newLine = System.getProperty("line.separator");
  25. private final String fileSep = System.getProperty("file.separator");
  26. static ArrayList<String> playerList = new ArrayList<String>();
  27. static ArrayList<StatsMemoryHandler> playerLoggers = new ArrayList<StatsMemoryHandler>();
  28. private String driver, username, password, host, db, logDir, connection;
  29. private int maxLogLines;
  30. public Stats() {
  31. propertiesFile = new PropertiesFile("stats.properties");
  32. statLogger.setUseParentHandlers(false);
  33. }
  34. public boolean load() {
  35. try {
  36. File f = new File("stats.properties");
  37. if (f.exists())
  38. {
  39. propertiesFile.load();
  40. }
  41. else
  42. {
  43. f.createNewFile();
  44. }
  45. } catch (Exception e) {
  46. log.log(Level.SEVERE, "[Stats] : Exception while creating Stats properties file.", e);
  47. }
  48. maxLogLines = propertiesFile.getInt("maxLogLines", 200);
  49. driver = propertiesFile.getString("driver", "com.mysql.jdbc.Driver");
  50. username = propertiesFile.getString("user", "mcstats");
  51. password = propertiesFile.getString("pass", "mcstats");
  52. host = propertiesFile.getString("host", "jdbc:mysql://localhost:3306/");
  53. db = propertiesFile.getString("db", "mcstats");
  54. try {
  55. propertiesFile.save();
  56. } catch (Exception e) {
  57. log.log(Level.SEVERE, "[Stats] : Exception while saving Stats properties file.", e);
  58. }
  59. try {
  60. Class.forName(driver);
  61. } catch (ClassNotFoundException ex) {
  62. log.log(Level.SEVERE, "[Stats] : Unable to find class " + driver, ex);
  63. }
  64. connection = host+db+"?user="+username+"&password="+password+"&database="+db;
  65. // Create loggers
  66. createLogger("movements", maxLogLines);
  67. createLogger("actions", maxLogLines);
  68. createLogger("connections", 5 );
  69. return true;
  70. }
  71. public void enable() {
  72. if (load())
  73. {
  74. log.info("[Stats] Mod Enabled.");
  75. }
  76. else
  77. {
  78. log.info("[Stats] Error while loading.");
  79. }
  80. }
  81. public void disable() {
  82. removeAllLoggers();
  83. log.info("[Stats] Mod Disabled.");
  84. }
  85. public String onLoginChecks(String user) {
  86. throw new UnsupportedOperationException("Not supported yet.");
  87. }
  88. public void onLogin(Player player) {
  89. StatsLogRecord logRecord = new StatsLogRecord(player.getName().toLowerCase());
  90. logRecord.setLogin();
  91. statLogger.log(logRecord);
  92. }
  93. public void onDisconnect(Player player) {
  94. StatsLogRecord logRecord = new StatsLogRecord(player.getName().toLowerCase());
  95. logRecord.setDisconnect();
  96. statLogger.log(logRecord);
  97. }
  98. public boolean onChat(Player player, String chatMessage) {
  99. StatsLogRecord logRecord = new StatsLogRecord(player.getName().toLowerCase());
  100. logRecord.setChat(chatMessage);
  101. statLogger.log(logRecord);
  102. return false;
  103. }
  104. public boolean onCommand(Player player, String[] split) {
  105. StatsLogRecord logRecord = new StatsLogRecord(player.getName().toLowerCase());
  106. logRecord.setCommand(split);
  107. statLogger.log(logRecord);
  108. return false;
  109. }
  110. public void onBan(Player player, String reason) {
  111. StatsLogRecord logRecord = new StatsLogRecord(player.getName().toLowerCase());
  112. logRecord.setBan(reason);
  113. statLogger.log(logRecord);
  114. }
  115. public void onIpBan(Player player, String reason) {
  116. StatsLogRecord logRecord = new StatsLogRecord(player.getName().toLowerCase());
  117. logRecord.setIpBan(reason);
  118. statLogger.log(logRecord);
  119. }
  120. public void onKick(Player player, String reason) {
  121. StatsLogRecord logRecord = new StatsLogRecord(player.getName().toLowerCase());
  122. logRecord.setKick(reason);
  123. statLogger.log(logRecord);
  124. }
  125. public boolean onBlockCreate(Player player, Block blockPlaced, Block blockClicked, int item) {
  126. StatsLogRecord logRecord = new StatsLogRecord(player.getName().toLowerCase());
  127. logRecord.setCreate(blockPlaced, blockClicked, item);
  128. statLogger.log(logRecord);
  129. return false;
  130. }
  131. public boolean onBlockDestroy(Player player, Block block) {
  132. StatsLogRecord logRecord = new StatsLogRecord(player.getName().toLowerCase());
  133. logRecord.setDestroy(block);
  134. statLogger.log(logRecord);
  135. return false;
  136. }
  137. public void onPlayerMove(Player player, Location fromLocation, Location toLocation) {
  138. StatsLogRecord logRecord = new StatsLogRecord(player.getName().toLowerCase());
  139. logRecord.setMovement(fromLocation, toLocation, Math.abs(fromLocation.x - toLocation.x) + Math.abs(fromLocation.y - toLocation.y) + Math.abs(fromLocation.z - toLocation.z));
  140. statLogger.log(logRecord);
  141. }
  142. public void createLogger(String table, int bufferSize) {
  143. try {
  144. // Create a memory handler with a memory of 200 records
  145. // and dumps the records into the file my.log when a
  146. // some abitrary condition occurs
  147. // set up the JDBCLogger handler
  148. StatsJDBCHandler jdbcHandler = new StatsJDBCHandler(driver,connection);
  149. StatsMemoryHandler mhandler = new StatsMemoryHandler(table, bufferSize, jdbcHandler);
  150. // Add to the desired logger
  151. statLogger.addHandler(mhandler);
  152. } catch (Exception e) {
  153. log.info("Exception" + e);
  154. }
  155. }
  156. private static void removeAllLoggers()
  157. {
  158. for (Handler statsHandler : statLogger.getHandlers())
  159. {
  160. ((StatsMemoryHandler)statsHandler).push();
  161. statsHandler.flush();
  162. statsHandler.close();
  163. statLogger.removeHandler(statsHandler);
  164. }
  165. }
  166. }