Skill Tree refactor & enabled to translate the skill tree node states.

This commit is contained in:
Ka0rX 2023-06-24 22:46:05 +01:00
parent df78c21aa3
commit 8000bc9b89
8 changed files with 71 additions and 56 deletions

View File

@ -48,7 +48,7 @@ import net.Indyuce.mmocore.skill.binding.BoundSkillInfo;
import net.Indyuce.mmocore.skill.binding.SkillSlot;
import net.Indyuce.mmocore.skill.cast.SkillCastingInstance;
import net.Indyuce.mmocore.skill.cast.SkillCastingMode;
import net.Indyuce.mmocore.skilltree.NodeStatus;
import net.Indyuce.mmocore.skilltree.SkillTreeStatus;
import net.Indyuce.mmocore.skilltree.SkillTreeNode;
import net.Indyuce.mmocore.skilltree.tree.SkillTree;
import net.Indyuce.mmocore.waypoint.Waypoint;
@ -112,7 +112,7 @@ public class PlayerData extends SynchronizedDataHolder implements OfflinePlayerD
/**
* Cached for easier access. Current status of each skill tree node.
*/
private final Map<SkillTreeNode, NodeStatus> nodeStates = new HashMap<>();
private final Map<SkillTreeNode, SkillTreeStatus> nodeStates = new HashMap<>();
private final Map<SkillTreeNode, Integer> nodeLevels = new HashMap<>();
private final Map<String, Integer> skillTreePoints = new HashMap<>();
@ -264,9 +264,9 @@ public class PlayerData extends SynchronizedDataHolder implements OfflinePlayerD
}
public boolean canIncrementNodeLevel(SkillTreeNode node) {
NodeStatus nodeStatus = nodeStates.get(node);
SkillTreeStatus skillTreeStatus = nodeStates.get(node);
//Check the State of the node
if (nodeStatus != NodeStatus.UNLOCKED && nodeStatus != NodeStatus.UNLOCKABLE) return false;
if (skillTreeStatus != SkillTreeStatus.UNLOCKED && skillTreeStatus != SkillTreeStatus.UNLOCKABLE) return false;
return node.hasPermissionRequirement(this) && getNodeLevel(node) < node.getMaxLevel() && (skillTreePoints.getOrDefault(node.getTree().getId(), 0) + skillTreePoints.getOrDefault("global", 0) >= node.getSkillTreePointsConsumed());
}
@ -280,7 +280,7 @@ public class PlayerData extends SynchronizedDataHolder implements OfflinePlayerD
// Claims the nodes experience table.
node.getExperienceTable().claim(this, getNodeLevel(node), node);
if (nodeStates.get(node) == NodeStatus.UNLOCKABLE) setNodeState(node, NodeStatus.UNLOCKED);
if (nodeStates.get(node) == SkillTreeStatus.UNLOCKABLE) setNodeState(node, SkillTreeStatus.UNLOCKED);
int pointToWithdraw = node.getSkillTreePointsConsumed();
if (skillTreePoints.get(node.getTree().getId()) > 0) {
int pointWithdrawn = Math.min(pointToWithdraw, skillTreePoints.get(node.getTree().getId()));
@ -303,11 +303,11 @@ public class PlayerData extends SynchronizedDataHolder implements OfflinePlayerD
skillTreePoints.put(treeId, skillTreePoints.get(treeId) - withdraw);
}
public void setNodeState(SkillTreeNode node, NodeStatus nodeStatus) {
nodeStates.put(node, nodeStatus);
public void setNodeState(SkillTreeNode node, SkillTreeStatus skillTreeStatus) {
nodeStates.put(node, skillTreeStatus);
}
public NodeStatus getNodeStatus(SkillTreeNode node) {
public SkillTreeStatus getNodeStatus(SkillTreeNode node) {
return nodeStates.get(node);
}
@ -334,7 +334,7 @@ public class PlayerData extends SynchronizedDataHolder implements OfflinePlayerD
skillTree.setupNodeStates(this);
}
public Map<SkillTreeNode, NodeStatus> getNodeStates() {
public Map<SkillTreeNode, SkillTreeStatus> getNodeStates() {
return new HashMap<>(nodeStates);
}

View File

@ -1,7 +1,9 @@
package net.Indyuce.mmocore.gui.skilltree;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.UtilityMethods;
import net.Indyuce.mmocore.MMOCore;
import java.util.logging.Level;
import net.Indyuce.mmocore.api.SoundEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
@ -15,7 +17,6 @@ import net.Indyuce.mmocore.gui.skilltree.display.*;
import net.Indyuce.mmocore.skilltree.*;
import net.Indyuce.mmocore.skilltree.tree.SkillTree;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
@ -32,6 +33,7 @@ import java.util.*;
public class SkillTreeViewer extends EditableInventory {
protected final Map<DisplayInfo, Icon> icons = new HashMap<>();
protected final Map<SkillTreeStatus, String> statusNames = new HashMap<>();
public SkillTreeViewer() {
super("skill-tree");
@ -40,20 +42,24 @@ public class SkillTreeViewer extends EditableInventory {
@Override
public void reload(FileConfiguration config) {
super.reload(config);
if (config.contains("status-names"))
for (SkillTreeStatus skillTreeStatus : SkillTreeStatus.values())
statusNames.put(skillTreeStatus, config.getString("status-names." + UtilityMethods.ymlName(skillTreeStatus.name()), skillTreeStatus.name()));
//Loads all the pathDisplayInfo
for (PathStatus status : PathStatus.values())
for (PathType pathType : PathType.values()) {
if (!config.contains("display.paths." + MMOCoreUtils.ymlName(status.name()) + "." + MMOCoreUtils.ymlName(pathType.name()))) {
MMOCore.log("Missing path type: " + MMOCoreUtils.ymlName(pathType.name()) + " for status: " + MMOCoreUtils.ymlName(status.name()));
MMOCore.log(Level.WARNING, "An error occurred while loading skill tree GUI: Missing path type: " + MMOCoreUtils.ymlName(pathType.name()) + " for status: " + MMOCoreUtils.ymlName(status.name()));
continue;
}
icons.put(new PathDisplayInfo(pathType, status), new Icon(config.getConfigurationSection("display.paths." + MMOCoreUtils.ymlName(status.name()) + "." + MMOCoreUtils.ymlName(pathType.name()))));
}
//Loads all the nodeDisplayInfo
for (NodeStatus status : NodeStatus.values())
for (SkillTreeStatus status : SkillTreeStatus.values())
for (NodeType nodeType : NodeType.values()) {
if (!config.contains("display.nodes." + MMOCoreUtils.ymlName(status.name()) + "." + MMOCoreUtils.ymlName(nodeType.name()))) {
MMOCore.log("Missing node type: " + MMOCoreUtils.ymlName(nodeType.name()) + " for status: " + MMOCoreUtils.ymlName(status.name()));
MMOCore.log(Level.WARNING, "An error occurred while loading skill tree GUI: Missing node type: " + MMOCoreUtils.ymlName(nodeType.name()) + " for status: " + MMOCoreUtils.ymlName(status.name()));
continue;
}
icons.put(new NodeDisplayInfo(nodeType, status), new Icon(config.getConfigurationSection("display.nodes." + MMOCoreUtils.ymlName(status.name()) + "." + MMOCoreUtils.ymlName(nodeType.name()))));
@ -219,10 +225,10 @@ public class SkillTreeViewer extends EditableInventory {
if (inv.getSkillTree().isNode(coordinates)) {
SkillTreeNode node = inv.getSkillTree().getNode(coordinates);
List<String> lore = new ArrayList<>();
Placeholders holders = getPlaceholders(inv, n);
getLore().forEach(str -> {
if (str.contains("{node-lore}")) {
lore.addAll(node.getLore(inv.getPlayerData()));
node.getLore(inv.getPlayerData()).forEach(s -> lore.add(holders.apply(inv.getPlayer(), s)));
} else if (str.contains("{strong-parents}")) {
lore.addAll(getParentsLore(inv, node, node.getParents(ParentType.STRONG)));
} else if (str.contains("{soft-parents}")) {
@ -230,7 +236,7 @@ public class SkillTreeViewer extends EditableInventory {
} else if (str.contains("{incompatible-parents}")) {
lore.addAll(getParentsLore(inv, node, node.getParents(ParentType.INCOMPATIBLE)));
} else
lore.add(getPlaceholders(inv, n).apply(inv.getPlayer(), str));
lore.add(holders.apply(inv.getPlayer(), str));
});
meta.setLore(lore);
meta.setDisplayName(node.getName());
@ -272,7 +278,8 @@ public class SkillTreeViewer extends EditableInventory {
if (inv.getSkillTree().isNode(inv.getCoordinates(n))) {
SkillTreeNode node = inv.getNode(n);
holders.register("current-level", inv.getPlayerData().getNodeLevel(node));
holders.register("current-state", inv.getPlayerData().getNodeStatus(node));
SkillTreeStatus status = inv.getPlayerData().getNodeStatus(node);
holders.register("current-state", statusNames.getOrDefault(status, status.name()));
holders.register("max-level", node.getMaxLevel());
holders.register("max-children", node.getMaxChildren());
holders.register("size", node.getSize());
@ -347,18 +354,18 @@ public class SkillTreeViewer extends EditableInventory {
if (skillTree.isNode(coordinates)) {
SkillTreeNode node = skillTree.getNode(coordinates);
NodeStatus nodeStatus = playerData.getNodeStatus(node);
SkillTreeStatus skillTreeStatus = playerData.getNodeStatus(node);
//If the node has its own display, it will be shown.
if (node.hasIcon(nodeStatus))
return node.getIcon(nodeStatus);
if (node.hasIcon(skillTreeStatus))
return node.getIcon(skillTreeStatus);
NodeType nodeType = NodeType.getNodeType(hasUpPathOrNode, hasRightPathOrNode, hasDownPathOrNode, hasLeftPathOrNode);
DisplayInfo displayInfo = new NodeDisplayInfo(nodeType, nodeStatus);
DisplayInfo displayInfo = new NodeDisplayInfo(nodeType, skillTreeStatus);
//Takes the display defined in the skill tree config if it exists.
if (skillTree.hasIcon(displayInfo))
return skillTree.getIcon(displayInfo);
Icon icon = icons.get(displayInfo);
Validate.notNull(icon, "The node " + node.getFullId() + " has no icon for the type " + nodeType + " and the status " + nodeStatus);
Validate.notNull(icon, "The node " + node.getFullId() + " has no icon for the type " + nodeType + " and the status " + skillTreeStatus);
return icon;
} else {
PathType pathType = PathType.getPathType(hasUpPathOrNode, hasRightPathOrNode, hasDownPathOrNode, hasLeftPathOrNode);
@ -486,7 +493,7 @@ public class SkillTreeViewer extends EditableInventory {
MMOCore.plugin.configManager.getSimpleMessage("upgrade-skill-node", "skill-node", node.getName(), "level", "" + playerData.getNodeLevel(node)).send(player);
MMOCore.plugin.soundManager.getSound(SoundEvent.LEVEL_UP).playTo(getPlayer());
open();
} else if (playerData.getNodeStatus(node) == NodeStatus.LOCKED || playerData.getNodeStatus(node) == NodeStatus.FULLY_LOCKED) {
} else if (playerData.getNodeStatus(node) == SkillTreeStatus.LOCKED || playerData.getNodeStatus(node) == SkillTreeStatus.FULLY_LOCKED) {
MMOCore.plugin.configManager.getSimpleMessage("locked-node").send(player);
MMOCore.plugin.soundManager.getSound(SoundEvent.NOT_ENOUGH_POINTS).playTo(getPlayer());

View File

@ -1,25 +1,25 @@
package net.Indyuce.mmocore.gui.skilltree.display;
import net.Indyuce.mmocore.skilltree.NodeStatus;
import net.Indyuce.mmocore.skilltree.SkillTreeStatus;
import java.util.Objects;
public class NodeDisplayInfo implements DisplayInfo{
private NodeStatus nodeStatus;
private SkillTreeStatus skillTreeStatus;
private NodeType nodeType;
public NodeDisplayInfo(NodeType nodeType,NodeStatus nodeStatus) {
this.nodeStatus = nodeStatus;
public NodeDisplayInfo(NodeType nodeType, SkillTreeStatus skillTreeStatus) {
this.skillTreeStatus = skillTreeStatus;
this.nodeType = nodeType;
}
public NodeStatus getNodeState() {
return nodeStatus;
public SkillTreeStatus getNodeState() {
return skillTreeStatus;
}
public NodeStatus getNodeStatus() {
return nodeStatus;
public SkillTreeStatus getNodeStatus() {
return skillTreeStatus;
}
public NodeType getNodeType() {
@ -31,18 +31,18 @@ public class NodeDisplayInfo implements DisplayInfo{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NodeDisplayInfo that = (NodeDisplayInfo) o;
return nodeStatus == that.nodeStatus && nodeType == that.nodeType;
return skillTreeStatus == that.skillTreeStatus && nodeType == that.nodeType;
}
@Override
public int hashCode() {
return Objects.hash(nodeStatus, nodeType);
return Objects.hash(skillTreeStatus, nodeType);
}
@Override
public String toString() {
return "NodeDisplayInfo{" +
"nodeStatus=" + nodeStatus +
"nodeStatus=" + skillTreeStatus +
", nodeType=" + nodeType +
'}';
}

View File

@ -9,12 +9,10 @@ import net.Indyuce.mmocore.experience.ExpCurve;
import net.Indyuce.mmocore.experience.ExperienceObject;
import net.Indyuce.mmocore.experience.droptable.ExperienceTable;
import net.Indyuce.mmocore.gui.api.item.Placeholders;
import net.Indyuce.mmocore.gui.skilltree.SkillTreeViewer;
import net.Indyuce.mmocore.gui.skilltree.display.Icon;
import net.Indyuce.mmocore.skilltree.tree.ParentInformation;
import net.Indyuce.mmocore.skilltree.tree.SkillTree;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
@ -30,7 +28,7 @@ public class SkillTreeNode implements ExperienceObject {
private String permissionRequired;
private final Map<NodeStatus, Icon> icons = new HashMap<>();
private final Map<SkillTreeStatus, Icon> icons = new HashMap<>();
private IntegerCoordinates coordinates;
/**
@ -68,7 +66,7 @@ public class SkillTreeNode implements ExperienceObject {
this.id = config.getName();
this.tree = tree;
if (config.isConfigurationSection("display")) {
for (NodeStatus status : NodeStatus.values()) {
for (SkillTreeStatus status : SkillTreeStatus.values()) {
String ymlStatus = MMOCoreUtils.ymlName(status.name());
if (!config.isConfigurationSection("display." + ymlStatus)) {
MMOCore.log("Could not find node display for status " + ymlStatus + " for node " + id + " in tree " + tree.getId() + ". Using default display.");
@ -105,11 +103,11 @@ public class SkillTreeNode implements ExperienceObject {
return tree;
}
public boolean hasIcon(NodeStatus status) {
public boolean hasIcon(SkillTreeStatus status) {
return icons.containsKey(status);
}
public Icon getIcon(NodeStatus status) {
public Icon getIcon(SkillTreeStatus status) {
return icons.get(status);
}

View File

@ -7,13 +7,13 @@ import net.Indyuce.mmocore.skilltree.tree.SkillTree;
public record SkillTreePath(SkillTree tree, IntegerCoordinates coordinates, SkillTreeNode from, SkillTreeNode to) {
public PathStatus getStatus(PlayerData playerData) {
NodeStatus fromStatus = playerData.getNodeStatus(from);
NodeStatus toStatus = playerData.getNodeStatus(to);
if (fromStatus == NodeStatus.UNLOCKED && toStatus == NodeStatus.UNLOCKED)
SkillTreeStatus fromStatus = playerData.getNodeStatus(from);
SkillTreeStatus toStatus = playerData.getNodeStatus(to);
if (fromStatus == SkillTreeStatus.UNLOCKED && toStatus == SkillTreeStatus.UNLOCKED)
return PathStatus.UNLOCKED;
if ((fromStatus == NodeStatus.UNLOCKABLE && toStatus == NodeStatus.LOCKED) || (fromStatus == NodeStatus.LOCKED && toStatus == NodeStatus.UNLOCKABLE))
if ((fromStatus == SkillTreeStatus.UNLOCKABLE && toStatus == SkillTreeStatus.LOCKED) || (fromStatus == SkillTreeStatus.LOCKED && toStatus == SkillTreeStatus.UNLOCKABLE))
return PathStatus.UNLOCKABLE;
if (fromStatus == NodeStatus.FULLY_LOCKED || toStatus == NodeStatus.FULLY_LOCKED)
if (fromStatus == SkillTreeStatus.FULLY_LOCKED || toStatus == SkillTreeStatus.FULLY_LOCKED)
return PathStatus.FULLY_LOCKED;
return PathStatus.LOCKED;
}

View File

@ -1,6 +1,6 @@
package net.Indyuce.mmocore.skilltree;
public enum NodeStatus {
public enum SkillTreeStatus {
/**
* The player does not have access to this skill tree node just yet.

View File

@ -12,7 +12,6 @@ import net.Indyuce.mmocore.skilltree.*;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -103,7 +102,7 @@ public abstract class SkillTree extends PostLoadObject implements RegisteredObje
}
//Loads all the nodeDisplayInfo
for (NodeStatus status : NodeStatus.values())
for (SkillTreeStatus status : SkillTreeStatus.values())
for (NodeType nodeType : NodeType.values()) {
ConfigurationSection section = config.getConfigurationSection("display.nodes." + MMOCoreUtils.ymlName(status.name()) + "." + MMOCoreUtils.ymlName(nodeType.name()));
if (section != null)
@ -173,9 +172,9 @@ public abstract class SkillTree extends PostLoadObject implements RegisteredObje
*/
private void setupNodeStateFrom(SkillTreeNode node, PlayerData playerData) {
if (playerData.getNodeLevel(node) > 0) {
playerData.setNodeState(node, NodeStatus.UNLOCKED);
playerData.setNodeState(node, SkillTreeStatus.UNLOCKED);
} else if (playerData.getNodeLevel(node) == 0 && node.isRoot()) {
playerData.setNodeState(node, NodeStatus.UNLOCKABLE);
playerData.setNodeState(node, SkillTreeStatus.UNLOCKABLE);
} else {
Set<SkillTreeNode> strongParents = node.getParents(ParentType.STRONG);
Set<SkillTreeNode> softParents = node.getParents(ParentType.SOFT);
@ -199,7 +198,7 @@ public abstract class SkillTree extends PostLoadObject implements RegisteredObje
numberChildren++;
//We must check if the parent is Fully Locked or not and if it can unlock a new node(with its max children constraint)
if (numberChildren >= strongParent.getMaxChildren() || playerData.getNodeStatus(strongParent) == NodeStatus.FULLY_LOCKED)
if (numberChildren >= strongParent.getMaxChildren() || playerData.getNodeStatus(strongParent) == SkillTreeStatus.FULLY_LOCKED)
isFullyLockedFromStrongParent = true;
}
@ -213,7 +212,7 @@ public abstract class SkillTree extends PostLoadObject implements RegisteredObje
for (SkillTreeNode child : softParent.getChildren())
if (playerData.getNodeLevel(child) > 0)
numberChildren++;
if (numberChildren < softParent.getMaxChildren() && playerData.getNodeStatus(softParent) != NodeStatus.FULLY_LOCKED)
if (numberChildren < softParent.getMaxChildren() && playerData.getNodeStatus(softParent) != SkillTreeStatus.FULLY_LOCKED)
isFullyLockedFromSoftParent = false;
}
for (SkillTreeNode incompatibleParent : incompatibleParents) {
@ -226,11 +225,11 @@ public abstract class SkillTree extends PostLoadObject implements RegisteredObje
boolean isFullyLocked = isFullyLockedFromSoftParent || isFullyLockedFromStrongParent || isFullyLockedFromIncompatibleParent;
boolean isUnlockable = isUnlockableFromSoftParent && isUnlockableFromStrongParent;
if (isFullyLocked)
playerData.setNodeState(node, NodeStatus.FULLY_LOCKED);
playerData.setNodeState(node, SkillTreeStatus.FULLY_LOCKED);
else if (isUnlockable)
playerData.setNodeState(node, NodeStatus.UNLOCKABLE);
playerData.setNodeState(node, SkillTreeStatus.UNLOCKABLE);
else
playerData.setNodeState(node, NodeStatus.LOCKED);
playerData.setNodeState(node, SkillTreeStatus.LOCKED);
}
//We recursively call the algorithm for all the children of the current node
for (SkillTreeNode child : node.getChildren())

View File

@ -77,7 +77,9 @@ items:
function: 'skill-tree-node'
slots: [1,2,3,4,5,6,7,8,10,11,12,13,14,15,16,17,19,20,21,22,23,24,25,26,28,29,30,31,32,33,34,35,37,38,39,40,41,42,43,44]
#The lore that will be displayed after the lore of the node
#The lore that will be displayed after the lore of the node.
#The {node-lore} placeholder will be replaced by the lore specified in the skill tree node config.
#All the placeholders you see here can also be used in the node lore.
lore:
- '&7Current State: &6{current-state}'
- '&7Current Level: &6{current-level}'
@ -99,6 +101,15 @@ items:
- '&e◆Points spent for {skill-tree}&e:&6 {point-spent}'
- '&e◆Current {skill-tree} &epoints: &6{skill-tree-points}'
- '&e◆Current &6global&e points: &6{global-points}'
#This is the name that will be displayed for each status.
status-names:
unlocked: 'Unlocked'
unlockable: 'Unlockable'
locked: 'Locked'
fully-locked: 'Fully Locked'
display:
paths:
unlocked: