diff --git a/gradle.properties b/gradle.properties index 27f60c6..0da7f3e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -48,7 +48,7 @@ mod_name=Fretux's Skill Engine # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=All Rights Reserved # The mod version. See https://semver.org/ -mod_version=1.1.4 +mod_version=1.2.0 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/net/fretux/skillengine/api/SkillEngineAPI.java b/src/main/java/net/fretux/skillengine/api/SkillEngineAPI.java index 88a4a53..09d57db 100644 --- a/src/main/java/net/fretux/skillengine/api/SkillEngineAPI.java +++ b/src/main/java/net/fretux/skillengine/api/SkillEngineAPI.java @@ -3,6 +3,10 @@ package net.fretux.skillengine.api; import net.fretux.skillengine.SkillEngine; import net.fretux.skillengine.capability.PlayerSkillData; import net.fretux.skillengine.capability.SkillEngineCapabilities; +import net.fretux.skillengine.client.SkilltreeClientState; +import net.fretux.skillengine.skilltree.AbilityNode; +import net.fretux.skillengine.skilltree.AbilityNodeRegistry; +import net.fretux.skillengine.skilltree.SkillLogic; import net.fretux.skillengine.skilltree.SkillNode; import net.fretux.skillengine.skilltree.SkillNodeRegistry; import net.minecraft.resources.ResourceLocation; @@ -10,44 +14,57 @@ import net.minecraft.world.entity.player.Player; import java.util.List; import java.util.Set; + public final class SkillEngineAPI { private SkillEngineAPI() {} + public static ResourceLocation id(String path) { return new ResourceLocation(SkillEngine.MODID, path); } + public static ResourceLocation rl(String namespace, String path) { return new ResourceLocation(namespace, path); } - public static SkillNode getNode(ResourceLocation id) { + + public static SkillNode getSkillNode(ResourceLocation id) { return SkillNodeRegistry.get(id); } - public static boolean hasNode(ResourceLocation id) { + + public static AbilityNode getAbilityNode(ResourceLocation id) { + return AbilityNodeRegistry.get(id); + } + + public static boolean hasSkillNode(ResourceLocation id) { return SkillNodeRegistry.get(id) != null; } - public static List getAllNodes() { + + public static boolean hasAbilityNode(ResourceLocation id) { + return AbilityNodeRegistry.get(id) != null; + } + + public static List getAllSkillNodes() { return List.copyOf(SkillNodeRegistry.all()); } + + public static List getAllAbilityNodes() { + return List.copyOf(AbilityNodeRegistry.all()); + } + public static PlayerSkillData getPlayerData(Player player) { return player.getCapability(SkillEngineCapabilities.PLAYER_SKILLS).orElse(null); } - public static Set getUnlockedNodes(Player player) { - PlayerSkillData data = getPlayerData(player); - return data != null ? data.getUnlockedNodes() : Set.of(); - } - + public static int getSkillPoints(Player player) { PlayerSkillData data = getPlayerData(player); return data != null ? data.getSkillPoints() : 0; } - + public static void addSkillPoints(Player player, int amount) { PlayerSkillData data = getPlayerData(player); - if (data != null) { - data.addSkillPoints(amount); - } + if (data != null) data.addSkillPoints(amount); } - + public static boolean consumeSkillPoints(Player player, int amount) { PlayerSkillData data = getPlayerData(player); if (data != null && data.getSkillPoints() >= amount) { @@ -57,26 +74,70 @@ public final class SkillEngineAPI { return false; } - public static boolean isUnlocked(Player player, ResourceLocation nodeId) { + public static Set getUnlockedSkillNodes(Player player) { PlayerSkillData data = getPlayerData(player); - return data != null && data.isUnlocked(nodeId); + return data != null ? data.getUnlockedNodes() : Set.of(); } - - public static boolean unlockNode(Player player, ResourceLocation nodeId) { + + public static boolean isSkillUnlocked(Player player, ResourceLocation id) { + PlayerSkillData data = getPlayerData(player); + return data != null && data.isUnlocked(id); + } + + public static boolean unlockSkill(Player player, ResourceLocation id) { PlayerSkillData data = getPlayerData(player); if (data == null) return false; - - SkillNode node = SkillNodeRegistry.get(nodeId); + SkillNode node = SkillNodeRegistry.get(id); if (node == null) return false; - - if (!net.fretux.skillengine.skilltree.SkillLogic.canUnlock(data, player, node)) { - return false; - } - + if (!SkillLogic.canUnlock(data, player, node)) return false; data.unlockNode(node); return true; } - public static boolean clientHasNode(ResourceLocation nodeId) { - return net.fretux.skillengine.client.SkilltreeClientState.isUnlocked(nodeId); + + public static boolean isAbilityUnlocked(Player player, ResourceLocation id) { + PlayerSkillData data = getPlayerData(player); + return data != null && data.isAbilityUnlocked(id); } -} + + public static Set getUnlockedAbilities(Player player) { + PlayerSkillData data = getPlayerData(player); + return data != null ? data.getUnlockedAbilities() : Set.of(); + } + + public static boolean unlockAbility(Player player, ResourceLocation id) { + PlayerSkillData data = getPlayerData(player); + if (data == null) return false; + AbilityNode ability = AbilityNodeRegistry.get(id); + if (ability == null) return false; + data.unlockAbility(ability); + return true; + } + + public static void bindAbility(Player player, int slot, ResourceLocation abilityId) { + PlayerSkillData data = getPlayerData(player); + if (data == null) return; + data.bindAbility(slot, abilityId); + } + + public static ResourceLocation getAbilityInSlot(Player player, int slot) { + PlayerSkillData data = getPlayerData(player); + return data != null ? data.getAbilityInSlot(slot) : null; + } + + public static ResourceLocation[] getAbilitySlots(Player player) { + PlayerSkillData data = getPlayerData(player); + return data != null ? data.getAbilitySlots() : new ResourceLocation[3]; + } + + public static boolean clientHasSkill(ResourceLocation id) { + return SkilltreeClientState.isUnlocked(id); + } + + public static int clientGetAbilitySlot(ResourceLocation id) { + return SkilltreeClientState.getSlotOfAbility(id); + } + + public static ResourceLocation[] clientGetAbilitySlots() { + return SkilltreeClientState.getAbilitySlots(); + } +} \ No newline at end of file diff --git a/src/main/java/net/fretux/skillengine/capability/PlayerSkillData.java b/src/main/java/net/fretux/skillengine/capability/PlayerSkillData.java index 5e55569..9a74ebd 100644 --- a/src/main/java/net/fretux/skillengine/capability/PlayerSkillData.java +++ b/src/main/java/net/fretux/skillengine/capability/PlayerSkillData.java @@ -17,21 +17,74 @@ public class PlayerSkillData { private final Set unlockedNodes = new HashSet<>(); private final Set unlockedAbilities = new HashSet<>(); private final Set activeTags = new HashSet<>(); + private final ResourceLocation[] abilitySlots = new ResourceLocation[3]; public int getSkillPoints() { return skillPoints; } public void addSkillPoints(int amount) { skillPoints += amount; } + public boolean isUnlocked(ResourceLocation nodeId) { return unlockedNodes.contains(nodeId); } - private final ResourceLocation[] abilitySlots = new ResourceLocation[3]; - public void unlockNode(SkillNode node) { + if (node == null) return; if (unlockedNodes.add(node.getId())) { activeTags.addAll(node.getTags()); skillPoints -= node.getCost(); } } + + public Set getUnlockedNodes() { + return unlockedNodes; + } + + public boolean hasTag(ResourceLocation tag) { + return activeTags.contains(tag); + } + + public Set getActiveTags() { + return activeTags; + } + + public boolean isAbilityUnlocked(ResourceLocation id) { + return unlockedAbilities.contains(id); + } + + public void unlockAbility(AbilityNode ability) { + if (ability == null) return; + unlockedAbilities.add(ability.getId()); + } + + public Set getUnlockedAbilities() { + return unlockedAbilities; + } + + public void bindAbility(int slot, ResourceLocation abilityId) { + bindHelper(slot, abilityId, abilitySlots); + } + + public static void bindHelper(int slot, ResourceLocation abilityId, ResourceLocation[] slots) { + int index = slot - 1; + if (index < 0 || index >= slots.length) return; + if (abilityId != null) { + for (int i = 0; i < slots.length; i++) { + if (abilityId.equals(slots[i])) { + slots[i] = null; + } + } + } + slots[index] = abilityId; + } + + public ResourceLocation getAbilityInSlot(int slot) { + int index = slot - 1; + if (index < 0 || index >= abilitySlots.length) return null; + return abilitySlots[index]; + } + + public ResourceLocation[] getAbilitySlots() { + return abilitySlots.clone(); + } public CompoundTag save() { CompoundTag tag = new CompoundTag(); @@ -42,78 +95,63 @@ public class PlayerSkillData { t.putString("Id", id.toString()); unlockedList.add(t); } - tag.put("Unlocked", unlockedList); - ListTag tagList = new ListTag(); + tag.put("UnlockedNodes", unlockedList); + ListTag tagsList = new ListTag(); for (ResourceLocation id : activeTags) { CompoundTag t = new CompoundTag(); t.putString("Tag", id.toString()); - tagList.add(t); + tagsList.add(t); } - tag.put("Tags", tagList); - ListTag abilityList = new ListTag(); - for (ResourceLocation abilitySlot : abilitySlots) { + tag.put("ActiveTags", tagsList); + ListTag slotList = new ListTag(); + for (ResourceLocation slot : abilitySlots) { CompoundTag t = new CompoundTag(); - if (abilitySlot != null) { - t.putString("Id", abilitySlot.toString()); - } else { - t.putString("Id", ""); - } - abilityList.add(t); + t.putString("Id", slot == null ? "" : slot.toString()); + slotList.add(t); } - tag.put("AbilitySlots", abilityList); - ListTag unlockedAbilitiesList = new ListTag(); + tag.put("AbilitySlots", slotList); + ListTag abilityList = new ListTag(); for (ResourceLocation id : unlockedAbilities) { CompoundTag t = new CompoundTag(); t.putString("Id", id.toString()); - unlockedAbilitiesList.add(t); + abilityList.add(t); } - tag.put("UnlockedAbilities", unlockedAbilitiesList); + tag.put("UnlockedAbilities", abilityList); return tag; } public void load(CompoundTag tag) { skillPoints = tag.getInt("SkillPoints"); unlockedNodes.clear(); - activeTags.clear(); unlockedAbilities.clear(); - ListTag unlockedList = tag.getList("Unlocked", Tag.TAG_COMPOUND); - for (Tag t : unlockedList) { - CompoundTag c = (CompoundTag) t; - unlockedNodes.add(new ResourceLocation(c.getString("Id"))); + activeTags.clear(); + ListTag nodeList = tag.getList("UnlockedNodes", Tag.TAG_COMPOUND); + for (Tag t : nodeList) { + ResourceLocation id = new ResourceLocation(((CompoundTag)t).getString("Id")); + unlockedNodes.add(id); + SkillNode node = SkillNodeRegistry.get(id); + if (node != null) activeTags.addAll(node.getTags()); } - ListTag tagList = tag.getList("Tags", Tag.TAG_COMPOUND); + ListTag tagList = tag.getList("ActiveTags", Tag.TAG_COMPOUND); for (Tag t : tagList) { - CompoundTag c = (CompoundTag) t; - activeTags.add(new ResourceLocation(c.getString("Tag"))); + activeTags.add(new ResourceLocation(((CompoundTag)t).getString("Tag"))); } - ListTag abilityList = tag.getList("AbilitySlots", Tag.TAG_COMPOUND); - for (int i = 0; i < abilitySlots.length && i < abilityList.size(); i++) { - CompoundTag t = abilityList.getCompound(i); - String id = t.getString("Id"); - if (!id.isEmpty()) { - abilitySlots[i] = new ResourceLocation(id); - } else { - abilitySlots[i] = null; - } + ListTag slots = tag.getList("AbilitySlots", Tag.TAG_COMPOUND); + for (int i = 0; i < 3 && i < slots.size(); i++) { + String id = slots.getCompound(i).getString("Id"); + abilitySlots[i] = id.isEmpty() ? null : new ResourceLocation(id); } - ListTag unlockedAbilitiesList = tag.getList("UnlockedAbilities", Tag.TAG_COMPOUND); - for (Tag t : unlockedAbilitiesList) { - CompoundTag c = (CompoundTag) t; - unlockedAbilities.add(new ResourceLocation(c.getString("Id"))); + ListTag abilityList = tag.getList("UnlockedAbilities", Tag.TAG_COMPOUND); + for (Tag t : abilityList) { + unlockedAbilities.add(new ResourceLocation(((CompoundTag)t).getString("Id"))); } } - - public Set getUnlockedNodes() { - return unlockedNodes; - } - + public int getTotalSkillCost() { int total = 0; for (ResourceLocation id : unlockedNodes) { SkillNode node = SkillNodeRegistry.get(id); - if (node != null) { - total += node.getCost(); - } + if (node != null) total += node.getCost(); } return total; } @@ -121,38 +159,6 @@ public class PlayerSkillData { public void clearAllNodes() { unlockedNodes.clear(); activeTags.clear(); - } - - public void bindAbility(int slot, ResourceLocation abilityId) { - bindHelperFunc(slot, abilityId, abilitySlots); - } - - public static void bindHelperFunc(int slot, ResourceLocation abilityId, ResourceLocation[] abilitySlots) { - int index = slot - 1; - if (index < 0 || index >= abilitySlots.length) return; - if (abilityId != null) { - for (int i = 0; i < abilitySlots.length; i++) { - if (abilityId.equals(abilitySlots[i])) { - abilitySlots[i] = null; - } - } - } - abilitySlots[index] = abilityId; - } - - public ResourceLocation[] getAbilitySlots() { - return abilitySlots.clone(); - } - - public boolean isAbilityUnlocked(ResourceLocation id) { - return unlockedAbilities.contains(id); - } - - public void unlockAbility(AbilityNode node) { - unlockedAbilities.add(node.getId()); - } - - public Set getUnlockedAbilities() { - return unlockedAbilities; + unlockedAbilities.clear(); } } \ No newline at end of file diff --git a/src/main/java/net/fretux/skillengine/client/SkilltreeClientState.java b/src/main/java/net/fretux/skillengine/client/SkilltreeClientState.java index c4d93f4..bf992bc 100644 --- a/src/main/java/net/fretux/skillengine/client/SkilltreeClientState.java +++ b/src/main/java/net/fretux/skillengine/client/SkilltreeClientState.java @@ -8,6 +8,7 @@ import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundEvents; +import java.util.Arrays; import java.util.HashSet; import java.util.Set; @@ -66,7 +67,7 @@ public class SkilltreeClientState { } public static void bindAbilityLocal(int slot, ResourceLocation abilityId) { - PlayerSkillData.bindHelperFunc(slot, abilityId, abilitySlots); + PlayerSkillData.bindHelper(slot, abilityId, abilitySlots); } public static void setAbilitySlots(ResourceLocation[] slots) { @@ -88,4 +89,8 @@ public class SkilltreeClientState { } return -1; } + + public static ResourceLocation[] getAbilitySlots() { + return Arrays.copyOf(abilitySlots, abilitySlots.length); + } } \ No newline at end of file