SkillTree debug and support for SQL

This commit is contained in:
Ka0rX 2022-06-19 12:02:06 +02:00
parent a5ab2c9269
commit d1bb2406de
8 changed files with 287 additions and 238 deletions

View File

@ -62,6 +62,7 @@ import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.*; import java.util.*;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -220,6 +221,15 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
} }
public Set<Map.Entry<String,Integer>> getNodeLevelsEntrySet() {
HashMap<String,Integer> nodeLevelsString=new HashMap<>();
for(SkillTreeNode node:nodeLevels.keySet()) {
nodeLevelsString.put(node.getFullId(),nodeLevels.get(node));
}
return nodeLevelsString.entrySet();
}
public void removeModifiersFrom(SkillTree skillTree) { public void removeModifiersFrom(SkillTree skillTree) {
for (SkillTreeNode node : skillTree.getNodes()) { for (SkillTreeNode node : skillTree.getNodes()) {
for (int i = 0; i < node.getMaxLevel(); i++) { 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) { public int getNodeLevel(SkillTreeNode node) {
return nodeLevels.get(node); return nodeLevels.get(node);
} }

View File

@ -332,7 +332,7 @@ public class SkillTreeViewer extends EditableInventory {
int xOffset=offset%9-middleSlot%9; int xOffset=offset%9-middleSlot%9;
int yOffset=offset/9-middleSlot/9; int yOffset=offset/9-middleSlot/9;
x += xOffset; x += xOffset;
y += yOffset; y += yOffset-1;
open(); open();
event.setCancelled(true); event.setCancelled(true);
return; return;

View File

@ -7,36 +7,39 @@ import net.Indyuce.mmocore.tree.skilltree.SkillTree;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class SkillTreeManager extends MMOCoreRegister<SkillTree> { public class SkillTreeManager extends MMOCoreRegister<SkillTree> {
private final ArrayList<SkillTreeNode> skillTreeNodes = new ArrayList<>(); private final HashMap<String,SkillTreeNode> skillTreeNodes = new HashMap<>();
@Override @Override
public void register(SkillTree tree){ public void register(SkillTree tree){
super.register(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) { public boolean has(int index) {
return index>=0 &&index<registered.values().stream().collect(Collectors.toList()).size(); return index>=0 &&index<registered.values().stream().collect(Collectors.toList()).size();
} }
public SkillTreeNode getNode(String fullId) {
return skillTreeNodes.get(fullId);
}
/** /**
* Useful to recursively go trough trees * Useful to recursively go trough trees
* *
* @return The list of all the roots (e.g the nodes without any parents * @return The list of all the roots (e.g the nodes without any parents
*/ */
public List<SkillTreeNode> getRootNodes() { public List<SkillTreeNode> 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<SkillTreeNode> getAllNodes() { public Collection<SkillTreeNode> getAllNodes() {
return skillTreeNodes; return skillTreeNodes.values();
} }
public SkillTree get(int index) { public SkillTree get(int index) {

View File

@ -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," + "experience INT(11) DEFAULT 0,class VARCHAR(20),guild VARCHAR(20),last_login LONG,"
+ "attributes LONGTEXT,professions LONGTEXT,times_claimed LONGTEXT,quests LONGTEXT," + "attributes LONGTEXT,professions LONGTEXT,times_claimed LONGTEXT,quests LONGTEXT,"
+ "waypoints LONGTEXT,friends LONGTEXT,skills LONGTEXT,bound_skills 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 @Override

View File

@ -29,7 +29,6 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
this.provider = provider; this.provider = provider;
} }
//TODO SkillTree pour SQL
@Override @Override
public void loadData(PlayerData data) { public void loadData(PlayerData data) {
provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + data.getUniqueId() + "';", (result) -> { provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + data.getUniqueId() + "';", (result) -> {
@ -110,6 +109,17 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
} }
} }
} }
//We load the skill trees
data.setSkillTreeReallocationPoints(result.getInt("skill_tree_reallocation_points"));
JsonObject object = MythicLib.plugin.getJson().parse(result.getString("skill_tree_points"), JsonObject.class);
for (Entry<String, JsonElement> 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<String, JsonElement> entry : object.entrySet()) {
data.setNodeLevel(MMOCore.plugin.skillTreeManager.getNode(entry.getKey()),entry.getValue().getAsInt());
}
data.setFullyLoaded(); data.setFullyLoaded();
MMOCore.sqlDebug("Loaded saved data for: '" + data.getUniqueId() + "'!"); MMOCore.sqlDebug("Loaded saved data for: '" + data.getUniqueId() + "'!");
@ -152,6 +162,12 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
sql.updateData("class_info", createClassInfoData(data).toString()); sql.updateData("class_info", createClassInfoData(data).toString());
//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());
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()));
} }

View File

@ -77,8 +77,7 @@ public class YAMLPlayerDataManager extends PlayerDataManager {
//Load skill tree nodes //Load skill tree nodes
for (SkillTreeNode node : MMOCore.plugin.skillTreeManager.getAllNodes()) { 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)) if (config.contains(path))
data.setNodeLevel(node, config.getInt(path)); data.setNodeLevel(node, config.getInt(path));
else { else {
@ -92,7 +91,6 @@ public class YAMLPlayerDataManager extends PlayerDataManager {
if (section != null) { if (section != null) {
for (String key : section.getKeys(false)) for (String key : section.getKeys(false))
data.setSkillTreePoints(key, section.getInt(key)); data.setSkillTreePoints(key, section.getInt(key));
} }
//Put 0 to the rest of the values if nothing has been given //Put 0 to the rest of the values if nothing has been given
List<String> skillTreeIds = MMOCore.plugin.skillTreeManager.getAll().stream().map(SkillTree::getId).collect(Collectors.toList()); List<String> 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.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. // Load class slots, use try so the player can log in.
if (config.contains("class-info")) if (config.contains("class-info"))
@ -149,7 +147,7 @@ public class YAMLPlayerDataManager extends PlayerDataManager {
for (String treeId : data.getSkillTreePoints().keySet()) { for (String treeId : data.getSkillTreePoints().keySet()) {
config.set("skill-tree-points." + treeId, data.getSkillTreePoint(treeId)); 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<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()));

View File

@ -52,6 +52,7 @@ public class SkillTreeNode implements Unlockable {
public SkillTreeNode(SkillTree tree, ConfigurationSection config) { public SkillTreeNode(SkillTree tree, ConfigurationSection config) {
Validate.notNull(config, "Config cannot be null"); Validate.notNull(config, "Config cannot be null");
this.id = config.getName(); this.id = config.getName();
this.tree = tree; this.tree = tree;
@ -106,7 +107,7 @@ public class SkillTreeNode implements Unlockable {
} }
} catch (NumberFormatException e) { } 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; maxLevel = config.contains("max-level") ? config.getInt("max-level") : 1;
maxChildren = config.contains("max-children") ? config.getInt("max-children") : 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 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")); coordinates = new IntegerCoordinates(config.getInt("coordinates.x"), config.getInt("coordinates.y"));
} }
}
/* /*
if (config.contains("modifiers")) { if (config.contains("modifiers")) {
for (String key : config.getConfigurationSection("modifiers").getKeys(false)) { for (String key : config.getConfigurationSection("modifiers").getKeys(false)) {
@ -126,8 +130,6 @@ public class SkillTreeNode implements Unlockable {
} }
*/ */
}
public SkillTree getTree() { public SkillTree getTree() {
return tree; return tree;
@ -195,6 +197,11 @@ public class SkillTreeNode implements Unlockable {
return id; return id;
} }
public String getFullId() {
return tree.getId() + "." + id;
}
public String getName() { public String getName() {
return MythicLib.plugin.parseColors(name); return MythicLib.plugin.parseColors(name);
} }

View File

@ -55,15 +55,24 @@ public abstract class SkillTree extends PostLoadObject implements RegisterObject
protected final List<SkillTreeNode> roots = new ArrayList<>(); protected final List<SkillTreeNode> roots = new ArrayList<>();
public SkillTree(ConfigurationSection config) { public SkillTree(ConfigurationSection config) {
super(config); super(config);
this.id = Objects.requireNonNull(config.getString("id"), "Could not find skill tree id"); 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")); 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))); 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")))); this.item = Material.valueOf(MMOCoreUtils.toEnumName(Objects.requireNonNull(config.getString("item"))));
Validate.isTrue(config.isConfigurationSection("nodes"), "Could not find any nodes in the tree"); Validate.isTrue(config.isConfigurationSection("nodes"), "Could not find any nodes in the tree");
for (String key : config.getConfigurationSection("nodes").getKeys(false)) { for (String key : config.getConfigurationSection("nodes").getKeys(false)) {
try {
SkillTreeNode node = new SkillTreeNode(this, config.getConfigurationSection("nodes." + key)); SkillTreeNode node = new SkillTreeNode(this, config.getConfigurationSection("nodes." + key));
nodes.put(node.getId(), node); 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 { try {
if (config.contains("paths")) { if (config.contains("paths")) {
@ -141,11 +150,13 @@ public abstract class SkillTree extends PostLoadObject implements RegisterObject
} }
public static SkillTree loadSkillTree(ConfigurationSection config) { public static SkillTree loadSkillTree(ConfigurationSection config) {
SkillTree skillTree = null;
try {
String string = config.getString("type"); 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!" + 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'"); "\nAllowed values: 'automatic','linked','custom'");
SkillTree skillTree = null;
if (string.equals("automatic")) { if (string.equals("automatic")) {
skillTree = new AutomaticSkillTree(config); skillTree = new AutomaticSkillTree(config);
skillTree.postLoad(); skillTree.postLoad();
@ -158,7 +169,9 @@ public abstract class SkillTree extends PostLoadObject implements RegisterObject
skillTree = new CustomSkillTree(config); skillTree = new CustomSkillTree(config);
skillTree.postLoad(); skillTree.postLoad();
} }
} catch (Exception e) {
MMOCore.plugin.getLogger().log(Level.SEVERE, "Couldn't load skill tree " + config.getName() + ": " + e.getMessage());
}
return skillTree; return skillTree;
} }
@ -189,10 +202,10 @@ public abstract class SkillTree extends PostLoadObject implements RegisterObject
} else if (playerData.getNodeLevel(node) == 0 && node.isRoot()) { } else if (playerData.getNodeLevel(node) == 0 && node.isRoot()) {
playerData.setNodeState(node, NodeState.UNLOCKABLE); playerData.setNodeState(node, NodeState.UNLOCKABLE);
} else { } else {
boolean isUnlockableFromStrongParent = node.getStrongParents().size()==0?true:true; boolean isUnlockableFromStrongParent = node.getStrongParents().size() == 0 ? true : true;
boolean isUnlockableFromSoftParent = node.getSoftParents().size()==0?true:false; boolean isUnlockableFromSoftParent = node.getSoftParents().size() == 0 ? true : false;
boolean isFullyLockedFromStrongParent = node.getStrongParents().size()==0?false:false; boolean isFullyLockedFromStrongParent = node.getStrongParents().size() == 0 ? false : false;
boolean isFullyLockedFromSoftParent = node.getSoftParents().size()==0?false:true; boolean isFullyLockedFromSoftParent = node.getSoftParents().size() == 0 ? false : true;
for (SkillTreeNode strongParent : node.getStrongParents()) { for (SkillTreeNode strongParent : node.getStrongParents()) {
if (playerData.getNodeLevel(strongParent) < node.getParentNeededLevel(strongParent)) { if (playerData.getNodeLevel(strongParent) < node.getParentNeededLevel(strongParent)) {
@ -210,7 +223,6 @@ public abstract class SkillTree extends PostLoadObject implements RegisterObject
} }
for (SkillTreeNode softParent : node.getSoftParents()) { for (SkillTreeNode softParent : node.getSoftParents()) {
if (playerData.getNodeLevel(softParent) > node.getParentNeededLevel(softParent)) { if (playerData.getNodeLevel(softParent) > node.getParentNeededLevel(softParent)) {
isUnlockableFromSoftParent = true; isUnlockableFromSoftParent = true;