Advertisement
Guest User

Untitled

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