Advertisement
Guest User

Untitled

a guest
Sep 1st, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.util.UUID;
  6.  
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.plugin.java.JavaPlugin;
  9.  
  10. import net.themikishou.zcore.basic.User;
  11. import net.themikishou.zcore.listeners.PlayerJoin;
  12. import net.themikishou.zcore.utils.UserUtils;
  13.  
  14.  
  15. public class ZCore extends JavaPlugin {
  16.  
  17. private Connection conn;
  18.  
  19. public void onEnable(){
  20. checkTable();
  21. try {
  22. loadData();
  23. } catch (SQLException e) {
  24. e.printStackTrace();
  25. }
  26. Bukkit.getServer().getPluginManager().registerEvents(new PlayerJoin(), this);
  27.  
  28. }
  29.  
  30. public void onDisable(){
  31. try {
  32. saveData();
  33. } catch (SQLException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37.  
  38. private void checkTable(){
  39. openConnection();
  40. StringBuilder sb = new StringBuilder();
  41. sb.append("create table if not exists users(");
  42. sb.append("uuid varchar(100) not null,");
  43. sb.append("name varchar(50) not null,");
  44. sb.append("rank int not null,");
  45. sb.append("kills int not null,");
  46. sb.append("deaths int not null,");
  47. sb.append("recs int not null,");
  48. sb.append("badges int not null,");
  49. sb.append("stone int not null,");
  50. sb.append("apples int not null,");
  51. sb.append("money int not null,");
  52. sb.append("level int not null,");
  53. sb.append("points int not null");
  54. sb.append("primary key(uuid));");
  55. try {
  56. conn.createStatement().executeUpdate(sb.toString());
  57. } catch (SQLException e) {
  58. e.printStackTrace();
  59. }
  60. closeConnection();
  61. }
  62.  
  63. private void loadData() throws SQLException{
  64. openConnection();
  65. int i = 0;
  66. ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM `users`");
  67. while(rs.next()){
  68. User u = User.get(UUID.fromString(rs.getString("uuid")));
  69. u.setName(rs.getString("name"));
  70. u.setKills(rs.getInt("kills"));
  71. u.setDeaths(rs.getInt("deaths"));
  72. u.setRecs(rs.getInt("recs"));
  73. u.setBadges(rs.getInt("badges"));
  74. u.setMoney(rs.getInt("money"));
  75. u.setApples(rs.getInt("apples"));
  76. u.setLevel(rs.getInt("level"));
  77. u.setPoints(rs.getInt("points"));
  78. i++;
  79.  
  80. }
  81. Bukkit.getConsoleSender().sendMessage("§a§lLoaded §6§l" + i + " §a§lusers");
  82. closeConnection();
  83. }
  84.  
  85. private void saveData() throws SQLException{
  86. openConnection();
  87. int i = 0;
  88. for(User u : UserUtils.getUsers()){
  89. StringBuilder sb = new StringBuilder();
  90. sb.append("INSERT INTO users (uuid, name, rank, kills, deaths, recs, badges, money, apples, stone, points) VALUES (");
  91. sb.append("'" + u.getName() +"',");
  92. sb.append("'" + u.getUuid().toString() +"',");
  93. sb.append("'" + u.getName() +"',");
  94. sb.append("'" + u.getKills() +"',");
  95. sb.append("'" + u.getDeaths() +"'");
  96. sb.append("'" + u.getRecs() +"'");
  97. sb.append("'" + u.getBadges() +"'");
  98. sb.append("'" + u.getMoney() +"'");
  99. sb.append("'" + u.getApples() +"'");
  100. sb.append("'" + u.getStone() +"'");
  101. sb.append("'" + u.getPoints() +"'");
  102. sb.append(") ON DUPLICATE KEY UPDATE ");
  103. sb.append("uuid='" + u.getUuid().toString() +"',");
  104. sb.append("name='" + u.getName() +"',");
  105. sb.append("kills='" + u.getKills() +"',");
  106. sb.append("deaths='" + u.getDeaths() +"'");
  107. sb.append("recs='" + u.getRecs() +"'");
  108. sb.append("badges='" + u.getBadges() +"'");
  109. sb.append("money='" + u.getMoney() +"'");
  110. sb.append("apples='" + u.getApples() +"'");
  111. sb.append("stone='" + u.getStone() +"'");
  112. sb.append("points='" + u.getPoints() +"'");
  113.  
  114. conn.createStatement().executeUpdate(sb.toString());
  115. i++;
  116. }
  117. Bukkit.getConsoleSender().sendMessage("§a§lSaved §6§l" + i + " §a§lusers");
  118. closeConnection();
  119. }
  120.  
  121. private synchronized void openConnection(){
  122. if(!isConnected()){
  123. try{
  124. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/youtube?user=root&password=");
  125. } catch(SQLException e){
  126. e.printStackTrace();
  127. }
  128. }
  129. }
  130.  
  131. private synchronized void closeConnection(){
  132. if(isConnected()){
  133. try{
  134. conn.close();
  135. } catch(SQLException e){
  136. e.printStackTrace();
  137. }
  138. }
  139. }
  140.  
  141. public boolean isConnected() {
  142. try{
  143. if(conn == null) return false;
  144. if(conn.isClosed()) return false;
  145. } catch(SQLException e){
  146. e.printStackTrace();
  147. }
  148. return true;
  149. }
  150.  
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement