Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dev.siea.lobbysystem.db;
- import dev.siea.lobbysystem.models.FinancialData;
- import dev.siea.lobbysystem.models.PlayerSettings;
- import org.bukkit.ChatColor;
- import org.bukkit.plugin.Plugin;
- import java.sql.*;
- import static org.bukkit.Bukkit.getServer;
- public class database {
- private static String url;
- private static String user;
- private static String psw;
- private static String name;
- private static Connection connection;
- public static Connection getConnection() throws SQLException {
- if (connection != null){
- return connection;
- }
- establishConnection();
- return connection;
- }
- public FinancialData findFinancialDataByUUID(String uuid) throws SQLException{
- PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM FinancialData WHERE uuid = ?");
- statement.setString(1, uuid);
- ResultSet results = statement.executeQuery();
- if (results.next()){
- double coins = results.getDouble("coins");
- FinancialData financialData = new FinancialData(uuid, coins);
- statement.close();
- return financialData;
- }else{
- statement.close();
- return null;
- }
- }
- public PlayerSettings findPlayerSettingsByUUID(String uuid) throws SQLException{
- PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM PlayerSettings WHERE uuid = ?");
- statement.setString(1, uuid);
- ResultSet results = statement.executeQuery();
- if (results.next()){
- boolean chatMessages = results.getBoolean("chatMessages");
- boolean privateMessages = results.getBoolean("privateMessages");
- boolean hideLobbyPlayers = results.getBoolean("hideLobbyPlayers");
- boolean deathMessages = results.getBoolean("deathMessages");
- boolean payMessages = results.getBoolean("payMessages");
- boolean allowPay = results.getBoolean("allowPay");
- boolean autoTpToSpawn = results.getBoolean("autoTpToSpawn");
- PlayerSettings playerSettings = new PlayerSettings(uuid, chatMessages, privateMessages, hideLobbyPlayers, deathMessages, payMessages, allowPay, autoTpToSpawn);
- statement.close();
- return playerSettings;
- }else{
- statement.close();
- return null;
- }
- }
- public void createFinancialData(FinancialData data) throws SQLException{
- PreparedStatement statement = getConnection()
- .prepareStatement("INSERT INTO FinancialData (uuid,coins) VALUES (?, ?)");
- statement.setString(1, data.getUuid());
- statement.setDouble(2, data.getCoins());
- statement.executeUpdate();
- statement.close();
- }
- public void createPlayerSettings(PlayerSettings data) throws SQLException{
- PreparedStatement statement = getConnection()
- .prepareStatement("INSERT INTO PlayerSettings (uuid,chatMessages,privateMessages,hideLobbyPlayers,deathMessages,payMessages,allowPay,autoTpToSpawn) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
- statement.setString(1, data.getUuid());
- statement.setBoolean(2, data.isChatMessages());
- statement.setBoolean(3, data.isPrivateMessages());
- statement.setBoolean(4, data.isHideLobbyPlayers());
- statement.setBoolean(5, data.isDeathMessages());
- statement.setBoolean(6, data.isPayMessages());
- statement.setBoolean(7, data.isAllowPay());
- statement.setBoolean(8, data.isAutoTpToSpawn());
- statement.executeUpdate();
- statement.close();
- }
- public void updateFinancialData(FinancialData data) throws SQLException{
- PreparedStatement statement = getConnection()
- .prepareStatement("UPDATE FinancialData SET coins = ? WHERE uuid = ?");
- statement.setDouble(1, data.getCoins());
- statement.setString(2, data.getUuid());
- statement.executeUpdate();
- statement.close();
- }
- public void updatePlayerSettings(PlayerSettings data) throws SQLException{
- PreparedStatement statement = getConnection()
- .prepareStatement("UPDATE PlayerSettings SET chatMessages = ?, privateMessages = ?, hideLobbyPlayers = ?, deathMessages = ?, payMessages = ?, allowPay = ?, autoTpToSpawn = ? WHERE uuid = ?");
- statement.setBoolean(1, data.isChatMessages());
- statement.setBoolean(2, data.isPrivateMessages());
- statement.setBoolean(3, data.isHideLobbyPlayers());
- statement.setBoolean(4, data.isDeathMessages());
- statement.setBoolean(5, data.isPayMessages());
- statement.setBoolean(6, data.isAutoTpToSpawn());
- statement.setBoolean(7, data.isChatMessages());
- statement.setString(8, data.getUuid());
- statement.executeUpdate();
- statement.close();
- }
- public static void onEnable(Plugin p) throws SQLException{
- String ip = p.getConfig().getString("ip");
- name = p.getConfig().getString("name");
- url = "jdbc:mysql://" + ip + "/" + name;
- user = p.getConfig().getString("user");
- psw = p.getConfig().getString("password");
- createTables();
- }
- public static void onDisable() throws SQLException{
- destroyConnection();
- }
- private static void establishConnection ()throws SQLException{
- connection = DriverManager.getConnection(url, user, psw);
- getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[DB] Verbindung zur DataBase konnte erfolgreich hergestellt werden");
- }
- private static void createTables() throws SQLException{
- Connection connection = getConnection();
- // CREATE && LOAD MONEY-TABLE
- Statement statementMoneyTable = connection.createStatement();
- String sqlMoneyTable = "CREATE TABLE IF NOT EXISTS FinancialData(uuid varchar(36) primary key, coins double)";
- statementMoneyTable.execute(sqlMoneyTable);
- statementMoneyTable.close();
- getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[DB] FinancialData table wurde erfolgreich geladen");
- // CREATE && LOAD SETTINGS-TABLE
- Statement statementSettings = connection.createStatement();
- String sqlSettings = "CREATE TABLE IF NOT EXISTS PlayerSettings(uuid varchar(36) primary key, chatMessages boolean, privateMessages boolean, hideLobbyPlayers boolean, deathMessages boolean, payMessages boolean, allowPay boolean, autoTpToSpawn boolean)";
- statementSettings.execute(sqlSettings);
- statementSettings.close();
- getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[DB] PlayerSettings table wurde erfolgreich geladen");
- }
- private static void destroyConnection() throws SQLException{
- connection.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment