Advertisement
Guest User

Untitled

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