Advertisement
Guest User

Das ist der CoinHanderl

a guest
Dec 17th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. package de.fallenbreak.coinsapi.database;
  2.  
  3. import de.fallenbreak.coinsapi.CoinsAPI;
  4. import de.fallenbreak.coinsapi.database.container.AsyncMySQL;
  5. import org.bukkit.Bukkit;
  6.  
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.util.UUID;
  11. import java.util.logging.Level;
  12.  
  13. public class CoinsHandler {
  14.  
  15. private final AsyncMySQL sql = CoinsAPI.getInstance().getAsyncMySQL();
  16. private final String table;
  17.  
  18. public CoinsHandler() {
  19. this.table = "coins";
  20. }
  21.  
  22. public void setupDatabase() {
  23. long time = System.currentTimeMillis();
  24. sql.update("CREATE TABLE IF NOT EXISTS coins(uuid VARCHAR(36), coins INT);");
  25. Bukkit.getLogger().log(Level.INFO, "Table " + this.table + " successfully created in " + (System.currentTimeMillis() - time) + "ms.");
  26. }
  27.  
  28. public boolean isPlayerExist(final UUID uuid) {
  29. try (final PreparedStatement statement = sql.prepare("SELECT * FROM " + this.table + " WHERE uuid ='" + uuid.toString() + "';")) {
  30.  
  31. final ResultSet resultSet = statement.executeQuery();
  32. boolean response = false;
  33.  
  34. if(resultSet.next()) {
  35. response = resultSet.getString("uuid") != null;
  36. }
  37.  
  38. return response;
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. return Boolean.parseBoolean(null);
  43. }
  44.  
  45. public void registerPlayer(final UUID uuid) {
  46. if(!this.isPlayerExist(uuid)) {
  47. sql.update("INSERT INTO " + this.table + "(uuid, coins) VALUES ('" + uuid.toString() + "', '0');");
  48. }
  49. }
  50.  
  51. public void setCoins(final UUID uuid, final int coinAmount) {
  52. if(this.isPlayerExist(uuid)) {
  53. sql.update("UPDATE " + this.table + " SET coins ='" + coinAmount + "' WHERE uuid ='" + uuid.toString() + "';");
  54. }
  55. }
  56.  
  57. public void addCoins(final UUID uuid, final int coinAmount) {
  58. this.setCoins(uuid, this.getCoins(uuid) + coinAmount);
  59. }
  60.  
  61. public void removeCoins(final UUID uuid, final int coinAmount) {
  62. this.setCoins(uuid, this.getCoins(uuid) - coinAmount);
  63. }
  64.  
  65. public int getCoins(final UUID uuid) {
  66. int coins = 0;
  67. try(final PreparedStatement statement = sql.prepare("SELECT coins FROM " + this.table + " WHERE uuid ='" + uuid.toString() + "';")) {
  68. final ResultSet resultSet = statement.executeQuery();
  69. if(resultSet.next()) {
  70. coins = resultSet.getInt("coins");
  71. }
  72. } catch (final SQLException e) {
  73. e.printStackTrace();
  74. }
  75. return coins;
  76. }
  77.  
  78. public String getTable() {
  79. return table;
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement