Advertisement
Guest User

Untitled

a guest
Feb 26th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1.  
  2. package de.Jeyprox.mysql;
  3.  
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.util.UUID;
  12.  
  13. import org.bukkit.Bukkit;
  14. import org.bukkit.configuration.file.YamlConfiguration;
  15.  
  16. public class Coins {
  17.  
  18. public static Connection con;
  19.  
  20. // Global Stats
  21.  
  22. private static void createmysqlcon() {
  23.  
  24. File file = new File("plugins/Coins/mysql.yml");
  25. YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  26.  
  27. if (!file.exists()) {
  28.  
  29. try {
  30. file.getParentFile().mkdirs();
  31. file.getParentFile().createNewFile();
  32. file.createNewFile();
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36.  
  37. cfg.options().header("Config fuer MySQL");
  38.  
  39. cfg.set("Coins.Host", "localhost");
  40. cfg.set("Coins.Port", "3306");
  41. cfg.set("Coins.DB", "Coins");
  42. cfg.set("Coins.User", "root");
  43. cfg.set("Coins.Password", "password");
  44.  
  45. try {
  46. cfg.save(file);
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. }
  50.  
  51. }
  52.  
  53. CHost = cfg.getString("Coins.Host");
  54. CPort = cfg.getString("Coins.Port");
  55. CDB = cfg.getString("Coins.DB");
  56. CUser = cfg.getString("Coins.User");
  57. CPassword = cfg.getString("Coins.Password");
  58. }
  59.  
  60. static String CHost;
  61. static String CPort;
  62. static String CDB;
  63. static String CUser;
  64. static String CPassword;
  65.  
  66. public static void connect() {
  67. createmysqlcon();
  68. if (!isconnected()) {
  69. try {
  70.  
  71. con = DriverManager.getConnection("jdbc:mysql://" + CHost + ":" + CPort + "/" + CDB + "?autoReconnect=true",
  72. CUser, CPassword);
  73.  
  74. } catch (SQLException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. createTable();
  79. }
  80.  
  81. public static void disconnect() {
  82. if (isconnected()) {
  83. try {
  84. con.close();
  85. } catch (SQLException e) {
  86. e.printStackTrace();
  87. }
  88.  
  89. }
  90.  
  91. }
  92.  
  93. public static void update(String qry) {
  94. try {
  95. PreparedStatement ps = con.prepareStatement(qry);
  96. ps.executeUpdate();
  97. } catch (SQLException e) {
  98. e.printStackTrace();
  99. }
  100. }
  101.  
  102. public static ResultSet getResult(String qry) {
  103. try {
  104. PreparedStatement ps = con.prepareStatement(qry);
  105. return ps.executeQuery();
  106. } catch (SQLException e) {
  107. e.printStackTrace();
  108. }
  109.  
  110. return null;
  111.  
  112. }
  113.  
  114. private static void createTable() {
  115. if (isconnected()) {
  116. try {
  117. con.createStatement().executeUpdate(
  118. "CREATE TABLE IF NOT EXISTS CoinsT (Spielername VARCHAR(100), UUID VARCHAR(100), Coins INT(255))");
  119. } catch (SQLException e) {
  120. e.printStackTrace();
  121. }
  122. }
  123. }
  124.  
  125. // Coins
  126.  
  127. public static int getCoins(String uuid) {
  128. ResultSet rs = Coins.getResult("SELECT * FROM CoinsT WHERE UUID='" + uuid + "'");
  129. try {
  130. while (rs.next()) {
  131. return rs.getInt("Coins");
  132. }
  133. } catch (SQLException e) {
  134. e.printStackTrace();
  135. return 0;
  136. }
  137. return 0;
  138. }
  139.  
  140. public static void addCoins(String uuid, int Coinsi) {
  141.  
  142. int oldCoins = getCoins(uuid);
  143. int newCoins = oldCoins + Coinsi;
  144.  
  145. Coins.update("UPDATE CoinsT SET Coins='" + newCoins + "' WHERE UUID='" + uuid + "'");
  146.  
  147. Bukkit.getPluginManager().callEvent(new CoinsEvent(Bukkit.getPlayer(UUID.fromString(uuid))));
  148. }
  149.  
  150. public static void removeCoins(String uuid, int Coinsi) {
  151.  
  152. if (!(getCoins(uuid) < 5)) {
  153. int oldcoins = getCoins(uuid);
  154. int newCoins = oldcoins - Coinsi;
  155.  
  156. Coins.update("UPDATE CoinsT SET Coins='" + newCoins + "' WHERE UUID='" + uuid + "'");
  157.  
  158. Bukkit.getPluginManager().callEvent(new CoinsEvent(Bukkit.getPlayer(UUID.fromString(uuid))));
  159. }
  160. }
  161.  
  162. //
  163.  
  164. public static boolean isconnected() {
  165. if (con == null) {
  166. return false;
  167. } else {
  168. return true;
  169. }
  170.  
  171. }
  172.  
  173. public static boolean playerExists(String uuid) {
  174. try {
  175. ResultSet rs = Coins.getResult("SELECT * FROM CoinsT WHERE UUID='" + uuid + "'");
  176. if (rs.next()) {
  177. return rs.getString("UUID") != null;
  178. }
  179. return false;
  180. } catch (SQLException e) {
  181. e.printStackTrace();
  182. }
  183. return false;
  184. }
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement