Advertisement
Guest User

Untitled

a guest
Nov 6th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. package me.mrdarkness462.thepit.pluginsupport.storage;
  2.  
  3. import me.mrdarkness462.thepit.pluginfiles.Settings;
  4. import me.mrdarkness462.thepit.thepitfeatures.playersync.InventoryConverter;
  5. import me.mrdarkness462.thepit.thepitfeatures.playersync.SyncType;
  6. import org.bukkit.entity.Player;
  7.  
  8. import java.sql.*;
  9. import java.util.List;
  10.  
  11. public class MySQL implements Database {
  12. private Connection connection;
  13. private Settings settings = new Settings();
  14. private String host = settings.DB_host();
  15. private int port = settings.DB_port();
  16. private String database = settings.DB_database();
  17. private String ssl = settings.DB_ssl();
  18. private String username = settings.DB_username();
  19. private String password = settings.DB_password();
  20. private static MySQL instance = new MySQL();
  21. private InventoryConverter inventoryConverter = new InventoryConverter();
  22.  
  23. private MySQL() {
  24. if (!connected()) {
  25. connect();
  26. }
  27. }
  28.  
  29. public void connect() {
  30. try {
  31. if (connection != null && !connection.isClosed()) {
  32. return;
  33. }
  34. synchronized (this) {
  35. if (connection != null && !connection.isClosed()) {
  36. return;
  37. }
  38. Class.forName("com.mysql.jdbc.Driver");
  39. connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true&user=" + username + "&password=" + password + "&useSSL=" + ssl);
  40. }
  41. } catch (SQLException | ClassNotFoundException ex) {
  42. ex.printStackTrace();
  43. }
  44. }
  45.  
  46. public boolean connected() {
  47. return connection != null;
  48. }
  49.  
  50. public void close() {
  51. if (connected()) {
  52. try {
  53. connection.close();
  54. } catch (SQLException ex) {
  55. ex.printStackTrace();
  56. }
  57. }
  58. }
  59.  
  60. public int getInt(Player p, String stats, String table) {
  61. if (!connected()) {
  62. connect();
  63. }
  64. try {
  65. ResultSet rs = connection.createStatement().executeQuery("SELECT " + stats + " FROM " + table + " WHERE UUID = '" + p.getUniqueId() + "';");
  66. if (rs.next()) {
  67. return rs.getInt(stats);
  68. }
  69. } catch (SQLException e) {
  70. e.printStackTrace();
  71. }
  72. return 0;
  73. }
  74.  
  75. public void setInt(Player p, int value, String stats, String table) {
  76. if (!connected()) {
  77. connect();
  78. }
  79. if (hasAccount(p, table)) {
  80. try {
  81. connection.createStatement().executeUpdate("UPDATE " + table + " SET " + stats + " = '" + value + "' WHERE UUID = '" + p.getUniqueId() + "';");
  82. } catch (SQLException ex) {
  83. ex.printStackTrace();
  84. }
  85. }
  86. }
  87.  
  88. public String getString(Player p, String stats, String table) {
  89. if (!connected()) {
  90. connect();
  91. }
  92. try {
  93. ResultSet rs = connection.createStatement().executeQuery("SELECT " + stats + " FROM " + table + " WHERE UUID = '" + p.getUniqueId() + "';");
  94. if (rs.next()) {
  95. return rs.getString(stats);
  96. }
  97. } catch (SQLException e) {
  98. e.printStackTrace();
  99. }
  100. return "";
  101. }
  102.  
  103. public void setString(Player p, String value, String stats, String table) {
  104. if (!connected()) {
  105. connect();
  106. }
  107. if (hasAccount(p, table)) {
  108. try {
  109. connection.createStatement().executeUpdate("UPDATE " + table + " SET " + stats + " = '" + value + "' WHERE UUID = '" + p.getUniqueId() + "';");
  110. } catch (SQLException ex) {
  111. ex.printStackTrace();
  112. }
  113. }
  114. }
  115.  
  116. public void update(Player p, String sql, String table) {
  117. if (!connected()) {
  118. connect();
  119. }
  120. if (hasAccount(p, table)) {
  121. try {
  122. connection.createStatement().executeUpdate("UPDATE " + table + " " + sql + " WHERE UUID = '" + p.getUniqueId() + "';");
  123. } catch (SQLException ex) {
  124. ex.printStackTrace();
  125. }
  126. }
  127. }
  128.  
  129. public void createTable(String table, List<String> columns) {
  130. if (!connected()) {
  131. connect();
  132. }
  133. try {
  134. connection.createStatement().execute("CREATE TABLE IF NOT EXISTS `" + table + "` (" + String.join(", ", columns) + ");");
  135. } catch (SQLException ex) {
  136. ex.printStackTrace();
  137. }
  138. }
  139.  
  140. public boolean hasAccount(Player p, String table) {
  141. if (!connected()) {
  142. connect();
  143. }
  144. ResultSet rs = null;
  145. Statement statement = null;
  146. try {
  147. statement = connection.createStatement();
  148. } catch (SQLException ex) {
  149. ex.printStackTrace();
  150. }
  151. try {
  152. if (statement != null) {
  153. rs = statement.executeQuery("SELECT UUID FROM " + table + " WHERE UUID = '" + p.getUniqueId() + "';");
  154. return rs.next();
  155. }
  156. return false;
  157. } catch (SQLException ex) {
  158. ex.printStackTrace();
  159. return false;
  160. } finally {
  161. try {
  162. if (rs != null) {
  163. rs.close();
  164. statement.close();
  165. }
  166. } catch (SQLException ex) {
  167. ex.printStackTrace();
  168. }
  169. }
  170. }
  171.  
  172. public void addColumn(String column, String type, String def, String table) {
  173. if (!connected()) {
  174. connect();
  175. }
  176. try {
  177. connection.createStatement().execute("ALTER TABLE `" + table + "` ADD COLUMN `" + column + "` " + type + " NOT NULL default '" + def + "';");
  178. } catch (SQLException ignored) {
  179. }
  180. }
  181.  
  182. public void addColumn(String column, String type, String table) {
  183. if (!connected()) {
  184. connect();
  185. }
  186. try {
  187. connection.createStatement().execute("ALTER TABLE `" + table + "` ADD COLUMN `" + column + "` " + type + ";");
  188. } catch (SQLException ignored) {
  189. }
  190. }
  191.  
  192. public void deletePlayer(Player p, String table) {
  193. if (!connected()) {
  194. connect();
  195. }
  196. try {
  197. connection.createStatement().execute("DELETE FROM " + table + " WHERE UUID = '" + p.getUniqueId() + "';");
  198. } catch (SQLException ex) {
  199. ex.printStackTrace();
  200. }
  201. }
  202.  
  203. public void createPlayer(String table, String columns, String values) {
  204. if (!connected()) {
  205. connect();
  206. }
  207. try {
  208. connection.createStatement().execute("INSERT INTO " + table + " (" + columns + ") VALUES (" + values + ");");
  209. } catch (SQLException ex) {
  210. ex.printStackTrace();
  211. }
  212. }
  213.  
  214. public void savePlayer(Player p) {
  215. if (!connected()) {
  216. connect();
  217. }
  218. String pInv = SyncType.PLAYER_INVENTORY.name() + " = '" + inventoryConverter.getString(p.getInventory()) + "', ";
  219. String pEch = SyncType.PLAYER_ENDERCHEST.name() + " = '" + inventoryConverter.getString(p.getEnderChest()) + "', ";
  220. String pExp = SyncType.PLAYER_XP.name() + " = " + p.getTotalExperience() + " ";
  221. String sync = pInv + pEch + pExp;
  222. try {
  223. PreparedStatement preparedStatement = connection.prepareStatement("UPDATE ThePitPlayerSync SET " + sync + "WHERE UUID = '" + p.getUniqueId() + "';");
  224. preparedStatement.executeUpdate();
  225. } catch (SQLException ex) {
  226. ex.printStackTrace();
  227. }
  228. }
  229.  
  230. public static MySQL getInstance() {
  231. return instance;
  232. }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement