Advertisement
Guest User

Untitled

a guest
Apr 13th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.31 KB | None | 0 0
  1. package tk.freedomfrontier.frontier;
  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.ArrayList;
  9. import org.apache.commons.lang.StringEscapeUtils;
  10. import org.bukkit.Bukkit;
  11. import org.bukkit.ChatColor;
  12. import org.bukkit.entity.Player;
  13.  
  14. public class Frontier_DatabaseInterface
  15. {
  16.  
  17. private static Connection connection;
  18. private static int queries;
  19.  
  20. public static Connection getConnection()
  21. {
  22. queries++;
  23. if (connection != null)
  24. {
  25. if (queries >= 2000)
  26. {
  27. closeConnection(connection);
  28. queries = 0;
  29. connection = null;
  30. return getConnection();
  31. }
  32. try
  33. {
  34. if (connection.isClosed())
  35. {
  36. connection = null;
  37. return getConnection();
  38. }
  39. connection.setAutoCommit(false);
  40. return connection;
  41. }
  42. catch (SQLException ex)
  43. {
  44. Frontier.plugin.handleException(ex);
  45. }
  46. }
  47. else
  48. {
  49. try
  50. {
  51. Class.forName("com.mysql.jdbc.Driver").newInstance();
  52. connection = DriverManager.getConnection("jdbc:mysql://" + Frontier.config.getString("database.ip") + "/" + Frontier.config.getString("database.database"), Frontier.config.getString("database.username"), Frontier.config.getString("database.password"));
  53. connection.setAutoCommit(false);
  54. return connection;
  55. }
  56. catch (SQLException | ClassNotFoundException | InstantiationException | IllegalAccessException ex)
  57. {
  58. Frontier.plugin.handleException(ex);
  59. Bukkit.broadcastMessage(ChatColor.RED + "The Frontier Plugin could not establish a connection to the MySQL Database, therefore it has shut down to protect the server from potential damage.");
  60. Bukkit.getPluginManager().disablePlugin(Frontier.plugin);
  61. return null;
  62. }
  63. }
  64. return connection;
  65. }
  66.  
  67. public static void prepareDatabase() throws SQLException
  68. {
  69. PreparedStatement statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS ANNOUNCEMENTS ("
  70. + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
  71. + "`MESSAGE` TEXT,"
  72. + "`INTERVAL` INT)"
  73. );
  74. statement.executeUpdate();
  75. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS AREAS ("
  76. + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
  77. + "`NAME` VARCHAR(255) UNIQUE,"
  78. + "`OWNER` TEXT,"
  79. + "`RANK` TEXT,"
  80. + "`WORLD` TEXT,"
  81. + "`X` FLOAT,"
  82. + "`Y` FLOAT,"
  83. + "`Z` FLOAT,"
  84. + "`RANGE` INT,"
  85. + "`ALLOWED` TEXT)"
  86. );
  87. statement.executeUpdate();
  88. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS CHATS ("
  89. + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
  90. + "`NAME` VARCHAR(255) UNIQUE,"
  91. + "`COLOUR` TEXT,"
  92. + "`OWNER` TEXT,"
  93. + "`RANK` TEXT,"
  94. + "`ALLOWED` TEXT)"
  95. );
  96. statement.executeUpdate();
  97. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS COMMANDS ("
  98. + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
  99. + "`COMMAND` TEXT,"
  100. + "`RANK` INT,"
  101. + "`MESSAGE` TEXT,"
  102. + "`KICK` BOOLEAN,"
  103. + "`ARGS` TEXT)"
  104. );
  105. statement.executeUpdate();
  106. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS IP_BANS ("
  107. + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
  108. + "`IP` VARCHAR(255) UNIQUE,"
  109. + "`REASON` TEXT,"
  110. + "`PERM` BOOLEAN)"
  111. );
  112. statement.executeUpdate();
  113. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS NAME_BANS ("
  114. + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
  115. + "`NAME` VARCHAR(255) UNIQUE,"
  116. + "`REASON` TEXT,"
  117. + "`PERM` BOOLEAN)"
  118. );
  119. statement.executeUpdate();
  120. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS PLAYERS ("
  121. + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
  122. + "`UUID` VARCHAR(255) UNIQUE,"
  123. + "`NAME` TEXT,"
  124. + "`IP` TEXT,"
  125. + "`RANK` TEXT,"
  126. + "`NICK` TEXT,"
  127. + "`TAG` TEXT,"
  128. + "`LOGIN` TEXT,"
  129. + "`CHAT` TEXT,"
  130. + "`MOTD` TEXT,"
  131. + "`IMPOSTER` BOOLEAN,"
  132. + "`BANHAMMER` BOOLEAN,"
  133. + "`BUILDER` BOOLEAN,"
  134. + "`DOUBLEJUMP` BOOLEAN,"
  135. + "`GODMODE` BOOLEAN,"
  136. + "`MUTE` BOOLEAN,"
  137. + "`FROZEN` BOOLEAN,"
  138. + "`CMDBLOCK` BOOLEAN,"
  139. + "`ELYTRAWEAPONS` BOOLEAN,"
  140. + "`LASTLOGIN` BIGINT,"
  141. + "`CHATLEVEL` INT)"
  142. );
  143. statement.executeUpdate();
  144. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS REPORTS ("
  145. + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
  146. + "`REPORTED` TEXT,"
  147. + "`REPORTER` TEXT,"
  148. + "`REASON` TEXT)"
  149. );
  150. statement.executeUpdate();
  151. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS UUID_BANS ("
  152. + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
  153. + "`UUID` VARCHAR(255) UNIQUE,"
  154. + "`REASON` TEXT,"
  155. + "`PERM` BOOLEAN)"
  156. );
  157. statement.executeUpdate();
  158. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS WORLDS ("
  159. + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
  160. + "`NAME` VARCHAR(255) UNIQUE,"
  161. + "`GENERATOR` TEXT,"
  162. + "`RANK` TEXT,"
  163. + "`ONENABLE` BOOLEAN)"
  164. );
  165. statement.executeUpdate();
  166. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS VERIFICATION ("
  167. + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
  168. + "`UUID` VARCHAR(255) UNIQUE,"
  169. + "`FORUMID` INT,"
  170. + "`CODE` TEXT)"
  171. );
  172. statement.executeUpdate();
  173. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS LEVELCHATS ("
  174. + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
  175. + "`PLAYER` TEXT,"
  176. + "`LEVEL` INT,"
  177. + "`MESSAGE` TEXT)"
  178. );
  179. statement.executeUpdate();
  180. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS CAPTCHAS ("
  181. + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
  182. + "`UUID` TEXT,"
  183. + "`IP` TEXT,"
  184. + "`EXPIRES` DATETIME,"
  185. + "`VERIFIED` BOOLEAN)"
  186. );
  187. statement.executeUpdate();
  188. getConnection().commit();
  189. }
  190.  
  191. public static void generateNewPlayer(Player player) throws SQLException
  192. {
  193. Connection c = getConnection();
  194. PreparedStatement statement = c.prepareStatement("INSERT IGNORE INTO PLAYERS (UUID, NAME, IP, RANK, NICK, TAG, LOGIN, CHAT, MOTD, IMPOSTER, BANHAMMER, BUILDER, DOUBLEJUMP, ELYTRAWEAPONS, GODMODE, MUTE, FROZEN, CMDBLOCK, LASTLOGIN, CHATLEVEL) VALUES (?, ?, ?, 'Op', 'off&r', 'off&r', '', '', 'off', 0, 0, 0, 0, 1, 0, 0, 0, 0, ?, 0)");
  195. statement.setString(1, StringEscapeUtils.escapeSql(player.getUniqueId().toString()));
  196. statement.setString(2, player.getName());
  197. statement.setString(3, player.getAddress().getAddress().getHostAddress());
  198. statement.setLong(4, System.nanoTime());
  199. statement.executeUpdate();
  200. c.commit();
  201. }
  202.  
  203. public static boolean updateInTable(String uniqueColumn, String uniqueValue, Object newValue, String columnToChange, String table) throws SQLException
  204. {
  205. Connection c = getConnection();
  206. PreparedStatement statement = c.prepareStatement("UPDATE " + table + " SET " + columnToChange + " = ? WHERE " + uniqueColumn + " = ?");
  207. statement.setObject(1, newValue);
  208. statement.setString(2, uniqueValue);
  209. int i = statement.executeUpdate();
  210. c.commit();
  211. return i > 0;
  212. }
  213.  
  214. public static ResultSet getAllResults(String uniqueColumn, String uniqueValue, String inTable) throws SQLException
  215. {
  216. Connection c = getConnection();
  217. PreparedStatement statement = null;
  218. if (uniqueColumn != null && uniqueValue != null)
  219. {
  220. statement = c.prepareStatement("SELECT * FROM " + inTable + " WHERE " + uniqueColumn + " = ?");
  221. statement.setString(1, uniqueValue);
  222. }
  223. else
  224. {
  225. statement = c.prepareStatement("SELECT * FROM " + inTable);
  226. }
  227. return statement.executeQuery();
  228. }
  229.  
  230. public static ArrayList<Object> getAsArrayList(String uniqueColumn, String uniqueValue, String lookingFor, String inTable) throws SQLException
  231. {
  232. ArrayList<Object> array = new ArrayList<>();
  233. Connection c = getConnection();
  234. PreparedStatement statement;
  235. if (uniqueColumn != null && uniqueValue != null)
  236. {
  237. statement = c.prepareStatement("SELECT * FROM " + inTable + " WHERE " + uniqueColumn + " = ?");
  238. statement.setString(1, uniqueValue);
  239. }
  240. else
  241. {
  242. statement = c.prepareStatement("SELECT * FROM " + inTable);
  243. }
  244. ResultSet set = statement.executeQuery();
  245. while (set.next())
  246. {
  247. array.add(set.getObject(lookingFor));
  248. }
  249. return array;
  250. }
  251.  
  252. public static Object getFromTable(String uniqueColumn, String uniqueValue, String lookingFor, String inTable) throws SQLException
  253. {
  254. Connection c = getConnection();
  255. PreparedStatement statement = c.prepareStatement("SELECT * FROM " + inTable + " WHERE " + uniqueColumn + " = ?");
  256. statement.setString(1, uniqueValue);
  257. ResultSet res = statement.executeQuery();
  258. if (res.next())
  259. {
  260. return res.getObject(lookingFor);
  261. }
  262. return null;
  263. }
  264.  
  265. public static Boolean getBooleanFromTable(String uniqueColumn, String uniqueValue, String lookingFor, String inTable) throws SQLException
  266. {
  267. Object result = getFromTable(uniqueColumn, uniqueValue, lookingFor, inTable);
  268. if (result instanceof Boolean)
  269. {
  270. return (Boolean) result;
  271. }
  272. if (result instanceof Integer)
  273. {
  274. return (Integer) result == 1;
  275. }
  276. return false;
  277. }
  278.  
  279. public static String getUuidFromName(String name) throws SQLException
  280. {
  281. Connection c = getConnection();
  282. PreparedStatement statement = c.prepareStatement("SELECT * FROM PLAYERS WHERE NAME = ?");
  283. statement.setString(1, name);
  284. ResultSet res = statement.executeQuery();
  285. if (res.next())
  286. {
  287. if (res.getObject("UUID") != null && res.getObject("UUID") instanceof String)
  288. {
  289. return (String) res.getObject("UUID");
  290. }
  291. }
  292. return null;
  293. }
  294.  
  295. public static String getIpFromName(String name) throws SQLException
  296. {
  297. Connection c = getConnection();
  298. PreparedStatement statement = c.prepareStatement("SELECT * FROM PLAYERS WHERE NAME = ?");
  299. statement.setString(1, name);
  300. ResultSet res = statement.executeQuery();
  301. if (res.next())
  302. {
  303. if (res.getObject("IP") != null && res.getObject("IP") instanceof String)
  304. {
  305. return (String) res.getObject("IP");
  306. }
  307. }
  308. return null;
  309. }
  310.  
  311. public static boolean playerExists(String uuid) throws SQLException
  312. {
  313. return getFromTable("UUID", uuid, "NAME", "PLAYERS") != null;
  314. }
  315.  
  316. public static boolean existsInTable(String uniqueColumn, Object uniqueValue, String table) throws SQLException
  317. {
  318. PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM " + table + " WHERE ? = ?");
  319. statement.setString(1, uniqueColumn);
  320. statement.setObject(2, uniqueValue);
  321. ResultSet set = statement.executeQuery();
  322. return set.next();
  323. }
  324.  
  325. public static String getLoginMessage(String uuid) throws SQLException
  326. {
  327. if (!playerExists(uuid))
  328. {
  329. return null;
  330. }
  331. Object obj = getFromTable("UUID", uuid, "LOGIN", "PLAYERS");
  332. if (obj instanceof String)
  333. {
  334. return (String) obj;
  335. }
  336. return null;
  337. }
  338.  
  339. public static String getRank(String uuid) throws SQLException
  340. {
  341. if (!playerExists(uuid))
  342. {
  343. return "Op";
  344. }
  345. Object obj = getFromTable("UUID", uuid, "RANK", "PLAYERS");
  346. if (obj instanceof String)
  347. {
  348. return (String) obj;
  349. }
  350. return "Op";
  351. }
  352.  
  353. public static String getTag(String uuid) throws SQLException
  354. {
  355. if (!playerExists(uuid))
  356. {
  357. return "&7[&c" + getRank(uuid) + "&7]";
  358. }
  359. Object obj = getFromTable("UUID", uuid, "TAG", "PLAYERS");
  360. if (obj instanceof String)
  361. {
  362. return (String) obj;
  363. }
  364. return "&7[&c" + getRank(uuid) + "&7]";
  365. }
  366.  
  367. public static String getNick(String uuid) throws SQLException
  368. {
  369. if (!playerExists(uuid))
  370. {
  371. return uuid;
  372. }
  373. Object obj = getFromTable("UUID", uuid, "NICK", "PLAYERS");
  374. if (obj instanceof String)
  375. {
  376. return (String) obj;
  377. }
  378. return uuid;
  379. }
  380.  
  381. public static boolean hasBanHammer(String uuid) throws SQLException
  382. {
  383. if (!playerExists(uuid))
  384. {
  385. return false;
  386. }
  387. Object obj = getFromTable("UUID", uuid, "BANHAMMER", "PLAYERS");
  388. if (obj instanceof Boolean)
  389. {
  390. return (Boolean) obj;
  391. }
  392. return false;
  393. }
  394.  
  395. public static boolean isGod(String uuid) throws SQLException
  396. {
  397. if (!playerExists(uuid))
  398. {
  399. return false;
  400. }
  401. Object obj = getFromTable("UUID", uuid, "GODMODE", "PLAYERS");
  402. if (obj instanceof Boolean)
  403. {
  404. return (Boolean) obj;
  405. }
  406. return false;
  407. }
  408.  
  409. public static void closeConnection(Connection connection)
  410. {
  411. try
  412. {
  413. if (connection != null)
  414. {
  415. connection.close();
  416. }
  417. }
  418. catch (SQLException e)
  419. {
  420. System.err.println(e);
  421. }
  422. }
  423. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement