Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.78 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.sql.Statement;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.logging.Level;
  10.  
  11. /**
  12. * MySQLSource.java - Used for accessing users and such from a mysql database
  13. * @author James
  14. */
  15. public class MySQLSource extends DataSource {
  16.  
  17. private String driver, username, password, db, groupsTable, kitsTable, homesTable, warpsTable, itemsTable, whitelistTable, reservelistTable, usersTable, bansTable;
  18.  
  19. public Connection getConnection() {
  20. try {
  21. return DriverManager.getConnection(db + "?autoReconnect=true&user=" + username + "&password=" + password);
  22. } catch (SQLException ex) {
  23. log.log(Level.SEVERE, "Unable to retreive connection", ex);
  24. }
  25. return null;
  26. }
  27.  
  28. public void initialize() {
  29. PropertiesFile properties = new PropertiesFile("mysql.properties");
  30. driver = properties.getString("driver", "com.mysql.jdbc.Driver");
  31. username = properties.getString("user", "root");
  32. password = properties.getString("pass", "root");
  33. db = properties.getString("db", "jdbc:mysql://localhost:3306/minecraft");
  34. groupsTable = properties.getString("","groups");
  35. kitsTable = properties.getString("kits","kits");
  36. homesTable = properties.getString("homes","homes");
  37. warpsTable = properties.getString("warps","warps");
  38. itemsTable = properties.getString("items","items");
  39. whitelistTable = properties.getString("whitelist","whitelist");
  40. reservelistTable = properties.getString("reservelist","reservelist");
  41. usersTable = properties.getString("users","users");
  42. bansTable = properties.getString("bans", "bans");
  43.  
  44. try {
  45. Class.forName(driver);
  46. } catch (ClassNotFoundException ex) {
  47. log.log(Level.SEVERE, "Unable to find class " + driver, ex);
  48. }
  49.  
  50. loadGroups();
  51. loadKits();
  52. loadHomes();
  53. loadWarps();
  54. loadItems();
  55. loadWhitelist();
  56. loadReserveList();
  57. //loadBanList();
  58. }
  59.  
  60. public void loadGroups() {
  61. synchronized (groupLock) {
  62. groups = new ArrayList<Group>();
  63. Connection conn = null;
  64. PreparedStatement ps = null;
  65. ResultSet rs = null;
  66. try {
  67. conn = getConnection();
  68. ps = conn.prepareStatement("SELECT * FROM " + groupsTable);
  69. rs = ps.executeQuery();
  70. while (rs.next()) {
  71. Group group = new Group();
  72. group.Administrator = rs.getBoolean("admin");
  73. group.CanModifyWorld = rs.getBoolean("canmodifyworld");
  74. group.Commands = rs.getString("commands").split(",");
  75. group.DefaultGroup = rs.getBoolean("defaultgroup");
  76. group.ID = rs.getInt("id");
  77. group.IgnoreRestrictions = rs.getBoolean("ignoresrestrictions");
  78. group.InheritedGroups = rs.getString("inheritedgroups").split(",");
  79. group.Name = rs.getString("name");
  80. group.Prefix = rs.getString("prefix");
  81. if (group.InheritedGroups.length == 1)
  82. if (group.InheritedGroups[0].equalsIgnoreCase(group.Name))
  83. group.InheritedGroups = new String[] { "" };
  84. groups.add(group);
  85. }
  86. } catch (SQLException ex) {
  87. log.log(Level.SEVERE, "Unable to retreive groups from group table", ex);
  88. } finally {
  89. try {
  90. if (ps != null) {
  91. ps.close();
  92. }
  93. if (rs != null) {
  94. rs.close();
  95. }
  96. if (conn != null) {
  97. conn.close();
  98. }
  99. } catch (SQLException ex) {
  100. }
  101. }
  102. }
  103. }
  104.  
  105. public void loadKits() {
  106. synchronized (kitLock) {
  107. kits = new ArrayList<Kit>();
  108. Connection conn = null;
  109. PreparedStatement ps = null;
  110. ResultSet rs = null;
  111. try {
  112. conn = getConnection();
  113. ps = conn.prepareStatement("SELECT * FROM " + kitsTable);
  114. rs = ps.executeQuery();
  115. while (rs.next()) {
  116. Kit kit = new Kit();
  117. kit.Delay = rs.getInt("delay");
  118. kit.Group = rs.getString("group");
  119. kit.ID = rs.getInt("id");
  120. kit.Name = rs.getString("name");
  121. kit.IDs = new HashMap<String, Integer>();
  122.  
  123. String[] ids = rs.getString("items").split(",");
  124. for (String str : ids) {
  125. String id = "";
  126. int amount = 1;
  127. if (str.contains(" ")) {
  128. id = str.split(" ")[0];
  129. amount = Integer.parseInt(str.split(" ")[1]);
  130. } else {
  131. id = str;
  132. }
  133. kit.IDs.put(id, amount);
  134. }
  135. kits.add(kit);
  136. }
  137. } catch (SQLException ex) {
  138. log.log(Level.SEVERE, "Unable to retreive kits from kit table", ex);
  139. } finally {
  140. try {
  141. if (ps != null) {
  142. ps.close();
  143. }
  144. if (rs != null) {
  145. rs.close();
  146. }
  147. if (conn != null) {
  148. conn.close();
  149. }
  150. } catch (SQLException ex) {
  151. }
  152. }
  153. }
  154. }
  155.  
  156. public void loadHomes() {
  157. synchronized (homeLock) {
  158. homes = new ArrayList<Warp>();
  159. if (!etc.getInstance().canSaveHomes()) {
  160. return;
  161. }
  162. Connection conn = null;
  163. PreparedStatement ps = null;
  164. ResultSet rs = null;
  165. try {
  166. conn = getConnection();
  167. ps = conn.prepareStatement("SELECT * FROM " + homesTable);
  168. rs = ps.executeQuery();
  169. while (rs.next()) {
  170. Location location = new Location();
  171. location.x = rs.getDouble("x");
  172. location.y = rs.getDouble("y");
  173. location.z = rs.getDouble("z");
  174. location.rotX = rs.getFloat("rotX");
  175. location.rotY = rs.getFloat("rotY");
  176. Warp home = new Warp();
  177. home.ID = rs.getInt("id");
  178. home.Location = location;
  179. home.Name = rs.getString("name");
  180. home.Group = rs.getString("group");
  181. homes.add(home);
  182. }
  183. } catch (SQLException ex) {
  184. log.log(Level.SEVERE, "Unable to retreive homes from home table", ex);
  185. } finally {
  186. try {
  187. if (ps != null) {
  188. ps.close();
  189. }
  190. if (rs != null) {
  191. rs.close();
  192. }
  193. if (conn != null) {
  194. conn.close();
  195. }
  196. } catch (SQLException ex) {
  197. }
  198. }
  199. }
  200. }
  201.  
  202. public void loadWarps() {
  203. synchronized (warpLock) {
  204. warps = new ArrayList<Warp>();
  205. Connection conn = null;
  206. PreparedStatement ps = null;
  207. ResultSet rs = null;
  208. try {
  209. conn = getConnection();
  210. ps = conn.prepareStatement("SELECT * FROM " + warpsTable);
  211. rs = ps.executeQuery();
  212. while (rs.next()) {
  213. Location location = new Location();
  214. location.x = rs.getDouble("x");
  215. location.y = rs.getDouble("y");
  216. location.z = rs.getDouble("z");
  217. location.rotX = rs.getFloat("rotX");
  218. location.rotY = rs.getFloat("rotY");
  219. Warp warp = new Warp();
  220. warp.ID = rs.getInt("id");
  221. warp.Location = location;
  222. warp.Name = rs.getString("name");
  223. warp.Group = rs.getString("group");
  224. warps.add(warp);
  225. }
  226. } catch (SQLException ex) {
  227. log.log(Level.SEVERE, "Unable to retreive warps from warp table", ex);
  228. } finally {
  229. try {
  230. if (ps != null) {
  231. ps.close();
  232. }
  233. if (rs != null) {
  234. rs.close();
  235. }
  236. if (conn != null) {
  237. conn.close();
  238. }
  239. } catch (SQLException ex) {
  240. }
  241. }
  242. }
  243. }
  244.  
  245. public void loadItems() {
  246. synchronized (itemLock) {
  247. items = new HashMap<String, Integer>();
  248. Connection conn = null;
  249. PreparedStatement ps = null;
  250. ResultSet rs = null;
  251. try {
  252. conn = getConnection();
  253. ps = conn.prepareStatement("SELECT * FROM " + itemsTable);
  254. rs = ps.executeQuery();
  255. while (rs.next()) {
  256. items.put(rs.getString("name"), rs.getInt("itemid"));
  257. }
  258. } catch (SQLException ex) {
  259. log.log(Level.SEVERE, "Unable to retreive items from item table", ex);
  260. } finally {
  261. try {
  262. if (ps != null) {
  263. ps.close();
  264. }
  265. if (rs != null) {
  266. rs.close();
  267. }
  268. if (conn != null) {
  269. conn.close();
  270. }
  271. } catch (SQLException ex) {
  272. }
  273. }
  274. }
  275. }
  276.  
  277. public void loadWhitelist() {
  278. synchronized (whiteListLock) {
  279. whiteList = new ArrayList<String>();
  280. Connection conn = null;
  281. PreparedStatement ps = null;
  282. ResultSet rs = null;
  283. try {
  284. conn = getConnection();
  285. ps = conn.prepareStatement("SELECT * FROM " + whitelistTable);
  286. rs = ps.executeQuery();
  287. while (rs.next()) {
  288. whiteList.add(rs.getString(1));
  289. }
  290. } catch (SQLException ex) {
  291. log.log(Level.SEVERE, "Unable to retreive users from whitelist table", ex);
  292. } finally {
  293. try {
  294. if (ps != null) {
  295. ps.close();
  296. }
  297. if (rs != null) {
  298. rs.close();
  299. }
  300. if (conn != null) {
  301. conn.close();
  302. }
  303. } catch (SQLException ex) {
  304. }
  305. }
  306. }
  307. }
  308.  
  309. public void loadReserveList() {
  310. synchronized (reserveListLock) {
  311. reserveList = new ArrayList<String>();
  312. Connection conn = null;
  313. PreparedStatement ps = null;
  314. ResultSet rs = null;
  315. try {
  316. conn = getConnection();
  317. ps = conn.prepareStatement("SELECT * FROM " + reservelistTable);
  318. rs = ps.executeQuery();
  319. while (rs.next()) {
  320. reserveList.add(rs.getString(1));
  321. }
  322. } catch (SQLException ex) {
  323. log.log(Level.SEVERE, "Unable to retreive users from whitelist table", ex);
  324. } finally {
  325. try {
  326. if (ps != null) {
  327. ps.close();
  328. }
  329. if (rs != null) {
  330. rs.close();
  331. }
  332. if (conn != null) {
  333. conn.close();
  334. }
  335. } catch (SQLException ex) {
  336. }
  337. }
  338. }
  339. }
  340.  
  341. //Users
  342. public void addPlayer(Player player) {
  343. Connection conn = null;
  344. PreparedStatement ps = null;
  345. ResultSet rs = null;
  346. try {
  347. conn = getConnection();
  348. ps = conn.prepareStatement("INSERT INTO " + usersTable + " (name, groups, prefix, commands, admin, canmodifyworld, ignoresrestrictions) VALUES (?,?,?,?,?,?,?)", Statement.RETURN_GENERATED_KEYS);
  349. ps.setString(1, player.getName());
  350. ps.setString(2, etc.combineSplit(0, player.getGroups(), ","));
  351. ps.setString(3, player.getPrefix());
  352. ps.setString(4, etc.combineSplit(0, player.getCommands(), ","));
  353. ps.setBoolean(5, player.getAdmin());
  354. ps.setBoolean(6, player.canModifyWorld());
  355. ps.setBoolean(7, player.ignoreRestrictions());
  356. ps.executeUpdate();
  357.  
  358. rs = ps.getGeneratedKeys();
  359. if (rs.next()) {
  360. player.setSqlId(rs.getInt(1));
  361. }
  362. } catch (SQLException ex) {
  363. log.log(Level.SEVERE, "Unable to insert user into users table", ex);
  364. } finally {
  365. try {
  366. if (ps != null) {
  367. ps.close();
  368. }
  369. if (rs != null) {
  370. rs.close();
  371. }
  372. if (conn != null) {
  373. conn.close();
  374. }
  375. } catch (SQLException ex) {
  376. }
  377. }
  378. }
  379.  
  380. public void modifyPlayer(Player player) {
  381. Connection conn = null;
  382. PreparedStatement ps = null;
  383. try {
  384. conn = getConnection();
  385. ps = conn.prepareStatement("UPDATE " + usersTable + " SET groups = ?, prefix = ?, commands = ?, admin = ?, canmodifyworld = ?, ignoresrestrictions = ? WHERE id = ?");
  386. ps.setString(1, etc.combineSplit(0, player.getGroups(), ","));
  387. ps.setString(2, player.getPrefix());
  388. ps.setString(3, etc.combineSplit(0, player.getCommands(), ","));
  389. ps.setBoolean(4, player.getAdmin());
  390. ps.setBoolean(5, player.canModifyWorld());
  391. ps.setBoolean(6, player.ignoreRestrictions());
  392. ps.setInt(7, player.getSqlId());
  393. ps.executeUpdate();
  394. } catch (SQLException ex) {
  395. log.log(Level.SEVERE, "Unable to update user in users table", ex);
  396. } finally {
  397. try {
  398. if (ps != null) {
  399. ps.close();
  400. }
  401. if (conn != null) {
  402. conn.close();
  403. }
  404. } catch (SQLException ex) {
  405. }
  406. }
  407. }
  408.  
  409. public boolean doesPlayerExist(String player) {
  410. boolean exists = false;
  411. Connection conn = null;
  412. PreparedStatement ps = null;
  413. ResultSet rs = null;
  414. try {
  415. conn = getConnection();
  416. ps = conn.prepareStatement("SELECT * FROM " + usersTable + " WHERE name = ?");
  417. ps.setString(1, player);
  418. rs = ps.executeQuery();
  419. if (rs.next()) {
  420. exists = true;
  421. }
  422. } catch (SQLException ex) {
  423. log.log(Level.SEVERE, "Unable to check if user exists", ex);
  424. } finally {
  425. try {
  426. if (ps != null) {
  427. ps.close();
  428. }
  429. if (rs != null) {
  430. rs.close();
  431. }
  432. if (conn != null) {
  433. conn.close();
  434. }
  435. } catch (SQLException ex) {
  436. }
  437. }
  438. return exists;
  439. }
  440.  
  441. //Groups
  442. public void addGroup(Group group) {
  443. throw new UnsupportedOperationException("Not supported yet.");
  444. }
  445.  
  446. public void modifyGroup(Group group) {
  447. throw new UnsupportedOperationException("Not supported yet.");
  448. }
  449.  
  450. //Kits
  451. public void addKit(Kit kit) {
  452. throw new UnsupportedOperationException("Not supported yet.");
  453. }
  454.  
  455. public void modifyKit(Kit kit) {
  456. throw new UnsupportedOperationException("Not supported yet.");
  457. }
  458.  
  459. //Homes
  460. public void addHome(Warp home) {
  461. Connection conn = null;
  462. PreparedStatement ps = null;
  463. ResultSet rs = null;
  464. try {
  465. conn = getConnection();
  466. ps = conn.prepareStatement("INSERT INTO " + homesTable + " (name, x, y, z, rotX, rotY, `group`) VALUES(?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
  467. ps.setString(1, home.Name);
  468. ps.setDouble(2, home.Location.x);
  469. ps.setDouble(3, home.Location.y);
  470. ps.setDouble(4, home.Location.z);
  471. ps.setFloat(5, home.Location.rotX);
  472. ps.setFloat(6, home.Location.rotY);
  473. ps.setString(7, home.Group);
  474. ps.executeUpdate();
  475.  
  476. rs = ps.getGeneratedKeys();
  477. if (rs.next()) {
  478. home.ID = rs.getInt(1);
  479. synchronized (homeLock) {
  480. homes.add(home);
  481. }
  482. }
  483. } catch (SQLException ex) {
  484. log.log(Level.SEVERE, "Unable to insert home into homes table", ex);
  485. } finally {
  486. try {
  487. if (ps != null) {
  488. ps.close();
  489. }
  490. if (rs != null) {
  491. rs.close();
  492. }
  493. if (conn != null) {
  494. conn.close();
  495. }
  496. } catch (SQLException ex) {
  497. }
  498. }
  499. }
  500.  
  501. public void changeHome(Warp home) {
  502. Connection conn = null;
  503. PreparedStatement ps = null;
  504. try {
  505. conn = getConnection();
  506. ps = conn.prepareStatement("UPDATE " + homesTable + " SET x = ?, y = ?, z = ?, rotX = ?, rotY = ?, `group` = ? WHERE name = ?");
  507. ps.setDouble(1, home.Location.x);
  508. ps.setDouble(2, home.Location.y);
  509. ps.setDouble(3, home.Location.z);
  510. ps.setFloat(4, home.Location.rotX);
  511. ps.setFloat(5, home.Location.rotY);
  512. ps.setString(6, home.Group);
  513. ps.setString(7, home.Name);
  514. ps.executeUpdate();
  515.  
  516. synchronized (homeLock) {
  517. Warp toRem = null;
  518. for (Warp h : homes) {
  519. if (h.Name.equalsIgnoreCase(home.Name)) {
  520. toRem = h;
  521. }
  522. }
  523. if (toRem != null) {
  524. homes.remove(toRem);
  525. }
  526. homes.add(home);
  527. }
  528. } catch (SQLException ex) {
  529. log.log(Level.SEVERE, "Unable to update home in homes table", ex);
  530. } finally {
  531. try {
  532. if (ps != null) {
  533. ps.close();
  534. }
  535. if (conn != null) {
  536. conn.close();
  537. }
  538. } catch (SQLException ex) {
  539. }
  540. }
  541. }
  542.  
  543. //Warps
  544. public void addWarp(Warp warp) {
  545. Connection conn = null;
  546. PreparedStatement ps = null;
  547. ResultSet rs = null;
  548. try {
  549. conn = getConnection();
  550. ps = conn.prepareStatement("INSERT INTO " + warpsTable + " (name, x, y, z, rotX, rotY, `group`) VALUES(?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
  551. ps.setString(1, warp.Name);
  552. ps.setDouble(2, warp.Location.x);
  553. ps.setDouble(3, warp.Location.y);
  554. ps.setDouble(4, warp.Location.z);
  555. ps.setFloat(5, warp.Location.rotX);
  556. ps.setFloat(6, warp.Location.rotY);
  557. ps.setString(7, warp.Group);
  558. ps.executeUpdate();
  559.  
  560. rs = ps.getGeneratedKeys();
  561. if (rs.next()) {
  562. warp.ID = rs.getInt(1);
  563. synchronized (warpLock) {
  564. warps.add(warp);
  565. }
  566. }
  567. } catch (SQLException ex) {
  568. log.log(Level.SEVERE, "Unable to insert warp into warps table", ex);
  569. } finally {
  570. try {
  571. if (ps != null) {
  572. ps.close();
  573. }
  574. if (rs != null) {
  575. rs.close();
  576. }
  577. if (conn != null) {
  578. conn.close();
  579. }
  580. } catch (SQLException ex) {
  581. }
  582. }
  583. }
  584.  
  585. public void changeWarp(Warp warp) {
  586. Connection conn = null;
  587. PreparedStatement ps = null;
  588. try {
  589. conn = getConnection();
  590. ps = conn.prepareStatement("UPDATE " + warpsTable + " SET x = ?, y = ?, z = ?, rotX = ?, rotY = ?, `group` = ? WHERE name = ?");
  591. ps.setDouble(1, warp.Location.x);
  592. ps.setDouble(2, warp.Location.y);
  593. ps.setDouble(3, warp.Location.z);
  594. ps.setFloat(4, warp.Location.rotX);
  595. ps.setFloat(5, warp.Location.rotY);
  596. ps.setString(6, warp.Group);
  597. ps.setString(7, warp.Name);
  598. ps.executeUpdate();
  599.  
  600. synchronized (warpLock) {
  601. Warp toRem = null;
  602. for (Warp h : warps) {
  603. if (h.Name.equalsIgnoreCase(warp.Name)) {
  604. toRem = h;
  605. }
  606. }
  607. if (toRem != null) {
  608. warps.remove(toRem);
  609. }
  610. warps.add(warp);
  611. }
  612. } catch (SQLException ex) {
  613. log.log(Level.SEVERE, "Unable to update warp in warps table", ex);
  614. } finally {
  615. try {
  616. if (ps != null) {
  617. ps.close();
  618. }
  619. if (conn != null) {
  620. conn.close();
  621. }
  622. } catch (SQLException ex) {
  623. }
  624. }
  625. }
  626.  
  627. public void removeWarp(Warp warp) {
  628. Connection conn = null;
  629. PreparedStatement ps = null;
  630. try {
  631. conn = getConnection();
  632. ps = conn.prepareStatement("DELETE FROM " + warpsTable + " WHERE id = ?");
  633. ps.setDouble(1, warp.ID);
  634. ps.executeUpdate();
  635. } catch (SQLException ex) {
  636. log.log(Level.SEVERE, "Unable to delete warp from warps table", ex);
  637. } finally {
  638. try {
  639. if (ps != null) {
  640. ps.close();
  641. }
  642. if (conn != null) {
  643. conn.close();
  644. }
  645. } catch (SQLException ex) {
  646. }
  647. }
  648. synchronized (warpLock) {
  649. warps.remove(warp);
  650. }
  651. }
  652.  
  653. //Whitelist
  654. public void addToWhitelist(String name) {
  655. if (isUserOnWhitelist(name))
  656. return;
  657.  
  658. Connection conn = null;
  659. PreparedStatement ps = null;
  660. try {
  661. conn = getConnection();
  662. ps = conn.prepareStatement("INSERT INTO " + whitelistTable + " VALUES(?)");
  663. ps.setString(1, name);
  664. ps.executeUpdate();
  665. synchronized (whiteListLock) {
  666. whiteList.add(name);
  667. }
  668. } catch (SQLException ex) {
  669. log.log(Level.SEVERE, "Unable to update whitelist", ex);
  670. } finally {
  671. try {
  672. if (ps != null) {
  673. ps.close();
  674. }
  675. if (conn != null) {
  676. conn.close();
  677. }
  678. } catch (SQLException ex) {
  679. }
  680. }
  681. }
  682.  
  683. public void removeFromWhitelist(String name) {
  684. if (!isUserOnWhitelist(name))
  685. return;
  686.  
  687. Connection conn = null;
  688. PreparedStatement ps = null;
  689. try {
  690. conn = getConnection();
  691. ps = conn.prepareStatement("DELETE FROM " + whitelistTable + " WHERE name = ?");
  692. ps.setString(1, name);
  693. ps.executeUpdate();
  694. synchronized (whiteListLock) {
  695. whiteList.add(name);
  696. }
  697. } catch (SQLException ex) {
  698. log.log(Level.SEVERE, "Unable to update whitelist", ex);
  699. } finally {
  700. try {
  701. if (ps != null) {
  702. ps.close();
  703. }
  704. if (conn != null) {
  705. conn.close();
  706. }
  707. } catch (SQLException ex) {
  708. }
  709. }
  710. }
  711.  
  712. //Reservelist
  713. public void addToReserveList(String name) {
  714. if (isUserOnReserveList(name))
  715. return;
  716.  
  717. Connection conn = null;
  718. PreparedStatement ps = null;
  719. try {
  720. conn = getConnection();
  721. ps = conn.prepareStatement("INSERT INTO " + reservelistTable + " VALUES(?)");
  722. ps.setString(1, name);
  723. ps.executeUpdate();
  724. synchronized (reserveListLock) {
  725. reserveList.add(name);
  726. }
  727. } catch (SQLException ex) {
  728. log.log(Level.SEVERE, "Unable to update reservelist", ex);
  729. } finally {
  730. try {
  731. if (ps != null) {
  732. ps.close();
  733. }
  734. if (conn != null) {
  735. conn.close();
  736. }
  737. } catch (SQLException ex) {
  738. }
  739. }
  740. }
  741.  
  742. public void removeFromReserveList(String name) {
  743. if (!isUserOnReserveList(name))
  744. return;
  745.  
  746. Connection conn = null;
  747. PreparedStatement ps = null;
  748. try {
  749. conn = getConnection();
  750. ps = conn.prepareStatement("DELETE FROM " + reservelistTable + " WHERE name = ?");
  751. ps.setString(1, name);
  752. ps.executeUpdate();
  753. synchronized (reserveListLock) {
  754. reserveList.add(name);
  755. }
  756. } catch (SQLException ex) {
  757. log.log(Level.SEVERE, "Unable to update reservelist", ex);
  758. } finally {
  759. try {
  760. if (ps != null) {
  761. ps.close();
  762. }
  763. if (conn != null) {
  764. conn.close();
  765. }
  766. } catch (SQLException ex) {
  767. }
  768. }
  769. }
  770.  
  771. public Player getPlayer(String name) {
  772. Player player = new Player();
  773. Connection conn = null;
  774. PreparedStatement ps = null;
  775. ResultSet rs = null;
  776. try {
  777. conn = getConnection();
  778. ps = conn.prepareStatement("SELECT * FROM " + usersTable + " WHERE name = ?");
  779. ps.setString(1, name);
  780. rs = ps.executeQuery();
  781. if (rs.next()) {
  782. player.setSqlId(rs.getInt("id"));
  783. player.setGroups(rs.getString("groups").split(","));
  784. player.setCommands(rs.getString("commands").split(","));
  785. player.setPrefix(rs.getString("prefix"));
  786. player.setAdmin(rs.getBoolean("admin"));
  787. player.setCanModifyWorld(rs.getBoolean("canmodifyworld"));
  788. player.setIgnoreRestrictions(rs.getBoolean("ignoresrestrictions"));
  789. player.setIps(rs.getString("ip").split(","));
  790. }
  791. } catch (SQLException ex) {
  792. log.log(Level.SEVERE, "Unable to retreive users from user table", ex);
  793. } finally {
  794. try {
  795. if (ps != null) {
  796. ps.close();
  797. }
  798. if (rs != null) {
  799. rs.close();
  800. }
  801. if (conn != null) {
  802. conn.close();
  803. }
  804. } catch (SQLException ex) {
  805. }
  806. }
  807. return player;
  808. }
  809.  
  810. public void loadBanList() {
  811. synchronized (banLock) {
  812. bans = new ArrayList<Ban>();
  813. Connection conn = null;
  814. PreparedStatement ps = null;
  815. ResultSet rs = null;
  816. try {
  817. conn = getConnection();
  818. ps = conn.prepareStatement("SELECT * FROM " + bansTable);
  819. rs = ps.executeQuery();
  820. while (rs.next()) {
  821. Ban ban = new Ban();
  822. ban.setName(rs.getString("name"));
  823. ban.setIp(rs.getString("ip"));
  824. ban.setReason(rs.getString("reason"));
  825. ban.setTimestamp(rs.getInt("length"));
  826. bans.add(ban);
  827. }
  828. } catch (SQLException ex) {
  829. log.log(Level.SEVERE, "Unable to retreive bans from ban table", ex);
  830. } finally {
  831. try {
  832. if (ps != null) {
  833. ps.close();
  834. }
  835. if (rs != null) {
  836. rs.close();
  837. }
  838. if (conn != null) {
  839. conn.close();
  840. }
  841. } catch (SQLException ex) {
  842. }
  843. }
  844. }
  845. }
  846.  
  847. public void modifyBan(Ban ban) {
  848. throw new UnsupportedOperationException("Not supported yet.");
  849. }
  850. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement