h31ix

Untitled

Jan 11th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. package net.h31ix.nasprotect;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Calendar;
  10. import java.util.Date;
  11. import java.util.HashMap;
  12. import java.util.Locale;
  13. import java.util.Map;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16. import org.bukkit.Location;
  17. import org.bukkit.block.Block;
  18. import org.bukkit.entity.Player;
  19. import org.bukkit.plugin.PluginManager;
  20. import org.bukkit.plugin.java.JavaPlugin;
  21.  
  22. public class Nasprotect extends JavaPlugin {
  23. private BlockBreakListener blockBreakListener = new BlockBreakListener(this);
  24. private BlockPlaceListener blockPlaceListener = new BlockPlaceListener(this);
  25. private NPPlayerListener playerListener = new NPPlayerListener(this);
  26.  
  27. public Player lastp = null;
  28. public Location lastb = null;
  29.  
  30. public Connection conn;
  31. private PluginManager pluginManager;
  32. private ResultSet rs = null;
  33. public Statement stmt;
  34.  
  35. public Map<Player, Boolean> freeze = new HashMap<Player, Boolean>();
  36. public Map<Player, Boolean> build = new HashMap<Player, Boolean>();
  37. public Map<Player, String> update = new HashMap<Player, String>();
  38.  
  39.  
  40. public void onDisable() {
  41. try {
  42. stmt.executeBatch();
  43. conn.close();
  44. } catch (SQLException ex) {
  45. Logger.getLogger(Nasprotect.class.getName()).log(Level.SEVERE, null, ex);
  46. }
  47. System.out.println(this + " is now disabled!");
  48. }
  49.  
  50.  
  51. public void onEnable() {
  52. connectSQL();
  53. getCommand("freeze").setExecutor(new CommandFreeze(this));
  54. getCommand("last").setExecutor(new CommandLast(this));
  55. getCommand("build").setExecutor(new CommandBuild(this));
  56. getCommand("np").setExecutor(new CommandNP(this));
  57. pluginManager = getServer().getPluginManager();
  58. pluginManager.registerEvent(org.bukkit.event.Event.Type.BLOCK_BREAK, blockBreakListener, org.bukkit.event.Event.Priority.Highest, this);
  59. pluginManager.registerEvent(org.bukkit.event.Event.Type.BLOCK_PLACE, blockPlaceListener, org.bukkit.event.Event.Priority.Highest, this);
  60. pluginManager.registerEvent(org.bukkit.event.Event.Type.PLAYER_MOVE, playerListener, org.bukkit.event.Event.Priority.Highest, this);
  61. pluginManager.registerEvent(org.bukkit.event.Event.Type.PLAYER_INTERACT, playerListener, org.bukkit.event.Event.Priority.Monitor, this);
  62.  
  63. System.out.println(this + " is now enabled!");
  64. getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
  65. public void run() {
  66. try {
  67. stmt.executeBatch();
  68. conn.commit();
  69. } catch (SQLException ex) {
  70. Logger.getLogger(Nasprotect.class.getName()).log(Level.SEVERE, null, ex);
  71. }
  72. System.out.println("Pushed all logs to master");
  73. }
  74. }, 1L, 3000L);
  75. }
  76.  
  77. public void getAction(Block block, Player player)
  78. {
  79. /**try {
  80. stmt = conn.createStatement();
  81. rs = stmt.executeQuery("SELECT * FROM mc_history WHERE loc_x BETWEEN '"+((int)block.getLocation().getX()-1)+"' AND '"+((int)block.getLocation().getX()+1)+"' AND loc_y BETWEEN '"+((int)block.getLocation().getY()-1)+"' AND '"+((int)block.getLocation().getY()+1)+"' AND loc_z BETWEEN '"+((int)block.getLocation().getZ()-1)+"' AND '"+((int)block.getLocation().getZ()+1)+"' AND world='"+block.getWorld().getName()+"'");
  82. while (rs.next())
  83. {
  84. player.sendMessage(rs.getString("player_name")+" "+rs.getString("type")+" "+rs.getString("block_name"));
  85. }
  86. } catch (SQLException ex) {
  87. Logger.getLogger(Nasprotect.class.getName()).log(Level.SEVERE, null, ex);
  88. }**/
  89. }
  90.  
  91. public void logBreak(Block block, Player player) throws SQLException
  92. {
  93. String sql = "INSERT INTO mc_history (date, type, player_name, world, loc_x, loc_y, loc_z, block_name) VALUES ('"+getDate()+"', 'BLOCK_BREAK', '"+player.getName()+"', '"+player.getWorld().getName()+"', '"+(int)player.getLocation().getX()+"', '"+(int)player.getLocation().getY()+"', '"+(int)player.getLocation().getZ()+"', '"+(block.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""))+"')";
  94. stmt.addBatch(sql);
  95. }
  96.  
  97. public void logPlace(Block block, Player player) throws SQLException
  98. {
  99. String sql = "INSERT INTO mc_history (date, type, player_name, world, loc_x, loc_y, loc_z, block_name) VALUES ('"+getDate()+"', 'BLOCK_PLACE', '"+player.getName()+"', '"+player.getWorld().getName()+"', '"+(int)player.getLocation().getX()+"', '"+(int)player.getLocation().getY()+"', '"+(int)player.getLocation().getZ()+"', '"+(block.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""))+"')";
  100. stmt.addBatch(sql);
  101. }
  102.  
  103. public String getDate()
  104. {
  105. Calendar cal = Calendar.getInstance();
  106. int year = cal.get(Calendar.YEAR);
  107. int month = cal.get(Calendar.MONTH);
  108. int day = cal.get(Calendar.DATE);
  109. Date date = new Date(year - 1900, month, day);
  110. SimpleDateFormat dformat = new SimpleDateFormat("MM/dd/yy");
  111. return dformat.format(date);
  112. }
  113.  
  114. private void connectSQL() {
  115. try {
  116. conn = DriverManager.getConnection("jdbc:mysql://x/x", "x", "x");
  117. conn.setAutoCommit(false);
  118. stmt = conn.createStatement();
  119. System.out.println("[NAS-P] Stats MySQL Connection Established!");
  120. } catch (SQLException ex) {
  121. Logger.getLogger(Nasprotect.class.getName()).log(Level.SEVERE, null, ex);
  122. System.out.println("[NAS-P] MySQL Connection FAILED.");
  123. pluginManager.disablePlugin(this);
  124. }
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment