Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2021
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. package net.maiky.tokens.engine;
  2.  
  3. import net.maiky.tokens.tokens;
  4. import org.bukkit.entity.Player;
  5.  
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.util.UUID;
  10.  
  11. public class SQLGetter {
  12.  
  13. public tokens plugin;
  14.  
  15. public SQLGetter(tokens plugin) {
  16. this.plugin = plugin;
  17. }
  18.  
  19. public void createTable() {
  20.  
  21. PreparedStatement ps;
  22. try {
  23. ps = plugin.MySQL.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS atokens (NAME VARCHAR(100),UUID VARCHAR(100),TOKENS INT(100),PRIMARY KEY (NAME))");
  24. ps.executeUpdate();
  25. } catch (SQLException e) {
  26. e.printStackTrace();
  27. }
  28.  
  29. }
  30.  
  31. public void createPlayer(Player player) {
  32. try {
  33. UUID uuid = player.getUniqueId();
  34. if(!exists(uuid)) {
  35. PreparedStatement ps = plugin.MySQL.getConnection().prepareStatement("INSERT IGNORE INTO atokens (NAME,UUID,TOKENS) VALUES(?,?,?)");
  36. ps.setString(1, player.getName());
  37. ps.setString(2, uuid.toString());
  38. ps.setString(3, "0");
  39. ps.executeUpdate();
  40.  
  41. return;
  42. }
  43. } catch (SQLException e) {
  44. //e.printStackTrace();
  45.  
  46. }
  47.  
  48. }
  49.  
  50. public boolean exists(UUID uuid) {
  51. try {
  52. PreparedStatement ps = plugin.MySQL.getConnection().prepareStatement("SELECT * FROM atokens WHERE UUID=?");
  53. ps.setString(1, uuid.toString());
  54. ResultSet results = ps.executeQuery();
  55. if(results.next()) {
  56. return true;
  57.  
  58. }
  59. return false;
  60. } catch (SQLException e) {
  61. //e.printStackTrace();
  62. }
  63. return false;
  64. }
  65.  
  66. public void addTokens(UUID uuid, int tokens) {
  67. try {
  68. PreparedStatement ps = plugin.MySQL.getConnection().prepareStatement("UPDATE atokens SET TOKENS=? WHERE UUID=?");
  69. ps.setInt(1, (getTokens(uuid) + tokens));
  70. ps.setString(2, uuid.toString());
  71. ps.executeUpdate();
  72. } catch (SQLException e) {
  73. e.printStackTrace();
  74.  
  75. }
  76.  
  77. }
  78. public void setTokens(UUID uuid, int tokens) {
  79. try {
  80. PreparedStatement ps = plugin.MySQL.getConnection().prepareStatement("UPDATE atokens SET TOKENS=? WHERE UUID=?");
  81. ps.setInt(1, (tokens));
  82. ps.setString(2, uuid.toString());
  83. ps.executeUpdate();
  84. } catch (SQLException e) {
  85. e.printStackTrace();
  86.  
  87. }
  88.  
  89. }
  90. public int getTokens(UUID uuid) {
  91. try {
  92. PreparedStatement ps = plugin.MySQL.getConnection().prepareStatement("SELECT TOKENS FROM atokens WHERE UUID=?");
  93. ps.setString(1, uuid.toString());
  94. ResultSet rs = ps.executeQuery();
  95. int tokens;
  96. if(rs.next()) {
  97. tokens = rs.getInt("TOKENS");
  98. return tokens;
  99. }
  100.  
  101. } catch (SQLException e) {
  102. e.printStackTrace();
  103.  
  104. }
  105. return 0;
  106.  
  107. }
  108. public int removeTokens(UUID uuid, int tokens) {
  109. try {
  110. PreparedStatement ps = plugin.MySQL.getConnection().prepareStatement("UPDATE atokens SET TOKENS=? WHERE UUID=?");
  111. ps.setInt(1, (getTokens(uuid) - tokens));
  112. ps.setString(2, uuid.toString());
  113. ps.executeUpdate();
  114.  
  115. } catch (SQLException e) {
  116. e.printStackTrace();
  117.  
  118. }
  119. return 0;
  120.  
  121. }
  122. public int clearTnokes(UUID uuid) {
  123. try {
  124. PreparedStatement ps = plugin.MySQL.getConnection().prepareStatement("DELETE FROM atokens WHERE UUID=?");
  125. ps.setString(1, uuid.toString());
  126. ps.executeUpdate();
  127.  
  128. } catch (SQLException e) {
  129. e.printStackTrace();
  130.  
  131. }
  132. return 0;
  133.  
  134. }
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement