Guest User

Untitled

a guest
Oct 2nd, 2023
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 KB | None | 0 0
  1. package dev.siea.lobbysystem.db;
  2.  
  3. import dev.siea.lobbysystem.models.FinancialData;
  4. import dev.siea.lobbysystem.models.PlayerSettings;
  5. import org.bukkit.ChatColor;
  6. import org.bukkit.plugin.Plugin;
  7.  
  8. import java.sql.*;
  9.  
  10. import static org.bukkit.Bukkit.getServer;
  11.  
  12.  
  13. public class database {
  14.  
  15. private static String url;
  16. private static String user;
  17. private static String psw;
  18. private static String name;
  19. private static Connection connection;
  20.  
  21. public static Connection getConnection() throws SQLException {
  22. if (connection != null){
  23. return connection;
  24. }
  25.  
  26. establishConnection();
  27. return connection;
  28. }
  29.  
  30. public FinancialData findFinancialDataByUUID(String uuid) throws SQLException{
  31. PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM FinancialData WHERE uuid = ?");
  32. statement.setString(1, uuid);
  33.  
  34. ResultSet results = statement.executeQuery();
  35.  
  36. if (results.next()){
  37. double coins = results.getDouble("coins");
  38. FinancialData financialData = new FinancialData(uuid, coins);
  39. statement.close();
  40. return financialData;
  41. }else{
  42. statement.close();
  43. return null;
  44. }
  45.  
  46. }
  47.  
  48. public PlayerSettings findPlayerSettingsByUUID(String uuid) throws SQLException{
  49. PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM PlayerSettings WHERE uuid = ?");
  50. statement.setString(1, uuid);
  51.  
  52. ResultSet results = statement.executeQuery();
  53.  
  54. if (results.next()){
  55. boolean chatMessages = results.getBoolean("chatMessages");
  56. boolean privateMessages = results.getBoolean("privateMessages");
  57. boolean hideLobbyPlayers = results.getBoolean("hideLobbyPlayers");
  58. boolean deathMessages = results.getBoolean("deathMessages");
  59. boolean payMessages = results.getBoolean("payMessages");
  60. boolean allowPay = results.getBoolean("allowPay");
  61. boolean autoTpToSpawn = results.getBoolean("autoTpToSpawn");
  62. PlayerSettings playerSettings = new PlayerSettings(uuid, chatMessages, privateMessages, hideLobbyPlayers, deathMessages, payMessages, allowPay, autoTpToSpawn);
  63. statement.close();
  64. return playerSettings;
  65. }else{
  66. statement.close();
  67. return null;
  68. }
  69.  
  70. }
  71.  
  72. public void createFinancialData(FinancialData data) throws SQLException{
  73. PreparedStatement statement = getConnection()
  74. .prepareStatement("INSERT INTO FinancialData (uuid,coins) VALUES (?, ?)");
  75. statement.setString(1, data.getUuid());
  76. statement.setDouble(2, data.getCoins());
  77. statement.executeUpdate();
  78. statement.close();
  79. }
  80.  
  81. public void createPlayerSettings(PlayerSettings data) throws SQLException{
  82. PreparedStatement statement = getConnection()
  83. .prepareStatement("INSERT INTO PlayerSettings (uuid,chatMessages,privateMessages,hideLobbyPlayers,deathMessages,payMessages,allowPay,autoTpToSpawn) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
  84. statement.setString(1, data.getUuid());
  85. statement.setBoolean(2, data.isChatMessages());
  86. statement.setBoolean(3, data.isPrivateMessages());
  87. statement.setBoolean(4, data.isHideLobbyPlayers());
  88. statement.setBoolean(5, data.isDeathMessages());
  89. statement.setBoolean(6, data.isPayMessages());
  90. statement.setBoolean(7, data.isAllowPay());
  91. statement.setBoolean(8, data.isAutoTpToSpawn());
  92. statement.executeUpdate();
  93. statement.close();
  94. }
  95.  
  96. public void updateFinancialData(FinancialData data) throws SQLException{
  97. PreparedStatement statement = getConnection()
  98. .prepareStatement("UPDATE FinancialData SET coins = ? WHERE uuid = ?");
  99. statement.setDouble(1, data.getCoins());
  100. statement.setString(2, data.getUuid());
  101. statement.executeUpdate();
  102. statement.close();
  103. }
  104.  
  105. public void updatePlayerSettings(PlayerSettings data) throws SQLException{
  106. PreparedStatement statement = getConnection()
  107. .prepareStatement("UPDATE PlayerSettings SET chatMessages = ?, privateMessages = ?, hideLobbyPlayers = ?, deathMessages = ?, payMessages = ?, allowPay = ?, autoTpToSpawn = ? WHERE uuid = ?");
  108. statement.setBoolean(1, data.isChatMessages());
  109. statement.setBoolean(2, data.isPrivateMessages());
  110. statement.setBoolean(3, data.isHideLobbyPlayers());
  111. statement.setBoolean(4, data.isDeathMessages());
  112. statement.setBoolean(5, data.isPayMessages());
  113. statement.setBoolean(6, data.isAutoTpToSpawn());
  114. statement.setBoolean(7, data.isChatMessages());
  115. statement.setString(8, data.getUuid());
  116. statement.executeUpdate();
  117. statement.close();
  118. }
  119.  
  120. public static void onEnable(Plugin p) throws SQLException{
  121. String ip = p.getConfig().getString("ip");
  122. name = p.getConfig().getString("name");
  123. url = "jdbc:mysql://" + ip + "/" + name;
  124. user = p.getConfig().getString("user");
  125. psw = p.getConfig().getString("password");
  126. createTables();
  127. }
  128.  
  129. public static void onDisable() throws SQLException{
  130. destroyConnection();
  131. }
  132.  
  133. private static void establishConnection ()throws SQLException{
  134. connection = DriverManager.getConnection(url, user, psw);
  135. getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[DB] Verbindung zur DataBase konnte erfolgreich hergestellt werden");
  136. }
  137.  
  138. private static void createTables() throws SQLException{
  139. Connection connection = getConnection();
  140.  
  141. // CREATE && LOAD MONEY-TABLE
  142. Statement statementMoneyTable = connection.createStatement();
  143. String sqlMoneyTable = "CREATE TABLE IF NOT EXISTS FinancialData(uuid varchar(36) primary key, coins double)";
  144. statementMoneyTable.execute(sqlMoneyTable);
  145. statementMoneyTable.close();
  146. getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[DB] FinancialData table wurde erfolgreich geladen");
  147.  
  148. // CREATE && LOAD SETTINGS-TABLE
  149. Statement statementSettings = connection.createStatement();
  150. 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)";
  151. statementSettings.execute(sqlSettings);
  152. statementSettings.close();
  153. getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[DB] PlayerSettings table wurde erfolgreich geladen");
  154. }
  155.  
  156. private static void destroyConnection() throws SQLException{
  157.  
  158. connection.close();
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment