Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.kaviozz.prac.utils;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.UUID;
- import org.bukkit.Bukkit;
- import org.bukkit.entity.Player;
- import fr.kaviozz.prac.match.Queue;
- public class DataBase
- {
- private String url_base;
- private String host;
- private String name;
- private String username;
- private String password;
- private String table;
- private Connection connection;
- private int DEFAULT_ELO = 1000;
- public DataBase(String url_base, String host, String name, String username, String password, String table)
- {
- this.url_base = url_base;
- this.host = host;
- this.name = name;
- this.username = username;
- this.password = password;
- this.table = table;
- }
- public void connect()
- {
- if (!isConnected()) {
- try
- {
- this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + "/" + this.name, this.username, this.password);
- createTable();
- }
- catch (SQLException e)
- {
- System.out.println("Error: Connect to database SQL...");
- e.printStackTrace();
- }
- }
- }
- public void createTable()
- {
- try
- {
- this.connection.prepareStatement("CREATE TABLE `players` (`uuid` VARCHAR(36) NOT NULL, `pseudo` VARCHAR(16) NOT NULL, `nodebuff` INT NOT NULL DEFAULT '1000', `debuff` INT NOT NULL DEFAULT '1000', `gapple` INT NOT NULL DEFAULT '1000', `archer` INT NOT NULL DEFAULT '1000', `axe` INT NOT NULL DEFAULT '1000', `classic` INT NOT NULL DEFAULT '1000', `soup` INT NOT NULL DEFAULT '1000', `globalelo` INT NOT NULL DEFAULT '1000', `wins` INT NOT NULL DEFAULT '0', `losses` INT NOT NULL DEFAULT '0', `coins` INT NOT NULL DEFAULT '0', `enablescoreboard` BOOL NOT NULL DEFAULT '1', `enableduel` BOOL NOT NULL DEFAULT '1', `country` VARCHAR(100) NOT NULL DEFAULT 'None', PRIMARY KEY (`uuid`), UNIQUE (`uuid`))").executeUpdate();
- }
- catch (SQLException e)
- {
- if (!e.getMessage().contains("already exists"))
- {
- System.out.println("Failed createTables");
- e.printStackTrace();
- }
- }
- }
- public void close()
- {
- if (isConnected()) {
- try
- {
- this.connection.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- }
- public boolean isConnected()
- {
- try
- {
- if ((this.connection == null) || (this.connection.isClosed()) || (!this.connection.isValid(5))) {
- return false;
- }
- return true;
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return false;
- }
- public Connection getConnection()
- {
- return this.connection;
- }
- public void createAccount(UUID uuid)
- {
- if (isConnected()) {
- try
- {
- PreparedStatement sts = getConnection().prepareStatement("SELECT pseudo FROM " + this.table + " WHERE uuid = ?");
- sts.setString(1, uuid.toString());
- ResultSet rs = sts.executeQuery();
- if (!rs.next())
- {
- sts.close();
- PreparedStatement sts2 = getConnection().prepareStatement("INSERT INTO " + this.table + " (uuid, pseudo, nodebuff, debuff, gapple, archer, axe, classic, soup, globalelo, wins, losses, coins, enablescoreboard, enableduel, country) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
- sts2.setString(1, uuid.toString());
- sts2.setString(2, Bukkit.getPlayer(uuid).getName());
- sts2.setInt(3, this.DEFAULT_ELO);
- sts2.setInt(4, this.DEFAULT_ELO);
- sts2.setInt(5, this.DEFAULT_ELO);
- sts2.setInt(6, this.DEFAULT_ELO);
- sts2.setInt(7, this.DEFAULT_ELO);
- sts2.setInt(8, this.DEFAULT_ELO);
- sts2.setInt(9, this.DEFAULT_ELO);
- sts2.setInt(10, this.DEFAULT_ELO);
- sts2.setInt(11, Integer.valueOf(0).intValue());
- sts2.setInt(12, Integer.valueOf(0).intValue());
- sts2.setInt(13, Integer.valueOf(0).intValue());
- sts2.setInt(14, Integer.valueOf(1).intValue());
- sts2.setInt(15, Integer.valueOf(1).intValue());
- sts2.setString(16, SkIP.getIPData(SkIP.getPlayerIP(Bukkit.getPlayer(uuid))).getCountryName());
- sts2.executeUpdate();
- sts2.close();
- }
- updatePlayerName(Bukkit.getPlayer(uuid));
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- } else {
- Bukkit.getPlayer(uuid).sendMessage("§cThe database is not connected! Please contact Kavioz_z!");
- }
- }
- public void updatePlayerName(Player player)
- {
- try
- {
- PreparedStatement sts = getConnection().prepareStatement("SELECT pseudo FROM " + this.table + " WHERE uuid = ?");
- sts.setString(1, player.getUniqueId().toString());
- ResultSet rs = sts.executeQuery();
- if (!rs.next())
- {
- sts.close();
- return;
- }
- if (!rs.getString("pseudo").equals(player.getName()))
- {
- sts.close();
- PreparedStatement sts2 = getConnection().prepareStatement("UPDATE " + this.table + " SET pseudo = ? WHERE uuid = ?");
- sts2.setString(1, player.getName());
- sts2.setString(2, player.getUniqueId().toString());
- sts2.executeUpdate();
- sts2.close();
- }
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- public Integer getPlayerElo(UUID uuid, String mode)
- {
- Integer elo = Integer.valueOf(0);
- try
- {
- PreparedStatement sts = getConnection().prepareStatement("SELECT " + mode.toLowerCase() + " FROM " + this.table + " WHERE uuid = ?");
- sts.setString(1, uuid.toString());
- ResultSet rs = sts.executeQuery();
- if (!rs.next())
- {
- sts.close();
- return Integer.valueOf(this.DEFAULT_ELO);
- }
- elo = Integer.valueOf(rs.getInt(mode));
- sts.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return elo;
- }
- public void addPlayerElo(UUID uuid, String mode, int elo)
- {
- try
- {
- PreparedStatement sts = getConnection().prepareStatement("UPDATE " + this.table + " SET " + mode.toLowerCase() + " = " + mode.toLowerCase() + " + ? WHERE uuid = ?");
- sts.setInt(1, elo);
- sts.setString(2, uuid.toString());
- sts.executeUpdate();
- sts.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- public void removePlayerElo(UUID uuid, String mode, int elo)
- {
- try
- {
- PreparedStatement sts = getConnection().prepareStatement("UPDATE " + this.table + " SET " + mode.toLowerCase() + " = " + mode.toLowerCase() + " - ? WHERE uuid = ?");
- sts.setInt(1, elo);
- sts.setString(2, uuid.toString());
- sts.executeUpdate();
- sts.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- public int getPlayerGlobalElo(UUID uuid)
- {
- Integer elo = Integer.valueOf(0);
- try
- {
- PreparedStatement sts = getConnection().prepareStatement("SELECT globalelo FROM " + this.table + " WHERE uuid = ?");
- sts.setString(1, uuid.toString());
- ResultSet rs = sts.executeQuery();
- if (!rs.next())
- {
- sts.close();
- return Integer.valueOf(this.DEFAULT_ELO).intValue();
- }
- setPlayerGlobalElo(uuid);
- elo = Integer.valueOf(rs.getInt("globalelo"));
- sts.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return elo.intValue();
- }
- public void setPlayerGlobalElo(UUID uuid)
- {
- int global = getPlayerElo(uuid, "NoDebuff").intValue() + getPlayerElo(uuid, "Debuff").intValue() + getPlayerElo(uuid, "Gapple").intValue() + getPlayerElo(uuid, "Archer").intValue() + getPlayerElo(uuid, "Axe").intValue() + getPlayerElo(uuid, "Classic").intValue() + getPlayerElo(uuid, "Soup").intValue();
- global /= Queue.getInstance().kitlistR.length;
- try
- {
- PreparedStatement sts = getConnection().prepareStatement("UPDATE " + this.table + " SET globalelo = ? WHERE uuid = ?");
- sts.setInt(1, global);
- sts.setString(2, uuid.toString());
- sts.executeUpdate();
- sts.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- public Integer getPlayerWins(UUID uuid)
- {
- Integer wins = Integer.valueOf(0);
- try
- {
- PreparedStatement sts = getConnection().prepareStatement("SELECT wins FROM " + this.table + " WHERE uuid = ?");
- sts.setString(1, uuid.toString());
- ResultSet rs = sts.executeQuery();
- if (!rs.next())
- {
- sts.close();
- return Integer.valueOf(0);
- }
- wins = Integer.valueOf(rs.getInt("wins"));
- sts.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return wins;
- }
- public Integer getPlayerLosses(UUID uuid)
- {
- Integer losses = Integer.valueOf(0);
- try
- {
- PreparedStatement sts = getConnection().prepareStatement("SELECT losses FROM " + this.table + " WHERE uuid = ?");
- sts.setString(1, uuid.toString());
- ResultSet rs = sts.executeQuery();
- if (!rs.next())
- {
- sts.close();
- return Integer.valueOf(0);
- }
- losses = Integer.valueOf(rs.getInt("losses"));
- sts.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return losses;
- }
- public void addPlayerWins(UUID uuid)
- {
- try
- {
- PreparedStatement sts = getConnection().prepareStatement("UPDATE " + this.table + " SET wins = wins + ? WHERE uuid = ?");
- sts.setInt(1, Integer.valueOf(1).intValue());
- sts.setString(2, uuid.toString());
- sts.executeUpdate();
- sts.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- public void addPlayerLosses(UUID uuid)
- {
- try
- {
- PreparedStatement sts = getConnection().prepareStatement("UPDATE " + this.table + " SET losses = losses + ? WHERE uuid = ?");
- sts.setInt(1, Integer.valueOf(1).intValue());
- sts.setString(2, uuid.toString());
- sts.executeUpdate();
- sts.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- public Integer getPlayerCoins(UUID uuid)
- {
- Integer coins = Integer.valueOf(0);
- try
- {
- PreparedStatement sts = getConnection().prepareStatement("SELECT coins FROM " + this.table + " WHERE uuid = ?");
- sts.setString(1, uuid.toString());
- ResultSet rs = sts.executeQuery();
- if (!rs.next())
- {
- sts.close();
- return Integer.valueOf(0);
- }
- coins = Integer.valueOf(rs.getInt("coins"));
- sts.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return coins;
- }
- public void addPlayerCoins(UUID uuid, Integer coins)
- {
- try
- {
- PreparedStatement sts = getConnection().prepareStatement("UPDATE " + this.table + " SET coins = coins + ? WHERE uuid = ?");
- sts.setInt(1, Integer.valueOf(coins.intValue()).intValue());
- sts.setString(2, uuid.toString());
- sts.executeUpdate();
- sts.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- public void removePlayerCoins(UUID uuid, Integer coins)
- {
- try
- {
- PreparedStatement sts = getConnection().prepareStatement("UPDATE " + this.table + " SET coins = coins - ? WHERE uuid = ?");
- sts.setInt(1, Integer.valueOf(coins.intValue()).intValue());
- sts.setString(2, uuid.toString());
- sts.executeUpdate();
- sts.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- public boolean isEnable(UUID uuid, String code)
- {
- Boolean b = Boolean.valueOf(true);
- try
- {
- PreparedStatement sts = getConnection().prepareStatement("SELECT enable" + code.toLowerCase() + " FROM " + this.table + " WHERE uuid = ?");
- sts.setString(1, uuid.toString());
- ResultSet rs = sts.executeQuery();
- if (!rs.next())
- {
- sts.close();
- return Boolean.valueOf(true).booleanValue();
- }
- b = Boolean.valueOf(rs.getBoolean("enable" + code));
- sts.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return b.booleanValue();
- }
- public void setEnable(UUID uuid, String code, boolean b)
- {
- try
- {
- PreparedStatement sts = getConnection().prepareStatement("UPDATE " + this.table + " SET enable" + code.toLowerCase() + " = ? WHERE uuid = ?");
- sts.setInt(1, Integer.valueOf(b ? 1 : 0).intValue());
- sts.setString(2, uuid.toString());
- sts.executeUpdate();
- sts.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement