Dateien nach "src/main/java/craftersland/games/money/database" hochladen
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package net.craftersland.games.money.database;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public interface AccountDatabaseInterface<X> {
|
||||
|
||||
|
||||
//Accountmethods
|
||||
public boolean hasAccount(UUID player);
|
||||
public boolean createAccount(UUID player);
|
||||
public X getBalance(UUID player);
|
||||
public boolean setBalance(UUID player, X amount);
|
||||
public boolean addToAccount(UUID player, X amount);
|
||||
public boolean removeFromAccount(UUID player, X amount);
|
||||
public UUID[] getAccounts();
|
||||
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
package net.craftersland.games.money.database;
|
||||
|
||||
public interface DatabaseManagerInterface {
|
||||
|
||||
public boolean setupDatabase();
|
||||
public boolean closeDatabase();
|
||||
}
|
@@ -0,0 +1,95 @@
|
||||
package net.craftersland.games.money.database;
|
||||
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import net.craftersland.games.money.Money;
|
||||
|
||||
public class DatabaseManagerMysql implements DatabaseManagerInterface{
|
||||
|
||||
private Connection conn = null;
|
||||
|
||||
// Hostname
|
||||
private String dbHost;
|
||||
|
||||
// Port -- Standard: 3306
|
||||
private String dbPort;
|
||||
|
||||
// Databankname
|
||||
private String database;
|
||||
|
||||
// Databank username
|
||||
private String dbUser;
|
||||
|
||||
// Databank password
|
||||
private String dbPassword;
|
||||
|
||||
private Money money;
|
||||
|
||||
public DatabaseManagerMysql(Money money) {
|
||||
this.money = money;
|
||||
|
||||
setupDatabase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setupDatabase() {
|
||||
try {
|
||||
//Load Drivers
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
|
||||
dbHost = money.getConfigurationHandler().getString("database.mysql.host");
|
||||
dbPort = money.getConfigurationHandler().getString("database.mysql.port");
|
||||
database = money.getConfigurationHandler().getString("database.mysql.databaseName");
|
||||
dbUser = money.getConfigurationHandler().getString("database.mysql.user");
|
||||
dbPassword = money.getConfigurationHandler().getString("database.mysql.password");
|
||||
|
||||
//Connect to database
|
||||
conn = DriverManager.getConnection("jdbc:mysql://" + dbHost + ":"
|
||||
+ dbPort + "/" + database + "?" + "user=" + dbUser + "&"
|
||||
+ "password=" + dbPassword);
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
//System.out.println("Could not locate drivers!");
|
||||
Money.log.severe("Could not locate drivers for mysql!");
|
||||
return false;
|
||||
} catch (SQLException e) {
|
||||
//System.out.println("Could not connect");
|
||||
Money.log.severe("Could not connect to mysql database!");
|
||||
return false;
|
||||
}
|
||||
|
||||
//Create tables if needed
|
||||
Statement query;
|
||||
try {
|
||||
query = conn.createStatement();
|
||||
|
||||
String accounts = "CREATE TABLE IF NOT EXISTS `bc_accounts` (id int(10) AUTO_INCREMENT, player_name varchar(50) NOT NULL UNIQUE, balance DOUBLE(30,2) NOT NULL, PRIMARY KEY(id));";
|
||||
query.executeUpdate(accounts);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
Money.log.info("Mysql has been set up!");
|
||||
return true;
|
||||
}
|
||||
|
||||
public Connection getConnection() {
|
||||
return conn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean closeDatabase() {
|
||||
try {
|
||||
conn.close();
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,167 @@
|
||||
package net.craftersland.games.money.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import net.craftersland.games.money.Money;
|
||||
|
||||
public class MoneyMysqlInterface implements AccountDatabaseInterface <Double>{
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private Money money;
|
||||
private Connection conn;
|
||||
|
||||
public MoneyMysqlInterface(Money money) {
|
||||
this.money = money;
|
||||
this.conn = ((DatabaseManagerMysql)money.getDatabaseManagerInterface()).getConnection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAccount(UUID player) {
|
||||
try {
|
||||
|
||||
String sql = "SELECT `player_name` FROM `bc_accounts` WHERE `player_name` = ?";
|
||||
PreparedStatement preparedUpdateStatement = conn.prepareStatement(sql);
|
||||
preparedUpdateStatement.setString(1, player.toString());
|
||||
|
||||
|
||||
ResultSet result = preparedUpdateStatement.executeQuery();
|
||||
|
||||
while (result.next()) {
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean createAccount(UUID player) {
|
||||
try {
|
||||
|
||||
String sql = "INSERT INTO `bc_accounts`(`player_name`, `balance`) " +
|
||||
"VALUES(?, ?)";
|
||||
PreparedStatement preparedStatement = conn.prepareStatement(sql);
|
||||
|
||||
preparedStatement.setString(1, player.toString());
|
||||
preparedStatement.setString(2, "0");
|
||||
|
||||
preparedStatement.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getBalance(UUID player) {
|
||||
if (!hasAccount(player)) {
|
||||
createAccount(player);
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
String sql = "SELECT `balance` FROM `bc_accounts` WHERE `player_name` = ?";
|
||||
|
||||
PreparedStatement preparedUpdateStatement = conn.prepareStatement(sql);
|
||||
preparedUpdateStatement.setString(1, player.toString());
|
||||
ResultSet result = preparedUpdateStatement.executeQuery();
|
||||
|
||||
while (result.next()) {
|
||||
return Double.parseDouble(result.getString("balance"));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setBalance(UUID player, Double amount) {
|
||||
if (!hasAccount(player)) {
|
||||
createAccount(player);
|
||||
}
|
||||
|
||||
try {
|
||||
String updateSql = "UPDATE `bc_accounts` " +
|
||||
"SET `balance` = ?" +
|
||||
"WHERE `player_name` = ?";
|
||||
PreparedStatement preparedUpdateStatement = conn.prepareStatement(updateSql);
|
||||
preparedUpdateStatement.setString(1, amount+"");
|
||||
preparedUpdateStatement.setString(2, player.toString());
|
||||
|
||||
preparedUpdateStatement.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addToAccount(UUID player, Double amount) {
|
||||
if (!hasAccount(player)) {
|
||||
createAccount(player);
|
||||
}
|
||||
|
||||
if (amount < 0) {
|
||||
return removeFromAccount(player, -amount);
|
||||
}
|
||||
|
||||
Double currentBalance = getBalance(player);
|
||||
if (currentBalance <= Double.MAX_VALUE-amount) {
|
||||
setBalance(player, currentBalance+amount);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeFromAccount(UUID player, Double amount) {
|
||||
if (!hasAccount(player)) {
|
||||
createAccount(player);
|
||||
}
|
||||
|
||||
if (amount < 0) {
|
||||
return addToAccount(player, -amount);
|
||||
}
|
||||
|
||||
Double currentBalance = getBalance(player);
|
||||
if (currentBalance >= -Double.MAX_VALUE+amount) {
|
||||
setBalance(player, currentBalance-amount);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID[] getAccounts() {
|
||||
|
||||
Statement query;
|
||||
try {
|
||||
query = conn.createStatement();
|
||||
|
||||
String sql = "SELECT `player_name` FROM `bc_accounts`";
|
||||
ResultSet result = query.executeQuery(sql);
|
||||
|
||||
List <UUID> loadingList= new ArrayList <UUID>();
|
||||
while (result.next()) {
|
||||
loadingList.add(UUID.fromString(result.getString("player_name")));
|
||||
}
|
||||
return loadingList.toArray(new UUID [0]);
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user