Merge pull request #16 from Altitude-Devs/Dev/config

Rework configuration
This commit is contained in:
destro174 2024-04-21 14:19:43 +02:00 committed by GitHub
commit 1af40415c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 366 additions and 110 deletions

View File

@ -34,6 +34,9 @@ public class PlayerShops extends JavaPlugin {
private InventoryListener inventoryListener;
private GriefPreventionListener griefPreventionListener;
@Getter
private DatabaseConfig databaseConfig;
public void onEnable() {
instance = this;
if(!setupEconomy()) {
@ -110,7 +113,8 @@ public class PlayerShops extends JavaPlugin {
public void reloadConfigs() {
Config.reload();
MessageConfig.reload();
DatabaseConfig.reload();
databaseConfig = new DatabaseConfig(Bukkit.getServerName());
databaseConfig.reload();
for (ShopType shopType : ShopType.values()) {
// preload ShopType to get the configs active

View File

@ -1,27 +1,162 @@
package com.alttd.playershops.config;
import com.alttd.galaxy.configuration.AbstractConfiguration;
import io.leangen.geantyref.TypeToken;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.ConfigurationOptions;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.yaml.NodeStyle;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
@SuppressWarnings("unused")
public class Config extends AbstractConfiguration {
public class Config {
public static File configPath = new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + "playershops");
private Config() {
super(Config.configPath, "config");
}
public static File CONFIG_PATH = new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + "playershops");
public static final Pattern PATH_PATTERN = Pattern.compile("\\.");
private static final String HEADER = "";
private static File CONFIG_FILE;
public static ConfigurationNode config;
public static YamlConfigurationLoader configLoader;
static Config config;
static int version;
static boolean verbose;
public static void reload() {
config = new Config();
CONFIG_FILE = new File(CONFIG_PATH, "config.yml");
version = config.getInt("config-version", 1);
config.set("config-version", 1);
configLoader = YamlConfigurationLoader.builder()
.file(CONFIG_FILE)
.nodeStyle(NodeStyle.BLOCK)
.build();
if (!CONFIG_FILE.getParentFile().exists()) {
if(!CONFIG_FILE.getParentFile().mkdirs()) {
return;
}
}
if (!CONFIG_FILE.exists()) {
try {
if(!CONFIG_FILE.createNewFile()) {
return;
}
} catch (IOException error) {
error.printStackTrace();
}
}
config.readConfig(Config.class, null);
try {
config = configLoader.load(ConfigurationOptions.defaults().header(HEADER).shouldCopyDefaults(false));
} catch (IOException e) {
e.printStackTrace();
}
verbose = getBoolean("verbose", true);
version = getInt("config-version", 1);
readConfig(Config.class, null);
try {
configLoader.save(config);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readConfig(Class<?> clazz, Object instance) {
for (Method method : clazz.getDeclaredMethods()) {
if (Modifier.isPrivate(method.getModifiers())) {
if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) {
try {
method.setAccessible(true);
method.invoke(instance);
} catch (InvocationTargetException | IllegalAccessException ex) {
ex.printStackTrace();
}
}
}
}
try {
configLoader.save(config);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void saveConfig() {
try {
configLoader.save(config);
} catch (IOException ex) {
ex.printStackTrace();
}
}
private static Object[] splitPath(String key) {
return PATH_PATTERN.split(key);
}
static void set(String path, Object def) {
if(config.node(splitPath(path)).virtual()) {
try {
config.node(splitPath(path)).set(def);
} catch (SerializationException e) {
e.printStackTrace();
}
}
}
private static void setString(String path, String def) {
try {
if(config.node(splitPath(path)).virtual())
config.node(splitPath(path)).set(io.leangen.geantyref.TypeToken.get(String.class), def);
} catch(SerializationException ex) {
ex.printStackTrace();
}
}
private static boolean getBoolean(String path, boolean def) {
set(path, def);
return config.node(splitPath(path)).getBoolean(def);
}
private static double getDouble(String path, double def) {
set(path, def);
return config.node(splitPath(path)).getDouble(def);
}
private static int getInt(String path, int def) {
set(path, def);
return config.node(splitPath(path)).getInt(def);
}
private static String getString(String path, String def) {
setString(path, def);
return config.node(splitPath(path)).getString(def);
}
private static Long getLong(String path, Long def) {
set(path, def);
return config.node(splitPath(path)).getLong(def);
}
private static <T> List<String> getList(String path, T def) {
try {
set(path, def);
return config.node(splitPath(path)).getList(TypeToken.get(String.class));
} catch(SerializationException ex) {
ex.printStackTrace();
}
return new ArrayList<>();
}
protected static ConfigurationNode getNode(String path) {
return config.node(splitPath(path));
}
public static int shopLimit = 100;
@ -31,11 +166,11 @@ public class Config extends AbstractConfiguration {
public static double shopCreationCost = 250; // minimum amount of balance to create a shop, this is to cover the cost to manage shops and upkeep
private static void shopSettings() {
String path = "shop-settings.";
shopLimit = config.getInt(path + "default-shop-limit", shopLimit);
usePermissionShopLimit = config.getBoolean(path + "use-permission-based-shop-limit", usePermissionShopLimit);
shopCreationWord = config.getString(path + "creation-word", shopCreationWord);
shopCreationBalance = config.getDouble(path + "creation-balance", shopCreationBalance);
shopCreationCost = config.getDouble(path + "creation-cost", shopCreationCost);
shopLimit = getInt(path + "default-shop-limit", shopLimit);
usePermissionShopLimit = getBoolean(path + "use-permission-based-shop-limit", usePermissionShopLimit);
shopCreationWord = getString(path + "creation-word", shopCreationWord);
shopCreationBalance = getDouble(path + "creation-balance", shopCreationBalance);
shopCreationCost = getDouble(path + "creation-cost", shopCreationCost);
}
}

View File

@ -1,49 +1,59 @@
package com.alttd.playershops.config;
import com.alttd.galaxy.configuration.AbstractConfiguration;
import com.alttd.playershops.shop.ShopType;
public class DatabaseConfig {
import java.util.HashMap;
public class DatabaseConfig extends AbstractConfiguration {
private DatabaseConfig() {
super(Config.configPath, "database");
private final String serverName;
private final String configPath;
private final String defaultPath;
public DatabaseConfig(String serverName) {
this.serverName = serverName;
this.configPath = "server." + this.serverName + ".";
this.defaultPath = "server.default.";
reload();
}
static DatabaseConfig config;
static int version;
public static void reload() {
config = new DatabaseConfig();
version = config.getInt("config-version", 1);
config.set("config-version", 1);
config.readConfig(DatabaseConfig.class, null);
public void reload() {
Config.readConfig(DatabaseConfig.class, this);
}
public static String DRIVER = "mysql";
public static String IP = "localhost";
public static String PORT = "3306";
public static String DATABASE_NAME = "PlayerShops";
public static String USERNAME = "root";
public static String PASSWORD = "root";
private static void set(String path, Object def) {
Config.set(path, def);
}
private static void loadDatabase() {
DRIVER = config.getString("database.driver", DRIVER);
IP = config.getString("database.ip", IP);
PORT = config.getString("database.port", PORT);
DATABASE_NAME = config.getString("database.name", DATABASE_NAME);
USERNAME = config.getString("database.username", USERNAME);
PASSWORD = config.getString("database.password", PASSWORD);
private String getString(String path, String def) {
set(defaultPath + path, def);
return Config.getNode(configPath + path).getString(
Config.getNode(defaultPath + path).getString(def));
}
private int getInt(String path, int def) {
set(defaultPath + path, def);
return Config.getNode(configPath + path).getInt(
Config.getNode(defaultPath + path).getInt(def));
}
public String DRIVER = "mysql";
public String IP = "localhost";
public String PORT = "3306";
public String DATABASE_NAME = "PlayerShops";
public String USERNAME = "root";
public String PASSWORD = "root";
private void loadDatabase() {
DRIVER = getString("database.driver", DRIVER);
IP = getString("database.ip", IP);
PORT = getString("database.port", PORT);
DATABASE_NAME = getString("database.name", DATABASE_NAME);
USERNAME = getString("database.username", USERNAME);
PASSWORD = getString("database.password", PASSWORD);
}
// Time in seconds between database tasks
public static int queueDelay = 5;
public static int maxDatabaseConnections = 10;
private static void databaseSettings() {
queueDelay = config.getInt("database.queue.delay" , queueDelay);
maxDatabaseConnections = config.getInt("database.maximum-connections" , maxDatabaseConnections);
public int queueDelay = 5;
public int maxDatabaseConnections = 10;
private void databaseSettings() {
queueDelay = getInt("database.queue.delay" , queueDelay);
maxDatabaseConnections = getInt("database.maximum-connections" , maxDatabaseConnections);
}
}

View File

@ -23,30 +23,30 @@ public class GuiIconConfig {
static int version;
public void init() {
Config.config.readConfig(GuiIconConfig.class, this);
Config.readConfig(GuiIconConfig.class, this);
}
private static void set(String path, Object def) {
Config.config.set(path, def);
Config.set(path, def);
}
private String getString(String path, String def) {
set(defaultPath + path, def);
return Config.config.getNode(configPath + path).getString(
Config.config.getNode(defaultPath + path).getString(def));
return Config.getNode(configPath + path).getString(
Config.getNode(defaultPath + path).getString(def));
}
private int getInt(String path, int def) {
set(defaultPath + path, def);
return Config.config.getNode(configPath + path).getInt(
Config.config.getNode(defaultPath + path).getInt(def));
return Config.getNode(configPath + path).getInt(
Config.getNode(defaultPath + path).getInt(def));
}
private List<String> getStringList(String path, List<String> def) {
try {
set(defaultPath + path, def);
return Config.config.getNode(configPath + path).getList(TypeToken.get(String.class),
Config.config.getNode(defaultPath + path).getList(TypeToken.get(String.class), def));
return Config.getNode(configPath + path).getList(TypeToken.get(String.class),
Config.getNode(defaultPath + path).getList(TypeToken.get(String.class), def));
} catch(SerializationException ignore) {}
return new ArrayList<>();
}

View File

@ -1,24 +1,128 @@
package com.alttd.playershops.config;
import com.alttd.galaxy.configuration.AbstractConfiguration;
import io.leangen.geantyref.TypeToken;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.ConfigurationOptions;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.yaml.NodeStyle;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("unused")
public class MessageConfig extends AbstractConfiguration {
public class MessageConfig {
public MessageConfig() {
super(Config.configPath, "messageconfig");
}
private static final String HEADER = "";
private static File CONFIG_FILE;
public static ConfigurationNode config;
public static YamlConfigurationLoader configLoader;
static MessageConfig config;
static int version;
public static void reload() {
config = new MessageConfig();
CONFIG_FILE = new File(Config.CONFIG_PATH, "messageconfig.yml");
version = config.getInt("config-version", 1);
config.set("config-version", 1);
configLoader = YamlConfigurationLoader.builder()
.file(CONFIG_FILE)
.nodeStyle(NodeStyle.BLOCK)
.build();
if (!CONFIG_FILE.getParentFile().exists()) {
if(!CONFIG_FILE.getParentFile().mkdirs()) {
return;
}
}
if (!CONFIG_FILE.exists()) {
try {
if(!CONFIG_FILE.createNewFile()) {
return;
}
} catch (IOException error) {
error.printStackTrace();
}
}
config.readConfig(MessageConfig.class, null);
try {
config = configLoader.load(ConfigurationOptions.defaults().header(HEADER).shouldCopyDefaults(false));
} catch (IOException e) {
e.printStackTrace();
}
Config.readConfig(MessageConfig.class, null);
try {
configLoader.save(config);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void saveConfig() {
try {
configLoader.save(config);
} catch (IOException ex) {
ex.printStackTrace();
}
}
private static Object[] splitPath(String key) {
return Config.PATH_PATTERN.split(key);
}
private static void set(String path, Object def) {
if(config.node(splitPath(path)).virtual()) {
try {
config.node(splitPath(path)).set(def);
} catch (SerializationException e) {
e.printStackTrace();
}
}
}
private static void setString(String path, String def) {
try {
if(config.node(splitPath(path)).virtual())
config.node(splitPath(path)).set(io.leangen.geantyref.TypeToken.get(String.class), def);
} catch(SerializationException ex) {
ex.printStackTrace();
}
}
private static boolean getBoolean(String path, boolean def) {
set(path, def);
return config.node(splitPath(path)).getBoolean(def);
}
private static double getDouble(String path, double def) {
set(path, def);
return config.node(splitPath(path)).getDouble(def);
}
private static int getInt(String path, int def) {
set(path, def);
return config.node(splitPath(path)).getInt(def);
}
private static String getString(String path, String def) {
setString(path, def);
return config.node(splitPath(path)).getString(def);
}
private static Long getLong(String path, Long def) {
set(path, def);
return config.node(splitPath(path)).getLong(def);
}
private static <T> List<String> getList(String path, T def) {
try {
set(path, def);
return config.node(splitPath(path)).getList(TypeToken.get(String.class));
} catch(SerializationException ex) {
ex.printStackTrace();
}
return new ArrayList<>();
}
public static String SHOP_ALREADY_EXISTS = "<red>This block is already a Shop</red>";
@ -29,19 +133,19 @@ public class MessageConfig extends AbstractConfiguration {
public static String NO_PERMISSION_FOR_SHOP_TYPE = "<red>You do not have permission to use <shop_type> shops.";
private static void loadErrorMessages() {
SHOP_ALREADY_EXISTS = config.getString("errors.shop-already-exists", SHOP_ALREADY_EXISTS);
NO_SHOP_CREATE_PERMISSION = config.getString("errors.no-shop-create-permission", NO_SHOP_CREATE_PERMISSION);
SHOP_LIMIT_REACHED = config.getString("errors.shop-limit-reached", SHOP_LIMIT_REACHED);
BREAK_SHOP_WHILE_CONVERSING = config.getString("errors.break-shop-while-conversing", BREAK_SHOP_WHILE_CONVERSING);
INSUFFICIENT_FUNDS = config.getString("errors.insufficient-funds", INSUFFICIENT_FUNDS);
NO_PERMISSION_FOR_SHOP_TYPE = config.getString("permissions-messages.shop-type", NO_PERMISSION_FOR_SHOP_TYPE);
SHOP_ALREADY_EXISTS = getString("errors.shop-already-exists", SHOP_ALREADY_EXISTS);
NO_SHOP_CREATE_PERMISSION = getString("errors.no-shop-create-permission", NO_SHOP_CREATE_PERMISSION);
SHOP_LIMIT_REACHED = getString("errors.shop-limit-reached", SHOP_LIMIT_REACHED);
BREAK_SHOP_WHILE_CONVERSING = getString("errors.break-shop-while-conversing", BREAK_SHOP_WHILE_CONVERSING);
INSUFFICIENT_FUNDS = getString("errors.insufficient-funds", INSUFFICIENT_FUNDS);
NO_PERMISSION_FOR_SHOP_TYPE = getString("permissions-messages.shop-type", NO_PERMISSION_FOR_SHOP_TYPE);
}
public static String SHOP_INFO = "<yellow>This shop <action> <gold><amount></gold> <item> for <gold><price></gold>.</yellow>";
public static String SHOP_STOCK_INFO = "<yellow>This <type> shop has <gold><stock></gold> stock.</yellow>";
private static void otherMessages() {
SHOP_INFO = config.getString("messages.shop-info", SHOP_INFO);
SHOP_STOCK_INFO = config.getString("messages.shop-stock-info", SHOP_STOCK_INFO);
SHOP_INFO = getString("messages.shop-info", SHOP_INFO);
SHOP_STOCK_INFO = getString("messages.shop-stock-info", SHOP_STOCK_INFO);
}
public static String CHANGE_PRICE_PROMPT = "What should the price be? Type cancel to cancel this action.";
@ -54,15 +158,15 @@ public class MessageConfig extends AbstractConfiguration {
public static String WITHDRAW_BALANCE_FAILED_PROMPT = "Your shop does not have enough balance to withdraw this amount. Type cancel to cancel this action.";
public static String CHANGE_ITEM_PROMPT = "Hold the item you would like to sell and type continue. Type cancel to cancel this action.";
private static void promptMessages() {
CHANGE_PRICE_PROMPT = config.getString("prompt.change-price", CHANGE_PRICE_PROMPT);
CHANGE_PRICE_FAILED_PROMPT = config.getString("prompt.change-price-failed", CHANGE_PRICE_FAILED_PROMPT);
CHANGE_AMOUNT_PROMPT = config.getString("prompt.change-amount", CHANGE_AMOUNT_PROMPT);
CHANGE_AMOUNT_FAILED_PROMPT = config.getString("prompt.change-amount-failed", CHANGE_AMOUNT_FAILED_PROMPT);
ADD_BALANCE_PROMPT = config.getString("prompt.add-balance", ADD_BALANCE_PROMPT);
ADD_BALANCE_FAILED_PROMPT = config.getString("prompt.add-balance-failed", ADD_BALANCE_FAILED_PROMPT);
WITHDRAW_BALANCE_PROMPT = config.getString("prompt.withdraw-balance", WITHDRAW_BALANCE_PROMPT);
WITHDRAW_BALANCE_FAILED_PROMPT = config.getString("prompt.withdraw-balance-failed", WITHDRAW_BALANCE_FAILED_PROMPT);
CHANGE_ITEM_PROMPT = config.getString("prompt.change-item",CHANGE_ITEM_PROMPT );
CHANGE_PRICE_PROMPT = getString("prompt.change-price", CHANGE_PRICE_PROMPT);
CHANGE_PRICE_FAILED_PROMPT = getString("prompt.change-price-failed", CHANGE_PRICE_FAILED_PROMPT);
CHANGE_AMOUNT_PROMPT = getString("prompt.change-amount", CHANGE_AMOUNT_PROMPT);
CHANGE_AMOUNT_FAILED_PROMPT = getString("prompt.change-amount-failed", CHANGE_AMOUNT_FAILED_PROMPT);
ADD_BALANCE_PROMPT = getString("prompt.add-balance", ADD_BALANCE_PROMPT);
ADD_BALANCE_FAILED_PROMPT = getString("prompt.add-balance-failed", ADD_BALANCE_FAILED_PROMPT);
WITHDRAW_BALANCE_PROMPT = getString("prompt.withdraw-balance", WITHDRAW_BALANCE_PROMPT);
WITHDRAW_BALANCE_FAILED_PROMPT = getString("prompt.withdraw-balance-failed", WITHDRAW_BALANCE_FAILED_PROMPT);
CHANGE_ITEM_PROMPT = getString("prompt.change-item",CHANGE_ITEM_PROMPT );
}
public static String GUI_HOME_TITLE = "<green>PlayerShops";
@ -72,12 +176,12 @@ public class MessageConfig extends AbstractConfiguration {
public static String GUI_MANAGE_TITLE = "<green>Shop Manager";
public static String GUI_PLAYER_SETTINGS_TITLE = "<green>Shop Settings Manager";
private static void guiTitles() {
GUI_HOME_TITLE = config.getString("gui.home-title", GUI_HOME_TITLE);
GUI_LIST_PLAYERS_TITLE = config.getString("gui.list-players-title", GUI_LIST_PLAYERS_TITLE);
GUI_LIST_SHOPS_TITLE = config.getString("gui.list-shops-title", GUI_LIST_SHOPS_TITLE);
GUI_LIST_TRANSACTIONS_TITLE = config.getString("gui.list-transactions-title", GUI_LIST_TRANSACTIONS_TITLE);
GUI_MANAGE_TITLE = config.getString("gui.manage-title", GUI_MANAGE_TITLE);
GUI_PLAYER_SETTINGS_TITLE = config.getString("gui.player-settings-title", GUI_PLAYER_SETTINGS_TITLE);
GUI_HOME_TITLE = getString("gui.home-title", GUI_HOME_TITLE);
GUI_LIST_PLAYERS_TITLE = getString("gui.list-players-title", GUI_LIST_PLAYERS_TITLE);
GUI_LIST_SHOPS_TITLE = getString("gui.list-shops-title", GUI_LIST_SHOPS_TITLE);
GUI_LIST_TRANSACTIONS_TITLE = getString("gui.list-transactions-title", GUI_LIST_TRANSACTIONS_TITLE);
GUI_MANAGE_TITLE = getString("gui.manage-title", GUI_MANAGE_TITLE);
GUI_PLAYER_SETTINGS_TITLE = getString("gui.player-settings-title", GUI_PLAYER_SETTINGS_TITLE);
}
}

View File

@ -20,24 +20,24 @@ public class ShopTypeConfig {
}
public void init() {
Config.config.readConfig(ShopTypeConfig.class, this);
Config.readConfig(ShopTypeConfig.class, this);
}
private static void set(String path, Object def) {
Config.config.set(path, def);
Config.set(path, def);
}
private String getString(String path, String def) {
set(defaultPath + path, def);
return Config.config.getNode(configPath + path).getString(
Config.config.getNode(defaultPath + path).getString(def));
return Config.getNode(configPath + path).getString(
Config.getNode(defaultPath + path).getString(def));
}
private List<String> getStringList(String path, List<String> def) {
try {
set(defaultPath + path, def);
return Config.config.getNode(configPath + path).getList(TypeToken.get(String.class),
Config.config.getNode(defaultPath + path).getList(TypeToken.get(String.class), def));
return Config.getNode(configPath + path).getList(TypeToken.get(String.class),
Config.getNode(defaultPath + path).getList(TypeToken.get(String.class), def));
} catch(SerializationException ignore) {}
return new ArrayList<>();
}

View File

@ -4,10 +4,8 @@ import com.alttd.playershops.config.Config;
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@ -51,7 +49,7 @@ public class PlayerSettings {
private void saveToFile() {
try {
File settingsDirectory = new File(Config.configPath, "PlayerSettings");
File settingsDirectory = new File(Config.CONFIG_PATH, "PlayerSettings");
if (!settingsDirectory.exists() && !settingsDirectory.mkdir()) {
return;
}
@ -76,7 +74,7 @@ public class PlayerSettings {
}
public static PlayerSettings loadFromFile(UUID uuid) {
File settingsDirectory = new File(Config.configPath, "PlayerSettings");
File settingsDirectory = new File(Config.CONFIG_PATH, "PlayerSettings");
if (!settingsDirectory.exists() && !settingsDirectory.mkdir()) {
return null; // could not create directory
}

View File

@ -1,5 +1,6 @@
package com.alttd.playershops.storage.database;
import com.alttd.playershops.PlayerShops;
import com.alttd.playershops.config.DatabaseConfig;
import java.sql.Connection;
@ -9,8 +10,10 @@ import java.sql.SQLException;
public class DatabaseConnection implements AutoCloseable {
private Connection connection;
private volatile boolean isActive;
PlayerShops playerShops;
public DatabaseConnection() {
public DatabaseConnection(PlayerShops playerShops) {
this.playerShops = playerShops;
try {
openConnection();
} catch (SQLException e) {
@ -34,9 +37,9 @@ public class DatabaseConnection implements AutoCloseable {
}
connection = DriverManager.getConnection(
"jdbc:mysql://" + DatabaseConfig.IP + ":" + DatabaseConfig.PORT + "/" + DatabaseConfig.DATABASE_NAME +
"jdbc:mysql://" + playerShops.getDatabaseConfig().IP + ":" + playerShops.getDatabaseConfig().PORT + "/" + playerShops.getDatabaseConfig().DATABASE_NAME +
"?autoReconnect=true&useSSL=false",
DatabaseConfig.USERNAME, DatabaseConfig.PASSWORD);
playerShops.getDatabaseConfig().USERNAME, playerShops.getDatabaseConfig().PASSWORD);
}
}

View File

@ -14,21 +14,23 @@ import java.util.List;
public class DatabaseManager {
PlayerShops playerShops;
DatabaseQueue databaseQueue;
private final List<DatabaseConnection> CONNECTIONPOOL = new ArrayList<>();
public DatabaseManager(PlayerShops playerShops) {
this.playerShops = playerShops;
databaseQueue = new DatabaseQueue(this);
int delay = DatabaseConfig.queueDelay * 20;
int delay = playerShops.getDatabaseConfig().queueDelay * 20;
databaseQueue.runTaskTimerAsynchronously(playerShops, delay, delay);
// preload out database connections, TODO FIND A BETTER WAY TO LIMIT THIS
for (int i = 1; i < DatabaseConfig.maxDatabaseConnections; i++) {
for (int i = 1; i < playerShops.getDatabaseConfig().maxDatabaseConnections; i++) {
CONNECTIONPOOL.add(null);
}
}
public DatabaseConnection getDatabaseConnection() {
for (int i = 0; i < DatabaseConfig.maxDatabaseConnections; i++) {
for (int i = 0; i < playerShops.getDatabaseConfig().maxDatabaseConnections; i++) {
DatabaseConnection connection = CONNECTIONPOOL.get(i);
if (connection == null) {
return generateDatabaseConnection(i);
@ -47,7 +49,7 @@ public class DatabaseManager {
}
private DatabaseConnection generateDatabaseConnection(int index) {
DatabaseConnection connection = new DatabaseConnection();
DatabaseConnection connection = new DatabaseConnection(playerShops);
CONNECTIONPOOL.set(index, connection);
return connection;