extend ability system to support dynamic slot counts, add skill tree validation, new prerequisites API, and extended mod commands

This commit is contained in:
2026-05-25 12:43:13 +02:00
parent 77fbada438
commit 7f5830d409
8 changed files with 56 additions and 22 deletions
@@ -20,6 +20,10 @@ import java.util.function.Supplier;
public class ClientboundSyncSkillDefinitionsPacket {
private static final int MAX_SYNCED_NODES = 4096;
private static final int MAX_LIST_ENTRIES = 512;
private static final int MAX_PREREQUISITES = 256;
private final List<SkillNode> skillNodes;
private final List<AbilityNode> abilityNodes;
@@ -42,13 +46,13 @@ public class ClientboundSyncSkillDefinitionsPacket {
}
public static ClientboundSyncSkillDefinitionsPacket decode(FriendlyByteBuf buf) {
int skillNodeCount = buf.readInt();
int skillNodeCount = readBoundedCount(buf, MAX_SYNCED_NODES, "skill node definitions");
List<SkillNode> skillNodes = new ArrayList<>(skillNodeCount);
for (int i = 0; i < skillNodeCount; i++) {
skillNodes.add(readSkillNode(buf));
}
int abilityNodeCount = buf.readInt();
int abilityNodeCount = readBoundedCount(buf, MAX_SYNCED_NODES, "ability node definitions");
List<AbilityNode> abilityNodes = new ArrayList<>(abilityNodeCount);
for (int i = 0; i < abilityNodeCount; i++) {
abilityNodes.add(readAbilityNode(buf));
@@ -136,7 +140,7 @@ public class ClientboundSyncSkillDefinitionsPacket {
}
private static List<ResourceLocation> readResourceLocationList(FriendlyByteBuf buf) {
int size = buf.readInt();
int size = readBoundedCount(buf, MAX_LIST_ENTRIES, "resource location list");
List<ResourceLocation> ids = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
ids.add(buf.readResourceLocation());
@@ -153,11 +157,19 @@ public class ClientboundSyncSkillDefinitionsPacket {
}
private static Map<String, Integer> readPrerequisites(FriendlyByteBuf buf) {
int size = buf.readInt();
int size = readBoundedCount(buf, MAX_PREREQUISITES, "prerequisites");
Map<String, Integer> prerequisites = new HashMap<>(size);
for (int i = 0; i < size; i++) {
prerequisites.put(buf.readUtf(), buf.readInt());
}
return prerequisites;
}
private static int readBoundedCount(FriendlyByteBuf buf, int max, String fieldName) {
int count = buf.readInt();
if (count < 0 || count > max) {
throw new IllegalArgumentException("Invalid " + fieldName + " count: " + count);
}
return count;
}
}
@@ -12,6 +12,9 @@ import java.util.function.Supplier;
public class ClientboundSyncSkillsPacket {
private static final int MAX_SYNCED_NODES = 4096;
private static final int MAX_ABILITY_SLOTS = 64;
private final Set<ResourceLocation> unlocked;
private final Set<ResourceLocation> unlockedAbilities;
private final int skillPoints;
@@ -46,15 +49,15 @@ public class ClientboundSyncSkillsPacket {
}
public static ClientboundSyncSkillsPacket decode(FriendlyByteBuf buf) {
int size = buf.readInt();
int size = readBoundedCount(buf, MAX_SYNCED_NODES, "unlocked skill nodes");
Set<ResourceLocation> unlocked = new HashSet<>();
for (int i = 0; i < size; i++) unlocked.add(buf.readResourceLocation());
int abilitySize = buf.readInt();
int abilitySize = readBoundedCount(buf, MAX_SYNCED_NODES, "unlocked abilities");
Set<ResourceLocation> unlockedAbilities = new HashSet<>();
for (int i = 0; i < abilitySize; i++) unlockedAbilities.add(buf.readResourceLocation());
int points = buf.readInt();
int slotsCount = buf.readInt();
ResourceLocation[] slots = new ResourceLocation[Math.max(slotsCount, 0)];
int slotsCount = readBoundedCount(buf, MAX_ABILITY_SLOTS, "ability slots");
ResourceLocation[] slots = new ResourceLocation[slotsCount];
for (int i = 0; i < slots.length; i++) {
boolean present = buf.readBoolean();
slots[i] = present ? buf.readResourceLocation() : null;
@@ -76,4 +79,12 @@ public class ClientboundSyncSkillsPacket {
});
ctx.get().setPacketHandled(true);
}
private static int readBoundedCount(FriendlyByteBuf buf, int max, String fieldName) {
int count = buf.readInt();
if (count < 0 || count > max) {
throw new IllegalArgumentException("Invalid " + fieldName + " count: " + count);
}
return count;
}
}
@@ -46,10 +46,11 @@ public class ServerboundActivateAbilityPacket {
int cd = data.getCooldown(msg.slot);
if (cd > 0) return;
ability.execute(player);
data.setCooldown(msg.slot, ability.getCooldown());
int cooldown = Math.max(0, ability.getCooldown());
data.setCooldown(msg.slot, cooldown);
PacketHandler.CHANNEL.send(
PacketDistributor.PLAYER.with(() -> player),
new ClientboundSyncCooldownPacket(msg.slot, ability.getCooldown())
new ClientboundSyncCooldownPacket(msg.slot, cooldown)
);
});
});
@@ -42,10 +42,11 @@ public class ServerboundUnlockNodePacket {
if (unlockPlan != null) {
for (SkillNode plannedNode : unlockPlan) {
data.unlockNode(plannedNode);
if (plannedNode.getCost() > 0) {
int cost = Math.max(0, plannedNode.getCost());
if (cost > 0) {
MinecraftForge.EVENT_BUS.post(new SkillPointsChangedEvent(
player,
plannedNode.getCost(),
cost,
SkillPointsChangedEvent.Reason.SPENT,
plannedNode.getId()
));