Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.playfactions;
- import java.io.File;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Calendar;
- import java.util.GregorianCalendar;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.concurrent.TimeUnit;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import org.bukkit.scheduler.BukkitRunnable;
- public class Mysql {
- private DatabaseType type;
- private String user;
- private String password;
- private String database;
- private String host;
- private Connection connection;
- public Mysql(DatabaseType type) {
- this.type = type;
- startDatabase();
- }
- public Mysql(DatabaseType type, String user, String password, String database, String host) {
- this.type = type;
- this.user = user;
- this.password = password;
- this.database = database;
- this.host = host;
- startDatabase();
- }
- private void selectDriver() {
- try {
- if (this.type == DatabaseType.MYSQL) {
- Class.forName("com.mysql.jdbc.Driver");
- } else if (this.type == DatabaseType.SQLITE) {
- Class.forName("org.sqlite.JDBC");
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- private void setConnection() {
- try {
- if (this.type == DatabaseType.MYSQL) {
- Class.forName("com.mysql.jdbc.Driver");
- this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + "/" + this.database,
- this.user, this.password);
- } else if (this.type == DatabaseType.SQLITE) {
- Class.forName("org.sqlite.JDBC");
- this.connection = DriverManager.getConnection("jdbc:sqlite:"
- + Main.instance.getDataFolder().getAbsolutePath() + File.separator + "database.db");
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e2) {
- e2.printStackTrace();
- }
- }
- private void startDatabase() {
- try {
- setConnection();
- Statement stmt = this.connection.createStatement();
- if (this.type == DatabaseType.SQLITE) {
- stmt.execute("CREATE TABLE IF NOT EXISTS " + Main.instance.getConfig().getString("mysql.tabela")
- + " (id INTEGER PRIMARY KEY AUTOINCREMENT, nick varchar(255), kit varchar(255), delay varchar(512),PRIMARY KEY (nick))");
- } else {
- stmt.execute("CREATE TABLE IF NOT EXISTS " + Main.instance.getConfig().getString("mysql.tabela")
- + " (id INTEGER NOT NULL AUTO_INCREMENT,nick varchar(255), kit varchar(255), delay varchar(512), PRIMARY KEY (id))");
- }
- stmt.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static enum DatabaseType {
- MYSQL("MYSQL", 0), SQLITE("SQLITE", 1);
- private DatabaseType(String s, int n) {
- }
- }
- private Map<String, Long> map = new HashMap<>();
- public void addDelay(String id, String kit) {
- this.map.put(id + ":" + kit, Long.valueOf(System.currentTimeMillis()));
- new BukkitRunnable() {
- @Override
- public void run() {
- try {
- connection.createStatement()
- .executeUpdate("INSERT INTO `" + Main.instance.getConfig().getString("mysql.tabela")
- + "` (`nick`,`delay`,`kit`) VALUES ('" + id + "', '" + System.currentTimeMillis() + "', '"
- + kit + "');");
- } catch (SQLException localSQLException) {
- }
- }
- }.runTaskAsynchronously(Main.instance);
- }
- public long getDelay(String id, String kit) {
- if (this.map.containsKey(id + ":" + kit)) {
- return ((Long) this.map.get(id + ":" + kit)).longValue();
- }
- try {
- selectDriver();
- PreparedStatement stmt = this.connection.prepareStatement("SELECT * FROM `"
- + Main.instance.getConfig().getString("mysql.tabela") + "` WHERE nick=? and kit=?");
- stmt.setString(1, id);
- stmt.setString(2, kit);
- ResultSet rs = stmt.executeQuery();
- if (rs.next()) {
- this.map.put(id + ":" + kit, Long.valueOf(rs.getLong("delay")));
- return rs.getLong("delay");
- }
- return System.currentTimeMillis();
- } catch (SQLException ex) {
- }
- return 0L;
- }
- public boolean acabouDelay(String id, long l, String kit) {
- String coisa = getDelayString(id, l, kit);
- return coisa.equals("0");
- }
- public String getDelayString(String id, long l, String kit) {
- long tempoAntes = getDelay(id, kit);
- long tempoAtual = System.currentTimeMillis() - l;
- String coisa = formatDifferenceStr(tempoAntes - tempoAtual);
- return coisa;
- }
- public void removeDelay(String id, String id2) {
- new BukkitRunnable() {
- @Override
- public void run() {
- try {
- selectDriver();
- ResultSet rs = connection.createStatement().executeQuery("SELECT * FROM `"
- + Main.instance.getConfig().getString("mysql.tabela") + "` WHERE `nick`='" + id + "';");
- if (rs.next()) {
- PreparedStatement stmt = connection.prepareStatement("DELETE FROM `"
- + Main.instance.getConfig().getString("mysql.tabela") + "` WHERE nick= ? and kit= ?");
- stmt.setString(1, id);
- stmt.setString(2, id2);
- stmt.executeUpdate();
- }
- } catch (SQLException localSQLException) {
- }
- }
- }.runTaskAsynchronously(Main.instance);
- }
- public boolean temDelay(String id, String kit) {
- if (this.map.containsKey(id + ":" + kit)) {
- return true;
- }
- try {
- selectDriver();
- PreparedStatement stmt = this.connection.prepareStatement("SELECT * FROM `"
- + Main.instance.getConfig().getString("mysql.tabela") + "` WHERE nick= ? and kit= ?");
- stmt.setString(1, id);
- stmt.setString(2, kit);
- ResultSet rs = stmt.executeQuery();
- if (rs.next()) {
- long delay = rs.getLong("delay");
- this.map.put(id + ":" + kit, Long.valueOf(delay));
- return true;
- }
- } catch (SQLException localSQLException) {
- }
- return false;
- }
- public static String formatDifferenceStr(long time) {
- if (time == 0L) {
- return "0";
- }
- long day = TimeUnit.MILLISECONDS.toDays(time);
- long hours = TimeUnit.MILLISECONDS.toHours(time) - day * 24L;
- long minutes = TimeUnit.MILLISECONDS.toMinutes(time) - TimeUnit.MILLISECONDS.toHours(time) * 60L;
- long seconds = TimeUnit.MILLISECONDS.toSeconds(time) - TimeUnit.MILLISECONDS.toMinutes(time) * 60L;
- StringBuilder sb = new StringBuilder();
- if (day > 0L) {
- sb.append(day).append(" ").append(day == 1L ? "dia" : "dias").append(" ");
- }
- if (hours > 0L) {
- sb.append(hours).append(" ").append(hours == 1L ? "hora" : "horas").append(" ");
- }
- if (minutes > 0L) {
- sb.append(minutes).append(" ").append(minutes == 1L ? "minuto" : "minutos").append(" ");
- }
- if (seconds > 0L) {
- sb.append(seconds).append(" ").append(seconds == 1L ? "segundo" : "segundos");
- }
- String diff = sb.toString();
- return diff.isEmpty() ? "0" : diff;
- }
- public String getDifferenceFormatStr(long timestamp) {
- return formatDifferenceStr(timestamp - System.currentTimeMillis());
- }
- public long parseDateDiff(String time, boolean future) throws Exception {
- Pattern timePattern = Pattern.compile(
- "(?:([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]*)?)?",
- 2);
- Matcher m = timePattern.matcher(time);
- int years = 0;
- int months = 0;
- int weeks = 0;
- int days = 0;
- int hours = 0;
- int minutes = 0;
- int seconds = 0;
- boolean found = false;
- while (m.find()) {
- if ((m.group() != null) && (!m.group().isEmpty())) {
- for (int i = 0; i < m.groupCount(); i++) {
- if ((m.group(i) != null) && (!m.group(i).isEmpty())) {
- found = true;
- break;
- }
- }
- if (found) {
- if ((m.group(1) != null) && (!m.group(1).isEmpty())) {
- years = Integer.parseInt(m.group(1));
- }
- if ((m.group(2) != null) && (!m.group(2).isEmpty())) {
- months = Integer.parseInt(m.group(2));
- }
- if ((m.group(3) != null) && (!m.group(3).isEmpty())) {
- weeks = Integer.parseInt(m.group(3));
- }
- if ((m.group(4) != null) && (!m.group(4).isEmpty())) {
- days = Integer.parseInt(m.group(4));
- }
- if ((m.group(5) != null) && (!m.group(5).isEmpty())) {
- hours = Integer.parseInt(m.group(5));
- }
- if ((m.group(6) != null) && (!m.group(6).isEmpty())) {
- minutes = Integer.parseInt(m.group(6));
- }
- if ((m.group(7) == null) || (m.group(7).isEmpty())) {
- break;
- }
- seconds = Integer.parseInt(m.group(7));
- break;
- }
- }
- }
- if (!found) {
- throw new Exception("Illegal Date");
- }
- if (years > 20) {
- throw new Exception("Illegal Date");
- }
- Calendar c = new GregorianCalendar();
- if (years > 0) {
- c.add(1, years * (future ? 1 : -1));
- }
- if (months > 0) {
- c.add(2, months * (future ? 1 : -1));
- }
- if (weeks > 0) {
- c.add(3, weeks * (future ? 1 : -1));
- }
- if (days > 0) {
- c.add(5, days * (future ? 1 : -1));
- }
- if (hours > 0) {
- c.add(11, hours * (future ? 1 : -1));
- }
- if (minutes > 0) {
- c.add(12, minutes * (future ? 1 : -1));
- }
- if (seconds > 0) {
- c.add(13, seconds * (future ? 1 : -1));
- }
- return c.getTimeInMillis();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement