Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.maiky.tokens.engine;
- import net.maiky.tokens.tokens;
- import org.bukkit.entity.Player;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.UUID;
- public class SQLGetter {
- public tokens plugin;
- public SQLGetter(tokens plugin) {
- this.plugin = plugin;
- }
- public void createTable() {
- PreparedStatement ps;
- try {
- ps = plugin.MySQL.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS atokens (NAME VARCHAR(100),UUID VARCHAR(100),TOKENS INT(100),PRIMARY KEY (NAME))");
- ps.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void createPlayer(Player player) {
- try {
- UUID uuid = player.getUniqueId();
- if(!exists(uuid)) {
- PreparedStatement ps = plugin.MySQL.getConnection().prepareStatement("INSERT IGNORE INTO atokens (NAME,UUID,TOKENS) VALUES(?,?,?)");
- ps.setString(1, player.getName());
- ps.setString(2, uuid.toString());
- ps.setString(3, "0");
- ps.executeUpdate();
- return;
- }
- } catch (SQLException e) {
- //e.printStackTrace();
- }
- }
- public boolean exists(UUID uuid) {
- try {
- PreparedStatement ps = plugin.MySQL.getConnection().prepareStatement("SELECT * FROM atokens WHERE UUID=?");
- ps.setString(1, uuid.toString());
- ResultSet results = ps.executeQuery();
- if(results.next()) {
- return true;
- }
- return false;
- } catch (SQLException e) {
- //e.printStackTrace();
- }
- return false;
- }
- public void addTokens(UUID uuid, int tokens) {
- try {
- PreparedStatement ps = plugin.MySQL.getConnection().prepareStatement("UPDATE atokens SET TOKENS=? WHERE UUID=?");
- ps.setInt(1, (getTokens(uuid) + tokens));
- ps.setString(2, uuid.toString());
- ps.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void setTokens(UUID uuid, int tokens) {
- try {
- PreparedStatement ps = plugin.MySQL.getConnection().prepareStatement("UPDATE atokens SET TOKENS=? WHERE UUID=?");
- ps.setInt(1, (tokens));
- ps.setString(2, uuid.toString());
- ps.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public int getTokens(UUID uuid) {
- try {
- PreparedStatement ps = plugin.MySQL.getConnection().prepareStatement("SELECT TOKENS FROM atokens WHERE UUID=?");
- ps.setString(1, uuid.toString());
- ResultSet rs = ps.executeQuery();
- int tokens;
- if(rs.next()) {
- tokens = rs.getInt("TOKENS");
- return tokens;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return 0;
- }
- public int removeTokens(UUID uuid, int tokens) {
- try {
- PreparedStatement ps = plugin.MySQL.getConnection().prepareStatement("UPDATE atokens SET TOKENS=? WHERE UUID=?");
- ps.setInt(1, (getTokens(uuid) - tokens));
- ps.setString(2, uuid.toString());
- ps.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return 0;
- }
- public int clearTnokes(UUID uuid) {
- try {
- PreparedStatement ps = plugin.MySQL.getConnection().prepareStatement("DELETE FROM atokens WHERE UUID=?");
- ps.setString(1, uuid.toString());
- ps.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement