Guest User

Untitled

a guest
Apr 13th, 2018
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.86 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. private static Connection connection;
  17. private static int queries;
  18.  
  19. public static Connection getConnection() {
  20. queries++;
  21. if (connection != null) {
  22. if (queries >= 2000) {
  23. closeConnection(connection);
  24. queries = 0;
  25. connection = null;
  26. return getConnection();
  27. }
  28. try {
  29. if (connection.isClosed()) {
  30. connection = null;
  31. return getConnection();
  32. }
  33. connection.setAutoCommit(false);
  34. return connection;
  35. } catch (SQLException ex) {
  36. Frontier.plugin.handleException(ex);
  37. }
  38. } else {
  39. try {
  40. Class.forName("com.mysql.jdbc.Driver").newInstance();
  41. 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"));
  42. connection.setAutoCommit(false);
  43. return connection;
  44. } catch (SQLException | ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
  45. Frontier.plugin.handleException(ex);
  46. 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.");
  47. Bukkit.getPluginManager().disablePlugin(Frontier.plugin);
  48. return null;
  49. }
  50. }
  51. return connection;
  52. }
  53.  
  54. public static void prepareDatabase() throws SQLException {
  55. PreparedStatement statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS ANNOUNCEMENTS (" + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "`MESSAGE` TEXT," + "`INTERVAL` INT)");
  56. statement.executeUpdate();
  57. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS AREAS (" + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "`NAME` VARCHAR(255) UNIQUE," + "`OWNER` TEXT," + "`RANK` TEXT," + "`WORLD` TEXT," + "`X` FLOAT," + "`Y` FLOAT," + "`Z` FLOAT," + "`RANGE` INT," + "`ALLOWED` TEXT)");
  58. statement.executeUpdate();
  59. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS CHATS (" + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "`NAME` VARCHAR(255) UNIQUE," + "`COLOUR` TEXT," + "`OWNER` TEXT," + "`RANK` TEXT," + "`ALLOWED` TEXT)");
  60. statement.executeUpdate();
  61. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS COMMANDS (" + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "`COMMAND` TEXT," + "`RANK` INT," + "`MESSAGE` TEXT," + "`KICK` BOOLEAN," + "`ARGS` TEXT)");
  62. statement.executeUpdate();
  63. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS IP_BANS (" + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "`IP` VARCHAR(255) UNIQUE," + "`REASON` TEXT," + "`PERM` BOOLEAN)");
  64. statement.executeUpdate();
  65. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS NAME_BANS (" + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "`NAME` VARCHAR(255) UNIQUE," + "`REASON` TEXT," + "`PERM` BOOLEAN)");
  66. statement.executeUpdate();
  67. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS PLAYERS (" + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "`UUID` VARCHAR(255) UNIQUE," + "`NAME` TEXT," + "`IP` TEXT," + "`RANK` TEXT," + "`NICK` TEXT," + "`TAG` TEXT," + "`LOGIN` TEXT," + "`CHAT` TEXT," + "`MOTD` TEXT," + "`IMPOSTER` BOOLEAN," + "`BANHAMMER` BOOLEAN," + "`BUILDER` BOOLEAN," + "`DOUBLEJUMP` BOOLEAN," + "`GODMODE` BOOLEAN," + "`MUTE` BOOLEAN," + "`FROZEN` BOOLEAN," + "`CMDBLOCK` BOOLEAN," + "`ELYTRAWEAPONS` BOOLEAN," + "`LASTLOGIN` BIGINT," + "`CHATLEVEL` INT)");
  68. statement.executeUpdate();
  69. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS REPORTS (" + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "`REPORTED` TEXT," + "`REPORTER` TEXT," + "`REASON` TEXT)");
  70. statement.executeUpdate();
  71. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS UUID_BANS (" + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "`UUID` VARCHAR(255) UNIQUE," + "`REASON` TEXT," + "`PERM` BOOLEAN)");
  72. statement.executeUpdate();
  73. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS WORLDS (" + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "`NAME` VARCHAR(255) UNIQUE," + "`GENERATOR` TEXT," + "`RANK` TEXT," + "`ONENABLE` BOOLEAN)");
  74. statement.executeUpdate();
  75. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS VERIFICATION (" + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "`UUID` VARCHAR(255) UNIQUE," + "`FORUMID` INT," + "`CODE` TEXT)");
  76. statement.executeUpdate();
  77. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS LEVELCHATS (" + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "`PLAYER` TEXT," + "`LEVEL` INT," + "`MESSAGE` TEXT)");
  78. statement.executeUpdate();
  79. statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS CAPTCHAS (" + "`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "`UUID` TEXT," + "`IP` TEXT," + "`EXPIRES` DATETIME," + "`VERIFIED` BOOLEAN)");
  80. statement.executeUpdate();
  81. getConnection().commit();
  82. }
  83.  
  84. public static void generateNewPlayer(Player player) throws SQLException {
  85. Connection c = getConnection();
  86. 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)");
  87. statement.setString(1, StringEscapeUtils.escapeSql(player.getUniqueId().toString()));
  88. statement.setString(2, player.getName());
  89. statement.setString(3, player.getAddress().getAddress().getHostAddress());
  90. statement.setLong(4, System.nanoTime());
  91. statement.executeUpdate();
  92. c.commit();
  93. }
  94.  
  95. public static boolean updateInTable(String uniqueColumn, String uniqueValue, Object newValue, String columnToChange, String table) throws SQLException {
  96. Connection c = getConnection();
  97. PreparedStatement statement = c.prepareStatement("UPDATE " + table + " SET " + columnToChange + " = ? WHERE " + uniqueColumn + " = ?");
  98. statement.setObject(1, newValue);
  99. statement.setString(2, uniqueValue);
  100. int i = statement.executeUpdate();
  101. c.commit();
  102. return i > 0;
  103. }
  104.  
  105. public static ResultSet getAllResults(String uniqueColumn, String uniqueValue, String inTable) throws SQLException {
  106. Connection c = getConnection();
  107. PreparedStatement statement = null;
  108. if (uniqueColumn != null && uniqueValue != null) {
  109. statement = c.prepareStatement("SELECT * FROM " + inTable + " WHERE " + uniqueColumn + " = ?");
  110. statement.setString(1, uniqueValue);
  111. } else {
  112. statement = c.prepareStatement("SELECT * FROM " + inTable);
  113. }
  114. return statement.executeQuery();
  115. }
  116.  
  117. public static ArrayList < Object > getAsArrayList(String uniqueColumn, String uniqueValue, String lookingFor, String inTable) throws SQLException {
  118. ArrayList < Object > array = new ArrayList < > ();
  119. Connection c = getConnection();
  120. PreparedStatement statement;
  121. if (uniqueColumn != null && uniqueValue != null) {
  122. statement = c.prepareStatement("SELECT * FROM " + inTable + " WHERE " + uniqueColumn + " = ?");
  123. statement.setString(1, uniqueValue);
  124. } else {
  125. statement = c.prepareStatement("SELECT * FROM " + inTable);
  126. }
  127. ResultSet set = statement.executeQuery();
  128. while (set.next()) {
  129. array.add(set.getObject(lookingFor));
  130. }
  131. return array;
  132. }
  133.  
  134. public static Object getFromTable(String uniqueColumn, String uniqueValue, String lookingFor, String inTable) throws SQLException {
  135. Connection c = getConnection();
  136. PreparedStatement statement = c.prepareStatement("SELECT * FROM " + inTable + " WHERE " + uniqueColumn + " = ?");
  137. statement.setString(1, uniqueValue);
  138. ResultSet res = statement.executeQuery();
  139. if (res.next()) {
  140. return res.getObject(lookingFor);
  141. }
  142. return null;
  143. }
  144.  
  145. public static Boolean getBooleanFromTable(String uniqueColumn, String uniqueValue, String lookingFor, String inTable) throws SQLException {
  146. Object result = getFromTable(uniqueColumn, uniqueValue, lookingFor, inTable);
  147. if (result instanceof Boolean) {
  148. return (Boolean) result;
  149. }
  150. if (result instanceof Integer) {
  151. return (Integer) result == 1;
  152. }
  153. return false;
  154. }
  155.  
  156. public static String getUuidFromName(String name) throws SQLException {
  157. Connection c = getConnection();
  158. PreparedStatement statement = c.prepareStatement("SELECT * FROM PLAYERS WHERE NAME = ?");
  159. statement.setString(1, name);
  160. ResultSet res = statement.executeQuery();
  161. if (res.next()) {
  162. if (res.getObject("UUID") != null && res.getObject("UUID") instanceof String) {
  163. return (String) res.getObject("UUID");
  164. }
  165. }
  166. return null;
  167. }
  168.  
  169. public static String getIpFromName(String name) throws SQLException {
  170. Connection c = getConnection();
  171. PreparedStatement statement = c.prepareStatement("SELECT * FROM PLAYERS WHERE NAME = ?");
  172. statement.setString(1, name);
  173. ResultSet res = statement.executeQuery();
  174. if (res.next()) {
  175. if (res.getObject("IP") != null && res.getObject("IP") instanceof String) {
  176. return (String) res.getObject("IP");
  177. }
  178. }
  179. return null;
  180. }
  181.  
  182. public static boolean playerExists(String uuid) throws SQLException {
  183. return getFromTable("UUID", uuid, "NAME", "PLAYERS") != null;
  184. }
  185.  
  186. public static boolean existsInTable(String uniqueColumn, Object uniqueValue, String table) throws SQLException {
  187. PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM " + table + " WHERE ? = ?");
  188. statement.setString(1, uniqueColumn);
  189. statement.setObject(2, uniqueValue);
  190. ResultSet set = statement.executeQuery();
  191. return set.next();
  192. }
  193.  
  194. public static String getLoginMessage(String uuid) throws SQLException {
  195. if (!playerExists(uuid)) {
  196. return null;
  197. }
  198. Object obj = getFromTable("UUID", uuid, "LOGIN", "PLAYERS");
  199. if (obj instanceof String) {
  200. return (String) obj;
  201. }
  202. return null;
  203. }
  204.  
  205. public static String getRank(String uuid) throws SQLException {
  206. if (!playerExists(uuid)) {
  207. return "Op";
  208. }
  209. Object obj = getFromTable("UUID", uuid, "RANK", "PLAYERS");
  210. if (obj instanceof String) {
  211. return (String) obj;
  212. }
  213. return "Op";
  214. }
  215.  
  216. public static String getTag(String uuid) throws SQLException {
  217. if (!playerExists(uuid)) {
  218. return "&7[&c" + getRank(uuid) + "&7]";
  219. }
  220. Object obj = getFromTable("UUID", uuid, "TAG", "PLAYERS");
  221. if (obj instanceof String) {
  222. return (String) obj;
  223. }
  224. return "&7[&c" + getRank(uuid) + "&7]";
  225. }
  226.  
  227. public static String getNick(String uuid) throws SQLException {
  228. if (!playerExists(uuid)) {
  229. return uuid;
  230. }
  231. Object obj = getFromTable("UUID", uuid, "NICK", "PLAYERS");
  232. if (obj instanceof String) {
  233. return (String) obj;
  234. }
  235. return uuid;
  236. }
  237.  
  238. public static boolean hasBanHammer(String uuid) throws SQLException {
  239. if (!playerExists(uuid)) {
  240. return false;
  241. }
  242. Object obj = getFromTable("UUID", uuid, "BANHAMMER", "PLAYERS");
  243. if (obj instanceof Boolean) {
  244. return (Boolean) obj;
  245. }
  246. return false;
  247. }
  248.  
  249. public static boolean isGod(String uuid) throws SQLException {
  250. if (!playerExists(uuid)) {
  251. return false;
  252. }
  253. Object obj = getFromTable("UUID", uuid, "GODMODE", "PLAYERS");
  254. if (obj instanceof Boolean) {
  255. return (Boolean) obj;
  256. }
  257. return false;
  258. }
  259.  
  260. public static void closeConnection(Connection connection) {
  261. try {
  262. if (connection != null) {
  263. connection.close();
  264. }
  265. } catch (SQLException e) {
  266. System.err.println(e);
  267. }
  268. }
  269. }
Add Comment
Please, Sign In to add comment