Advertisement
Guest User

Untitled

a guest
Mar 29th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.11 KB | None | 0 0
  1. package com.playfactions;
  2.  
  3. import java.io.File;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.Calendar;
  11. import java.util.GregorianCalendar;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14. import java.util.concurrent.TimeUnit;
  15. import java.util.regex.Matcher;
  16. import java.util.regex.Pattern;
  17.  
  18. import org.bukkit.scheduler.BukkitRunnable;
  19.  
  20. public class Mysql {
  21. private DatabaseType type;
  22. private String user;
  23. private String password;
  24. private String database;
  25. private String host;
  26. private Connection connection;
  27.  
  28. public Mysql(DatabaseType type) {
  29. this.type = type;
  30. startDatabase();
  31. }
  32.  
  33. public Mysql(DatabaseType type, String user, String password, String database, String host) {
  34. this.type = type;
  35. this.user = user;
  36. this.password = password;
  37. this.database = database;
  38. this.host = host;
  39. startDatabase();
  40. }
  41.  
  42. private void selectDriver() {
  43. try {
  44. if (this.type == DatabaseType.MYSQL) {
  45. Class.forName("com.mysql.jdbc.Driver");
  46. } else if (this.type == DatabaseType.SQLITE) {
  47. Class.forName("org.sqlite.JDBC");
  48. }
  49. } catch (ClassNotFoundException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53.  
  54. private void setConnection() {
  55. try {
  56. if (this.type == DatabaseType.MYSQL) {
  57. Class.forName("com.mysql.jdbc.Driver");
  58. this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + "/" + this.database,
  59. this.user, this.password);
  60. } else if (this.type == DatabaseType.SQLITE) {
  61. Class.forName("org.sqlite.JDBC");
  62. this.connection = DriverManager.getConnection("jdbc:sqlite:"
  63. + Main.instance.getDataFolder().getAbsolutePath() + File.separator + "database.db");
  64. }
  65. } catch (ClassNotFoundException e) {
  66. e.printStackTrace();
  67. } catch (SQLException e2) {
  68. e2.printStackTrace();
  69. }
  70. }
  71.  
  72. private void startDatabase() {
  73. try {
  74. setConnection();
  75. Statement stmt = this.connection.createStatement();
  76. if (this.type == DatabaseType.SQLITE) {
  77. stmt.execute("CREATE TABLE IF NOT EXISTS " + Main.instance.getConfig().getString("mysql.tabela")
  78. + " (id INTEGER PRIMARY KEY AUTOINCREMENT, nick varchar(255), kit varchar(255), delay varchar(512),PRIMARY KEY (nick))");
  79. } else {
  80. stmt.execute("CREATE TABLE IF NOT EXISTS " + Main.instance.getConfig().getString("mysql.tabela")
  81. + " (id INTEGER NOT NULL AUTO_INCREMENT,nick varchar(255), kit varchar(255), delay varchar(512), PRIMARY KEY (id))");
  82. }
  83. stmt.close();
  84. } catch (Exception e) {
  85. e.printStackTrace();
  86. }
  87. }
  88.  
  89. public static enum DatabaseType {
  90. MYSQL("MYSQL", 0), SQLITE("SQLITE", 1);
  91.  
  92. private DatabaseType(String s, int n) {
  93. }
  94. }
  95.  
  96. private Map<String, Long> map = new HashMap<>();
  97.  
  98. public void addDelay(String id, String kit) {
  99. this.map.put(id + ":" + kit, Long.valueOf(System.currentTimeMillis()));
  100. new BukkitRunnable() {
  101. @Override
  102. public void run() {
  103. try {
  104. connection.createStatement()
  105. .executeUpdate("INSERT INTO `" + Main.instance.getConfig().getString("mysql.tabela")
  106. + "` (`nick`,`delay`,`kit`) VALUES ('" + id + "', '" + System.currentTimeMillis() + "', '"
  107. + kit + "');");
  108. } catch (SQLException localSQLException) {
  109. }
  110. }
  111. }.runTaskAsynchronously(Main.instance);
  112. }
  113.  
  114. public long getDelay(String id, String kit) {
  115. if (this.map.containsKey(id + ":" + kit)) {
  116. return ((Long) this.map.get(id + ":" + kit)).longValue();
  117. }
  118. try {
  119. selectDriver();
  120. PreparedStatement stmt = this.connection.prepareStatement("SELECT * FROM `"
  121. + Main.instance.getConfig().getString("mysql.tabela") + "` WHERE nick=? and kit=?");
  122. stmt.setString(1, id);
  123. stmt.setString(2, kit);
  124. ResultSet rs = stmt.executeQuery();
  125. if (rs.next()) {
  126. this.map.put(id + ":" + kit, Long.valueOf(rs.getLong("delay")));
  127. return rs.getLong("delay");
  128. }
  129. return System.currentTimeMillis();
  130. } catch (SQLException ex) {
  131. }
  132. return 0L;
  133. }
  134.  
  135. public boolean acabouDelay(String id, long l, String kit) {
  136. String coisa = getDelayString(id, l, kit);
  137. return coisa.equals("0");
  138. }
  139.  
  140. public String getDelayString(String id, long l, String kit) {
  141. long tempoAntes = getDelay(id, kit);
  142. long tempoAtual = System.currentTimeMillis() - l;
  143. String coisa = formatDifferenceStr(tempoAntes - tempoAtual);
  144. return coisa;
  145. }
  146.  
  147. public void removeDelay(String id, String id2) {
  148. new BukkitRunnable() {
  149. @Override
  150. public void run() {
  151. try {
  152. selectDriver();
  153. ResultSet rs = connection.createStatement().executeQuery("SELECT * FROM `"
  154. + Main.instance.getConfig().getString("mysql.tabela") + "` WHERE `nick`='" + id + "';");
  155. if (rs.next()) {
  156. PreparedStatement stmt = connection.prepareStatement("DELETE FROM `"
  157. + Main.instance.getConfig().getString("mysql.tabela") + "` WHERE nick= ? and kit= ?");
  158. stmt.setString(1, id);
  159. stmt.setString(2, id2);
  160. stmt.executeUpdate();
  161. }
  162. } catch (SQLException localSQLException) {
  163. }
  164. }
  165. }.runTaskAsynchronously(Main.instance);
  166. }
  167.  
  168. public boolean temDelay(String id, String kit) {
  169. if (this.map.containsKey(id + ":" + kit)) {
  170. return true;
  171. }
  172. try {
  173. selectDriver();
  174. PreparedStatement stmt = this.connection.prepareStatement("SELECT * FROM `"
  175. + Main.instance.getConfig().getString("mysql.tabela") + "` WHERE nick= ? and kit= ?");
  176. stmt.setString(1, id);
  177. stmt.setString(2, kit);
  178. ResultSet rs = stmt.executeQuery();
  179. if (rs.next()) {
  180. long delay = rs.getLong("delay");
  181. this.map.put(id + ":" + kit, Long.valueOf(delay));
  182. return true;
  183. }
  184. } catch (SQLException localSQLException) {
  185. }
  186. return false;
  187. }
  188.  
  189. public static String formatDifferenceStr(long time) {
  190. if (time == 0L) {
  191. return "0";
  192. }
  193. long day = TimeUnit.MILLISECONDS.toDays(time);
  194. long hours = TimeUnit.MILLISECONDS.toHours(time) - day * 24L;
  195. long minutes = TimeUnit.MILLISECONDS.toMinutes(time) - TimeUnit.MILLISECONDS.toHours(time) * 60L;
  196. long seconds = TimeUnit.MILLISECONDS.toSeconds(time) - TimeUnit.MILLISECONDS.toMinutes(time) * 60L;
  197. StringBuilder sb = new StringBuilder();
  198. if (day > 0L) {
  199. sb.append(day).append(" ").append(day == 1L ? "dia" : "dias").append(" ");
  200. }
  201. if (hours > 0L) {
  202. sb.append(hours).append(" ").append(hours == 1L ? "hora" : "horas").append(" ");
  203. }
  204. if (minutes > 0L) {
  205. sb.append(minutes).append(" ").append(minutes == 1L ? "minuto" : "minutos").append(" ");
  206. }
  207. if (seconds > 0L) {
  208. sb.append(seconds).append(" ").append(seconds == 1L ? "segundo" : "segundos");
  209. }
  210. String diff = sb.toString();
  211. return diff.isEmpty() ? "0" : diff;
  212. }
  213.  
  214. public String getDifferenceFormatStr(long timestamp) {
  215. return formatDifferenceStr(timestamp - System.currentTimeMillis());
  216. }
  217.  
  218. public long parseDateDiff(String time, boolean future) throws Exception {
  219. Pattern timePattern = Pattern.compile(
  220. "(?:([0-9]+)\\s*y[a-z]*[,\\s]*)?(?:([0-9]+)\\s*mo[a-z]*[,\\s]*)?(?:([0-9]+)\\s*w[a-z]*[,\\s]*)?(?:([0-9]+)\\s*d[a-z]*[,\\s]*)?(?:([0-9]+)\\s*h[a-z]*[,\\s]*)?(?:([0-9]+)\\s*m[a-z]*[,\\s]*)?(?:([0-9]+)\\s*(?:s[a-z]*)?)?",
  221. 2);
  222. Matcher m = timePattern.matcher(time);
  223. int years = 0;
  224. int months = 0;
  225. int weeks = 0;
  226. int days = 0;
  227. int hours = 0;
  228. int minutes = 0;
  229. int seconds = 0;
  230. boolean found = false;
  231. while (m.find()) {
  232. if ((m.group() != null) && (!m.group().isEmpty())) {
  233. for (int i = 0; i < m.groupCount(); i++) {
  234. if ((m.group(i) != null) && (!m.group(i).isEmpty())) {
  235. found = true;
  236. break;
  237. }
  238. }
  239. if (found) {
  240. if ((m.group(1) != null) && (!m.group(1).isEmpty())) {
  241. years = Integer.parseInt(m.group(1));
  242. }
  243. if ((m.group(2) != null) && (!m.group(2).isEmpty())) {
  244. months = Integer.parseInt(m.group(2));
  245. }
  246. if ((m.group(3) != null) && (!m.group(3).isEmpty())) {
  247. weeks = Integer.parseInt(m.group(3));
  248. }
  249. if ((m.group(4) != null) && (!m.group(4).isEmpty())) {
  250. days = Integer.parseInt(m.group(4));
  251. }
  252. if ((m.group(5) != null) && (!m.group(5).isEmpty())) {
  253. hours = Integer.parseInt(m.group(5));
  254. }
  255. if ((m.group(6) != null) && (!m.group(6).isEmpty())) {
  256. minutes = Integer.parseInt(m.group(6));
  257. }
  258. if ((m.group(7) == null) || (m.group(7).isEmpty())) {
  259. break;
  260. }
  261. seconds = Integer.parseInt(m.group(7));
  262. break;
  263. }
  264. }
  265. }
  266. if (!found) {
  267. throw new Exception("Illegal Date");
  268. }
  269. if (years > 20) {
  270. throw new Exception("Illegal Date");
  271. }
  272. Calendar c = new GregorianCalendar();
  273. if (years > 0) {
  274. c.add(1, years * (future ? 1 : -1));
  275. }
  276. if (months > 0) {
  277. c.add(2, months * (future ? 1 : -1));
  278. }
  279. if (weeks > 0) {
  280. c.add(3, weeks * (future ? 1 : -1));
  281. }
  282. if (days > 0) {
  283. c.add(5, days * (future ? 1 : -1));
  284. }
  285. if (hours > 0) {
  286. c.add(11, hours * (future ? 1 : -1));
  287. }
  288. if (minutes > 0) {
  289. c.add(12, minutes * (future ? 1 : -1));
  290. }
  291. if (seconds > 0) {
  292. c.add(13, seconds * (future ? 1 : -1));
  293. }
  294. return c.getTimeInMillis();
  295. }
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement