Now uses HikariCP instead of jasync. (SQL)

(Requires latest MMOLib)
This commit is contained in:
ASangarin 2020-10-05 16:48:34 +02:00
parent 3af6408708
commit 06dc77a1f6
6 changed files with 102 additions and 141 deletions

Binary file not shown.

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.Indyuce</groupId>
<artifactId>MMOCore</artifactId>
<version>1.4.7</version>
<version>1.4.8</version>
<name>MMOCore</name>
<description>Offer your players a brand new RPG experience.</description>

View File

@ -1,54 +1,23 @@
package net.Indyuce.mmocore.manager.data.mysql;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import org.bukkit.configuration.ConfigurationSection;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.manager.data.DataProvider;
import net.Indyuce.mmocore.manager.data.GuildDataManager;
import net.Indyuce.mmocore.manager.data.PlayerDataManager;
import net.Indyuce.mmocore.manager.data.yaml.YAMLGuildDataManager;
import net.mmogroup.mmolib.sql.QueryResult;
import net.mmogroup.mmolib.sql.ResultSet;
import net.mmogroup.mmolib.sql.mysql.MySQLConnection;
import net.mmogroup.mmolib.sql.mysql.MySQLConnectionBuilder;
import net.mmogroup.mmolib.sql.pool.ConnectionPool;
import net.mmogroup.mmolib.sql.MMODataSource;
public class MySQLDataProvider implements DataProvider {
public class MySQLDataProvider extends MMODataSource implements DataProvider {
private final MySQLPlayerDataManager playerManager = new MySQLPlayerDataManager(this);
private final YAMLGuildDataManager guildManager = new YAMLGuildDataManager();
private final MySQLConfig config;
private ConnectionPool<MySQLConnection> connection;
public MySQLDataProvider() {
config = new MySQLConfig(MMOCore.plugin.getConfig().getConfigurationSection("mysql"));
connection = MySQLConnectionBuilder.createConnectionPool(config.getConnectionString());
executeUpdate("CREATE TABLE IF NOT EXISTS mmocore_playerdata (uuid VARCHAR(36),class_points INT(11) DEFAULT 0,skill_points INT(11) DEFAULT 0,attribute_points INT(11) DEFAULT 0,attribute_realloc_points INT(11) DEFAULT 0,level INT(11) DEFAULT 1,experience INT(11) DEFAULT 0,class VARCHAR(20),guild VARCHAR(20),last_login LONG,attributes JSON,professions JSON,quests JSON,waypoints JSON,friends JSON,skills JSON,bound_skills JSON,class_info JSON,PRIMARY KEY (uuid));");
}
public ResultSet getResult(String sql) {
try {
CompletableFuture<QueryResult> future = connection.sendPreparedStatement(sql);
return future.get().getRows();
} catch (InterruptedException | ExecutionException e) {
MMOCore.log(Level.SEVERE, "MySQL Operation Failed!");
e.printStackTrace();
return null;
}
}
public void executeUpdate(String sql) {
try {
connection.sendPreparedStatement(sql).get();
} catch (InterruptedException | ExecutionException e) {
MMOCore.log(Level.SEVERE, "MySQL Operation Failed!");
e.printStackTrace();
}
@Override
public void load() {
executeUpdateAsync("CREATE TABLE IF NOT EXISTS mmocore_playerdata"
+ "(uuid VARCHAR(36),class_points INT(11) DEFAULT 0,skill_points INT(11)"
+ "DEFAULT 0,attribute_points INT(11) DEFAULT 0,attribute_realloc_points INT(11)"
+ "DEFAULT 0,level INT(11) DEFAULT 1,experience INT(11) DEFAULT 0,class VARCHAR(20),"
+ "guild VARCHAR(20),last_login LONG,attributes JSON,professions JSON,quests JSON,waypoints"
+ "JSON,friends JSON,skills JSON,bound_skills JSON,class_info JSON,PRIMARY KEY (uuid));");
}
@Override
@ -60,24 +29,4 @@ public class MySQLDataProvider implements DataProvider {
public GuildDataManager getGuildManager() {
return guildManager;
}
public class MySQLConfig {
private final String db, host, user, pass;
private final int port;
public MySQLConfig(ConfigurationSection config) {
db = config.getString("database", "minecraft");
host = config.getString("host", "localhost");
port = config.getInt("port", 3306);
user = config.getString("user", "mmolover");
pass = config.getString("pass", "ILoveAria");
}
public String getConnectionString() {
StringBuilder sb = new StringBuilder("jdbc:mysql://");
sb.append(host).append(":").append(port).append("/").append(db)
.append("?user=").append(user).append("&password=").append(pass);
return sb.toString();
}
}
}

View File

@ -1,10 +1,13 @@
package net.Indyuce.mmocore.manager.data.mysql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.stream.Collectors;
@ -22,8 +25,6 @@ import net.Indyuce.mmocore.api.player.stats.StatType;
import net.Indyuce.mmocore.manager.data.PlayerDataManager;
import net.Indyuce.mmocore.manager.data.mysql.MySQLTableEditor.Table;
import net.mmogroup.mmolib.MMOLib;
import net.mmogroup.mmolib.sql.ResultSet;
import net.mmogroup.mmolib.sql.RowData;
public class MySQLPlayerDataManager extends PlayerDataManager {
private final MySQLDataProvider provider;
@ -34,70 +35,70 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
@Override
public void loadData(PlayerData data) {
ResultSet result = provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + data.getUniqueId() + "';");
try {
ResultSet result = provider.getResultAsync("SELECT * FROM mmocore_playerdata WHERE uuid = '" + data.getUniqueId() + "';").get();
if (!result.first()) {
data.setLevel(getDefaultData().getLevel());
data.setClassPoints(getDefaultData().getClassPoints());
data.setSkillPoints(getDefaultData().getSkillPoints());
data.setAttributePoints(getDefaultData().getAttributePoints());
data.setAttributeReallocationPoints(getDefaultData().getAttributeReallocationPoints());
data.setExperience(0);
data.setMana(data.getStats().getStat(StatType.MAX_MANA));
data.setStamina(data.getStats().getStat(StatType.MAX_STAMINA));
data.setStellium(data.getStats().getStat(StatType.MAX_STELLIUM));
data.getQuestData().updateBossBar();
// player data not initialized yet
if (result.size() < 1) {
data.setLevel(getDefaultData().getLevel());
data.setClassPoints(getDefaultData().getClassPoints());
data.setSkillPoints(getDefaultData().getSkillPoints());
data.setAttributePoints(getDefaultData().getAttributePoints());
data.setAttributeReallocationPoints(getDefaultData().getAttributeReallocationPoints());
data.setExperience(0);
return;
}
data.setClassPoints(result.getInt("class_points"));
data.setSkillPoints(result.getInt("skill_points"));
data.setAttributePoints(result.getInt("attribute_points"));
data.setAttributeReallocationPoints(result.getInt("attribute_realloc_points"));
data.setLevel(result.getInt("level"));
data.setExperience(result.getInt("experience"));
if (!isEmpty(result.getString("class")))
data.setClass(MMOCore.plugin.classManager.get(result.getString("class")));
data.setMana(data.getStats().getStat(StatType.MAX_MANA));
data.setStamina(data.getStats().getStat(StatType.MAX_STAMINA));
data.setStellium(data.getStats().getStat(StatType.MAX_STELLIUM));
if (!isEmpty(result.getString("guild")))
data.setGuild(MMOCore.plugin.dataProvider.getGuildManager().stillInGuild(data.getUniqueId(), result.getString("guild")));
if (!isEmpty(result.getString("attributes")))
data.getAttributes().load(result.getString("attributes"));
if (!isEmpty(result.getString("professions")))
data.getCollectionSkills().load(result.getString("professions"));
if (!isEmpty(result.getString("quests")))
data.getQuestData().load(result.getString("quests"));
data.getQuestData().updateBossBar();
return;
}
RowData row = result.get(0);
data.setClassPoints(row.getInt("class_points"));
data.setSkillPoints(row.getInt("skill_points"));
data.setAttributePoints(row.getInt("attribute_points"));
data.setAttributeReallocationPoints(row.getInt("attribute_realloc_points"));
data.setLevel(row.getInt("level"));
data.setExperience(row.getInt("experience"));
if (!isEmpty(row.getString("class")))
data.setClass(MMOCore.plugin.classManager.get(row.getString("class")));
data.setMana(data.getStats().getStat(StatType.MAX_MANA));
data.setStamina(data.getStats().getStat(StatType.MAX_STAMINA));
data.setStellium(data.getStats().getStat(StatType.MAX_STELLIUM));
if (!isEmpty(row.getString("guild")))
data.setGuild(MMOCore.plugin.dataProvider.getGuildManager().stillInGuild(data.getUniqueId(), row.getString("guild")));
if (!isEmpty(row.getString("attributes")))
data.getAttributes().load(row.getString("attributes"));
if (!isEmpty(row.getString("professions")))
data.getCollectionSkills().load(row.getString("professions"));
if (!isEmpty(row.getString("quests")))
data.getQuestData().load(row.getString("quests"));
data.getQuestData().updateBossBar();
if (!isEmpty(row.getString("waypoints")))
data.getWaypoints().addAll(getJSONArray(row.getString("waypoints")));
if (!isEmpty(row.getString("friends")))
getJSONArray(row.getString("friends")).forEach(str -> data.getFriends().add(UUID.fromString(str)));
if (!isEmpty(row.getString("skills"))) {
JsonObject object = MMOLib.plugin.getJson().parse(row.getString("skills"), JsonObject.class);
for (Entry<String, JsonElement> entry : object.entrySet())
data.setSkillLevel(entry.getKey(), entry.getValue().getAsInt());
}
if (!isEmpty(row.getString("bound_skills")))
for (String skill : getJSONArray(row.getString("bound_skills")))
if (data.getProfess().hasSkill(skill))
data.getBoundSkills().add(data.getProfess().getSkill(skill));
if (!isEmpty(row.getString("class_info"))) {
JsonObject object = MMOLib.plugin.getJson().parse(row.getString("class_info"), JsonObject.class);
for (Entry<String, JsonElement> entry : object.entrySet()) {
try {
PlayerClass profess = MMOCore.plugin.classManager.get(entry.getKey());
Validate.notNull(profess, "Could not find class '" + entry.getKey() + "'");
data.applyClassInfo(profess, new SavedClassInformation(entry.getValue().getAsJsonObject()));
} catch (IllegalArgumentException exception) {
MMOCore.log(Level.WARNING, "Could not load class info '" + entry.getKey() + "': " + exception.getMessage());
if (!isEmpty(result.getString("waypoints")))
data.getWaypoints().addAll(getJSONArray(result.getString("waypoints")));
if (!isEmpty(result.getString("friends")))
getJSONArray(result.getString("friends")).forEach(str -> data.getFriends().add(UUID.fromString(str)));
if (!isEmpty(result.getString("skills"))) {
JsonObject object = MMOLib.plugin.getJson().parse(result.getString("skills"), JsonObject.class);
for (Entry<String, JsonElement> entry : object.entrySet())
data.setSkillLevel(entry.getKey(), entry.getValue().getAsInt());
}
if (!isEmpty(result.getString("bound_skills")))
for (String skill : getJSONArray(result.getString("bound_skills")))
if (data.getProfess().hasSkill(skill))
data.getBoundSkills().add(data.getProfess().getSkill(skill));
if (!isEmpty(result.getString("class_info"))) {
JsonObject object = MMOLib.plugin.getJson().parse(result.getString("class_info"), JsonObject.class);
for (Entry<String, JsonElement> entry : object.entrySet()) {
try {
PlayerClass profess = MMOCore.plugin.classManager.get(entry.getKey());
Validate.notNull(profess, "Could not find class '" + entry.getKey() + "'");
data.applyClassInfo(profess, new SavedClassInformation(entry.getValue().getAsJsonObject()));
} catch (IllegalArgumentException exception) {
MMOCore.log(Level.WARNING, "Could not load class info '" + entry.getKey() + "': " + exception.getMessage());
}
}
}
} catch (SQLException | InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
@ -180,23 +181,25 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
public MySQLOfflinePlayerData(UUID uuid) {
super(uuid);
ResultSet result = provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + uuid + "';");
if (result.size() < 1) {
level = 0;
lastLogin = 0;
profess = MMOCore.plugin.classManager.getDefaultClass();
friends = new ArrayList<UUID>();
} else {
RowData row = result.get(0);
level = row.getInt("level");
lastLogin = row.getLong("last_login");
profess = isEmpty(row.getString("class")) ? MMOCore.plugin.classManager.getDefaultClass()
: MMOCore.plugin.classManager.get(row.getString("class"));
if (!isEmpty(row.getString("friends")))
getJSONArray(row.getString("friends")).forEach(str -> friends.add(UUID.fromString(str)));
else
try {
ResultSet result = provider.getResultAsync("SELECT * FROM mmocore_playerdata WHERE uuid = '" + uuid + "';").get();
if (!result.first()) {
level = 0;
lastLogin = 0;
profess = MMOCore.plugin.classManager.getDefaultClass();
friends = new ArrayList<UUID>();
} else {
level = result.getInt("level");
lastLogin = result.getLong("last_login");
profess = isEmpty(result.getString("class")) ? MMOCore.plugin.classManager.getDefaultClass()
: MMOCore.plugin.classManager.get(result.getString("class"));
if (!isEmpty(result.getString("friends")))
getJSONArray(result.getString("friends")).forEach(str -> friends.add(UUID.fromString(str)));
else
friends = new ArrayList<UUID>();
}
} catch (SQLException | InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}

View File

@ -4,6 +4,7 @@ import java.util.Collection;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
@ -20,7 +21,12 @@ public class MySQLTableEditor {
}
public void updateData(String key, Object value) {
((MySQLDataProvider) MMOCore.plugin.dataProvider).executeUpdate("INSERT INTO " + table + "(uuid, " + key + ") VALUES('" + uuid + "', '" + value + "') ON DUPLICATE KEY UPDATE " + key + "='" + value + "';");
try {
((MySQLDataProvider) MMOCore.plugin.dataProvider).executeUpdateAsync("INSERT INTO " + table + "(uuid, " + key
+ ") VALUES('" + uuid + "', '" + value + "') ON DUPLICATE KEY UPDATE " + key + "='" + value + "';").get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
public void updateJSONArray(String key, Collection<String> collection) {
@ -38,8 +44,7 @@ public class MySQLTableEditor {
}
public enum Table {
PLAYERDATA("mmocore_playerdata"),
GUILDDATA("mmocore_guilddata");
PLAYERDATA("mmocore_playerdata"), GUILDDATA("mmocore_guilddata");
final String tableName;

View File

@ -8,11 +8,15 @@ auto-save:
# MySQL Support
mysql:
enabled: false
database: minecraft
host: localhost
port: 3306
database: minecraft
user: mmolover
pass: ILoveAria
properties:
cachePrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
# The default values for all playerdata
# All new players will start with these values