From 271da2523674751e2e8ae4ae946e9c7089f4bc83 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Sun, 6 Mar 2016 20:29:12 +0100 Subject: [PATCH] Update to V1.15 Add config option for max MySQL connections Bugfix for SQLite when used with UUIDs --- pom.xml | 2 +- resources/config.yml | 4 +- .../MinePacks/Database/Config.java | 11 +++-- .../MinePacks/Database/MySQL.java | 2 +- .../MinePacks/Database/SQL.java | 4 +- .../MinePacks/Database/SQLite.java | 42 +++++++++++++++---- 6 files changed, 49 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index ccb459f..1891841 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 at.pcgamingfreaks MinePacks - 1.14 + 1.15 scm:git:git@github.com:GeorgH93/Bukkit_Minepacks.git diff --git a/resources/config.yml b/resources/config.yml index 47a9e51..4141ca0 100644 --- a/resources/config.yml +++ b/resources/config.yml @@ -55,6 +55,8 @@ Database: Database: minecraft User: minecraft Password: minecraft + #The max amount of connections to the database the connection pool will open + MaxConnections: 4 # Tables settings for shared tables when using MySQL - Advanced MySQL Settings # Use these settings only if you know what you are doing!!!! # Do only change this settings if you know what you are doing and have some basic MySQL knowlage!!! @@ -82,4 +84,4 @@ Database: auto-update: true # Config file version. Don't touch it! -Version: 9 \ No newline at end of file +Version: 10 \ No newline at end of file diff --git a/src/at/pcgamingfreaks/MinePacks/Database/Config.java b/src/at/pcgamingfreaks/MinePacks/Database/Config.java index 5976b77..0dadf8a 100644 --- a/src/at/pcgamingfreaks/MinePacks/Database/Config.java +++ b/src/at/pcgamingfreaks/MinePacks/Database/Config.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014-2015 GeorgH93 + * Copyright (C) 2014-2016 GeorgH93 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,11 +27,11 @@ public class Config extends Configuration { - private static final int CONFIG_VERSION = 9; + private static final int CONFIG_VERSION = 10; public Config(JavaPlugin plugin) { - super(plugin, CONFIG_VERSION, 9); + super(plugin, CONFIG_VERSION, 10); } @Override @@ -108,6 +108,11 @@ public String getMySQLPassword() return config.getString("Database.MySQL.Password", ""); } + public int getMySQLMaxConnections() + { + return config.getInt("Database.MySQL.MaxConnections", 4); + } + public String getUserTable() { return config.getString("Database.Tables.User", "backpack_players"); diff --git a/src/at/pcgamingfreaks/MinePacks/Database/MySQL.java b/src/at/pcgamingfreaks/MinePacks/Database/MySQL.java index 7385bc1..7f70679 100644 --- a/src/at/pcgamingfreaks/MinePacks/Database/MySQL.java +++ b/src/at/pcgamingfreaks/MinePacks/Database/MySQL.java @@ -41,7 +41,7 @@ protected HikariConfig getPoolConfig() poolConfig.setUsername(plugin.config.getMySQLUser()); poolConfig.setPassword(plugin.config.getMySQLPassword()); poolConfig.setMinimumIdle(1); - poolConfig.setMaximumPoolSize(8); + poolConfig.setMaximumPoolSize(plugin.config.getMySQLMaxConnections()); return poolConfig; } diff --git a/src/at/pcgamingfreaks/MinePacks/Database/SQL.java b/src/at/pcgamingfreaks/MinePacks/Database/SQL.java index 3d8aa6a..cace200 100644 --- a/src/at/pcgamingfreaks/MinePacks/Database/SQL.java +++ b/src/at/pcgamingfreaks/MinePacks/Database/SQL.java @@ -203,7 +203,7 @@ protected final void buildQuerys() protected abstract void updateQuerysForDialect(); - private void runStatementAsync(final String query, final Object... args) + protected void runStatementAsync(final String query, final Object... args) { Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { @@ -215,7 +215,7 @@ public void run() }); } - private void runStatement(final String query, final Object... args) + protected void runStatement(final String query, final Object... args) { try(Connection connection = getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(query)) { diff --git a/src/at/pcgamingfreaks/MinePacks/Database/SQLite.java b/src/at/pcgamingfreaks/MinePacks/Database/SQLite.java index a399cb3..b459a7b 100644 --- a/src/at/pcgamingfreaks/MinePacks/Database/SQLite.java +++ b/src/at/pcgamingfreaks/MinePacks/Database/SQLite.java @@ -21,6 +21,9 @@ import com.zaxxer.hikari.HikariConfig; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + import java.io.File; import java.sql.Connection; import java.sql.ResultSet; @@ -79,7 +82,14 @@ protected void updateQuerysForDialect() } Query_DeleteOldBackpacks = "DELETE FROM `{TableBackpacks}` WHERE `{FieldBPLastUpdate}` < DATE('now', '-{VarMaxAge} days')"; Query_UpdateBP = Query_UpdateBP.replaceAll("\\{NOW\\}", "DATE('now')"); - Query_UpdatePlayerAdd = Query_UpdatePlayerAdd.replaceAll("INSERT IGNORE INTO", "INSERT OR IGNORE INTO"); + if(useUUIDs) + { + Query_UpdatePlayerAdd = "INSERT OR IGNORE INTO `{TablePlayers}` (`{FieldName}`,`{FieldUUID}`) VALUES (?,?);"; + } + else + { + Query_UpdatePlayerAdd = Query_UpdatePlayerAdd.replaceAll("INSERT IGNORE INTO", "INSERT OR IGNORE INTO"); + } } @Override @@ -87,25 +97,21 @@ protected void checkDB() { try(Connection connection = getConnection(); Statement stmt = connection.createStatement()) { - stmt.execute("CREATE TABLE IF NOT EXISTS `backpack_players` (`player_id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` CHAR(16) NOT NULL UNIQUE" + ((useUUIDs) ? ", `uuid` CHAR(32) UNIQUE" : "") + ");"); + stmt.execute("CREATE TABLE IF NOT EXISTS `backpack_players` (`player_id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` CHAR(16) NOT NULL" + ((useUUIDs) ? " , `uuid` CHAR(32) UNIQUE" : " UNIQUE") + ");"); if(useUUIDs) { try { stmt.execute("ALTER TABLE `backpack_players` ADD COLUMN `uuid` CHAR(32);"); } - catch(SQLException ignored) - { - } + catch(SQLException ignored) {} } stmt.execute("CREATE TABLE IF NOT EXISTS `backpacks` (`owner` INT UNSIGNED PRIMARY KEY, `itemstacks` BLOB, `version` INT DEFAULT 0);"); try { stmt.execute("ALTER TABLE `backpacks` ADD COLUMN `version` INT DEFAULT 0;"); } - catch(SQLException ignored) - { - } + catch(SQLException ignored) {} if(maxAge > 0) { try @@ -124,4 +130,24 @@ protected void checkDB() e.printStackTrace(); } } + + @Override + public void updatePlayer(final Player player) + { + if(useUUIDs) + { + Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + @Override + public void run() + { + runStatement(Query_UpdatePlayerAdd, player.getName(), getPlayerFormattedUUID(player)); + runStatement("UPDATE `" + Table_Players + "` SET `" + Field_Name + "`=? WHERE `" + Field_UUID + "`=?;", player.getName(), getPlayerFormattedUUID(player)); + } + }); + } + else + { + runStatementAsync(Query_UpdatePlayerAdd, player.getName()); + } + } } \ No newline at end of file