/src/main/java/com/sk89q/worldguard/blacklist/loggers/DatabaseLoggerHandler.java

https://github.com/talmor/worldguard · Java · 207 lines · 104 code · 18 blank · 85 comment · 20 complexity · ea18566eb01624e45925aaf1920449a1 MD5 · raw file

  1. // $Id$
  2. /*
  3. * WorldGuard
  4. * Copyright (C) 2010 sk89q <http://www.sk89q.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.sk89q.worldguard.blacklist.loggers;
  20. import java.sql.Connection;
  21. import java.sql.DriverManager;
  22. import java.sql.PreparedStatement;
  23. import java.sql.SQLException;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import com.sk89q.worldedit.Vector;
  27. import com.sk89q.worldguard.LocalPlayer;
  28. import com.sk89q.worldguard.blacklist.events.BlacklistEvent;
  29. import com.sk89q.worldguard.blacklist.events.BlockBreakBlacklistEvent;
  30. import com.sk89q.worldguard.blacklist.events.BlockPlaceBlacklistEvent;
  31. import com.sk89q.worldguard.blacklist.events.BlockInteractBlacklistEvent;
  32. import com.sk89q.worldguard.blacklist.events.DestroyWithBlacklistEvent;
  33. import com.sk89q.worldguard.blacklist.events.ItemAcquireBlacklistEvent;
  34. import com.sk89q.worldguard.blacklist.events.ItemDropBlacklistEvent;
  35. import com.sk89q.worldguard.blacklist.events.ItemUseBlacklistEvent;
  36. /**
  37. *
  38. * @author sk89q
  39. */
  40. public class DatabaseLoggerHandler implements BlacklistLoggerHandler {
  41. /**
  42. * Logger.
  43. */
  44. private static final Logger logger = Logger.getLogger("Minecraft.WorldGuard");
  45. /**
  46. * DSN.
  47. */
  48. private String dsn;
  49. /**
  50. * Username.
  51. */
  52. private String user;
  53. /**
  54. * Password.
  55. */
  56. private String pass;
  57. /**
  58. * Table.
  59. */
  60. private String table;
  61. /**
  62. * World name.
  63. */
  64. private String worldName;
  65. /**
  66. * Database connection.
  67. */
  68. private Connection conn;
  69. /**
  70. * Construct the object.
  71. *
  72. * @param dsn
  73. * @param user
  74. * @param pass
  75. * @param table
  76. * @param worldName
  77. */
  78. public DatabaseLoggerHandler(String dsn, String user, String pass, String table, String worldName) {
  79. this.dsn = dsn;
  80. this.user = user;
  81. this.pass = pass;
  82. this.table = table;
  83. this.worldName = worldName;
  84. }
  85. /**
  86. * Gets the database connection.
  87. *
  88. * @return
  89. * @throws SQLException
  90. */
  91. private Connection getConnection() throws SQLException {
  92. if (conn == null || conn.isClosed()) {
  93. conn = DriverManager.getConnection(dsn, user, pass);
  94. }
  95. return conn;
  96. }
  97. /**
  98. * Log an event to the database.
  99. *
  100. * @param event
  101. * @param name
  102. * @param x
  103. * @param y
  104. * @param z
  105. * @param item
  106. * @param comment
  107. */
  108. private void logEvent(String event, LocalPlayer player, Vector pos, int item,
  109. String comment) {
  110. try {
  111. Connection conn = getConnection();
  112. PreparedStatement stmt = conn.prepareStatement(
  113. "INSERT INTO " + table
  114. + "(event, world, player, x, y, z, item, time, comment) VALUES "
  115. + "(?, ?, ?, ?, ?, ?, ?, ?, ?)");
  116. stmt.setString(1, event);
  117. stmt.setString(2, worldName);
  118. stmt.setString(3, player.getName());
  119. stmt.setInt(4, pos.getBlockX());
  120. stmt.setInt(5, pos.getBlockY());
  121. stmt.setInt(6, pos.getBlockZ());
  122. stmt.setInt(7, item);
  123. stmt.setInt(8, (int)(System.currentTimeMillis() / 1000));
  124. stmt.setString(9, comment);
  125. stmt.executeUpdate();
  126. } catch (SQLException e) {
  127. logger.log(Level.SEVERE, "Failed to log blacklist event to database: "
  128. + e.getMessage());
  129. }
  130. }
  131. /**
  132. * Log an event.
  133. *
  134. * @param event
  135. */
  136. public void logEvent(BlacklistEvent event, String comment) {
  137. // Block break
  138. if (event instanceof BlockBreakBlacklistEvent) {
  139. BlockBreakBlacklistEvent evt = (BlockBreakBlacklistEvent)event;
  140. logEvent("BREAK", evt.getPlayer(), evt.getPosition(),
  141. evt.getType(), comment);
  142. // Block place
  143. } else if (event instanceof BlockPlaceBlacklistEvent) {
  144. BlockPlaceBlacklistEvent evt = (BlockPlaceBlacklistEvent)event;
  145. logEvent("PLACE", evt.getPlayer(), evt.getPosition(),
  146. evt.getType(), comment);
  147. // Block interact
  148. } else if (event instanceof BlockInteractBlacklistEvent) {
  149. BlockInteractBlacklistEvent evt = (BlockInteractBlacklistEvent)event;
  150. logEvent("INTERACT", evt.getPlayer(), evt.getPosition(),
  151. evt.getType(), comment);
  152. // Destroy with
  153. } else if (event instanceof DestroyWithBlacklistEvent) {
  154. DestroyWithBlacklistEvent evt = (DestroyWithBlacklistEvent)event;
  155. logEvent("DESTROY_WITH", evt.getPlayer(), evt.getPosition(),
  156. evt.getType(), comment);
  157. // Acquire
  158. } else if (event instanceof ItemAcquireBlacklistEvent) {
  159. ItemAcquireBlacklistEvent evt = (ItemAcquireBlacklistEvent)event;
  160. logEvent("ACQUIRE", evt.getPlayer(), evt.getPlayer().getPosition(),
  161. evt.getType(), comment);
  162. // Drop
  163. } else if (event instanceof ItemDropBlacklistEvent) {
  164. ItemDropBlacklistEvent evt = (ItemDropBlacklistEvent)event;
  165. logEvent("DROP", evt.getPlayer(), evt.getPlayer().getPosition(),
  166. evt.getType(), comment);
  167. // Use
  168. } else if (event instanceof ItemUseBlacklistEvent) {
  169. ItemUseBlacklistEvent evt = (ItemUseBlacklistEvent)event;
  170. logEvent("USE", evt.getPlayer(), evt.getPlayer().getPosition(),
  171. evt.getType(), comment);
  172. // Unknown
  173. } else {
  174. logEvent("UNKNOWN", event.getPlayer(), event.getPlayer().getPosition(),
  175. -1, comment);
  176. }
  177. }
  178. /**
  179. * Close the connection.
  180. */
  181. public void close() {
  182. try {
  183. if (conn != null && !conn.isClosed()) {
  184. conn.close();
  185. }
  186. } catch (SQLException e) {
  187. }
  188. }
  189. }