Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.configuration.file.FileConfiguration;
- import org.bukkit.entity.Player;
- public class MySQL
- {
- public static String host = FFA.cfg.getString("MySQL.Host");
- public static String port = FFA.cfg.getString("MySQL.Port");
- public static String database = FFA.cfg.getString("MySQL.Datenbank");
- public static String username = FFA.cfg.getString("MySQL.Benutzername");
- public static String password = FFA.cfg.getString("MySQL.Passwort");
- public static String host2 = FFA.cfg.getString("Inventory.MySQL.Host");
- public static String port2 = FFA.cfg.getString("Inventory.MySQL.Port");
- public static String database2 = FFA.cfg.getString("Inventory.MySQL.Datenbank");
- public static String username2 = FFA.cfg.getString("Inventory.MySQL.Benutzername");
- public static String password2 = FFA.cfg.getString("Inventory.MySQL.Passwort");
- public static Connection connection;
- public static Connection connection2;
- public static Connection getConnection()
- {
- return connection;
- }
- public static Connection getConnection2()
- {
- return connection2;
- }
- public static boolean isConnected()
- {
- return connection != null;
- }
- public static boolean isConnected2()
- {
- return connection2 != null;
- }
- public static void connect()
- {
- if (!isConnected()) {
- try
- {
- connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
- }
- catch (SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- }
- public static void connect2()
- {
- if (!isConnected2()) {
- try
- {
- connection2 = DriverManager.getConnection("jdbc:mysql://" + host2 + ":" + port2 + "/" + database2, username2, password2);
- }
- catch (SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- }
- public static void disconnect()
- {
- if (isConnected()) {
- try
- {
- connection.close();
- }
- catch (SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- }
- public static void disconnect2()
- {
- if (isConnected2()) {
- try
- {
- connection2.close();
- }
- catch (SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- }
- public static void createTableIfNotExists()
- {
- if (isConnected()) {
- try
- {
- PreparedStatement ps = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS FFA (Spielername VARCHAR(100), UUID VARCHAR(100), Kills INT(100), Tode INT(100))");
- ps.executeUpdate();
- ps.close();
- }
- catch (SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- }
- public static void createTableIfNotExists2()
- {
- if (isConnected2()) {
- try
- {
- PreparedStatement ps = getConnection2().prepareStatement("CREATE TABLE IF NOT EXISTS FFA (Spielername VARCHAR(100), UUID VARCHAR(100), Inventory VARCHAR(10000))");
- ps.executeUpdate();
- ps.close();
- }
- catch (SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- }
- public static boolean isUserExisting(Player p)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA WHERE UUID = ?");
- ps.setString(1, p.getUniqueId().toString());
- ResultSet result = ps.executeQuery();
- boolean user = result.next();
- result.close();
- ps.close();
- return user;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return false;
- }
- public static boolean isUserExisting(String player)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA WHERE Spielername = ?");
- ps.setString(1, player);
- ResultSet result = ps.executeQuery();
- boolean user = result.next();
- result.close();
- ps.close();
- return user;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return false;
- }
- public static boolean isUserInvExisting(Player p)
- {
- try
- {
- PreparedStatement ps = connection2.prepareStatement("SELECT * FROM FFA WHERE UUID = ?");
- ps.setString(1, p.getUniqueId().toString());
- ResultSet result = ps.executeQuery();
- boolean user = result.next();
- result.close();
- ps.close();
- return user;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return false;
- }
- public static void registerPlayer(Player p)
- {
- if (isUserExisting(p)) {
- return;
- }
- try
- {
- PreparedStatement ps = connection.prepareStatement("INSERT INTO FFA (Spielername, UUID, Kills, Tode, Punkte) VALUES (?, ?, ?, ?, ?)");
- ps.setString(1, p.getName());
- ps.setString(2, p.getUniqueId().toString());
- ps.setInt(3, 0);
- ps.setInt(4, 0);
- ps.setInt(5, 1000);
- ps.execute();
- ps.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- public static void registerInv(Player p)
- {
- if (isUserInvExisting(p)) {
- return;
- }
- try
- {
- PreparedStatement ps = connection2.prepareStatement("INSERT INTO FFA (Spielername, UUID, Inventory) VALUES (?, ?, ?)");
- ps.setString(1, p.getName());
- ps.setString(2, p.getUniqueId().toString());
- ps.setString(3, "null");
- ps.execute();
- ps.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- public static void setInv(Player p, String inv)
- {
- try
- {
- PreparedStatement ps = connection2.prepareStatement("UPDATE FFA SET Inventory = ? WHERE UUID = ?");
- ps.setString(1, inv);
- ps.setString(2, p.getUniqueId().toString());
- ps.executeUpdate();
- ps.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- public static String getInv(Player p)
- {
- try
- {
- PreparedStatement ps = connection2.prepareStatement("SELECT * FROM FFA WHERE UUID = ?");
- ps.setString(1, p.getUniqueId().toString());
- ResultSet result = ps.executeQuery();
- result.next();
- String fire = result.getString("Inventory");
- result.close();
- ps.close();
- return fire;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return "null";
- }
- public static void setPlayerName(Player p)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("UPDATE FFA SET Spielername = ? WHERE UUID = ?");
- ps.setString(1, p.getName());
- ps.setString(2, p.getUniqueId().toString());
- ps.executeUpdate();
- ps.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- public static String getPlayer(String player)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA WHERE Spielername = ?");
- ps.setString(1, player);
- ResultSet result = ps.executeQuery();
- result.next();
- String name = result.getString("Spielername");
- result.close();
- ps.close();
- return name;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return "null";
- }
- public static int getKills(Player p)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA WHERE UUID = ?");
- ps.setString(1, p.getUniqueId().toString());
- ResultSet result = ps.executeQuery();
- result.next();
- int kills = result.getInt("Kills");
- result.close();
- ps.close();
- return kills;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return -1;
- }
- public static int getKills(String player)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA WHERE Spielername = ?");
- ps.setString(1, player);
- ResultSet result = ps.executeQuery();
- result.next();
- int kills = result.getInt("Kills");
- result.close();
- ps.close();
- return kills;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return -1;
- }
- public static int getDeaths(Player p)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA WHERE UUID = ?");
- ps.setString(1, p.getUniqueId().toString());
- ResultSet result = ps.executeQuery();
- result.next();
- int deaths = result.getInt("Tode");
- result.close();
- ps.close();
- return deaths;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return -1;
- }
- public static int getDeaths(String player)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA WHERE Spielername = ?");
- ps.setString(1, player);
- ResultSet result = ps.executeQuery();
- result.next();
- int deaths = result.getInt("Tode");
- result.close();
- ps.close();
- return deaths;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return -1;
- }
- public static double getKD(Player p)
- {
- try
- {
- PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM FFA WHERE UUID = ?");
- ps1.setString(1, p.getUniqueId().toString());
- ResultSet result1 = ps1.executeQuery();
- result1.next();
- double kills = result1.getInt("Kills");
- result1.close();
- ps1.close();
- PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM FFA WHERE UUID = ?");
- ps2.setString(1, p.getUniqueId().toString());
- ResultSet result2 = ps2.executeQuery();
- result2.next();
- double deaths = result2.getInt("Tode");
- result2.close();
- ps2.close();
- if ((kills == 0.0D) && (deaths == 0.0D)) {
- return 0.0D;
- }
- if (deaths == 0.0D) {
- return kills;
- }
- return Math.round(kills / deaths * 100.0D) / 100.0D;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return -1.0D;
- }
- public static double getKD(String player)
- {
- try
- {
- PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM FFA WHERE Spielername = ?");
- ps1.setString(1, player);
- ResultSet result1 = ps1.executeQuery();
- result1.next();
- double kills = result1.getInt("Kills");
- result1.close();
- ps1.close();
- PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM FFA WHERE Spielername = ?");
- ps2.setString(1, player);
- ResultSet result2 = ps2.executeQuery();
- result2.next();
- double deaths = result2.getInt("Tode");
- result2.close();
- ps2.close();
- if ((kills == 0.0D) && (deaths == 0.0D)) {
- return 0.0D;
- }
- if (deaths == 0.0D) {
- return kills;
- }
- return Math.round(kills / deaths * 100.0D) / 100.0D;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return -1.0D;
- }
- public static int getRank(Player p)
- {
- int rank = -1;
- try
- {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA ORDER BY Kills DESC");
- ResultSet result = ps.executeQuery();
- while (result.next())
- {
- String uuid2 = result.getString("UUID");
- if (uuid2.equalsIgnoreCase(p.getUniqueId().toString()))
- {
- rank = result.getRow();
- break;
- }
- }
- result.close();
- ps.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return rank;
- }
- public static int getRank(String player)
- {
- int rank = -1;
- try
- {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM FFA ORDER BY Kills DESC");
- ResultSet result = ps.executeQuery();
- while (result.next())
- {
- String uuid2 = result.getString("Spielername");
- if (uuid2.equalsIgnoreCase(player))
- {
- rank = result.getRow();
- break;
- }
- }
- result.close();
- ps.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return rank;
- }
- public static void addKill(Player p)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("UPDATE FFA SET Kills = ? WHERE uuid = ?");
- ps.setInt(1, getKills(p) + 1);
- ps.setString(2, p.getUniqueId().toString());
- ps.executeUpdate();
- ps.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- public static void addDeath(Player p)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("UPDATE FFA SET Tode = ? WHERE uuid = ?");
- ps.setInt(1, getDeaths(p) + 1);
- ps.setString(2, p.getUniqueId().toString());
- ps.executeUpdate();
- ps.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- public static int getPoints(UUID p)
- {
- try
- {
- PreparedStatement st = connection.prepareStatement("SELECT Punkte FROM FFA WHERE uuid = ?");
- st.setString(1, p.toString());
- ResultSet rs = st.executeQuery();
- rs.next();
- int points = rs.getInt("Punkte");
- rs.close();
- st.close();
- return points;
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return -1;
- }
- public static void setPoints(Player p, int points)
- {
- try
- {
- PreparedStatement st = connection.prepareStatement("UPDATE FFA SET Punkte = ? WHERE uuid = ?");
- st.setInt(1, points);
- st.setString(2, p.getUniqueId().toString());
- st.executeUpdate();
- st.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement