Advertisement
Guest User

Untitled

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