Added an SQL debug option to config.yml

This commit is contained in:
ASangarin 2020-12-27 20:33:05 +01:00
parent f8b7ac20b3
commit 0662f460f2
4 changed files with 123 additions and 135 deletions

View File

@ -136,6 +136,8 @@ public class MMOCore extends JavaPlugin {
public final MMOLoadManager loadManager = new MMOLoadManager();
public boolean shouldDebugSQL = false;
public void onLoad() {
plugin = this;
@ -174,6 +176,7 @@ public class MMOCore extends JavaPlugin {
if (getConfig().isConfigurationSection("mysql") && getConfig().getBoolean("mysql.enabled"))
dataProvider = new MySQLDataProvider(getConfig());
shouldDebugSQL = getConfig().getBoolean("mysql.debug");
if (getConfig().isConfigurationSection("default-playerdata"))
dataProvider.getDataManager().loadDefaultData(getConfig().getConfigurationSection("default-playerdata"));
@ -462,4 +465,9 @@ public class MMOCore extends JavaPlugin {
public boolean hasEconomy() {
return economy != null && economy.isValid();
}
public static void sqlDebug(String s) {
if(!MMOCore.plugin.shouldDebugSQL) return;
MMOCore.plugin.getLogger().warning("- [SQL Debug] " + s);
}
}

View File

@ -1,21 +1,7 @@
package net.Indyuce.mmocore.manager.data.mysql;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
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;
import org.apache.commons.lang.Validate;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.OfflinePlayerData;
import net.Indyuce.mmocore.api.player.PlayerData;
@ -25,6 +11,17 @@ 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 org.apache.commons.lang.Validate;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class MySQLPlayerDataManager extends PlayerDataManager {
private final MySQLDataProvider provider;
@ -35,94 +32,88 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
@Override
public void loadData(PlayerData data) {
try {
provider.getResultAsync("SELECT * FROM mmocore_playerdata WHERE uuid = '" + data.getUniqueId() + "';",
(result) -> {
provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + data.getUniqueId() + "';", (result) -> {
try {
MMOCore.sqlDebug("Loading data for: '" + data.getUniqueId() + "'...");
if (!result.first()) {
data.setLevel(getDefaultData().getLevel());
data.setClassPoints(getDefaultData().getClassPoints());
data.setSkillPoints(getDefaultData().getSkillPoints());
data.setAttributePoints(getDefaultData().getAttributePoints());
data.setAttributeReallocationPoints(getDefaultData().getAttrReallocPoints());
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();
data.setFullyLoaded();
MMOCore.sqlDebug("Loaded DEFAULT data for: '" + data.getUniqueId() + "' as no saved data was found.");
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();
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 {
if (!result.first()) {
data.setLevel(getDefaultData().getLevel());
data.setClassPoints(getDefaultData().getClassPoints());
data.setSkillPoints(getDefaultData().getSkillPoints());
data.setAttributePoints(getDefaultData().getAttributePoints());
data.setAttributeReallocationPoints(getDefaultData().getAttrReallocPoints());
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();
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();
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());
}
}
}
data.setFullyLoaded();
} catch (SQLException e) {
e.printStackTrace();
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());
}
}).get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
data.setFullyLoaded();
MMOCore.sqlDebug("Loaded saved data for: '" + data.getUniqueId() + "'!");
MMOCore.sqlDebug(String.format("{ class: %s, level: %d }", data.getProfess().getId(), data.getLevel()));
} catch (SQLException e) {
e.printStackTrace();
}
});
}
private boolean isEmpty(String s) {
return s == null || s.equalsIgnoreCase("null") || s.equalsIgnoreCase("{}") || s.equalsIgnoreCase("[]")
|| s.equalsIgnoreCase("");
return s == null || s.equalsIgnoreCase("null") || s.equalsIgnoreCase("{}") || s.equalsIgnoreCase("[]") || s.equalsIgnoreCase("");
}
@Override
public void saveData(PlayerData data) {
MySQLTableEditor sql = new MySQLTableEditor(Table.PLAYERDATA, data.getUniqueId());
MMOCore.sqlDebug("Saving data for: '" + data.getUniqueId() + "'...");
sql.updateData("class_points", data.getClassPoints());
sql.updateData("skill_points", data.getSkillPoints());
@ -135,10 +126,8 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
sql.updateData("guild", data.hasGuild() ? data.getGuild().getId() : null);
sql.updateJSONArray("waypoints", data.getWaypoints());
sql.updateJSONArray("friends",
data.getFriends().stream().map(UUID::toString).collect(Collectors.toList()));
sql.updateJSONArray("bound_skills",
data.getBoundSkills().stream().map(skill -> skill.getSkill().getId()).collect(Collectors.toList()));
sql.updateJSONArray("friends", data.getFriends().stream().map(UUID::toString).collect(Collectors.toList()));
sql.updateJSONArray("bound_skills", data.getBoundSkills().stream().map(skill -> skill.getSkill().getId()).collect(Collectors.toList()));
sql.updateJSONObject("skills", data.mapSkillLevels().entrySet());
@ -147,6 +136,9 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
sql.updateData("quests", data.getQuestData().toJsonString());
sql.updateData("class_info", createClassInfoData(data).toString());
MMOCore.sqlDebug("Saved data for: " + data.getUniqueId());
MMOCore.sqlDebug(String.format("{ class: %s, level: %d }", data.getProfess().getId(), data.getLevel()));
}
private JsonObject createClassInfoData(PlayerData data) {
@ -192,39 +184,34 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
public MySQLOfflinePlayerData(UUID uuid) {
super(uuid);
try {
provider.getResultAsync("SELECT * FROM mmocore_playerdata WHERE uuid = '" + uuid + "';", (result) -> {
try {
if (!result.first()) {
level = 0;
lastLogin = 0;
profess = MMOCore.plugin.classManager.getDefaultClass();
friends = new ArrayList<>();
} 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<>();
}
} catch (SQLException e) {
e.printStackTrace();
provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + uuid + "';", (result) -> {
try {
MMOCore.sqlDebug("Loading OFFLINE data for '" + uuid + "'.");
if (!result.first()) {
level = 0;
lastLogin = 0;
profess = MMOCore.plugin.classManager.getDefaultClass();
friends = new ArrayList<>();
MMOCore.sqlDebug("Default OFFLINE data loaded.");
} 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<>();
MMOCore.sqlDebug("Saved OFFLINE data loaded.");
}
}).get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
});
}
@Override
public void removeFriend(UUID uuid) {
friends.remove(uuid);
new MySQLTableEditor(Table.PLAYERDATA, uuid).updateDataAsync("friends",
friends.stream().map(UUID::toString).collect(Collectors.toList()));
new MySQLTableEditor(Table.PLAYERDATA, uuid).updateData("friends", friends.stream().map(UUID::toString).collect(Collectors.toList()));
}
@Override
@ -250,7 +237,7 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
@Override
public void remove(PlayerData data) {
if(data.isFullyLoaded()) saveData(data);
if (data.isFullyLoaded()) saveData(data);
remove(data.getUniqueId());
}
}

View File

@ -4,7 +4,6 @@ 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;
@ -25,15 +24,6 @@ public class MySQLTableEditor {
+ ") VALUES('" + uuid + "', '" + value + "') ON DUPLICATE KEY UPDATE " + key + "='" + value + "';");
}
public void updateDataAsync(String key, Object 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) {
JsonArray json = new JsonArray();
for (String s : collection)

View File

@ -8,7 +8,7 @@
# a Spigot Plugin by Team Requiem
# DO NOT TOUCH
config-version: 4
config-version: 5
# Auto-Save feature automatically saves playerdata
# (class, level, etc.) and guild data
@ -29,6 +29,9 @@ mysql:
cachePrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
# Will verbose to the console whenever
# data is saved/loaded from the SQL database.
debug: false
# The default values for all playerdata
# All new players will start with these values