Add skill and ability unlock checks and logic
This commit is contained in:
+1
-1
@@ -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.2.2
|
mod_version=1.2.3
|
||||||
# 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
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package net.fretux.skillengine.network;
|
package net.fretux.skillengine.network;
|
||||||
|
|
||||||
|
import net.fretux.ascend.player.PlayerStats;
|
||||||
|
import net.fretux.ascend.player.PlayerStatsProvider;
|
||||||
import net.fretux.skillengine.SkillEngine;
|
import net.fretux.skillengine.SkillEngine;
|
||||||
|
import net.fretux.skillengine.capability.PlayerSkillData;
|
||||||
import net.fretux.skillengine.capability.SkillEngineCapabilities;
|
import net.fretux.skillengine.capability.SkillEngineCapabilities;
|
||||||
import net.fretux.skillengine.skilltree.*;
|
import net.fretux.skillengine.skilltree.*;
|
||||||
import net.minecraft.network.FriendlyByteBuf;
|
import net.minecraft.network.FriendlyByteBuf;
|
||||||
@@ -9,13 +12,16 @@ import net.minecraft.server.level.ServerPlayer;
|
|||||||
import net.minecraftforge.network.NetworkEvent;
|
import net.minecraftforge.network.NetworkEvent;
|
||||||
import net.minecraftforge.network.PacketDistributor;
|
import net.minecraftforge.network.PacketDistributor;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class ServerboundUnlockNodePacket {
|
public class ServerboundUnlockNodePacket {
|
||||||
private final ResourceLocation nodeId;
|
private final ResourceLocation nodeId;
|
||||||
|
|
||||||
public ServerboundUnlockNodePacket(ResourceLocation nodeId) {
|
public ServerboundUnlockNodePacket(ResourceLocation nodeId) {
|
||||||
this.nodeId = nodeId;
|
this.nodeId = nodeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void encode(ServerboundUnlockNodePacket msg, FriendlyByteBuf buf) {
|
public static void encode(ServerboundUnlockNodePacket msg, FriendlyByteBuf buf) {
|
||||||
buf.writeResourceLocation(msg.nodeId);
|
buf.writeResourceLocation(msg.nodeId);
|
||||||
}
|
}
|
||||||
@@ -26,50 +32,74 @@ public class ServerboundUnlockNodePacket {
|
|||||||
|
|
||||||
public static void handle(ServerboundUnlockNodePacket msg, Supplier<NetworkEvent.Context> ctx) {
|
public static void handle(ServerboundUnlockNodePacket msg, Supplier<NetworkEvent.Context> ctx) {
|
||||||
ctx.get().enqueueWork(() -> {
|
ctx.get().enqueueWork(() -> {
|
||||||
|
|
||||||
ServerPlayer player = ctx.get().getSender();
|
ServerPlayer player = ctx.get().getSender();
|
||||||
if (player == null) {
|
if (player == null) return;
|
||||||
SkillEngine.LOGGER.warn("Unlock packet received but sender was null");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
SkillNode node = SkillNodeRegistry.get(msg.nodeId);
|
SkillNode node = SkillNodeRegistry.get(msg.nodeId);
|
||||||
AbilityNode ability = AbilityNodeRegistry.get(msg.nodeId);
|
AbilityNode ability = AbilityNodeRegistry.get(msg.nodeId);
|
||||||
if (node == null && ability == null) {
|
|
||||||
SkillEngine.LOGGER.warn("Unlock packet for unknown node or ability {}", msg.nodeId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
player.getCapability(SkillEngineCapabilities.PLAYER_SKILLS).ifPresent(data -> {
|
|
||||||
if (node != null) {
|
|
||||||
SkillEngine.LOGGER.info("Received skill unlock request for {} from {} (points={})",
|
|
||||||
msg.nodeId, player.getGameProfile().getName(), data.getSkillPoints());
|
|
||||||
if (SkillLogic.canUnlock(data, player, node)) {
|
|
||||||
data.unlockNode(node);
|
|
||||||
|
|
||||||
|
player.getCapability(SkillEngineCapabilities.PLAYER_SKILLS).ifPresent(data -> {
|
||||||
|
|
||||||
|
// === SKILL NODE UNLOCK ===
|
||||||
|
if (node != null) {
|
||||||
|
if (canUnlockSkillNode(player, data, node)) {
|
||||||
|
data.unlockNode(node);
|
||||||
|
PacketHandler.syncSkillsTo(player);
|
||||||
PacketHandler.CHANNEL.send(
|
PacketHandler.CHANNEL.send(
|
||||||
PacketDistributor.PLAYER.with(() -> player),
|
PacketDistributor.PLAYER.with(() -> player),
|
||||||
new ClientboundNodeUnlockedPacket(node.getId(), data.getSkillPoints())
|
new ClientboundNodeUnlockedPacket(node.getId(), data.getSkillPoints())
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
SkillEngine.LOGGER.info("Cannot unlock skill node {} for {}",
|
SkillEngine.LOGGER.info("[SKILLENGINE] Rejecting unlock for {}", node.getId());
|
||||||
msg.nodeId, player.getGameProfile().getName());
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// === ABILITY NODE UNLOCK ===
|
||||||
if (ability != null) {
|
if (ability != null) {
|
||||||
SkillEngine.LOGGER.info("Received ability unlock request for {} from {} (points={})",
|
if (canUnlockAbility(player, data, ability)) {
|
||||||
msg.nodeId, player.getGameProfile().getName(), data.getSkillPoints());
|
|
||||||
if (!data.isAbilityUnlocked(ability.getId())) {
|
|
||||||
data.unlockAbility(ability);
|
data.unlockAbility(ability);
|
||||||
|
PacketHandler.syncSkillsTo(player);
|
||||||
PacketHandler.CHANNEL.send(
|
PacketHandler.CHANNEL.send(
|
||||||
PacketDistributor.PLAYER.with(() -> player),
|
PacketDistributor.PLAYER.with(() -> player),
|
||||||
new ClientboundNodeUnlockedPacket(ability.getId(), data.getSkillPoints())
|
new ClientboundNodeUnlockedPacket(ability.getId(), data.getSkillPoints())
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
SkillEngine.LOGGER.info("Ability {} already unlocked for {}",
|
SkillEngine.LOGGER.info("[SKILLENGINE] Rejecting ability unlock for {}", ability.getId());
|
||||||
msg.nodeId, player.getGameProfile().getName());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
ctx.get().setPacketHandled(true);
|
ctx.get().setPacketHandled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean canUnlockSkillNode(ServerPlayer player, PlayerSkillData data, SkillNode node) {
|
||||||
|
if (data.isUnlocked(node.getId())) return false;
|
||||||
|
if (data.getSkillPoints() < node.getCost()) return false;
|
||||||
|
for (ResourceLocation ex : node.getExclusiveWith()) {
|
||||||
|
if (data.isUnlocked(ex)) return false;
|
||||||
|
}
|
||||||
|
for (ResourceLocation parent : node.getLinks()) {
|
||||||
|
if (!data.isUnlocked(parent)) return false;
|
||||||
|
}
|
||||||
|
PlayerStats stats = player.getCapability(PlayerStatsProvider.PLAYER_STATS).orElse(null);
|
||||||
|
if (stats == null) return false;
|
||||||
|
for (var e : node.getPrereqAttributes().entrySet()) {
|
||||||
|
if (stats.getAttributeLevel(e.getKey()) < e.getValue()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static boolean canUnlockAbility(ServerPlayer player, PlayerSkillData data, AbilityNode ability) {
|
||||||
|
if (data.isAbilityUnlocked(ability.getId())) return false;
|
||||||
|
for (ResourceLocation parent : ability.getLinks()) {
|
||||||
|
boolean ok = data.isUnlocked(parent) || data.isAbilityUnlocked(parent);
|
||||||
|
if (!ok) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user