mirror of
https://github.com/amalthea-mc/ShopChest.git
synced 2024-11-22 10:22:29 +00:00
Added MySQL Support
This commit is contained in:
parent
0015adefb5
commit
70449399bc
26
config.yml
26
config.yml
@ -72,6 +72,32 @@ blacklist: []
|
||||
# - "STONE:1"
|
||||
# - "DIAMOND_BLOCK"
|
||||
|
||||
database:
|
||||
|
||||
# Select the type of database which should be used
|
||||
# Either use 'SQLite' or 'MySQL'. Otherwise you will break the plugin!
|
||||
type: "SQLite"
|
||||
|
||||
# If the specified is 'MySQL', specify the...
|
||||
# (You can leave this empty if you're using SQLite)
|
||||
mysql:
|
||||
|
||||
# ...hostname where the database is accessible
|
||||
hostname: ""
|
||||
|
||||
# ...port where the database is accessible (default: 3306)
|
||||
port: 3306
|
||||
|
||||
# ...database you want to use
|
||||
database: ""
|
||||
|
||||
# ...username you are going to login with
|
||||
username: ""
|
||||
|
||||
# ...password you are going to login with
|
||||
# Be careful, as anyone who can read this file, can read the password!
|
||||
password: ""
|
||||
|
||||
# Priority: default < group < player
|
||||
shop-limits:
|
||||
|
||||
|
@ -11,6 +11,8 @@ import de.epiceric.shopchest.interfaces.utils.*;
|
||||
import de.epiceric.shopchest.shop.Shop;
|
||||
import de.epiceric.shopchest.shop.Shop.ShopType;
|
||||
import de.epiceric.shopchest.sql.Database;
|
||||
import de.epiceric.shopchest.sql.MySQL;
|
||||
import de.epiceric.shopchest.sql.SQLite;
|
||||
import de.epiceric.shopchest.utils.Metrics;
|
||||
import de.epiceric.shopchest.utils.Metrics.Graph;
|
||||
import de.epiceric.shopchest.utils.Metrics.Plotter;
|
||||
@ -66,6 +68,7 @@ public class ShopChest extends JavaPlugin {
|
||||
@Override
|
||||
public void onEnable() {
|
||||
logger = getLogger();
|
||||
instance = this;
|
||||
|
||||
if (getServer().getPluginManager().getPlugin("Vault") == null) {
|
||||
logger.severe("Could not find plugin 'Vault'!");
|
||||
@ -121,7 +124,13 @@ public class ShopChest extends JavaPlugin {
|
||||
reloadConfig();
|
||||
saveDefaultConfig();
|
||||
|
||||
database = new Database(this);
|
||||
if (Config.database_type() == Database.DatabaseType.SQLite){
|
||||
logger.info("Using SQLite");
|
||||
database = new SQLite(this);
|
||||
} else {
|
||||
logger.info("Using MySQL");
|
||||
database = new MySQL(this);
|
||||
}
|
||||
|
||||
switch (Utils.getVersion(getServer())) {
|
||||
|
||||
@ -161,8 +170,6 @@ public class ShopChest extends JavaPlugin {
|
||||
|
||||
setupPermissions();
|
||||
|
||||
instance = this;
|
||||
|
||||
UpdateChecker uc = new UpdateChecker(this, getDescription().getWebsite());
|
||||
UpdateCheckerResult result = uc.updateNeeded();
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.epiceric.shopchest.config;
|
||||
|
||||
import de.epiceric.shopchest.ShopChest;
|
||||
import de.epiceric.shopchest.sql.Database;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
@ -11,6 +12,30 @@ public class Config {
|
||||
|
||||
private static ShopChest plugin = ShopChest.getInstance();
|
||||
|
||||
public static String database_mysql_host() {
|
||||
return plugin.getConfig().getString("database.mysql.hostname");
|
||||
}
|
||||
|
||||
public static int database_mysql_port() {
|
||||
return plugin.getConfig().getInt("database.mysql.port");
|
||||
}
|
||||
|
||||
public static String database_mysql_database() {
|
||||
return plugin.getConfig().getString("database.mysql.database");
|
||||
}
|
||||
|
||||
public static String database_mysql_username() {
|
||||
return plugin.getConfig().getString("database.mysql.username");
|
||||
}
|
||||
|
||||
public static String database_mysql_password() {
|
||||
return plugin.getConfig().getString("database.mysql.password");
|
||||
}
|
||||
|
||||
public static Database.DatabaseType database_type() {
|
||||
return Database.DatabaseType.valueOf(plugin.getConfig().getString("database.type"));
|
||||
}
|
||||
|
||||
public static Set<String> minimum_prices() {
|
||||
return (plugin.getConfig().getConfigurationSection("minimum-prices") == null) ? new HashSet<String>() : plugin.getConfig().getConfigurationSection("minimum-prices").getKeys(true);
|
||||
}
|
||||
|
@ -16,57 +16,33 @@ import java.io.IOException;
|
||||
import java.sql.*;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Database {
|
||||
public abstract class Database {
|
||||
|
||||
private ShopChest plugin;
|
||||
private Connection connection;
|
||||
public ShopChest plugin;
|
||||
public Connection connection;
|
||||
|
||||
public Database(ShopChest instance) {
|
||||
plugin = instance;
|
||||
public Database(ShopChest plugin) {
|
||||
this.plugin = plugin;
|
||||
initialize();
|
||||
}
|
||||
|
||||
private Connection getSQLConnection() {
|
||||
File dbFile = new File(plugin.getDataFolder(), "shops.db");
|
||||
|
||||
if (!dbFile.exists()) {
|
||||
try {
|
||||
dbFile.createNewFile();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (connection != null && !connection.isClosed()) {
|
||||
return connection;
|
||||
}
|
||||
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
connection = DriverManager.getConnection("jdbc:sqlite:" + dbFile);
|
||||
|
||||
return connection;
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
public abstract Connection getConnection();
|
||||
|
||||
private void initialize() {
|
||||
connection = getSQLConnection();
|
||||
connection = getConnection();
|
||||
|
||||
try {
|
||||
String queryCreateTable = "CREATE TABLE IF NOT EXISTS shop_list (" +
|
||||
"`id` int(11) NOT NULL," +
|
||||
"`vendor` varchar(32) NOT NULL," +
|
||||
"`product` varchar(32) NOT NULL," +
|
||||
"`world` varchar(32) NOT NULL," +
|
||||
"`vendor` tinytext NOT NULL," +
|
||||
"`product` text NOT NULL," +
|
||||
"`world` tinytext NOT NULL," +
|
||||
"`x` int(11) NOT NULL," +
|
||||
"`y` int(11) NOT NULL," +
|
||||
"`z` int(11) NOT NULL," +
|
||||
"`buyprice` float(32) NOT NULL," +
|
||||
"`sellprice` float(32) NOT NULL," +
|
||||
"`shoptype` varchar(32) NOT NULL," +
|
||||
"`shoptype` tinytext NOT NULL," +
|
||||
"PRIMARY KEY (`id`)" +
|
||||
");";
|
||||
|
||||
@ -74,7 +50,7 @@ public class Database {
|
||||
s.executeUpdate(queryCreateTable);
|
||||
s.close();
|
||||
|
||||
PreparedStatement ps = connection.prepareStatement("SELECT * FROM shop_list WHERE id = ?");
|
||||
PreparedStatement ps = connection.prepareStatement("SELECT * FROM shop_list");
|
||||
ResultSet rs = ps.executeQuery();
|
||||
close(ps, rs);
|
||||
|
||||
@ -283,4 +259,9 @@ public class Database {
|
||||
SELLPRICE,
|
||||
SHOPTYPE;
|
||||
}
|
||||
|
||||
public enum DatabaseType {
|
||||
SQLite,
|
||||
MySQL;
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package de.epiceric.shopchest.sql;
|
||||
|
||||
import de.epiceric.shopchest.ShopChest;
|
||||
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class Error {
|
||||
public static void execute(ShopChest plugin, Exception ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Couldn't execute MySQL statement: ", ex);
|
||||
}
|
||||
|
||||
public static void close(ShopChest plugin, Exception ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Failed to close MySQL connection: ", ex);
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package de.epiceric.shopchest.sql;
|
||||
|
||||
public class Errors {
|
||||
public static String sqlConnectionExecute() {
|
||||
return "Couldn't execute MySQL statement: ";
|
||||
}
|
||||
|
||||
public static String sqlConnectionClose() {
|
||||
return "Failed to close MySQL connection: ";
|
||||
}
|
||||
|
||||
public static String noSQLConnection() {
|
||||
return "Unable to retreive MYSQL connection: ";
|
||||
}
|
||||
|
||||
public static String noTableFound() {
|
||||
return "Database Error: No Table Found";
|
||||
}
|
||||
}
|
36
src/de/epiceric/shopchest/sql/MySQL.java
Normal file
36
src/de/epiceric/shopchest/sql/MySQL.java
Normal file
@ -0,0 +1,36 @@
|
||||
package de.epiceric.shopchest.sql;
|
||||
|
||||
import de.epiceric.shopchest.ShopChest;
|
||||
import de.epiceric.shopchest.config.Config;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
|
||||
public class MySQL extends Database {
|
||||
|
||||
public MySQL(ShopChest plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
try {
|
||||
if (connection != null && !connection.isClosed()) {
|
||||
return connection;
|
||||
}
|
||||
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
|
||||
String connectUrl = "jdbc:mysql://" + Config.database_mysql_host() + ":" + Config.database_mysql_port() + "/" + Config.database_mysql_database();
|
||||
plugin.getLogger().info("Connecting to MySQL Server \"" + connectUrl + "\" as user \"" + Config.database_mysql_username() + "\"");
|
||||
|
||||
connection = DriverManager.getConnection(connectUrl, Config.database_mysql_username(), Config.database_mysql_password());
|
||||
|
||||
return connection;
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
44
src/de/epiceric/shopchest/sql/SQLite.java
Normal file
44
src/de/epiceric/shopchest/sql/SQLite.java
Normal file
@ -0,0 +1,44 @@
|
||||
package de.epiceric.shopchest.sql;
|
||||
|
||||
import de.epiceric.shopchest.ShopChest;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
|
||||
public class SQLite extends Database {
|
||||
|
||||
public SQLite(ShopChest plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
File folder = plugin.getDataFolder();
|
||||
File dbFile = new File(folder, "shops.db");
|
||||
|
||||
if (!dbFile.exists()) {
|
||||
try {
|
||||
dbFile.createNewFile();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (connection != null && !connection.isClosed()) {
|
||||
return connection;
|
||||
}
|
||||
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
connection = DriverManager.getConnection("jdbc:sqlite:" + dbFile);
|
||||
|
||||
return connection;
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user