From 5523e875cfc12eb3315623d0578107bb45d72d7f Mon Sep 17 00:00:00 2001 From: Ka0rX Date: Thu, 4 May 2023 11:21:11 +0100 Subject: [PATCH] Permission requirement for skill tree node according to ticket #801. Check the wiki for further info. Make sure to reload messages.yml. --- .../net/Indyuce/mmocore/api/player/PlayerData.java | 2 +- .../mmocore/gui/skilltree/SkillTreeViewer.java | 14 +++----------- .../Indyuce/mmocore/skilltree/SkillTreeNode.java | 7 +++++++ .../src/main/resources/default/messages.yml | 1 + 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java index a8774c5f..002c5dd4 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java @@ -278,7 +278,7 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc NodeStatus nodeStatus = nodeStates.get(node); //Check the State of the node if (nodeStatus != NodeStatus.UNLOCKED && nodeStatus != NodeStatus.UNLOCKABLE) return false; - return getNodeLevel(node) < node.getMaxLevel() && (skillTreePoints.getOrDefault(node.getTree().getId(), 0) + skillTreePoints.getOrDefault("global", 0) >= node.getSkillTreePointsConsumed()); + return node.hasPermissionRequirement(this)&&getNodeLevel(node) < node.getMaxLevel() && (skillTreePoints.getOrDefault(node.getTree().getId(), 0) + skillTreePoints.getOrDefault("global", 0) >= node.getSkillTreePointsConsumed()); } /** diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/gui/skilltree/SkillTreeViewer.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/gui/skilltree/SkillTreeViewer.java index 4bf5728e..b67bc46d 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/gui/skilltree/SkillTreeViewer.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/gui/skilltree/SkillTreeViewer.java @@ -421,14 +421,12 @@ public class SkillTreeViewer extends EditableInventory { if (spent < 1) { MMOCore.plugin.configManager.getSimpleMessage("no-skill-tree-points-spent").send(player); MMOCore.plugin.soundManager.getSound(SoundEvent.NOT_ENOUGH_POINTS).playTo(getPlayer()); - event.setCancelled(true); return; } if (getPlayerData().getSkillTreeReallocationPoints() <= 0) { MMOCore.plugin.configManager.getSimpleMessage("not-skill-tree-reallocation-point").send(player); MMOCore.plugin.soundManager.getSound(SoundEvent.NOT_ENOUGH_POINTS).playTo(getPlayer()); - event.setCancelled(true); return; } else { int reallocated = playerData.getPointSpent(skillTree); @@ -439,7 +437,6 @@ public class SkillTreeViewer extends EditableInventory { skillTree.setupNodeStates(playerData); MMOCore.plugin.configManager.getSimpleMessage("reallocated-points", "points", "" + playerData.getSkillTreePoint(skillTree.getId()), "skill-tree", skillTree.getName()).send(player); MMOCore.plugin.soundManager.getSound(SoundEvent.RESET_SKILL_TREE).playTo(player); - event.setCancelled(true); open(); return; @@ -452,7 +449,6 @@ public class SkillTreeViewer extends EditableInventory { MMOCore.plugin.soundManager.getSound(SoundEvent.CHANGE_SKILL_TREE).playTo(player); skillTree = MMOCore.plugin.skillTreeManager.get(id); open(); - event.setCancelled(true); return; } @@ -462,16 +458,13 @@ public class SkillTreeViewer extends EditableInventory { int x = container.get(new NamespacedKey(MMOCore.plugin, "coordinates.x"), PersistentDataType.INTEGER); int y = container.get(new NamespacedKey(MMOCore.plugin, "coordinates.y"), PersistentDataType.INTEGER); if (!skillTree.isNode(new IntegerCoordinates(x, y))) { - event.setCancelled(true); return; } SkillTreeNode node = skillTree.getNode(new IntegerCoordinates(x, y)); if (playerData.getPointSpent(skillTree) >= skillTree.getMaxPointSpent()) { MMOCore.plugin.configManager.getSimpleMessage("max-points-reached").send(player); MMOCore.plugin.soundManager.getSound(SoundEvent.NOT_ENOUGH_POINTS).playTo(getPlayer()); - event.setCancelled(true); return; - } if (playerData.canIncrementNodeLevel(node)) { @@ -479,23 +472,22 @@ 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(); - event.setCancelled(true); } else if (playerData.getNodeStatus(node) == NodeStatus.LOCKED || playerData.getNodeStatus(node) == NodeStatus.FULLY_LOCKED) { MMOCore.plugin.configManager.getSimpleMessage("locked-node").send(player); MMOCore.plugin.soundManager.getSound(SoundEvent.NOT_ENOUGH_POINTS).playTo(getPlayer()); - event.setCancelled(true); } else if (playerData.getNodeLevel(node) >= node.getMaxLevel()) { MMOCore.plugin.configManager.getSimpleMessage("skill-node-max-level-hit").send(player); MMOCore.plugin.soundManager.getSound(SoundEvent.NOT_ENOUGH_POINTS).playTo(getPlayer()); - event.setCancelled(true); + }else if(!node.hasPermissionRequirement(playerData)){ + MMOCore.plugin.configManager.getSimpleMessage("missing-skill-node-permission").send(player); + MMOCore.plugin.soundManager.getSound(SoundEvent.NOT_ENOUGH_POINTS).playTo(getPlayer()); } //Else the player doesn't doesn't have the skill tree points else { MMOCore.plugin.configManager.getSimpleMessage("not-enough-skill-tree-points", "point", "" + node.getSkillTreePointsConsumed()).send(player); MMOCore.plugin.soundManager.getSound(SoundEvent.NOT_ENOUGH_POINTS).playTo(getPlayer()); - event.setCancelled(true); } } diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/skilltree/SkillTreeNode.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/skilltree/SkillTreeNode.java index 48da591d..1a7f1788 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/skilltree/SkillTreeNode.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/skilltree/SkillTreeNode.java @@ -24,6 +24,8 @@ public class SkillTreeNode implements ExperienceObject { private final SkillTree tree; private final String name, id; + private String permissionRequired; + private final Map icons = new HashMap<>(); private IntegerCoordinates coordinates; @@ -76,6 +78,7 @@ public class SkillTreeNode implements ExperienceObject { size = Objects.requireNonNull(config.getInt("size")); isRoot = config.getBoolean("is-root", false); skillTreePointsConsumed = config.getInt("point-consumed", 1); + permissionRequired = config.getString("permission-required"); Validate.isTrue(skillTreePointsConsumed > 0, "The skill tree points consumed by a node must be greater than 0."); if (config.contains("lores")) for (String key : config.getConfigurationSection("lores").getKeys(false)) @@ -151,6 +154,10 @@ public class SkillTreeNode implements ExperienceObject { return maxChildren; } + public boolean hasPermissionRequirement(PlayerData playerData){ + return permissionRequired == null || playerData.getPlayer().hasPermission(permissionRequired); + } + public Set getSoftParents() { return softParents.keySet(); } diff --git a/MMOCore-Dist/src/main/resources/default/messages.yml b/MMOCore-Dist/src/main/resources/default/messages.yml index 33d8687b..c6c9bd57 100644 --- a/MMOCore-Dist/src/main/resources/default/messages.yml +++ b/MMOCore-Dist/src/main/resources/default/messages.yml @@ -216,6 +216,7 @@ no-skill-tree-points-spent: '&cYou have not spent any skill tree points.' locked-node: '&cThis skill is locked!' upgrade-skill-node: '&eYour skill node &6{skill-node} &eis now Level &6{level}&e!' skill-node-max-level-hit: '&cYou already hit the max level for that skill node.' +missing-skill-node-permission: '&cYou do not have the permission to unlock this skill node.' not-enough-skill-tree-points: '&cYou need {point} skill tree point.' reallocated-points: '&eYou successfully reset the skill tree {skill-tree}. &eYou now have &6{points} &eskill tree points.' not-skill-tree-reallocation-point: '&cYou do not have 1 skill tree reallocation point.'