forked from Upstream/mmocore
Now uses HikariCP instead of jasync. (SQL)
(Requires latest MMOLib)
This commit is contained in:
parent
3af6408708
commit
06dc77a1f6
BIN
lib/MMOLib.jar
BIN
lib/MMOLib.jar
Binary file not shown.
2
pom.xml
2
pom.xml
@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>net.Indyuce</groupId>
|
<groupId>net.Indyuce</groupId>
|
||||||
<artifactId>MMOCore</artifactId>
|
<artifactId>MMOCore</artifactId>
|
||||||
<version>1.4.7</version>
|
<version>1.4.8</version>
|
||||||
<name>MMOCore</name>
|
<name>MMOCore</name>
|
||||||
<description>Offer your players a brand new RPG experience.</description>
|
<description>Offer your players a brand new RPG experience.</description>
|
||||||
|
|
||||||
|
@ -1,54 +1,23 @@
|
|||||||
package net.Indyuce.mmocore.manager.data.mysql;
|
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.DataProvider;
|
||||||
import net.Indyuce.mmocore.manager.data.GuildDataManager;
|
import net.Indyuce.mmocore.manager.data.GuildDataManager;
|
||||||
import net.Indyuce.mmocore.manager.data.PlayerDataManager;
|
import net.Indyuce.mmocore.manager.data.PlayerDataManager;
|
||||||
import net.Indyuce.mmocore.manager.data.yaml.YAMLGuildDataManager;
|
import net.Indyuce.mmocore.manager.data.yaml.YAMLGuildDataManager;
|
||||||
import net.mmogroup.mmolib.sql.QueryResult;
|
import net.mmogroup.mmolib.sql.MMODataSource;
|
||||||
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;
|
|
||||||
|
|
||||||
public class MySQLDataProvider implements DataProvider {
|
public class MySQLDataProvider extends MMODataSource implements DataProvider {
|
||||||
private final MySQLPlayerDataManager playerManager = new MySQLPlayerDataManager(this);
|
private final MySQLPlayerDataManager playerManager = new MySQLPlayerDataManager(this);
|
||||||
private final YAMLGuildDataManager guildManager = new YAMLGuildDataManager();
|
private final YAMLGuildDataManager guildManager = new YAMLGuildDataManager();
|
||||||
private final MySQLConfig config;
|
|
||||||
|
|
||||||
private ConnectionPool<MySQLConnection> connection;
|
@Override
|
||||||
|
public void load() {
|
||||||
public MySQLDataProvider() {
|
executeUpdateAsync("CREATE TABLE IF NOT EXISTS mmocore_playerdata"
|
||||||
config = new MySQLConfig(MMOCore.plugin.getConfig().getConfigurationSection("mysql"));
|
+ "(uuid VARCHAR(36),class_points INT(11) DEFAULT 0,skill_points INT(11)"
|
||||||
connection = MySQLConnectionBuilder.createConnectionPool(config.getConnectionString());
|
+ "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),"
|
||||||
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));");
|
+ "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
|
@Override
|
||||||
@ -60,24 +29,4 @@ public class MySQLDataProvider implements DataProvider {
|
|||||||
public GuildDataManager getGuildManager() {
|
public GuildDataManager getGuildManager() {
|
||||||
return guildManager;
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
package net.Indyuce.mmocore.manager.data.mysql;
|
package net.Indyuce.mmocore.manager.data.mysql;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.stream.Collectors;
|
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.PlayerDataManager;
|
||||||
import net.Indyuce.mmocore.manager.data.mysql.MySQLTableEditor.Table;
|
import net.Indyuce.mmocore.manager.data.mysql.MySQLTableEditor.Table;
|
||||||
import net.mmogroup.mmolib.MMOLib;
|
import net.mmogroup.mmolib.MMOLib;
|
||||||
import net.mmogroup.mmolib.sql.ResultSet;
|
|
||||||
import net.mmogroup.mmolib.sql.RowData;
|
|
||||||
|
|
||||||
public class MySQLPlayerDataManager extends PlayerDataManager {
|
public class MySQLPlayerDataManager extends PlayerDataManager {
|
||||||
private final MySQLDataProvider provider;
|
private final MySQLDataProvider provider;
|
||||||
@ -34,70 +35,70 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void loadData(PlayerData data) {
|
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
|
return;
|
||||||
if (result.size() < 1) {
|
}
|
||||||
data.setLevel(getDefaultData().getLevel());
|
|
||||||
data.setClassPoints(getDefaultData().getClassPoints());
|
data.setClassPoints(result.getInt("class_points"));
|
||||||
data.setSkillPoints(getDefaultData().getSkillPoints());
|
data.setSkillPoints(result.getInt("skill_points"));
|
||||||
data.setAttributePoints(getDefaultData().getAttributePoints());
|
data.setAttributePoints(result.getInt("attribute_points"));
|
||||||
data.setAttributeReallocationPoints(getDefaultData().getAttributeReallocationPoints());
|
data.setAttributeReallocationPoints(result.getInt("attribute_realloc_points"));
|
||||||
data.setExperience(0);
|
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.setMana(data.getStats().getStat(StatType.MAX_MANA));
|
||||||
data.setStamina(data.getStats().getStat(StatType.MAX_STAMINA));
|
data.setStamina(data.getStats().getStat(StatType.MAX_STAMINA));
|
||||||
data.setStellium(data.getStats().getStat(StatType.MAX_STELLIUM));
|
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();
|
data.getQuestData().updateBossBar();
|
||||||
|
if (!isEmpty(result.getString("waypoints")))
|
||||||
return;
|
data.getWaypoints().addAll(getJSONArray(result.getString("waypoints")));
|
||||||
}
|
if (!isEmpty(result.getString("friends")))
|
||||||
|
getJSONArray(result.getString("friends")).forEach(str -> data.getFriends().add(UUID.fromString(str)));
|
||||||
RowData row = result.get(0);
|
if (!isEmpty(result.getString("skills"))) {
|
||||||
|
JsonObject object = MMOLib.plugin.getJson().parse(result.getString("skills"), JsonObject.class);
|
||||||
data.setClassPoints(row.getInt("class_points"));
|
for (Entry<String, JsonElement> entry : object.entrySet())
|
||||||
data.setSkillPoints(row.getInt("skill_points"));
|
data.setSkillLevel(entry.getKey(), entry.getValue().getAsInt());
|
||||||
data.setAttributePoints(row.getInt("attribute_points"));
|
}
|
||||||
data.setAttributeReallocationPoints(row.getInt("attribute_realloc_points"));
|
if (!isEmpty(result.getString("bound_skills")))
|
||||||
data.setLevel(row.getInt("level"));
|
for (String skill : getJSONArray(result.getString("bound_skills")))
|
||||||
data.setExperience(row.getInt("experience"));
|
if (data.getProfess().hasSkill(skill))
|
||||||
if (!isEmpty(row.getString("class")))
|
data.getBoundSkills().add(data.getProfess().getSkill(skill));
|
||||||
data.setClass(MMOCore.plugin.classManager.get(row.getString("class")));
|
if (!isEmpty(result.getString("class_info"))) {
|
||||||
data.setMana(data.getStats().getStat(StatType.MAX_MANA));
|
JsonObject object = MMOLib.plugin.getJson().parse(result.getString("class_info"), JsonObject.class);
|
||||||
data.setStamina(data.getStats().getStat(StatType.MAX_STAMINA));
|
for (Entry<String, JsonElement> entry : object.entrySet()) {
|
||||||
data.setStellium(data.getStats().getStat(StatType.MAX_STELLIUM));
|
try {
|
||||||
if (!isEmpty(row.getString("guild")))
|
PlayerClass profess = MMOCore.plugin.classManager.get(entry.getKey());
|
||||||
data.setGuild(MMOCore.plugin.dataProvider.getGuildManager().stillInGuild(data.getUniqueId(), row.getString("guild")));
|
Validate.notNull(profess, "Could not find class '" + entry.getKey() + "'");
|
||||||
if (!isEmpty(row.getString("attributes")))
|
data.applyClassInfo(profess, new SavedClassInformation(entry.getValue().getAsJsonObject()));
|
||||||
data.getAttributes().load(row.getString("attributes"));
|
} catch (IllegalArgumentException exception) {
|
||||||
if (!isEmpty(row.getString("professions")))
|
MMOCore.log(Level.WARNING, "Could not load class info '" + entry.getKey() + "': " + exception.getMessage());
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (SQLException | InterruptedException | ExecutionException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,23 +181,25 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
|
|||||||
public MySQLOfflinePlayerData(UUID uuid) {
|
public MySQLOfflinePlayerData(UUID uuid) {
|
||||||
super(uuid);
|
super(uuid);
|
||||||
|
|
||||||
ResultSet result = provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + uuid + "';");
|
try {
|
||||||
if (result.size() < 1) {
|
ResultSet result = provider.getResultAsync("SELECT * FROM mmocore_playerdata WHERE uuid = '" + uuid + "';").get();
|
||||||
level = 0;
|
if (!result.first()) {
|
||||||
lastLogin = 0;
|
level = 0;
|
||||||
profess = MMOCore.plugin.classManager.getDefaultClass();
|
lastLogin = 0;
|
||||||
friends = new ArrayList<UUID>();
|
profess = MMOCore.plugin.classManager.getDefaultClass();
|
||||||
} 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
|
|
||||||
friends = new ArrayList<UUID>();
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import java.util.Collection;
|
|||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
@ -20,7 +21,12 @@ public class MySQLTableEditor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void updateData(String key, Object value) {
|
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) {
|
public void updateJSONArray(String key, Collection<String> collection) {
|
||||||
@ -38,8 +44,7 @@ public class MySQLTableEditor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public enum Table {
|
public enum Table {
|
||||||
PLAYERDATA("mmocore_playerdata"),
|
PLAYERDATA("mmocore_playerdata"), GUILDDATA("mmocore_guilddata");
|
||||||
GUILDDATA("mmocore_guilddata");
|
|
||||||
|
|
||||||
final String tableName;
|
final String tableName;
|
||||||
|
|
||||||
|
@ -8,11 +8,15 @@ auto-save:
|
|||||||
# MySQL Support
|
# MySQL Support
|
||||||
mysql:
|
mysql:
|
||||||
enabled: false
|
enabled: false
|
||||||
database: minecraft
|
|
||||||
host: localhost
|
host: localhost
|
||||||
port: 3306
|
port: 3306
|
||||||
|
database: minecraft
|
||||||
user: mmolover
|
user: mmolover
|
||||||
pass: ILoveAria
|
pass: ILoveAria
|
||||||
|
properties:
|
||||||
|
cachePrepStmts: true
|
||||||
|
prepStmtCacheSize: 250
|
||||||
|
prepStmtCacheSqlLimit: 2048
|
||||||
|
|
||||||
# The default values for all playerdata
|
# The default values for all playerdata
|
||||||
# All new players will start with these values
|
# All new players will start with these values
|
||||||
|
Loading…
Reference in New Issue
Block a user