Advertisement
Guest User

Untitled

a guest
May 10th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.98 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.UUID;
  7. import org.bukkit.configuration.file.FileConfiguration;
  8. import org.bukkit.entity.Player;
  9.  
  10. public class MySQL
  11. {
  12. public static String host = FFA.cfg.getString("MySQL.Host");
  13. public static String port = FFA.cfg.getString("MySQL.Port");
  14. public static String database = FFA.cfg.getString("MySQL.Datenbank");
  15. public static String username = FFA.cfg.getString("MySQL.Benutzername");
  16. public static String password = FFA.cfg.getString("MySQL.Passwort");
  17. public static String host2 = FFA.cfg.getString("Inventory.MySQL.Host");
  18. public static String port2 = FFA.cfg.getString("Inventory.MySQL.Port");
  19. public static String database2 = FFA.cfg.getString("Inventory.MySQL.Datenbank");
  20. public static String username2 = FFA.cfg.getString("Inventory.MySQL.Benutzername");
  21. public static String password2 = FFA.cfg.getString("Inventory.MySQL.Passwort");
  22. public static Connection connection;
  23. public static Connection connection2;
  24.  
  25. public static Connection getConnection()
  26. {
  27. return connection;
  28. }
  29.  
  30. public static Connection getConnection2()
  31. {
  32. return connection2;
  33. }
  34.  
  35. public static boolean isConnected()
  36. {
  37. return connection != null;
  38. }
  39.  
  40. public static boolean isConnected2()
  41. {
  42. return connection2 != null;
  43. }
  44.  
  45. public static void connect()
  46. {
  47. if (!isConnected()) {
  48. try
  49. {
  50. connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  51. }
  52. catch (SQLException ex)
  53. {
  54. ex.printStackTrace();
  55. }
  56. }
  57. }
  58.  
  59. public static void connect2()
  60. {
  61. if (!isConnected2()) {
  62. try
  63. {
  64. connection2 = DriverManager.getConnection("jdbc:mysql://" + host2 + ":" + port2 + "/" + database2, username2, password2);
  65. }
  66. catch (SQLException ex)
  67. {
  68. ex.printStackTrace();
  69. }
  70. }
  71. }
  72.  
  73. public static void disconnect()
  74. {
  75. if (isConnected()) {
  76. try
  77. {
  78. connection.close();
  79. }
  80. catch (SQLException ex)
  81. {
  82. ex.printStackTrace();
  83. }
  84. }
  85. }
  86.  
  87. public static void disconnect2()
  88. {
  89. if (isConnected2()) {
  90. try
  91. {
  92. connection2.close();
  93. }
  94. catch (SQLException ex)
  95. {
  96. ex.printStackTrace();
  97. }
  98. }
  99. }
  100.  
  101. public static void createTableIfNotExists()
  102. {
  103. if (isConnected()) {
  104. try
  105. {
  106. PreparedStatement ps = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS FFA (Spielername VARCHAR(100), UUID VARCHAR(100), Kills INT(100), Tode INT(100))");
  107. ps.executeUpdate();
  108. ps.close();
  109. }
  110. catch (SQLException ex)
  111. {
  112. ex.printStackTrace();
  113. }
  114. }
  115. }
  116.  
  117. public static void createTableIfNotExists2()
  118. {
  119. if (isConnected2()) {
  120. try
  121. {
  122. PreparedStatement ps = getConnection2().prepareStatement("CREATE TABLE IF NOT EXISTS FFA (Spielername VARCHAR(100), UUID VARCHAR(100), Inventory VARCHAR(10000))");
  123. ps.executeUpdate();
  124. ps.close();
  125. }
  126. catch (SQLException ex)
  127. {
  128. ex.printStackTrace();
  129. }
  130. }
  131. }
  132.  
  133. public static boolean isUserExisting(Player p)
  134. {
  135. try
  136. {
  137. PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA WHERE UUID = ?");
  138. ps.setString(1, p.getUniqueId().toString());
  139. ResultSet result = ps.executeQuery();
  140. boolean user = result.next();
  141. result.close();
  142. ps.close();
  143. return user;
  144. }
  145. catch (Exception ex)
  146. {
  147. ex.printStackTrace();
  148. }
  149. return false;
  150. }
  151.  
  152. public static boolean isUserExisting(String player)
  153. {
  154. try
  155. {
  156. PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA WHERE Spielername = ?");
  157. ps.setString(1, player);
  158. ResultSet result = ps.executeQuery();
  159. boolean user = result.next();
  160. result.close();
  161. ps.close();
  162. return user;
  163. }
  164. catch (Exception ex)
  165. {
  166. ex.printStackTrace();
  167. }
  168. return false;
  169. }
  170.  
  171. public static boolean isUserInvExisting(Player p)
  172. {
  173. try
  174. {
  175. PreparedStatement ps = connection2.prepareStatement("SELECT * FROM FFA WHERE UUID = ?");
  176. ps.setString(1, p.getUniqueId().toString());
  177. ResultSet result = ps.executeQuery();
  178. boolean user = result.next();
  179. result.close();
  180. ps.close();
  181. return user;
  182. }
  183. catch (Exception ex)
  184. {
  185. ex.printStackTrace();
  186. }
  187. return false;
  188. }
  189.  
  190. public static void registerPlayer(Player p)
  191. {
  192. if (isUserExisting(p)) {
  193. return;
  194. }
  195. try
  196. {
  197. PreparedStatement ps = connection.prepareStatement("INSERT INTO FFA (Spielername, UUID, Kills, Tode, Punkte) VALUES (?, ?, ?, ?, ?)");
  198. ps.setString(1, p.getName());
  199. ps.setString(2, p.getUniqueId().toString());
  200. ps.setInt(3, 0);
  201. ps.setInt(4, 0);
  202. ps.setInt(5, 1000);
  203. ps.execute();
  204. ps.close();
  205. }
  206. catch (Exception ex)
  207. {
  208. ex.printStackTrace();
  209. }
  210. }
  211.  
  212. public static void registerInv(Player p)
  213. {
  214. if (isUserInvExisting(p)) {
  215. return;
  216. }
  217. try
  218. {
  219. PreparedStatement ps = connection2.prepareStatement("INSERT INTO FFA (Spielername, UUID, Inventory) VALUES (?, ?, ?)");
  220. ps.setString(1, p.getName());
  221. ps.setString(2, p.getUniqueId().toString());
  222. ps.setString(3, "null");
  223. ps.execute();
  224. ps.close();
  225. }
  226. catch (Exception ex)
  227. {
  228. ex.printStackTrace();
  229. }
  230. }
  231.  
  232. public static void setInv(Player p, String inv)
  233. {
  234. try
  235. {
  236. PreparedStatement ps = connection2.prepareStatement("UPDATE FFA SET Inventory = ? WHERE UUID = ?");
  237. ps.setString(1, inv);
  238. ps.setString(2, p.getUniqueId().toString());
  239. ps.executeUpdate();
  240. ps.close();
  241. }
  242. catch (Exception ex)
  243. {
  244. ex.printStackTrace();
  245. }
  246. }
  247.  
  248. public static String getInv(Player p)
  249. {
  250. try
  251. {
  252. PreparedStatement ps = connection2.prepareStatement("SELECT * FROM FFA WHERE UUID = ?");
  253. ps.setString(1, p.getUniqueId().toString());
  254. ResultSet result = ps.executeQuery();
  255. result.next();
  256. String fire = result.getString("Inventory");
  257. result.close();
  258. ps.close();
  259. return fire;
  260. }
  261. catch (Exception ex)
  262. {
  263. ex.printStackTrace();
  264. }
  265. return "null";
  266. }
  267.  
  268. public static void setPlayerName(Player p)
  269. {
  270. try
  271. {
  272. PreparedStatement ps = connection.prepareStatement("UPDATE FFA SET Spielername = ? WHERE UUID = ?");
  273. ps.setString(1, p.getName());
  274. ps.setString(2, p.getUniqueId().toString());
  275. ps.executeUpdate();
  276. ps.close();
  277. }
  278. catch (Exception ex)
  279. {
  280. ex.printStackTrace();
  281. }
  282. }
  283.  
  284. public static String getPlayer(String player)
  285. {
  286. try
  287. {
  288. PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA WHERE Spielername = ?");
  289. ps.setString(1, player);
  290. ResultSet result = ps.executeQuery();
  291. result.next();
  292. String name = result.getString("Spielername");
  293. result.close();
  294. ps.close();
  295. return name;
  296. }
  297. catch (Exception ex)
  298. {
  299. ex.printStackTrace();
  300. }
  301. return "null";
  302. }
  303.  
  304. public static int getKills(Player p)
  305. {
  306. try
  307. {
  308. PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA WHERE UUID = ?");
  309. ps.setString(1, p.getUniqueId().toString());
  310. ResultSet result = ps.executeQuery();
  311. result.next();
  312. int kills = result.getInt("Kills");
  313. result.close();
  314. ps.close();
  315. return kills;
  316. }
  317. catch (Exception ex)
  318. {
  319. ex.printStackTrace();
  320. }
  321. return -1;
  322. }
  323.  
  324. public static int getKills(String player)
  325. {
  326. try
  327. {
  328. PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA WHERE Spielername = ?");
  329. ps.setString(1, player);
  330. ResultSet result = ps.executeQuery();
  331. result.next();
  332. int kills = result.getInt("Kills");
  333. result.close();
  334. ps.close();
  335. return kills;
  336. }
  337. catch (Exception ex)
  338. {
  339. ex.printStackTrace();
  340. }
  341. return -1;
  342. }
  343.  
  344. public static int getDeaths(Player p)
  345. {
  346. try
  347. {
  348. PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA WHERE UUID = ?");
  349. ps.setString(1, p.getUniqueId().toString());
  350. ResultSet result = ps.executeQuery();
  351. result.next();
  352. int deaths = result.getInt("Tode");
  353. result.close();
  354. ps.close();
  355. return deaths;
  356. }
  357. catch (Exception ex)
  358. {
  359. ex.printStackTrace();
  360. }
  361. return -1;
  362. }
  363.  
  364. public static int getDeaths(String player)
  365. {
  366. try
  367. {
  368. PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA WHERE Spielername = ?");
  369. ps.setString(1, player);
  370. ResultSet result = ps.executeQuery();
  371. result.next();
  372. int deaths = result.getInt("Tode");
  373. result.close();
  374. ps.close();
  375. return deaths;
  376. }
  377. catch (Exception ex)
  378. {
  379. ex.printStackTrace();
  380. }
  381. return -1;
  382. }
  383.  
  384. public static double getKD(Player p)
  385. {
  386. try
  387. {
  388. PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM FFA WHERE UUID = ?");
  389. ps1.setString(1, p.getUniqueId().toString());
  390. ResultSet result1 = ps1.executeQuery();
  391. result1.next();
  392. double kills = result1.getInt("Kills");
  393. result1.close();
  394. ps1.close();
  395. PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM FFA WHERE UUID = ?");
  396. ps2.setString(1, p.getUniqueId().toString());
  397. ResultSet result2 = ps2.executeQuery();
  398. result2.next();
  399. double deaths = result2.getInt("Tode");
  400. result2.close();
  401. ps2.close();
  402. if ((kills == 0.0D) && (deaths == 0.0D)) {
  403. return 0.0D;
  404. }
  405. if (deaths == 0.0D) {
  406. return kills;
  407. }
  408. return Math.round(kills / deaths * 100.0D) / 100.0D;
  409. }
  410. catch (Exception ex)
  411. {
  412. ex.printStackTrace();
  413. }
  414. return -1.0D;
  415. }
  416.  
  417. public static double getKD(String player)
  418. {
  419. try
  420. {
  421. PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM FFA WHERE Spielername = ?");
  422. ps1.setString(1, player);
  423. ResultSet result1 = ps1.executeQuery();
  424. result1.next();
  425. double kills = result1.getInt("Kills");
  426. result1.close();
  427. ps1.close();
  428. PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM FFA WHERE Spielername = ?");
  429. ps2.setString(1, player);
  430. ResultSet result2 = ps2.executeQuery();
  431. result2.next();
  432. double deaths = result2.getInt("Tode");
  433. result2.close();
  434. ps2.close();
  435. if ((kills == 0.0D) && (deaths == 0.0D)) {
  436. return 0.0D;
  437. }
  438. if (deaths == 0.0D) {
  439. return kills;
  440. }
  441. return Math.round(kills / deaths * 100.0D) / 100.0D;
  442. }
  443. catch (Exception ex)
  444. {
  445. ex.printStackTrace();
  446. }
  447. return -1.0D;
  448. }
  449.  
  450. public static int getRank(Player p)
  451. {
  452. int rank = -1;
  453. try
  454. {
  455. PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA ORDER BY Kills DESC");
  456. ResultSet result = ps.executeQuery();
  457. while (result.next())
  458. {
  459. String uuid2 = result.getString("UUID");
  460. if (uuid2.equalsIgnoreCase(p.getUniqueId().toString()))
  461. {
  462. rank = result.getRow();
  463. break;
  464. }
  465. }
  466. result.close();
  467. ps.close();
  468. }
  469. catch (Exception ex)
  470. {
  471. ex.printStackTrace();
  472. }
  473. return rank;
  474. }
  475.  
  476. public static int getRank(String player)
  477. {
  478. int rank = -1;
  479. try
  480. {
  481. PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA ORDER BY Kills DESC");
  482. ResultSet result = ps.executeQuery();
  483. while (result.next())
  484. {
  485. String uuid2 = result.getString("Spielername");
  486. if (uuid2.equalsIgnoreCase(player))
  487. {
  488. rank = result.getRow();
  489. break;
  490. }
  491. }
  492. result.close();
  493. ps.close();
  494. }
  495. catch (Exception ex)
  496. {
  497. ex.printStackTrace();
  498. }
  499. return rank;
  500. }
  501.  
  502. public static void addKill(Player p)
  503. {
  504. try
  505. {
  506. PreparedStatement ps = connection.prepareStatement("UPDATE FFA SET Kills = ? WHERE uuid = ?");
  507. ps.setInt(1, getKills(p) + 1);
  508. ps.setString(2, p.getUniqueId().toString());
  509. ps.executeUpdate();
  510. ps.close();
  511. }
  512. catch (Exception ex)
  513. {
  514. ex.printStackTrace();
  515. }
  516. }
  517.  
  518. public static void addDeath(Player p)
  519. {
  520. try
  521. {
  522. PreparedStatement ps = connection.prepareStatement("UPDATE FFA SET Tode = ? WHERE uuid = ?");
  523. ps.setInt(1, getDeaths(p) + 1);
  524. ps.setString(2, p.getUniqueId().toString());
  525. ps.executeUpdate();
  526. ps.close();
  527. }
  528. catch (Exception ex)
  529. {
  530. ex.printStackTrace();
  531. }
  532. }
  533.  
  534. public static int getPoints(UUID p)
  535. {
  536. try
  537. {
  538. PreparedStatement st = connection.prepareStatement("SELECT Punkte FROM FFA WHERE uuid = ?");
  539. st.setString(1, p.toString());
  540. ResultSet rs = st.executeQuery();
  541. rs.next();
  542. int points = rs.getInt("Punkte");
  543. rs.close();
  544. st.close();
  545. return points;
  546. }
  547. catch (Exception e)
  548. {
  549. e.printStackTrace();
  550. }
  551. return -1;
  552. }
  553.  
  554. public static void setPoints(Player p, int points)
  555. {
  556. try
  557. {
  558. PreparedStatement st = connection.prepareStatement("UPDATE FFA SET Punkte = ? WHERE uuid = ?");
  559. st.setInt(1, points);
  560. st.setString(2, p.getUniqueId().toString());
  561. st.executeUpdate();
  562. st.close();
  563. }
  564. catch (Exception ex)
  565. {
  566. ex.printStackTrace();
  567. }
  568. }
  569. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement