Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. package me.stoneminer02.blockprotection;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.event.EventHandler;
  10. import org.bukkit.event.block.BlockBreakEvent;
  11. import org.bukkit.event.block.BlockPlaceEvent;
  12. import org.bukkit.plugin.Plugin;
  13. import org.bukkit.plugin.java.JavaPlugin;
  14.  
  15. public class Main extends JavaPlugin
  16. {
  17. private Plugin plugin;
  18. code.husky.mysql.MySQL MySQL = new code.husky.mysql.MySQL(plugin,
  19. getConfig().getString("hostname"), getConfig().getString("port"),
  20. getConfig().getString("database"), getConfig()
  21. .getString("username"), getConfig().getString("password"));
  22. Connection c = null;
  23. Statement statement = null;
  24.  
  25. public void onEnable()
  26. {
  27. plugin = this;
  28. try
  29. {
  30. c = MySQL.openConnection();
  31. } catch (Exception ex)
  32. {
  33. ex.printStackTrace();
  34. }
  35. try
  36. {
  37. statement = c.createStatement();
  38. } catch (SQLException e)
  39. {
  40. e.printStackTrace();
  41. }
  42. saveDefaultConfig();
  43. }
  44.  
  45. @EventHandler
  46. public void onBlockPlace(BlockPlaceEvent event) throws SQLException
  47. {
  48. Player player = (Player) event.getPlayer();
  49. String location = event.getBlockPlaced().getLocation().toString();
  50. statement.executeUpdate("INSERT INTO blocks (`PlayerName`, `Location`) VALUES ('" + player.getName() + "', '"+ location +"');");
  51. }
  52.  
  53. @EventHandler
  54. public void onBlockBreak(BlockBreakEvent event) throws SQLException
  55. {
  56. Player player = (Player) event.getPlayer();
  57. ResultSet res = statement.executeQuery("SELECT * FROM blocks WHERE PlayerName = '" + player.getName() + "';");
  58. res.next();
  59. if(player.getName() != res.getString("PlayerName"))
  60. {
  61. String location = res.getString("Location");
  62. if(event.getBlock().getLocation().toString() == location)
  63. {
  64. player.sendMessage("NOPE!");
  65. event.setCancelled(true);
  66. }
  67. }
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement