From d1bb2406de345ea07dce553aef58f33f80a54653 Mon Sep 17 00:00:00 2001 From: Ka0rX Date: Sun, 19 Jun 2022 12:02:06 +0200 Subject: [PATCH] SkillTree debug and support for SQL --- .../mmocore/api/player/PlayerData.java | 12 + .../Indyuce/mmocore/gui/SkillTreeViewer.java | 2 +- .../mmocore/manager/SkillTreeManager.java | 19 +- .../manager/data/mysql/MySQLDataProvider.java | 3 +- .../data/mysql/MySQLPlayerDataManager.java | 408 +++++++++--------- .../data/yaml/YAMLPlayerDataManager.java | 8 +- .../Indyuce/mmocore/tree/SkillTreeNode.java | 15 +- .../mmocore/tree/skilltree/SkillTree.java | 58 ++- 8 files changed, 287 insertions(+), 238 deletions(-) diff --git a/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java b/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java index f5cfb9a9..c10a803a 100644 --- a/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java +++ b/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java @@ -62,6 +62,7 @@ import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; import java.util.*; import java.util.logging.Level; +import java.util.logging.Logger; import java.util.stream.Collectors; @@ -220,6 +221,15 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc } + + public Set> getNodeLevelsEntrySet() { + HashMap nodeLevelsString=new HashMap<>(); + for(SkillTreeNode node:nodeLevels.keySet()) { + nodeLevelsString.put(node.getFullId(),nodeLevels.get(node)); + } + return nodeLevelsString.entrySet(); + } + public void removeModifiersFrom(SkillTree skillTree) { for (SkillTreeNode node : skillTree.getNodes()) { for (int i = 0; i < node.getMaxLevel(); i++) { @@ -331,6 +341,8 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc } public int getNodeLevel(SkillTreeNode node) { + + return nodeLevels.get(node); } diff --git a/src/main/java/net/Indyuce/mmocore/gui/SkillTreeViewer.java b/src/main/java/net/Indyuce/mmocore/gui/SkillTreeViewer.java index ebcaa83e..1df83887 100644 --- a/src/main/java/net/Indyuce/mmocore/gui/SkillTreeViewer.java +++ b/src/main/java/net/Indyuce/mmocore/gui/SkillTreeViewer.java @@ -332,7 +332,7 @@ public class SkillTreeViewer extends EditableInventory { int xOffset=offset%9-middleSlot%9; int yOffset=offset/9-middleSlot/9; x += xOffset; - y += yOffset; + y += yOffset-1; open(); event.setCancelled(true); return; diff --git a/src/main/java/net/Indyuce/mmocore/manager/SkillTreeManager.java b/src/main/java/net/Indyuce/mmocore/manager/SkillTreeManager.java index 7dfe3af5..c69c3008 100644 --- a/src/main/java/net/Indyuce/mmocore/manager/SkillTreeManager.java +++ b/src/main/java/net/Indyuce/mmocore/manager/SkillTreeManager.java @@ -7,36 +7,39 @@ import net.Indyuce.mmocore.tree.skilltree.SkillTree; import org.bukkit.configuration.file.YamlConfiguration; import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; public class SkillTreeManager extends MMOCoreRegister { - private final ArrayList skillTreeNodes = new ArrayList<>(); + private final HashMap skillTreeNodes = new HashMap<>(); @Override public void register(SkillTree tree){ super.register(tree); - tree.getNodes().forEach((node)->skillTreeNodes.add(node)); + tree.getNodes().forEach((node)->skillTreeNodes.put(node.getFullId(),node)); } public boolean has(int index) { return index>=0 &&index getRootNodes() { - return skillTreeNodes.stream().filter(treeNode -> treeNode.getSoftParents().size()==0).collect(Collectors.toList()); + return skillTreeNodes.values().stream().filter(treeNode -> treeNode.getSoftParents().size()==0).collect(Collectors.toList()); } - public ArrayList getAllNodes() { - return skillTreeNodes; + public Collection getAllNodes() { + return skillTreeNodes.values(); } public SkillTree get(int index) { diff --git a/src/main/java/net/Indyuce/mmocore/manager/data/mysql/MySQLDataProvider.java b/src/main/java/net/Indyuce/mmocore/manager/data/mysql/MySQLDataProvider.java index e720ab8f..72e4364a 100644 --- a/src/main/java/net/Indyuce/mmocore/manager/data/mysql/MySQLDataProvider.java +++ b/src/main/java/net/Indyuce/mmocore/manager/data/mysql/MySQLDataProvider.java @@ -42,7 +42,8 @@ getResultAsync("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" + "experience INT(11) DEFAULT 0,class VARCHAR(20),guild VARCHAR(20),last_login LONG," + "attributes LONGTEXT,professions LONGTEXT,times_claimed LONGTEXT,quests LONGTEXT," + "waypoints LONGTEXT,friends LONGTEXT,skills LONGTEXT,bound_skills LONGTEXT," - + "class_info LONGTEXT,PRIMARY KEY (uuid));"); + + "class_info LONGTEXT,skill_tree_reallocation_points INT(11) DEFAULT 0,skill_tree_points" + + " LONGTEXT,skill_tree_nodes_level LONGTEXT,PRIMARY KEY (uuid));"); } @Override diff --git a/src/main/java/net/Indyuce/mmocore/manager/data/mysql/MySQLPlayerDataManager.java b/src/main/java/net/Indyuce/mmocore/manager/data/mysql/MySQLPlayerDataManager.java index 9183299a..33c41266 100644 --- a/src/main/java/net/Indyuce/mmocore/manager/data/mysql/MySQLPlayerDataManager.java +++ b/src/main/java/net/Indyuce/mmocore/manager/data/mysql/MySQLPlayerDataManager.java @@ -23,231 +23,247 @@ import java.util.logging.Level; import java.util.stream.Collectors; public class MySQLPlayerDataManager extends PlayerDataManager { - private final MySQLDataProvider provider; + private final MySQLDataProvider provider; - public MySQLPlayerDataManager(MySQLDataProvider provider) { - this.provider = provider; - } + public MySQLPlayerDataManager(MySQLDataProvider provider) { + this.provider = provider; + } - //TODO SkillTree pour SQL - @Override - public void loadData(PlayerData data) { - provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + data.getUniqueId() + "';", (result) -> { - try { - MMOCore.sqlDebug("Loading data for: '" + data.getUniqueId() + "'..."); + @Override + public void loadData(PlayerData data) { + provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + data.getUniqueId() + "';", (result) -> { + try { + MMOCore.sqlDebug("Loading data for: '" + data.getUniqueId() + "'..."); - if (!result.next()) { - data.setLevel(getDefaultData().getLevel()); - data.setClassPoints(getDefaultData().getClassPoints()); - data.setSkillPoints(getDefaultData().getSkillPoints()); - data.setAttributePoints(getDefaultData().getAttributePoints()); - data.setAttributeReallocationPoints(getDefaultData().getAttrReallocPoints()); - data.setExperience(0); - data.getQuestData().updateBossBar(); + if (!result.next()) { + data.setLevel(getDefaultData().getLevel()); + data.setClassPoints(getDefaultData().getClassPoints()); + data.setSkillPoints(getDefaultData().getSkillPoints()); + data.setAttributePoints(getDefaultData().getAttributePoints()); + data.setAttributeReallocationPoints(getDefaultData().getAttrReallocPoints()); + data.setExperience(0); + data.getQuestData().updateBossBar(); - if (!data.hasUsedTemporaryData()) { - 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 (!data.hasUsedTemporaryData()) { + 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.setFullyLoaded(); - MMOCore.sqlDebug("Loaded DEFAULT data for: '" + data.getUniqueId() + "' as no saved data was found."); - return; - } + 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.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"))); - if (!isEmpty(result.getString("times_claimed"))) { - JsonObject json = new JsonParser().parse(result.getString("times_claimed")).getAsJsonObject(); - json.entrySet().forEach(entry -> data.getItemClaims().put(entry.getKey(), entry.getValue().getAsInt())); - } + if (!isEmpty(result.getString("times_claimed"))) { + JsonObject json = new JsonParser().parse(result.getString("times_claimed")).getAsJsonObject(); + json.entrySet().forEach(entry -> data.getItemClaims().put(entry.getKey(), entry.getValue().getAsInt())); + } - if (!data.hasUsedTemporaryData()) { - 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 (!data.hasUsedTemporaryData()) { + 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"))) { - Guild guild = provider.getGuildManager().getGuild(result.getString("guild")); - data.setGuild(guild.getMembers().has(data.getUniqueId()) ? guild : null); - } - 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 = MythicLib.plugin.getJson().parse(result.getString("skills"), JsonObject.class); - for (Entry 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 = MythicLib.plugin.getJson().parse(result.getString("class_info"), JsonObject.class); - for (Entry 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("guild"))) { + Guild guild = provider.getGuildManager().getGuild(result.getString("guild")); + data.setGuild(guild.getMembers().has(data.getUniqueId()) ? guild : null); + } + 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 = MythicLib.plugin.getJson().parse(result.getString("skills"), JsonObject.class); + for (Entry 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 = MythicLib.plugin.getJson().parse(result.getString("class_info"), JsonObject.class); + for (Entry 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()); + } + } + } + //We load the skill trees + data.setSkillTreeReallocationPoints(result.getInt("skill_tree_reallocation_points")); - 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(); - } - }); - } + JsonObject object = MythicLib.plugin.getJson().parse(result.getString("skill_tree_points"), JsonObject.class); + for (Entry entry : object.entrySet()) { + data.setSkillTreePoints(entry.getKey(), entry.getValue().getAsInt()); + } + object = MythicLib.plugin.getJson().parse(result.getString("skill_tree_nodes_level"), JsonObject.class); + for (Entry entry : object.entrySet()) { + data.setNodeLevel(MMOCore.plugin.skillTreeManager.getNode(entry.getKey()),entry.getValue().getAsInt()); + } - private boolean isEmpty(String s) { - return s == null || s.equalsIgnoreCase("null") || s.equalsIgnoreCase("{}") || s.equalsIgnoreCase("[]") || s.equalsIgnoreCase(""); - } + 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(); + } + }); + } - @Override - public void saveData(PlayerData data) { - MySQLTableEditor sql = new MySQLTableEditor(Table.PLAYERDATA, data.getUniqueId()); - MMOCore.sqlDebug("Saving data for: '" + data.getUniqueId() + "'..."); + private boolean isEmpty(String s) { + return s == null || s.equalsIgnoreCase("null") || s.equalsIgnoreCase("{}") || s.equalsIgnoreCase("[]") || s.equalsIgnoreCase(""); + } - sql.updateData("class_points", data.getClassPoints()); - sql.updateData("skill_points", data.getSkillPoints()); - sql.updateData("attribute_points", data.getAttributePoints()); - sql.updateData("attribute_realloc_points", data.getAttributeReallocationPoints()); - sql.updateData("level", data.getLevel()); - sql.updateData("experience", data.getExperience()); - sql.updateData("class", data.getProfess().getId()); - sql.updateData("last_login", data.getLastLogin()); - sql.updateData("guild", data.hasGuild() ? data.getGuild().getId() : null); + @Override + public void saveData(PlayerData data) { + MySQLTableEditor sql = new MySQLTableEditor(Table.PLAYERDATA, data.getUniqueId()); + MMOCore.sqlDebug("Saving data for: '" + data.getUniqueId() + "'..."); - 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().getHandler().getId()).collect(Collectors.toList())); + sql.updateData("class_points", data.getClassPoints()); + sql.updateData("skill_points", data.getSkillPoints()); + sql.updateData("attribute_points", data.getAttributePoints()); + sql.updateData("attribute_realloc_points", data.getAttributeReallocationPoints()); + sql.updateData("level", data.getLevel()); + sql.updateData("experience", data.getExperience()); + sql.updateData("class", data.getProfess().getId()); + sql.updateData("last_login", data.getLastLogin()); + sql.updateData("guild", data.hasGuild() ? data.getGuild().getId() : null); - sql.updateJSONObject("skills", data.mapSkillLevels().entrySet()); - sql.updateJSONObject("times_claimed", data.getItemClaims().entrySet()); + 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().getHandler().getId()).collect(Collectors.toList())); - sql.updateData("attributes", data.getAttributes().toJsonString()); - sql.updateData("professions", data.getCollectionSkills().toJsonString()); - sql.updateData("quests", data.getQuestData().toJsonString()); + sql.updateJSONObject("skills", data.mapSkillLevels().entrySet()); + sql.updateJSONObject("times_claimed", data.getItemClaims().entrySet()); - sql.updateData("class_info", createClassInfoData(data).toString()); + sql.updateData("attributes", data.getAttributes().toJsonString()); + sql.updateData("professions", data.getCollectionSkills().toJsonString()); + sql.updateData("quests", data.getQuestData().toJsonString()); - MMOCore.sqlDebug("Saved data for: " + data.getUniqueId()); - MMOCore.sqlDebug(String.format("{ class: %s, level: %d }", data.getProfess().getId(), data.getLevel())); - } + sql.updateData("class_info", createClassInfoData(data).toString()); - private JsonObject createClassInfoData(PlayerData data) { - JsonObject json = new JsonObject(); - for (String c : data.getSavedClasses()) { - SavedClassInformation info = data.getClassInfo(c); - JsonObject classinfo = new JsonObject(); - classinfo.addProperty("level", info.getLevel()); - classinfo.addProperty("experience", info.getExperience()); - classinfo.addProperty("skill-points", info.getSkillPoints()); - classinfo.addProperty("attribute-points", info.getAttributePoints()); - classinfo.addProperty("attribute-realloc-points", info.getAttributeReallocationPoints()); - JsonObject skillinfo = new JsonObject(); - for (String skill : info.getSkillKeys()) - skillinfo.addProperty(skill, info.getSkillLevel(skill)); - classinfo.add("skill", skillinfo); - JsonObject attributeinfo = new JsonObject(); - for (String attribute : info.getAttributeKeys()) - attributeinfo.addProperty(attribute, info.getAttributeLevel(attribute)); - classinfo.add("attribute", attributeinfo); + //Load skill tree on + sql.updateData("skill_tree_reallocation_points", data.getSkillTreeReallocationPoints()); + sql.updateJSONObject("skill_tree_points", data.getSkillTreePoints().entrySet()); + sql.updateJSONObject("skill_tree_nodes_level", data.getNodeLevelsEntrySet()); - json.add(c, classinfo); - } - return json; - } + MMOCore.sqlDebug("Saved data for: " + data.getUniqueId()); + MMOCore.sqlDebug(String.format("{ class: %s, level: %d }", data.getProfess().getId(), data.getLevel())); + } - @NotNull - @Override - public OfflinePlayerData getOffline(UUID uuid) { - return isLoaded(uuid) ? get(uuid) : new MySQLOfflinePlayerData(uuid); - } + private JsonObject createClassInfoData(PlayerData data) { + JsonObject json = new JsonObject(); + for (String c : data.getSavedClasses()) { + SavedClassInformation info = data.getClassInfo(c); + JsonObject classinfo = new JsonObject(); + classinfo.addProperty("level", info.getLevel()); + classinfo.addProperty("experience", info.getExperience()); + classinfo.addProperty("skill-points", info.getSkillPoints()); + classinfo.addProperty("attribute-points", info.getAttributePoints()); + classinfo.addProperty("attribute-realloc-points", info.getAttributeReallocationPoints()); + JsonObject skillinfo = new JsonObject(); + for (String skill : info.getSkillKeys()) + skillinfo.addProperty(skill, info.getSkillLevel(skill)); + classinfo.add("skill", skillinfo); + JsonObject attributeinfo = new JsonObject(); + for (String attribute : info.getAttributeKeys()) + attributeinfo.addProperty(attribute, info.getAttributeLevel(attribute)); + classinfo.add("attribute", attributeinfo); - private Collection getJSONArray(String json) { - return new ArrayList<>(Arrays.asList(MythicLib.plugin.getJson().parse(json, String[].class))); - } + json.add(c, classinfo); + } - public class MySQLOfflinePlayerData extends OfflinePlayerData { - private int level; - private long lastLogin; - private PlayerClass profess; - private List friends; + return json; + } - public MySQLOfflinePlayerData(UUID uuid) { - super(uuid); + @NotNull + @Override + public OfflinePlayerData getOffline(UUID uuid) { + return isLoaded(uuid) ? get(uuid) : new MySQLOfflinePlayerData(uuid); + } - provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + uuid + "';", (result) -> { - try { - MMOCore.sqlDebug("Loading OFFLINE data for '" + uuid + "'."); - if (!result.next()) { - 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."); - } - } catch (SQLException e) { - e.printStackTrace(); - } - }); - } + private Collection getJSONArray(String json) { + return new ArrayList<>(Arrays.asList(MythicLib.plugin.getJson().parse(json, String[].class))); + } - @Override - public void removeFriend(UUID uuid) { - friends.remove(uuid); - new MySQLTableEditor(Table.PLAYERDATA, uuid).updateData("friends", friends.stream().map(UUID::toString).collect(Collectors.toList())); - } + public class MySQLOfflinePlayerData extends OfflinePlayerData { + private int level; + private long lastLogin; + private PlayerClass profess; + private List friends; - @Override - public boolean hasFriend(UUID uuid) { - return friends.contains(uuid); - } + public MySQLOfflinePlayerData(UUID uuid) { + super(uuid); - @Override - public PlayerClass getProfess() { - return profess; - } + provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + uuid + "';", (result) -> { + try { + MMOCore.sqlDebug("Loading OFFLINE data for '" + uuid + "'."); + if (!result.next()) { + 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."); + } + } catch (SQLException e) { + e.printStackTrace(); + } + }); + } - @Override - public int getLevel() { - return level; - } + @Override + public void removeFriend(UUID uuid) { + friends.remove(uuid); + new MySQLTableEditor(Table.PLAYERDATA, uuid).updateData("friends", friends.stream().map(UUID::toString).collect(Collectors.toList())); + } - @Override - public long getLastLogin() { - return lastLogin; - } - } + @Override + public boolean hasFriend(UUID uuid) { + return friends.contains(uuid); + } + + @Override + public PlayerClass getProfess() { + return profess; + } + + @Override + public int getLevel() { + return level; + } + + @Override + public long getLastLogin() { + return lastLogin; + } + } } diff --git a/src/main/java/net/Indyuce/mmocore/manager/data/yaml/YAMLPlayerDataManager.java b/src/main/java/net/Indyuce/mmocore/manager/data/yaml/YAMLPlayerDataManager.java index 1a74fb23..7872d50e 100644 --- a/src/main/java/net/Indyuce/mmocore/manager/data/yaml/YAMLPlayerDataManager.java +++ b/src/main/java/net/Indyuce/mmocore/manager/data/yaml/YAMLPlayerDataManager.java @@ -77,8 +77,7 @@ public class YAMLPlayerDataManager extends PlayerDataManager { //Load skill tree nodes for (SkillTreeNode node : MMOCore.plugin.skillTreeManager.getAllNodes()) { - String path = "skill-tree-nodes." + node.getTree().getId() + "." + node.getId(); - + String path = "skill-tree-nodes." + node.getFullId(); if (config.contains(path)) data.setNodeLevel(node, config.getInt(path)); else { @@ -92,7 +91,6 @@ public class YAMLPlayerDataManager extends PlayerDataManager { if (section != null) { for (String key : section.getKeys(false)) data.setSkillTreePoints(key, section.getInt(key)); - } //Put 0 to the rest of the values if nothing has been given List skillTreeIds = MMOCore.plugin.skillTreeManager.getAll().stream().map(SkillTree::getId).collect(Collectors.toList()); @@ -102,7 +100,7 @@ public class YAMLPlayerDataManager extends PlayerDataManager { data.setSkillTreePoints(treeId, 0); } - data.setSkillTreeReallocationPoints(config.getInt("skill-tree-reallocation-points",0)); + data.setSkillTreeReallocationPoints(config.getInt("skill-tree-reallocation-points", 0)); // Load class slots, use try so the player can log in. if (config.contains("class-info")) @@ -149,7 +147,7 @@ public class YAMLPlayerDataManager extends PlayerDataManager { for (String treeId : data.getSkillTreePoints().keySet()) { config.set("skill-tree-points." + treeId, data.getSkillTreePoint(treeId)); } - config.set("skill-tree-reallocation-points",data.getSkillTreeReallocationPoints()); + config.set("skill-tree-reallocation-points", data.getSkillTreeReallocationPoints()); List boundSkills = new ArrayList<>(); data.getBoundSkills().forEach(skill -> boundSkills.add(skill.getSkill().getHandler().getId())); diff --git a/src/main/java/net/Indyuce/mmocore/tree/SkillTreeNode.java b/src/main/java/net/Indyuce/mmocore/tree/SkillTreeNode.java index 6e9a386e..e439b904 100644 --- a/src/main/java/net/Indyuce/mmocore/tree/SkillTreeNode.java +++ b/src/main/java/net/Indyuce/mmocore/tree/SkillTreeNode.java @@ -52,6 +52,7 @@ public class SkillTreeNode implements Unlockable { public SkillTreeNode(SkillTree tree, ConfigurationSection config) { + Validate.notNull(config, "Config cannot be null"); this.id = config.getName(); this.tree = tree; @@ -106,7 +107,7 @@ public class SkillTreeNode implements Unlockable { } } catch (NumberFormatException e) { - MMOCore.plugin.getLogger().log(Level.WARNING, "Couldn't load modifiers for the skill node " + tree.getId() + "." + id+ " :Problem with the Number Format."); + MMOCore.plugin.getLogger().log(Level.WARNING, "Couldn't load modifiers for the skill node " + tree.getId() + "." + id + " :Problem with the Number Format."); } } @@ -114,9 +115,12 @@ public class SkillTreeNode implements Unlockable { maxLevel = config.contains("max-level") ? config.getInt("max-level") : 1; maxChildren = config.contains("max-children") ? config.getInt("max-children") : 1; //If coordinates are precised adn we are not wiht an automaticTree we set them up - if ((!(tree instanceof AutomaticSkillTree)) && config.contains("coordinates.x") && config.contains("coordinates.y")) { + if ((!(tree instanceof AutomaticSkillTree))) { + Validate.isTrue(config.contains("coordinates.x") && config.contains("coordinates.y"),"No coordinates specified"); coordinates = new IntegerCoordinates(config.getInt("coordinates.x"), config.getInt("coordinates.y")); } + } + /* if (config.contains("modifiers")) { for (String key : config.getConfigurationSection("modifiers").getKeys(false)) { @@ -126,8 +130,6 @@ public class SkillTreeNode implements Unlockable { } */ - } - public SkillTree getTree() { return tree; @@ -195,6 +197,11 @@ public class SkillTreeNode implements Unlockable { return id; } + + public String getFullId() { + return tree.getId() + "." + id; + } + public String getName() { return MythicLib.plugin.parseColors(name); } diff --git a/src/main/java/net/Indyuce/mmocore/tree/skilltree/SkillTree.java b/src/main/java/net/Indyuce/mmocore/tree/skilltree/SkillTree.java index 01a6a013..823b2cc1 100644 --- a/src/main/java/net/Indyuce/mmocore/tree/skilltree/SkillTree.java +++ b/src/main/java/net/Indyuce/mmocore/tree/skilltree/SkillTree.java @@ -55,15 +55,24 @@ public abstract class SkillTree extends PostLoadObject implements RegisterObject protected final List roots = new ArrayList<>(); public SkillTree(ConfigurationSection config) { + super(config); this.id = Objects.requireNonNull(config.getString("id"), "Could not find skill tree id"); this.name = MythicLib.plugin.parseColors(Objects.requireNonNull(config.getString("name"), "Could not find skill tree name")); Objects.requireNonNull(config.getStringList("lore"), "Could not find skill tree lore").forEach(str -> lore.add(MythicLib.plugin.parseColors(str))); this.item = Material.valueOf(MMOCoreUtils.toEnumName(Objects.requireNonNull(config.getString("item")))); Validate.isTrue(config.isConfigurationSection("nodes"), "Could not find any nodes in the tree"); + + for (String key : config.getConfigurationSection("nodes").getKeys(false)) { - SkillTreeNode node = new SkillTreeNode(this, config.getConfigurationSection("nodes." + key)); - nodes.put(node.getId(), node); + try { + + SkillTreeNode node = new SkillTreeNode(this, config.getConfigurationSection("nodes." + key)); + nodes.put(node.getId(), node); + + } catch (Exception e) { + MMOCore.plugin.getLogger().log(Level.SEVERE,"Couldn't load skill tree node "+id+"."+key+": "+e.getMessage()); + } } try { if (config.contains("paths")) { @@ -141,24 +150,28 @@ public abstract class SkillTree extends PostLoadObject implements RegisterObject } public static SkillTree loadSkillTree(ConfigurationSection config) { - String string = config.getString("type"); - - Validate.isTrue(string.equals("automatic") || string.equals("linked") || string.equals("custom"), "You must precise the type of the skill tree in the yml!" + - "\nAllowed values: 'automatic','linked','custom'"); SkillTree skillTree = null; - if (string.equals("automatic")) { - skillTree = new AutomaticSkillTree(config); - skillTree.postLoad(); - } - if (string.equals("linked")) { - skillTree = new LinkedSkillTree(config); - skillTree.postLoad(); - } - if (string.equals("custom")) { - skillTree = new CustomSkillTree(config); - skillTree.postLoad(); - } + try { + String string = config.getString("type"); + + Validate.isTrue(string.equals("automatic") || string.equals("linked") || string.equals("custom"), "You must precise the type of the skill tree in the yml!" + + "\nAllowed values: 'automatic','linked','custom'"); + if (string.equals("automatic")) { + skillTree = new AutomaticSkillTree(config); + skillTree.postLoad(); + } + if (string.equals("linked")) { + skillTree = new LinkedSkillTree(config); + skillTree.postLoad(); + } + if (string.equals("custom")) { + skillTree = new CustomSkillTree(config); + skillTree.postLoad(); + } + } catch (Exception e) { + MMOCore.plugin.getLogger().log(Level.SEVERE, "Couldn't load skill tree " + config.getName() + ": " + e.getMessage()); + } return skillTree; } @@ -189,10 +202,10 @@ public abstract class SkillTree extends PostLoadObject implements RegisterObject } else if (playerData.getNodeLevel(node) == 0 && node.isRoot()) { playerData.setNodeState(node, NodeState.UNLOCKABLE); } else { - boolean isUnlockableFromStrongParent = node.getStrongParents().size()==0?true:true; - boolean isUnlockableFromSoftParent = node.getSoftParents().size()==0?true:false; - boolean isFullyLockedFromStrongParent = node.getStrongParents().size()==0?false:false; - boolean isFullyLockedFromSoftParent = node.getSoftParents().size()==0?false:true; + boolean isUnlockableFromStrongParent = node.getStrongParents().size() == 0 ? true : true; + boolean isUnlockableFromSoftParent = node.getSoftParents().size() == 0 ? true : false; + boolean isFullyLockedFromStrongParent = node.getStrongParents().size() == 0 ? false : false; + boolean isFullyLockedFromSoftParent = node.getSoftParents().size() == 0 ? false : true; for (SkillTreeNode strongParent : node.getStrongParents()) { if (playerData.getNodeLevel(strongParent) < node.getParentNeededLevel(strongParent)) { @@ -210,7 +223,6 @@ public abstract class SkillTree extends PostLoadObject implements RegisterObject } - for (SkillTreeNode softParent : node.getSoftParents()) { if (playerData.getNodeLevel(softParent) > node.getParentNeededLevel(softParent)) { isUnlockableFromSoftParent = true;