Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. package d3x.BlockLogLite;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. //import java.sql.ResultSetMetaData;
  9. import java.sql.Statement;
  10. import java.util.HashMap;
  11. import java.util.logging.Logger;
  12.  
  13. import org.bukkit.Server;
  14. import org.bukkit.block.Block;
  15. //import org.bukkit.entity.Player;
  16. import org.bukkit.plugin.PluginDescriptionFile;
  17. import org.bukkit.plugin.PluginLoader;
  18. import org.bukkit.plugin.java.JavaPlugin;
  19.  
  20. public class BlockLogLite extends JavaPlugin {
  21. static final Logger log = Logger.getLogger("Minecraft");
  22. Connection db;
  23. BlockLogLiteBlockListener blockListener = new BlockLogLiteBlockListener(this);
  24. HashMap<String, Integer> playerIdCache = new HashMap<String, Integer>();
  25. public void onEnable() {
  26. //log.info("[BlockLogLite] onEnable();");
  27. try
  28. {
  29. getDataFolder().mkdirs();
  30. File config = new File(getDataFolder(), "config.yml");
  31. if ( !config.exists() )
  32. config.createNewFile();
  33. Class.forName("com.mysql.jdbc.Driver");
  34. db = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
  35. Statement s = db.createStatement();
  36. s.execute("CREATE TABLE IF NOT EXISTS `bll_players` (`id` int(11) NOT NULL AUTO_INCREMENT, `player` varchar(32) NOT NULL, PRIMARY KEY (`id`), KEY `player` (`player`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  37. s.execute("CREATE TABLE IF NOT EXISTS `bll_data` (`id` int(11) NOT NULL AUTO_INCREMENT, `date` int(11) unsigned NOT NULL, `playerid` int(11) NOT NULL, `action` tinyint(4) NOT NULL, `x` int(11) NOT NULL, `y` int(11) NOT NULL, `z` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `playerid` (`playerid`), KEY `x` (`x`), KEY `y` (`y`), KEY `z` (`z`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  38. s.close();
  39. }
  40. catch (Exception e) { e.printStackTrace(); }
  41. this.getServer().getPluginManager().registerEvent(org.bukkit.event.Event.Type.BLOCK_PLACED, blockListener, org.bukkit.event.Event.Priority.Monitor, this);
  42. this.getServer().getPluginManager().registerEvent(org.bukkit.event.Event.Type.BLOCK_BREAK, blockListener, org.bukkit.event.Event.Priority.Monitor, this);
  43. }
  44. public int getPlayerId(String player)
  45. {
  46. //log.info("[BlockLogLite] getPlayerId(" player ");");
  47. if ( playerIdCache.containsKey(player) )
  48. {
  49. //log.info("[BlockLogLite] - Found in cache");
  50. return (int) playerIdCache.get(player);
  51. }
  52. else
  53. {
  54. //log.info("[BlockLogLite] - Looking in database");
  55. try
  56. {
  57. Statement s = db.createStatement();
  58. ResultSet result = s.executeQuery("SELECT id FROM `bll_players` WHERE player = '" player "';");
  59. int id = 0;
  60. if ( result.next() )
  61. {
  62. //log.info("[BlockLogLite] -- Found in DB");
  63. id = result.getInt("id");
  64. playerIdCache.put(player, id);
  65. //log.info("[BlockLogLite] --- Cached player with id " id);
  66. s.close();
  67. return id;
  68. }
  69. else
  70. {
  71. //log.info("[BlockLogLite] -- Nothing there, inserting...");
  72. try
  73. {
  74. s.close();
  75. s = db.createStatement();
  76. s.executeUpdate("INSERT INTO `bll_players` (`player`) VALUES ('" player "');", s.RETURN_GENERATED_KEYS);
  77. result = s.getGeneratedKeys();
  78. if ( result.next() )
  79. {
  80. id = result.getInt("GENERATED_KEY");
  81. //log.info("[BlockLogLite] --- Inserted with id " id);
  82. playerIdCache.put(player, id);
  83. //log.info("[BlockLogLite] --- Cached player with id " id);
  84. }
  85. else
  86. log.warning("[BlockLogLite] getPlayerId(" player "): Somehow a freaking INSERT failed?");
  87. s.close();
  88. }
  89. catch (Exception ex) { ex.printStackTrace(); }
  90. return id;
  91. }
  92. }
  93. catch (Exception ex) { ex.printStackTrace(); }
  94. return 0;
  95. }
  96. }
  97. public void blockAction(String player, int blockAction, Block block)
  98. {
  99. //log.info("[BlockLogLite] blockAction(" player ", " blockAction ", " block.getX() ", " block.getY() ", " block.getZ() ", " block.getTypeId() ");");
  100. int playerId = getPlayerId(player);
  101. if ( playerId > 0 )
  102. {
  103. try
  104. {
  105. Statement s = db.createStatement();
  106. s.executeUpdate("INSERT INTO `bll_data` (`date`, `playerid`, `action`, `x`, `y`, `z`, `block_id`) VALUES (UNIX_TIMESTAMP(), " playerId ", " blockAction ", " block.getX() ", " block.getY() ", " block.getZ() ", " block.getTypeId() ");");
  107. s.close();
  108. }
  109. catch (Exception ex) { ex.printStackTrace(); }
  110. }
  111. else
  112. log.warning("[BlockLogLite] getPlayerId(" player "): failed (returned 0)");
  113. }
  114. public void onDisable() {
  115. //log.info("[BlockLogLite] onDisable();");
  116. try
  117. {
  118. db.close();
  119. }
  120. catch (Exception e) { e.printStackTrace(); }
  121. }
  122. }
  123.  
  124. package d3x.BlockLogLite;
  125.  
  126. import org.bukkit.event.block.BlockBreakEvent;
  127. import org.bukkit.event.block.BlockListener;
  128. import org.bukkit.event.block.BlockPlaceEvent;
  129.  
  130. public class BlockLogLiteBlockListener extends BlockListener
  131. {
  132. BlockLogLite plugin;
  133. public BlockLogLiteBlockListener(BlockLogLite i)
  134. {
  135. plugin = i;
  136. }
  137. @Override
  138. public void onBlockPlace(BlockPlaceEvent e)
  139. {
  140. plugin.blockAction(e.getPlayer().getName(), 1, e.getBlock());
  141. }
  142. @Override
  143. public void onBlockBreak(BlockBreakEvent e)
  144. {
  145. plugin.blockAction(e.getPlayer().getName(), 2, e.getBlock());
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement