Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.fallenbreak.coinsapi.database;
- import de.fallenbreak.coinsapi.CoinsAPI;
- import de.fallenbreak.coinsapi.database.container.AsyncMySQL;
- import org.bukkit.Bukkit;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.UUID;
- import java.util.logging.Level;
- public class CoinsHandler {
- private final AsyncMySQL sql = CoinsAPI.getInstance().getAsyncMySQL();
- private final String table;
- public CoinsHandler() {
- this.table = "coins";
- }
- public void setupDatabase() {
- long time = System.currentTimeMillis();
- sql.update("CREATE TABLE IF NOT EXISTS coins(uuid VARCHAR(36), coins INT);");
- Bukkit.getLogger().log(Level.INFO, "Table " + this.table + " successfully created in " + (System.currentTimeMillis() - time) + "ms.");
- }
- public boolean isPlayerExist(final UUID uuid) {
- try (final PreparedStatement statement = sql.prepare("SELECT * FROM " + this.table + " WHERE uuid ='" + uuid.toString() + "';")) {
- final ResultSet resultSet = statement.executeQuery();
- boolean response = false;
- if(resultSet.next()) {
- response = resultSet.getString("uuid") != null;
- }
- return response;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return Boolean.parseBoolean(null);
- }
- public void registerPlayer(final UUID uuid) {
- if(!this.isPlayerExist(uuid)) {
- sql.update("INSERT INTO " + this.table + "(uuid, coins) VALUES ('" + uuid.toString() + "', '0');");
- }
- }
- public void setCoins(final UUID uuid, final int coinAmount) {
- if(this.isPlayerExist(uuid)) {
- sql.update("UPDATE " + this.table + " SET coins ='" + coinAmount + "' WHERE uuid ='" + uuid.toString() + "';");
- }
- }
- public void addCoins(final UUID uuid, final int coinAmount) {
- this.setCoins(uuid, this.getCoins(uuid) + coinAmount);
- }
- public void removeCoins(final UUID uuid, final int coinAmount) {
- this.setCoins(uuid, this.getCoins(uuid) - coinAmount);
- }
- public int getCoins(final UUID uuid) {
- int coins = 0;
- try(final PreparedStatement statement = sql.prepare("SELECT coins FROM " + this.table + " WHERE uuid ='" + uuid.toString() + "';")) {
- final ResultSet resultSet = statement.executeQuery();
- if(resultSet.next()) {
- coins = resultSet.getInt("coins");
- }
- } catch (final SQLException e) {
- e.printStackTrace();
- }
- return coins;
- }
- public String getTable() {
- return table;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement