Update SkillEngine API

This commit is contained in:
2025-12-05 20:24:06 +01:00
parent 9ce984a54a
commit d4ad08a35a
4 changed files with 181 additions and 109 deletions
+1 -1
View File
@@ -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. # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=All Rights Reserved mod_license=All Rights Reserved
# The mod version. See https://semver.org/ # 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. # 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. # This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html # See https://maven.apache.org/guides/mini/guide-naming-conventions.html
@@ -3,6 +3,10 @@ package net.fretux.skillengine.api;
import net.fretux.skillengine.SkillEngine; import net.fretux.skillengine.SkillEngine;
import net.fretux.skillengine.capability.PlayerSkillData; import net.fretux.skillengine.capability.PlayerSkillData;
import net.fretux.skillengine.capability.SkillEngineCapabilities; 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.SkillNode;
import net.fretux.skillengine.skilltree.SkillNodeRegistry; import net.fretux.skillengine.skilltree.SkillNodeRegistry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
@@ -10,31 +14,46 @@ import net.minecraft.world.entity.player.Player;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
public final class SkillEngineAPI { public final class SkillEngineAPI {
private SkillEngineAPI() {} private SkillEngineAPI() {}
public static ResourceLocation id(String path) { public static ResourceLocation id(String path) {
return new ResourceLocation(SkillEngine.MODID, path); return new ResourceLocation(SkillEngine.MODID, path);
} }
public static ResourceLocation rl(String namespace, String path) { public static ResourceLocation rl(String namespace, String path) {
return new ResourceLocation(namespace, path); return new ResourceLocation(namespace, path);
} }
public static SkillNode getNode(ResourceLocation id) {
public static SkillNode getSkillNode(ResourceLocation id) {
return SkillNodeRegistry.get(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; return SkillNodeRegistry.get(id) != null;
} }
public static List<SkillNode> getAllNodes() {
public static boolean hasAbilityNode(ResourceLocation id) {
return AbilityNodeRegistry.get(id) != null;
}
public static List<SkillNode> getAllSkillNodes() {
return List.copyOf(SkillNodeRegistry.all()); return List.copyOf(SkillNodeRegistry.all());
} }
public static List<AbilityNode> getAllAbilityNodes() {
return List.copyOf(AbilityNodeRegistry.all());
}
public static PlayerSkillData getPlayerData(Player player) { public static PlayerSkillData getPlayerData(Player player) {
return player.getCapability(SkillEngineCapabilities.PLAYER_SKILLS).orElse(null); return player.getCapability(SkillEngineCapabilities.PLAYER_SKILLS).orElse(null);
} }
public static Set<ResourceLocation> getUnlockedNodes(Player player) {
PlayerSkillData data = getPlayerData(player);
return data != null ? data.getUnlockedNodes() : Set.of();
}
public static int getSkillPoints(Player player) { public static int getSkillPoints(Player player) {
PlayerSkillData data = getPlayerData(player); PlayerSkillData data = getPlayerData(player);
@@ -43,9 +62,7 @@ public final class SkillEngineAPI {
public static void addSkillPoints(Player player, int amount) { public static void addSkillPoints(Player player, int amount) {
PlayerSkillData data = getPlayerData(player); PlayerSkillData data = getPlayerData(player);
if (data != null) { if (data != null) data.addSkillPoints(amount);
data.addSkillPoints(amount);
}
} }
public static boolean consumeSkillPoints(Player player, int amount) { public static boolean consumeSkillPoints(Player player, int amount) {
@@ -57,26 +74,70 @@ public final class SkillEngineAPI {
return false; return false;
} }
public static boolean isUnlocked(Player player, ResourceLocation nodeId) { public static Set<ResourceLocation> getUnlockedSkillNodes(Player player) {
PlayerSkillData data = getPlayerData(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); PlayerSkillData data = getPlayerData(player);
if (data == null) return false; if (data == null) return false;
SkillNode node = SkillNodeRegistry.get(id);
SkillNode node = SkillNodeRegistry.get(nodeId);
if (node == null) return false; if (node == null) return false;
if (!SkillLogic.canUnlock(data, player, node)) return false;
if (!net.fretux.skillengine.skilltree.SkillLogic.canUnlock(data, player, node)) {
return false;
}
data.unlockNode(node); data.unlockNode(node);
return true; 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<ResourceLocation> 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();
} }
} }
@@ -17,22 +17,75 @@ public class PlayerSkillData {
private final Set<ResourceLocation> unlockedNodes = new HashSet<>(); private final Set<ResourceLocation> unlockedNodes = new HashSet<>();
private final Set<ResourceLocation> unlockedAbilities = new HashSet<>(); private final Set<ResourceLocation> unlockedAbilities = new HashSet<>();
private final Set<ResourceLocation> activeTags = new HashSet<>(); private final Set<ResourceLocation> activeTags = new HashSet<>();
private final ResourceLocation[] abilitySlots = new ResourceLocation[3];
public int getSkillPoints() { return skillPoints; } public int getSkillPoints() { return skillPoints; }
public void addSkillPoints(int amount) { skillPoints += amount; } public void addSkillPoints(int amount) { skillPoints += amount; }
public boolean isUnlocked(ResourceLocation nodeId) { public boolean isUnlocked(ResourceLocation nodeId) {
return unlockedNodes.contains(nodeId); return unlockedNodes.contains(nodeId);
} }
private final ResourceLocation[] abilitySlots = new ResourceLocation[3];
public void unlockNode(SkillNode node) { public void unlockNode(SkillNode node) {
if (node == null) return;
if (unlockedNodes.add(node.getId())) { if (unlockedNodes.add(node.getId())) {
activeTags.addAll(node.getTags()); activeTags.addAll(node.getTags());
skillPoints -= node.getCost(); skillPoints -= node.getCost();
} }
} }
public Set<ResourceLocation> getUnlockedNodes() {
return unlockedNodes;
}
public boolean hasTag(ResourceLocation tag) {
return activeTags.contains(tag);
}
public Set<ResourceLocation> 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<ResourceLocation> 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() { public CompoundTag save() {
CompoundTag tag = new CompoundTag(); CompoundTag tag = new CompoundTag();
tag.putInt("SkillPoints", skillPoints); tag.putInt("SkillPoints", skillPoints);
@@ -42,78 +95,63 @@ public class PlayerSkillData {
t.putString("Id", id.toString()); t.putString("Id", id.toString());
unlockedList.add(t); unlockedList.add(t);
} }
tag.put("Unlocked", unlockedList); tag.put("UnlockedNodes", unlockedList);
ListTag tagList = new ListTag(); ListTag tagsList = new ListTag();
for (ResourceLocation id : activeTags) { for (ResourceLocation id : activeTags) {
CompoundTag t = new CompoundTag(); CompoundTag t = new CompoundTag();
t.putString("Tag", id.toString()); t.putString("Tag", id.toString());
tagList.add(t); tagsList.add(t);
} }
tag.put("Tags", tagList); tag.put("ActiveTags", tagsList);
ListTag abilityList = new ListTag(); ListTag slotList = new ListTag();
for (ResourceLocation abilitySlot : abilitySlots) { for (ResourceLocation slot : abilitySlots) {
CompoundTag t = new CompoundTag(); CompoundTag t = new CompoundTag();
if (abilitySlot != null) { t.putString("Id", slot == null ? "" : slot.toString());
t.putString("Id", abilitySlot.toString()); slotList.add(t);
} else {
t.putString("Id", "");
} }
abilityList.add(t); tag.put("AbilitySlots", slotList);
} ListTag abilityList = new ListTag();
tag.put("AbilitySlots", abilityList);
ListTag unlockedAbilitiesList = new ListTag();
for (ResourceLocation id : unlockedAbilities) { for (ResourceLocation id : unlockedAbilities) {
CompoundTag t = new CompoundTag(); CompoundTag t = new CompoundTag();
t.putString("Id", id.toString()); t.putString("Id", id.toString());
unlockedAbilitiesList.add(t); abilityList.add(t);
} }
tag.put("UnlockedAbilities", unlockedAbilitiesList); tag.put("UnlockedAbilities", abilityList);
return tag; return tag;
} }
public void load(CompoundTag tag) { public void load(CompoundTag tag) {
skillPoints = tag.getInt("SkillPoints"); skillPoints = tag.getInt("SkillPoints");
unlockedNodes.clear(); unlockedNodes.clear();
activeTags.clear();
unlockedAbilities.clear(); unlockedAbilities.clear();
ListTag unlockedList = tag.getList("Unlocked", Tag.TAG_COMPOUND); activeTags.clear();
for (Tag t : unlockedList) { ListTag nodeList = tag.getList("UnlockedNodes", Tag.TAG_COMPOUND);
CompoundTag c = (CompoundTag) t; for (Tag t : nodeList) {
unlockedNodes.add(new ResourceLocation(c.getString("Id"))); 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) { for (Tag t : tagList) {
CompoundTag c = (CompoundTag) t; activeTags.add(new ResourceLocation(((CompoundTag)t).getString("Tag")));
activeTags.add(new ResourceLocation(c.getString("Tag")));
} }
ListTag abilityList = tag.getList("AbilitySlots", Tag.TAG_COMPOUND); ListTag slots = tag.getList("AbilitySlots", Tag.TAG_COMPOUND);
for (int i = 0; i < abilitySlots.length && i < abilityList.size(); i++) { for (int i = 0; i < 3 && i < slots.size(); i++) {
CompoundTag t = abilityList.getCompound(i); String id = slots.getCompound(i).getString("Id");
String id = t.getString("Id"); abilitySlots[i] = id.isEmpty() ? null : new ResourceLocation(id);
if (!id.isEmpty()) {
abilitySlots[i] = new ResourceLocation(id);
} else {
abilitySlots[i] = null;
} }
ListTag abilityList = tag.getList("UnlockedAbilities", Tag.TAG_COMPOUND);
for (Tag t : abilityList) {
unlockedAbilities.add(new ResourceLocation(((CompoundTag)t).getString("Id")));
} }
ListTag unlockedAbilitiesList = tag.getList("UnlockedAbilities", Tag.TAG_COMPOUND);
for (Tag t : unlockedAbilitiesList) {
CompoundTag c = (CompoundTag) t;
unlockedAbilities.add(new ResourceLocation(c.getString("Id")));
}
}
public Set<ResourceLocation> getUnlockedNodes() {
return unlockedNodes;
} }
public int getTotalSkillCost() { public int getTotalSkillCost() {
int total = 0; int total = 0;
for (ResourceLocation id : unlockedNodes) { for (ResourceLocation id : unlockedNodes) {
SkillNode node = SkillNodeRegistry.get(id); SkillNode node = SkillNodeRegistry.get(id);
if (node != null) { if (node != null) total += node.getCost();
total += node.getCost();
}
} }
return total; return total;
} }
@@ -121,38 +159,6 @@ public class PlayerSkillData {
public void clearAllNodes() { public void clearAllNodes() {
unlockedNodes.clear(); unlockedNodes.clear();
activeTags.clear(); activeTags.clear();
} unlockedAbilities.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<ResourceLocation> getUnlockedAbilities() {
return unlockedAbilities;
} }
} }
@@ -8,6 +8,7 @@ import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundEvents;
import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@@ -66,7 +67,7 @@ public class SkilltreeClientState {
} }
public static void bindAbilityLocal(int slot, ResourceLocation abilityId) { public static void bindAbilityLocal(int slot, ResourceLocation abilityId) {
PlayerSkillData.bindHelperFunc(slot, abilityId, abilitySlots); PlayerSkillData.bindHelper(slot, abilityId, abilitySlots);
} }
public static void setAbilitySlots(ResourceLocation[] slots) { public static void setAbilitySlots(ResourceLocation[] slots) {
@@ -88,4 +89,8 @@ public class SkilltreeClientState {
} }
return -1; return -1;
} }
public static ResourceLocation[] getAbilitySlots() {
return Arrays.copyOf(abilitySlots, abilitySlots.length);
}
} }