Advertisement
Guest User

Untitled

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