Advertisement
Guest User

Untitled

a guest
Nov 25th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.16 KB | None | 0 0
  1. package de.dave_911.SkyPvP.MySQL;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.UUID;
  9.  
  10. import org.bukkit.entity.Player;
  11.  
  12. import de.dave_911.SkyPvP.SkyPvP;
  13.  
  14. public class MySQL {
  15.  
  16. public static String host = SkyPvP.cfg.getString("MySQL.Host");
  17. public static String port = SkyPvP.cfg.getString("MySQL.Port");
  18. public static String database = SkyPvP.cfg.getString("MySQL.Datenbank");
  19. public static String username = SkyPvP.cfg.getString("MySQL.Benutzername");
  20. public static String password = SkyPvP.cfg.getString("MySQL.Passwort");
  21.  
  22. public static Connection connection;
  23.  
  24. public static Connection getConnection() {
  25. return connection;
  26. }
  27.  
  28. public static boolean isConnected() {
  29. return (connection == null ? false : true);
  30. }
  31.  
  32. public static void connect() {
  33. if (!isConnected()) {
  34. try {
  35. connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  36. } catch (SQLException ex) {
  37. ex.printStackTrace();
  38. }
  39. }
  40. }
  41.  
  42. public static void disconnect() {
  43. if (isConnected()) {
  44. try {
  45. connection.close();
  46. } catch (SQLException ex) {
  47. ex.printStackTrace();
  48. }
  49. }
  50. }
  51.  
  52. public static void createTableIfNotExists() {
  53. if (isConnected()) {
  54. try {
  55. PreparedStatement ps = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS SkyPvP (Spielername VARCHAR(100), UUID VARCHAR(100), Punkte INT(100), Kills INT(100), Tode INT(100), HighestLevel INT(100))");
  56. ps.executeUpdate();
  57. ps.close();
  58. } catch (SQLException ex) {
  59. ex.printStackTrace();
  60. }
  61. }
  62. }
  63.  
  64. public static boolean isPlayerExisting(Player p) {
  65. try {
  66. PreparedStatement ps = connection.prepareStatement("SELECT * FROM SkyPvP WHERE UUID = ?");
  67. ps.setString(1, p.getUniqueId().toString());
  68. ResultSet result = ps.executeQuery();
  69. boolean isExisting = result.next();
  70. result.close();
  71. ps.close();
  72. return isExisting;
  73. } catch (Exception ex) {
  74. ex.printStackTrace();
  75. }
  76. return false;
  77. }
  78.  
  79. public static boolean isPlayerExisting(UUID uuid) {
  80. try {
  81. PreparedStatement ps = connection.prepareStatement("SELECT * FROM SkyPvP WHERE UUID = ?");
  82. ps.setString(1, uuid.toString());
  83. ResultSet result = ps.executeQuery();
  84. boolean isExisting = result.next();
  85. result.close();
  86. ps.close();
  87. return isExisting;
  88. } catch (Exception ex) {
  89. ex.printStackTrace();
  90. }
  91. return false;
  92. }
  93.  
  94. public static void registerPlayer(UUID uuid, String name) {
  95. if (isPlayerExisting(uuid)) {
  96. return;
  97. }
  98. try {
  99. PreparedStatement ps = connection.prepareStatement("INSERT INTO SkyPvP (Spielername, UUID, Punkte, Kills, Tode, HighestLevel) VALUES (?, ?, ?, ?, ?, ?)");
  100. ps.setString(1, name);
  101. ps.setString(2, uuid.toString());
  102. ps.setInt(3, 100);
  103. ps.setInt(4, 0);
  104. ps.setInt(5, 0);
  105. ps.setInt(6, 0);
  106. ps.execute();
  107. ps.close();
  108. } catch (Exception ex) {
  109. ex.printStackTrace();
  110. }
  111. }
  112.  
  113. public static void setPlayerName(UUID uuid, String name) {
  114. try {
  115. PreparedStatement ps = connection.prepareStatement("UPDATE SkyPvP SET Spielername = ? WHERE UUID = ?");
  116. ps.setString(1, name);
  117. ps.setString(2, uuid.toString());
  118. ps.executeUpdate();
  119. ps.close();
  120. } catch (Exception ex) {
  121. ex.printStackTrace();
  122. }
  123. }
  124.  
  125. public static int getPoints(Player p) {
  126. try {
  127. PreparedStatement ps = connection.prepareStatement("SELECT * FROM SkyPvP WHERE UUID = ?");
  128. ps.setString(1, p.getUniqueId().toString());
  129. ResultSet result = ps.executeQuery();
  130. result.next();
  131. int points = result.getInt("Punkte");
  132. result.close();
  133. ps.close();
  134. return points;
  135. } catch (Exception ex) {
  136. ex.printStackTrace();
  137. }
  138. return -1;
  139. }
  140.  
  141. public static int getPoints(UUID uuid) {
  142. try {
  143. PreparedStatement ps = connection.prepareStatement("SELECT * FROM SkyPvP WHERE UUID = ?");
  144. ps.setString(1, uuid.toString());
  145. ResultSet result = ps.executeQuery();
  146. result.next();
  147. int points = result.getInt("Punkte");
  148. result.close();
  149. ps.close();
  150. return points;
  151. } catch (Exception ex) {
  152. ex.printStackTrace();
  153. }
  154. return -1;
  155. }
  156.  
  157. public static void setPoints(Player p, int points) {
  158. try {
  159. PreparedStatement ps = connection.prepareStatement("UPDATE SkyPvP SET Punkte = ? WHERE UUID = ?");
  160. ps.setInt(1, points);
  161. ps.setString(2, p.getUniqueId().toString());
  162. ps.executeUpdate();
  163. ps.close();
  164. } catch (Exception ex) {
  165. ex.printStackTrace();
  166. }
  167. }
  168.  
  169. public static void addPoints(Player p, int points) {
  170. setPoints(p, getPoints(p) + points);
  171. }
  172.  
  173. public static void removePoints(Player p, int points) {
  174. setPoints(p, getPoints(p) - points);
  175. }
  176.  
  177. public static int getKills(Player p) {
  178. try {
  179. PreparedStatement ps = connection.prepareStatement("SELECT * FROM SkyPvP WHERE UUID = ?");
  180. ps.setString(1, p.getUniqueId().toString());
  181. ResultSet result = ps.executeQuery();
  182. result.next();
  183. int kills = result.getInt("Kills");
  184. result.close();
  185. ps.close();
  186. return kills;
  187. } catch (Exception ex) {
  188. ex.printStackTrace();
  189. }
  190. return -1;
  191. }
  192.  
  193. public static int getKills(UUID uuid) {
  194. try {
  195. PreparedStatement ps = connection.prepareStatement("SELECT * FROM SkyPvP WHERE UUID = ?");
  196. ps.setString(1, uuid.toString());
  197. ResultSet result = ps.executeQuery();
  198. result.next();
  199. int kills = result.getInt("Kills");
  200. result.close();
  201. ps.close();
  202. return kills;
  203. } catch (Exception ex) {
  204. ex.printStackTrace();
  205. }
  206. return -1;
  207. }
  208.  
  209. public static int getDeaths(Player p) {
  210. try {
  211. PreparedStatement ps = connection.prepareStatement("SELECT * FROM SkyPvP WHERE UUID = ?");
  212. ps.setString(1, p.getUniqueId().toString());
  213. ResultSet result = ps.executeQuery();
  214. result.next();
  215. int deaths = result.getInt("Tode");
  216. result.close();
  217. ps.close();
  218. return deaths;
  219. } catch (Exception ex) {
  220. ex.printStackTrace();
  221. }
  222. return -1;
  223. }
  224.  
  225. public static int getDeaths(UUID uuid) {
  226. try {
  227. PreparedStatement ps = connection.prepareStatement("SELECT * FROM SkyPvP WHERE UUID = ?");
  228. ps.setString(1, uuid.toString());
  229. ResultSet result = ps.executeQuery();
  230. result.next();
  231. int deaths = result.getInt("Tode");
  232. result.close();
  233. ps.close();
  234. return deaths;
  235. } catch (Exception ex) {
  236. ex.printStackTrace();
  237. }
  238. return -1;
  239. }
  240.  
  241. public static double getKD(Player p) {
  242. try {
  243. PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM SkyPvP WHERE UUID = ?");
  244. ps1.setString(1, p.getUniqueId().toString());
  245. ResultSet result1 = ps1.executeQuery();
  246. result1.next();
  247. double kills = result1.getInt("Kills");
  248. result1.close();
  249. ps1.close();
  250. PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM SkyPvP WHERE UUID = ?");
  251. ps2.setString(1, p.getUniqueId().toString());
  252. ResultSet result2 = ps2.executeQuery();
  253. result2.next();
  254. double deaths = result2.getInt("Tode");
  255. result2.close();
  256. ps2.close();
  257. if (kills == 0 && deaths == 0) {
  258. return 0;
  259. }
  260. if (deaths == 0) {
  261. return kills;
  262. }
  263. double kd = Math.round((kills / deaths) * 100) / 100.0;
  264. return kd;
  265. } catch (Exception ex) {
  266. ex.printStackTrace();
  267. }
  268. return -1;
  269. }
  270.  
  271. public static double getKD(UUID uuid) {
  272. try {
  273. PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM SkyPvP WHERE UUID = ?");
  274. ps1.setString(1, uuid.toString());
  275. ResultSet result1 = ps1.executeQuery();
  276. result1.next();
  277. double kills = result1.getInt("Kills");
  278. result1.close();
  279. ps1.close();
  280. PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM SkyPvP WHERE UUID = ?");
  281. ps2.setString(1, uuid.toString());
  282. ResultSet result2 = ps2.executeQuery();
  283. result2.next();
  284. double deaths = result2.getInt("Tode");
  285. result2.close();
  286. ps2.close();
  287. if (kills == 0 && deaths == 0) {
  288. return 0;
  289. }
  290. if (deaths == 0) {
  291. return kills;
  292. }
  293. double kd = Math.round((kills / deaths) * 100) / 100.0;
  294. return kd;
  295. } catch (Exception ex) {
  296. ex.printStackTrace();
  297. }
  298. return -1;
  299. }
  300.  
  301. public static int getHighestLevel(Player p) {
  302. try {
  303. PreparedStatement ps = connection.prepareStatement("SELECT * FROM SkyPvP WHERE UUID = ?");
  304. ps.setString(1, p.getUniqueId().toString());
  305. ResultSet result = ps.executeQuery();
  306. result.next();
  307. int deaths = result.getInt("HighestLevel");
  308. result.close();
  309. ps.close();
  310. return deaths;
  311. } catch (Exception ex) {
  312. ex.printStackTrace();
  313. }
  314. return -1;
  315. }
  316.  
  317. public static int getHighestLevel(UUID uuid) {
  318. try {
  319. PreparedStatement ps = connection.prepareStatement("SELECT * FROM SkyPvP WHERE UUID = ?");
  320. ps.setString(1, uuid.toString());
  321. ResultSet result = ps.executeQuery();
  322. result.next();
  323. int deaths = result.getInt("HighestLevel");
  324. result.close();
  325. ps.close();
  326. return deaths;
  327. } catch (Exception ex) {
  328. ex.printStackTrace();
  329. }
  330. return -1;
  331. }
  332.  
  333. public static void setHighestLevel(Player p, int highestLevel) {
  334. try {
  335. PreparedStatement ps = connection.prepareStatement("UPDATE SkyPvP SET HighestLevel = ? WHERE UUID = ?");
  336. ps.setInt(1, highestLevel);
  337. ps.setString(2, p.getUniqueId().toString());
  338. ps.executeUpdate();
  339. ps.close();
  340. } catch (Exception ex) {
  341. ex.printStackTrace();
  342. }
  343. }
  344.  
  345. public static int getRank(Player p) {
  346. int rank = -1;
  347. try {
  348. PreparedStatement ps = connection.prepareStatement("SELECT * FROM SkyPvP ORDER BY Kills DESC");
  349. ResultSet result = ps.executeQuery();
  350. while (result.next()) {
  351. String uuid2 = result.getString("UUID");
  352. if (uuid2.equalsIgnoreCase(p.getUniqueId().toString())) {
  353. rank = result.getRow();
  354. break;
  355. }
  356. }
  357. result.close();
  358. ps.close();
  359. } catch (Exception ex) {
  360. ex.printStackTrace();
  361. }
  362. return rank;
  363. }
  364.  
  365. public static int getRank(UUID uuid) {
  366. int rank = -1;
  367. try {
  368. PreparedStatement ps = connection.prepareStatement("SELECT * FROM SkyPvP ORDER BY Kills DESC");
  369. ResultSet result = ps.executeQuery();
  370. while (result.next()) {
  371. String uuid2 = result.getString("UUID");
  372. if (uuid2.equalsIgnoreCase(uuid.toString())) {
  373. rank = result.getRow();
  374. break;
  375. }
  376. }
  377. result.close();
  378. ps.close();
  379. } catch (Exception ex) {
  380. ex.printStackTrace();
  381. }
  382. return rank;
  383. }
  384.  
  385. public static void addKill(Player p) {
  386. try {
  387. PreparedStatement ps = connection.prepareStatement("UPDATE SkyPvP SET Kills = ? WHERE UUID = ?");
  388. ps.setInt(1, getKills(p) + 1);
  389. ps.setString(2, p.getUniqueId().toString());
  390. ps.executeUpdate();
  391. ps.close();
  392. } catch (Exception ex) {
  393. ex.printStackTrace();
  394. }
  395. }
  396.  
  397. public static void addDeath(Player p) {
  398. try {
  399. PreparedStatement ps = connection.prepareStatement("UPDATE SkyPvP SET Tode = ? WHERE UUID = ?");
  400. ps.setInt(1, getDeaths(p) + 1);
  401. ps.setString(2, p.getUniqueId().toString());
  402. ps.executeUpdate();
  403. ps.close();
  404. } catch (Exception ex) {
  405. ex.printStackTrace();
  406. }
  407. }
  408. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement