Advertisement
Guest User

Untitled

a guest
Nov 4th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. public class Core extends JavaPlugin implements Listener {
  2.  
  3. public static Connection connection;
  4. public static Core instance;
  5.  
  6. public void onEnable(){
  7. instance = this;
  8. getServer().getPluginManager().registerEvents(this, this);
  9. }
  10.  
  11. public void onDisable(){
  12. try {
  13. if (connection != null && connection.isClosed())
  14. connection.close();
  15. }catch (SQLException e){
  16. e.printStackTrace();
  17. }
  18. }
  19.  
  20. public synchronized static void openConnection(){
  21. try {
  22. connection = DriverManager.getConnection("jdbc:mysql://sql6.freesqldatabase.com:3306/sql6142980", "sql6142980", "YXH5m6ikp8");
  23.  
  24. }catch (Exception e){
  25. e.printStackTrace();
  26. }
  27. }
  28. public synchronized static void closeConnection(){
  29. try {
  30. connection.close();
  31. }catch (Exception e){
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. public synchronized static boolean playerDataContainsPlayer(Player player){
  37. try {
  38. PreparedStatement sql = connection.prepareStatement("SELECT * FROM `player_data` WHERE player=?;");
  39. sql.setString(1, player.getName());
  40. ResultSet resultSet = sql.executeQuery();
  41. boolean containsPlayer = resultSet.next();
  42.  
  43. sql.close();
  44. resultSet.close();
  45.  
  46. return containsPlayer;
  47.  
  48. }catch (Exception e){
  49. e.printStackTrace();
  50. return false;
  51. }
  52.  
  53.  
  54. }
  55.  
  56. @EventHandler
  57. public void onPlayerLogin(PlayerLoginEvent event){
  58. openConnection();
  59. try {
  60. int previousLogins = 0;
  61. if(playerDataContainsPlayer(event.getPlayer())){
  62. PreparedStatement sql = connection.prepareStatement("SELECT logins FROM `player_data` WHERE player=?;");
  63. sql.setString(1, event.getPlayer().getName());
  64.  
  65. ResultSet result = sql.executeQuery();
  66. result.next();
  67.  
  68. previousLogins = result.getInt("logins");
  69.  
  70. PreparedStatement loginsUpdate = connection.prepareStatement("UPDATE `player_data` SET logins=? WHERE player=?;");
  71. loginsUpdate.setInt(1, previousLogins + 1);
  72. loginsUpdate.setString(2, event.getPlayer().getName());
  73. loginsUpdate.executeUpdate();
  74. loginsUpdate.close();
  75. sql.close();
  76. result.close();
  77. }else{
  78. PreparedStatement newPlayer = connection.prepareStatement("INSERT INTO `player_data` values(?,0,0,0,1);");
  79. newPlayer.setString(1, event.getPlayer().getName());
  80. newPlayer.execute();
  81. newPlayer.close();
  82.  
  83. PreparedStatement newCoins = connection.prepareStatement("INSERT INTO `player_data` values(?,0,0,0,0);");
  84. newCoins.setString(1, event.getPlayer().getName());
  85. newCoins.setInt(2, 0);
  86. newCoins.execute();
  87. newCoins.close();
  88. }
  89. }catch (Exception e){
  90. e.printStackTrace();
  91. }finally {
  92. closeConnection();
  93. }
  94. }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement