mirror of
https://github.com/AppleDash/SaneEconomy.git
synced 2024-11-29 13:36:13 +01:00
Basic implementation of MySQL support.
This commit is contained in:
parent
921d6fdfca
commit
f3bd331273
@ -1,5 +1,5 @@
|
|||||||
group 'org.appledash'
|
group 'org.appledash'
|
||||||
version '0.1.9-SNAPSHOT'
|
version '0.2.0-SNAPSHOT'
|
||||||
|
|
||||||
apply plugin: 'java'
|
apply plugin: 'java'
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import org.appledash.saneeconomy.economy.Currency;
|
|||||||
import org.appledash.saneeconomy.economy.EconomyManager;
|
import org.appledash.saneeconomy.economy.EconomyManager;
|
||||||
import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend;
|
import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend;
|
||||||
import org.appledash.saneeconomy.economy.backend.type.EconomyStorageBackendFlatfile;
|
import org.appledash.saneeconomy.economy.backend.type.EconomyStorageBackendFlatfile;
|
||||||
|
import org.appledash.saneeconomy.economy.backend.type.EconomyStorageBackendMySQL;
|
||||||
import org.appledash.saneeconomy.listeners.JoinQuitListener;
|
import org.appledash.saneeconomy.listeners.JoinQuitListener;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
@ -78,6 +79,26 @@ public class SaneEconomy extends JavaPlugin {
|
|||||||
File backendFile = new File(getDataFolder(), backendFileName);
|
File backendFile = new File(getDataFolder(), backendFileName);
|
||||||
backend = new EconomyStorageBackendFlatfile(backendFile);
|
backend = new EconomyStorageBackendFlatfile(backendFile);
|
||||||
getLogger().info("Initialized flatfile backend with file " + backendFile.getAbsolutePath());
|
getLogger().info("Initialized flatfile backend with file " + backendFile.getAbsolutePath());
|
||||||
|
} else if (backendType.equalsIgnoreCase("mysql")) {
|
||||||
|
String backendHost = getConfig().getString("backend.host");
|
||||||
|
int backendPort = getConfig().getInt("backend.port", 3306);
|
||||||
|
String backendDb = getConfig().getString("backend.database");
|
||||||
|
String backendUser = getConfig().getString("backend.username");
|
||||||
|
String backendPass = getConfig().getString("backend.password");
|
||||||
|
|
||||||
|
String jdbcUrl = String.format("jdbc:mysql://%s:%d/%s", backendHost, backendPort, backendDb);
|
||||||
|
|
||||||
|
EconomyStorageBackendMySQL mySQLBackend = new EconomyStorageBackendMySQL(jdbcUrl, backendUser, backendPass);
|
||||||
|
backend = mySQLBackend;
|
||||||
|
|
||||||
|
getLogger().info("Initialized MySQL backend to host " + backendHost);
|
||||||
|
getLogger().info("Testing connection...");
|
||||||
|
if (!mySQLBackend.testConnection()) {
|
||||||
|
getLogger().severe("MySQL connection failed - cannot continue!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
getLogger().info("Connection successful!");
|
||||||
} else {
|
} else {
|
||||||
getLogger().severe("Unknown storage backend " + backendType + "!");
|
getLogger().severe("Unknown storage backend " + backendType + "!");
|
||||||
return false;
|
return false;
|
||||||
|
@ -0,0 +1,152 @@
|
|||||||
|
package org.appledash.saneeconomy.economy.backend.type;
|
||||||
|
|
||||||
|
import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by AppleDash on 6/14/2016.
|
||||||
|
* Blackjack is still best pony.
|
||||||
|
*/
|
||||||
|
public class EconomyStorageBackendMySQL implements EconomyStorageBackend {
|
||||||
|
private final String dbUrl;
|
||||||
|
private final String dbUser;
|
||||||
|
private final String dbPassword;
|
||||||
|
|
||||||
|
public EconomyStorageBackendMySQL(String dbUrl, String dbUser, String dbPassword) {
|
||||||
|
this.dbUrl = dbUrl;
|
||||||
|
this.dbUser = dbUser;
|
||||||
|
this.dbPassword = dbPassword;
|
||||||
|
|
||||||
|
try {
|
||||||
|
Class.forName("com.mysql.jdbc.Driver");
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new RuntimeException("No MySQL driver found.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection openConnection() {
|
||||||
|
try {
|
||||||
|
return DriverManager.getConnection(dbUrl, dbUser, dbPassword);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException("Database unavailable.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean testConnection() {
|
||||||
|
try {
|
||||||
|
openConnection().close();
|
||||||
|
createTable();
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createTable() {
|
||||||
|
Connection conn = openConnection();
|
||||||
|
try {
|
||||||
|
PreparedStatement ps = conn.prepareStatement("CREATE TABLE IF NOT EXISTS `player_balances` (player_uuid CHAR(38), balance DECIMAL(18, 2))");
|
||||||
|
ps.executeUpdate();
|
||||||
|
conn.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException("Failed to create tables!", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean accountExists(Player player) {
|
||||||
|
return accountExists(player, openConnection());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getBalance(Player player) {
|
||||||
|
Connection conn = openConnection();
|
||||||
|
try {
|
||||||
|
PreparedStatement statement = conn.prepareStatement("SELECT balance FROM `player_balances` WHERE `player_uuid` = ?");
|
||||||
|
statement.setString(1, player.getUniqueId().toString());
|
||||||
|
|
||||||
|
ResultSet rs = statement.executeQuery();
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
double bal = rs.getDouble("balance");
|
||||||
|
conn.close();
|
||||||
|
return bal;
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException("SQL error has occurred.", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0.0D;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setBalance(Player player, double newBalance) {
|
||||||
|
Connection conn = openConnection();
|
||||||
|
ensureAccountExists(player, conn);
|
||||||
|
try {
|
||||||
|
PreparedStatement statement = conn.prepareStatement("UPDATE `player_balances` SET balance = ? WHERE `player_uuid` = ?");
|
||||||
|
statement.setDouble(1, newBalance);
|
||||||
|
statement.setString(2, player.getUniqueId().toString());
|
||||||
|
statement.executeUpdate();
|
||||||
|
conn.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException("SQL error has occurred.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double addBalance(Player player, double amount) {
|
||||||
|
// TODO: Optimize?
|
||||||
|
double curBalance = getBalance(player);
|
||||||
|
double newBalance = curBalance + amount;
|
||||||
|
|
||||||
|
setBalance(player, newBalance);
|
||||||
|
|
||||||
|
return newBalance;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double subtractBalance(Player player, double amount) {
|
||||||
|
// TODO: Optimize?
|
||||||
|
double curBalance = getBalance(player);
|
||||||
|
double newBalance = curBalance - amount;
|
||||||
|
|
||||||
|
setBalance(player, newBalance);
|
||||||
|
|
||||||
|
return newBalance;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ensureAccountExists(Player player, Connection conn) {
|
||||||
|
if (!accountExists(player, conn)) {
|
||||||
|
try {
|
||||||
|
PreparedStatement statement = conn.prepareStatement("INSERT INTO `player_balances` (player_uuid, balance) VALUES (?, 0.0)");
|
||||||
|
statement.setString(1, player.getUniqueId().toString());
|
||||||
|
statement.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException("SQL error has occurred.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean accountExists(Player player, Connection conn) {
|
||||||
|
try {
|
||||||
|
PreparedStatement statement = conn.prepareStatement("SELECT 1 FROM `player_balances` WHERE `player_uuid` = ?");
|
||||||
|
statement.setString(1, player.getUniqueId().toString());
|
||||||
|
|
||||||
|
ResultSet rs = statement.executeQuery();
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException("SQL error has occurred.", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
name: SaneEconomy
|
name: SaneEconomy
|
||||||
main: org.appledash.saneeconomy.SaneEconomy
|
main: org.appledash.saneeconomy.SaneEconomy
|
||||||
version: 0.1.9
|
version: 0.2.0
|
||||||
commands:
|
commands:
|
||||||
balance:
|
balance:
|
||||||
aliases: [bal]
|
aliases: [bal]
|
||||||
|
Loading…
Reference in New Issue
Block a user