forked from Upstream/mmocore
Refactor SQL API.
This commit is contained in:
parent
a64a2d0ab8
commit
8765cb1495
@ -112,7 +112,7 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
|
|||||||
if (!isEmpty(result.getString("friends")))
|
if (!isEmpty(result.getString("friends")))
|
||||||
MMOCoreUtils.jsonArrayToList(result.getString("friends")).forEach(str -> data.getFriends().add(UUID.fromString(str)));
|
MMOCoreUtils.jsonArrayToList(result.getString("friends")).forEach(str -> data.getFriends().add(UUID.fromString(str)));
|
||||||
if (!isEmpty(result.getString("skills"))) {
|
if (!isEmpty(result.getString("skills"))) {
|
||||||
JsonObject object=new Gson().fromJson(result.getString("skills"), JsonObject.class);
|
JsonObject object = new Gson().fromJson(result.getString("skills"), JsonObject.class);
|
||||||
for (Entry<String, JsonElement> entry : object.entrySet())
|
for (Entry<String, JsonElement> entry : object.entrySet())
|
||||||
data.setSkillLevel(entry.getKey(), entry.getValue().getAsInt());
|
data.setSkillLevel(entry.getKey(), entry.getValue().getAsInt());
|
||||||
}
|
}
|
||||||
@ -175,7 +175,7 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
runnable.runTaskTimerAsynchronously(MMOCore.plugin, 0L, 40L);
|
runnable.runTaskTimerAsynchronously(MMOCore.plugin, 0L, 20L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -183,42 +183,38 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
|
|||||||
|
|
||||||
MySQLTableEditor sql = new MySQLTableEditor(MySQLTableEditor.Table.PLAYERDATA, data.getUniqueId(), provider);
|
MySQLTableEditor sql = new MySQLTableEditor(MySQLTableEditor.Table.PLAYERDATA, data.getUniqueId(), provider);
|
||||||
MMOCore.sqlDebug("Saving data for: '" + data.getUniqueId() + "'...");
|
MMOCore.sqlDebug("Saving data for: '" + data.getUniqueId() + "'...");
|
||||||
|
MySQLRequest request = new MySQLRequest(data.getUniqueId());
|
||||||
sql.updateData("class_points", data.getClassPoints());
|
request.addData("class_points", data.getClassPoints());
|
||||||
sql.updateData("skill_points", data.getSkillPoints());
|
request.addData("skill_points", data.getSkillPoints());
|
||||||
sql.updateData("skill_reallocation_points", data.getSkillReallocationPoints());
|
request.addData("skill_reallocation_points", data.getSkillReallocationPoints());
|
||||||
sql.updateData("attribute_points", data.getAttributePoints());
|
request.addData("attribute_points", data.getAttributePoints());
|
||||||
sql.updateData("attribute_realloc_points", data.getAttributeReallocationPoints());
|
request.addData("attribute_realloc_points", data.getAttributeReallocationPoints());
|
||||||
sql.updateData("skill_tree_reallocation_points", data.getSkillTreeReallocationPoints());
|
request.addJSONArray("waypoints", data.getWaypoints());
|
||||||
sql.updateData("mana", data.getMana());
|
request.addData("skill_tree_reallocation_points", data.getSkillTreeReallocationPoints());
|
||||||
sql.updateData("stellium", data.getStellium());
|
request.addData("mana", data.getMana());
|
||||||
sql.updateData("stamina", data.getStamina());
|
request.addData("stellium", data.getStellium());
|
||||||
sql.updateData("level", data.getLevel());
|
request.addData("stamina", data.getStamina());
|
||||||
sql.updateData("experience", data.getExperience());
|
request.addData("level", data.getLevel());
|
||||||
sql.updateData("class", data.getProfess().getId());
|
request.addData("experience", data.getExperience());
|
||||||
sql.updateData("last_login", data.getLastLogin());
|
request.addData("class", data.getProfess().getId());
|
||||||
sql.updateData("guild", data.hasGuild() ? data.getGuild().getId() : null);
|
request.addData("last_login", data.getLastLogin());
|
||||||
|
request.addData("guild", data.hasGuild() ? data.getGuild().getId() : null);
|
||||||
sql.updateJSONArray("waypoints", data.getWaypoints());
|
request.addJSONArray("waypoints", data.getWaypoints());
|
||||||
sql.updateJSONArray("friends", data.getFriends().stream().map(UUID::toString).collect(Collectors.toList()));
|
request.addJSONArray("friends", data.getFriends().stream().map(UUID::toString).collect(Collectors.toList()));
|
||||||
List<String> boundSkills = new ArrayList<>();
|
List<String> boundSkills = new ArrayList<>();
|
||||||
data.getBoundSkills().forEach(skill -> boundSkills.add(skill.getSkill().getHandler().getId()));
|
data.getBoundSkills().forEach(skill -> boundSkills.add(skill.getSkill().getHandler().getId()));
|
||||||
data.getBoundPassiveSkills().forEach(skill -> boundSkills.add(skill.getTriggeredSkill().getHandler().getId()));
|
data.getBoundPassiveSkills().forEach(skill -> boundSkills.add(skill.getTriggeredSkill().getHandler().getId()));
|
||||||
sql.updateJSONArray("bound_skills", boundSkills);
|
request.addJSONArray("bound_skills", boundSkills);
|
||||||
|
request.addJSONObject("skills", data.mapSkillLevels().entrySet());
|
||||||
sql.updateJSONObject("skills", data.mapSkillLevels().entrySet());
|
request.addJSONObject("times_claimed", data.getItemClaims().entrySet());
|
||||||
sql.updateJSONObject("times_claimed", data.getItemClaims().entrySet());
|
request.addJSONObject("skill_tree_points", data.getSkillTreePoints().entrySet());
|
||||||
sql.updateJSONObject("skill_tree_points", data.getSkillTreePoints().entrySet());
|
request.addJSONObject("skill_tree_levels", data.getNodeLevelsEntrySet());
|
||||||
sql.updateJSONObject("skill_tree_levels", data.getNodeLevelsEntrySet());
|
request.addData("attributes", data.getAttributes().toJsonString());
|
||||||
|
request.addData("professions", data.getCollectionSkills().toJsonString());
|
||||||
|
request.addData("quests", data.getQuestData().toJsonString());
|
||||||
sql.updateData("attributes", data.getAttributes().toJsonString());
|
request.addData("class_info", createClassInfoData(data).toString());
|
||||||
sql.updateData("professions", data.getCollectionSkills().toJsonString());
|
request.addData("is_saved", 1);
|
||||||
sql.updateData("quests", data.getQuestData().toJsonString());
|
sql.updateData(request);
|
||||||
|
|
||||||
sql.updateData("class_info", createClassInfoData(data).toString());
|
|
||||||
Bukkit.getScheduler().runTaskLater(MMOCore.plugin,()->sql.updateData("is_saved", 1),10L);
|
|
||||||
|
|
||||||
MMOCore.sqlDebug("Saved data for: " + data.getUniqueId());
|
MMOCore.sqlDebug("Saved data for: " + data.getUniqueId());
|
||||||
MMOCore.sqlDebug(String.format("{ class: %s, level: %d }", data.getProfess().getId(), data.getLevel()));
|
MMOCore.sqlDebug(String.format("{ class: %s, level: %d }", data.getProfess().getId(), data.getLevel()));
|
||||||
}
|
}
|
||||||
@ -233,8 +229,8 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
|
|||||||
classinfo.addProperty("skill-points", info.getSkillPoints());
|
classinfo.addProperty("skill-points", info.getSkillPoints());
|
||||||
classinfo.addProperty("attribute-points", info.getAttributePoints());
|
classinfo.addProperty("attribute-points", info.getAttributePoints());
|
||||||
classinfo.addProperty("attribute-realloc-points", info.getAttributeReallocationPoints());
|
classinfo.addProperty("attribute-realloc-points", info.getAttributeReallocationPoints());
|
||||||
classinfo.addProperty("skill-reallocation-points",info.getSkillReallocationPoints());
|
classinfo.addProperty("skill-reallocation-points", info.getSkillReallocationPoints());
|
||||||
classinfo.addProperty("skill-tree-reallocation-points",info.getSkillTreeReallocationPoints());
|
classinfo.addProperty("skill-tree-reallocation-points", info.getSkillTreeReallocationPoints());
|
||||||
|
|
||||||
JsonObject skillinfo = new JsonObject();
|
JsonObject skillinfo = new JsonObject();
|
||||||
for (String skill : info.getSkillKeys())
|
for (String skill : info.getSkillKeys())
|
||||||
|
@ -0,0 +1,76 @@
|
|||||||
|
package net.Indyuce.mmocore.manager.data.mysql;
|
||||||
|
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import net.Indyuce.mmocore.MMOCore;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class MySQLRequest {
|
||||||
|
private final Map<String, String> requestMap = new HashMap<>();
|
||||||
|
private final UUID uuid;
|
||||||
|
|
||||||
|
public MySQLRequest(UUID uuid) {
|
||||||
|
this.uuid = uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addData(String key, Object value) {
|
||||||
|
requestMap.put(key, "" + value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String formatCollection(Collection<String> strings, boolean withComma) {
|
||||||
|
StringBuilder values = new StringBuilder();
|
||||||
|
for (String key : strings) {
|
||||||
|
if (withComma)
|
||||||
|
values.append("'");
|
||||||
|
values.append(key);
|
||||||
|
if (withComma)
|
||||||
|
values.append("'");
|
||||||
|
values.append(",");
|
||||||
|
}
|
||||||
|
//Remove the last coma
|
||||||
|
values.deleteCharAt(values.length() - 1);
|
||||||
|
return values.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String formatMap() {
|
||||||
|
StringBuilder values = new StringBuilder();
|
||||||
|
for (String key : requestMap.keySet()) {
|
||||||
|
//values.append("'");
|
||||||
|
values.append(key);
|
||||||
|
//values.append("'");
|
||||||
|
values.append("=");
|
||||||
|
values.append("'");
|
||||||
|
values.append(requestMap.get(key));
|
||||||
|
values.append("'");
|
||||||
|
values.append(",");
|
||||||
|
}
|
||||||
|
//Remove the last coma
|
||||||
|
values.deleteCharAt(values.length() - 1);
|
||||||
|
return values.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String getRequestString() {
|
||||||
|
String result = "(uuid, " + formatCollection(requestMap.keySet(),false)
|
||||||
|
+ ") VALUES('" + uuid + "'," + formatCollection(requestMap.values(),true) + ")" +
|
||||||
|
" ON DUPLICATE KEY UPDATE " + formatMap() + ";";
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void addJSONArray(String key, Collection<String> collection) {
|
||||||
|
JsonArray json = new JsonArray();
|
||||||
|
for (String s : collection)
|
||||||
|
json.add(s);
|
||||||
|
addData(key, json.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addJSONObject(String key, Set<Map.Entry<String, Integer>> collection) {
|
||||||
|
JsonObject json = new JsonObject();
|
||||||
|
for (Map.Entry<String, Integer> entry : collection)
|
||||||
|
json.addProperty(entry.getKey(), entry.getValue());
|
||||||
|
addData(key, json.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -2,6 +2,7 @@ package net.Indyuce.mmocore.manager.data.mysql;
|
|||||||
|
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
import org.denizen.dungeons.requests.RequestDataKey;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
@ -19,24 +20,16 @@ public class MySQLTableEditor {
|
|||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void updateData(String key, Object value) {
|
public void updateData(String key, Object value) {
|
||||||
provider.executeUpdate("INSERT INTO " + table + "(uuid, " + key
|
provider.executeUpdate("INSERT INTO " + table + "(uuid, " + key
|
||||||
+ ") VALUES('" + uuid + "', '" + value + "') ON DUPLICATE KEY UPDATE " + key + "='" + value + "';");
|
+ ") VALUES('" + uuid + "', '" + value + "') ON DUPLICATE KEY UPDATE " + key + "='" + value + "';");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateJSONArray(String key, Collection<String> collection) {
|
public void updateData(MySQLRequest request) {
|
||||||
JsonArray json = new JsonArray();
|
provider.executeUpdate("INSERT INTO " + table + request.getRequestString());
|
||||||
for (String s : collection)
|
|
||||||
json.add(s);
|
|
||||||
updateData(key, json.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateJSONObject(String key, Set<Entry<String, Integer>> collection) {
|
|
||||||
JsonObject json = new JsonObject();
|
|
||||||
for (Entry<String, Integer> entry : collection)
|
|
||||||
json.addProperty(entry.getKey(), entry.getValue());
|
|
||||||
updateData(key, json.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum Table {
|
public enum Table {
|
||||||
PLAYERDATA("mmocore_playerdata"), GUILDDATA("mmocore_guilddata");
|
PLAYERDATA("mmocore_playerdata"), GUILDDATA("mmocore_guilddata");
|
||||||
|
Loading…
Reference in New Issue
Block a user