Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 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. Bukkit.getServer().getPluginManager().registerEvents(new PlayerJoin(), this);
  44.  
  45. }
  46.  
  47. public void onDisable(){
  48. try {
  49. saveData();
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54.  
  55. public void checkTable(){
  56. openConnection();
  57. StringBuilder sb = new StringBuilder();
  58. sb.append("create table if not exists users(");
  59. sb.append("uuid varchar(100) not null,");
  60. sb.append("name varchar(50) not null,");
  61. sb.append("kills int not null,");
  62. sb.append("deaths int not null,");
  63. sb.append("recs int not null,");
  64. sb.append("badges int not null,");
  65. sb.append("money int not null,");
  66. sb.append("apples int not null,");
  67. sb.append("stone int not null,");
  68. sb.append("level int not null,");
  69. sb.append("donatecoins int not null,");
  70. sb.append("points int not null,");
  71. sb.append("firstIp varchar(50) not null,");
  72. sb.append("lastIp varchar(50) not null,");
  73. sb.append("homeLocation text not null,");
  74. sb.append("lastLocation text not null,");
  75. sb.append("god 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.setHomeLocation(LocationUtils.locationFromString(rs.getString("homeLocation")));
  103. u.setLastLocation(LocationUtils.locationFromString(rs.getString("lastLocation")));
  104. u.setFirstIP(rs.getString("firstIp"));
  105. u.setLastIP(rs.getString("lastIp"));
  106. u.setGod(rs.getBoolean("god"));
  107. u.setName(rs.getString("name"));
  108. u.setNoblesse(rs.getBoolean("noblesse"));
  109. i++;
  110.  
  111. }
  112. Bukkit.getConsoleSender().sendMessage("§a§lLoaded §6§l" + i + " §a§lusers");
  113. closeConnection();
  114. }
  115.  
  116. public void saveData() throws SQLException{
  117. openConnection();
  118. int i = 0;
  119. for(User u : UserUtils.getUsers()){
  120. StringBuilder sb = new StringBuilder();
  121. sb.append("INSERT INTO users (uuid, name, kills, deaths, recs, badges, apples, money, donatecoins, stone, level,"
  122. + "points, firstIp, lastIp, homeLocation, lastLocation, god, noblesse) VALUES (");
  123. sb.append("'" + u.getUUID().toString() +"',");
  124. sb.append("'" + u.getName() +"',");
  125. sb.append("'" + u.getKills() +"',");
  126. sb.append("'" + u.getDeaths() +"',");
  127. sb.append("'" + u.getRecs() +"',");
  128. sb.append("'" + u.getBadges() +"',");
  129. sb.append("'" + u.getApples() +"',");
  130. sb.append("'" + u.getMoney() +"',");
  131. sb.append("'" + u.getDonateCoins() +"',");
  132. sb.append("'" + u.getStone() +"',");
  133. sb.append("'" + u.getLevel() +"',");
  134. sb.append("'" + u.getPoints() +"',");
  135. sb.append("'" + u.getFirstIP() +"',");
  136. sb.append("'" + u.getLastIP() +"',");
  137. sb.append("'" + u.getHomeLocation() +"',");
  138. sb.append("'" + u.getLastLocation() +"',");
  139. sb.append("'" + (u.isGod() ? 1 : 0) +"',");
  140. sb.append("'" + (u.isNoblesse() ? 1 : 0) +"'");
  141. sb.append(") ON DUPLICATE KEY UPDATE ");
  142. sb.append("name='" + u.getName() +"',");
  143. sb.append("kills='" + u.getKills() +"',");
  144. sb.append("deaths='" + u.getDeaths() +"',");
  145. sb.append("recs='" + u.getRecs() +"',");
  146. sb.append("badges='" + u.getBadges() +"',");
  147. sb.append("apples='" + u.getApples() +"',");
  148. sb.append("money='" + u.getMoney() +"',");
  149. sb.append("donatecoins='" + u.getDonateCoins() +"',");
  150. sb.append("stone='" + u.getStone() +"',");
  151. sb.append("level='" + u.getLevel() +"',");
  152. sb.append("points='" + u.getPoints() +"',");
  153. sb.append("firstIp='" + u.getFirstIP() +"',");
  154. sb.append("lastIp'" + u.getLastIP() +"',");
  155. sb.append("homeLocation='" + u.getHomeLocation() +"',");
  156. sb.append("lastLocation='" + u.getLastLocation() +"',");
  157. sb.append("god='" + (u.isGod() ? 1 : 0) +"',");
  158. sb.append("noblesse='" + (u.isNoblesse() ? 1 : 0) +"';");
  159. conn.createStatement().executeUpdate(sb.toString());
  160. i++;
  161. }
  162. Bukkit.getConsoleSender().sendMessage("§a§lSaved §6§l" + i + " §a§lusers");
  163. closeConnection();
  164. }
  165.  
  166. public void openConnection(){
  167. if(!isConnected()){
  168. try{
  169. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/youtube?user=root&password=");
  170. } catch(SQLException e){
  171. e.printStackTrace();
  172. }
  173. }
  174. }
  175.  
  176. public void closeConnection(){
  177. if(isConnected()){
  178. try{
  179. conn.close();
  180. } catch(SQLException e){
  181. e.printStackTrace();
  182. }
  183.  
  184. }
  185. }
  186. public boolean isConnected() {
  187. try{
  188. if(conn == null) return false;
  189. if(conn.isClosed()) return false;
  190. } catch(SQLException e){
  191. e.printStackTrace();
  192. }
  193. return true;
  194. }
  195.  
  196.  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement