Advertisement
Guest User

Untitled

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