Core/src/main/java/eu/univento/core/api/player/Coins.java

60 lines
1.3 KiB
Java

package eu.univento.core.api.player;
import eu.univento.core.Core;
import eu.univento.core.api.database.MySQL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* coins management
* @author joethei
* @version 1.0
*/
public class Coins{
/**
* gets coins from player
* @param p CustomPlayer
* @return coins as integer
*/
static int getCoins(CustomPlayer p) {
int id = p.getID();
MySQL sql = Core.returnSQL();
try {
sql.openConnection();
PreparedStatement st = sql.getConnection().prepareStatement("SELECT Coins FROM users WHERE ID='" + id + "';");
ResultSet rs = st.executeQuery();
if(rs.next()) {
int coins = rs.getInt("Coins");
sql.closeConnection();
return coins;
}
return 0;
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
return 0;
}
}
/**
* sets coins of player
* @param p CustomPlayer
* @param coins coins to set
*/
static void setCoins(CustomPlayer p, int coins) {
int id = p.getID();
MySQL sql = Core.returnSQL();
try {
sql.openConnection();
PreparedStatement st = sql.getConnection().prepareStatement("UPDATE users SET Coins='" + coins + "' WHERE ID='" + id + "';");
st.execute();
sql.closeConnection();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}