extend skill unlock packet to include ability type and improve logout handling logic

This commit is contained in:
2026-05-21 16:32:11 +02:00
parent 30310b6eb9
commit bf4452f6ac
7 changed files with 32 additions and 15 deletions
@@ -13,20 +13,19 @@ import net.minecraftforge.network.PacketDistributor;
@Mod.EventBusSubscriber(modid = SkillEngine.MODID)
public class AbilityCooldownHandler {
private static int syncCounter = 0;
@SubscribeEvent
public static void onServerTick(TickEvent.PlayerTickEvent event) {
if (event.phase != TickEvent.Phase.END) return;
if (event.player.level().isClientSide) return;
if (!(event.player instanceof ServerPlayer player)) return;
event.player.getCapability(SkillEngineCapabilities.PLAYER_SKILLS).ifPresent(data -> {
data.tickCooldowns();
syncCounter++;
if (syncCounter >= 10) {
syncCounter = 0;
data.tickTagCooldowns();
if (player.tickCount % 10 == 0) {
for (int i = 1; i <= 3; i++) {
PacketHandler.CHANNEL.send(
PacketDistributor.PLAYER.with(() -> (ServerPlayer) event.player),
PacketDistributor.PLAYER.with(() -> player),
new ClientboundSyncCooldownPacket(i, data.getCooldown(i))
);
}
@@ -69,6 +69,7 @@ public final class SkillEngineAPI {
}
public static boolean consumeSkillPoints(Player player, int amount) {
if (amount <= 0) return false;
PlayerSkillData data = getPlayerData(player);
if (data != null && data.getSkillPoints() >= amount) {
data.addSkillPoints(-amount);
@@ -34,6 +34,12 @@ public class AscendCompatHandler {
initAscendLevel(event.getEntity());
}
@SubscribeEvent
public static void onPlayerLogout(PlayerEvent.PlayerLoggedOutEvent event) {
if (event.getEntity().level().isClientSide) return;
LAST_ASCEND_LEVEL.remove(event.getEntity().getUUID());
}
@SubscribeEvent
public static void onPlayerClone(PlayerEvent.Clone event) {
if (event.getEntity().level().isClientSide) return;
@@ -8,6 +8,7 @@ import net.fretux.skillengine.network.PacketHandler;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@@ -51,6 +52,14 @@ public class AscendResetDetector {
});
}
@SubscribeEvent
public static void onPlayerLogout(PlayerEvent.PlayerLoggedOutEvent event) {
if (event.getEntity().level().isClientSide) return;
UUID id = event.getEntity().getUUID();
LAST_ATTRIBUTE_SUM.remove(id);
LAST_UNSPENT.remove(id);
}
private static void resetSkillEngine(PlayerSkillData data, Player player) {
int refund = data.getTotalSkillCost();
data.addSkillPoints(refund);
@@ -1,6 +1,5 @@
package net.fretux.skillengine.network;
import net.fretux.skillengine.skilltree.AbilityNodeRegistry;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
@@ -12,32 +11,35 @@ import java.util.function.Supplier;
public class ClientboundNodeUnlockedPacket {
private final ResourceLocation id;
private final int newSkillPoints;
private final boolean ability;
public ClientboundNodeUnlockedPacket(ResourceLocation id, int newSkillPoints) {
public ClientboundNodeUnlockedPacket(ResourceLocation id, int newSkillPoints, boolean ability) {
this.id = id;
this.newSkillPoints = newSkillPoints;
this.ability = ability;
}
public static void encode(ClientboundNodeUnlockedPacket msg, FriendlyByteBuf buf) {
buf.writeResourceLocation(msg.id);
buf.writeInt(msg.newSkillPoints);
buf.writeBoolean(msg.ability);
}
public static ClientboundNodeUnlockedPacket decode(FriendlyByteBuf buf) {
ResourceLocation id = buf.readResourceLocation();
int points = buf.readInt();
return new ClientboundNodeUnlockedPacket(id, points);
boolean ability = buf.readBoolean();
return new ClientboundNodeUnlockedPacket(id, points, ability);
}
public static void handle(ClientboundNodeUnlockedPacket msg, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
boolean isAbility = AbilityNodeRegistry.get(msg.id) != null;
DistExecutor.unsafeRunWhenOn(
Dist.CLIENT,
() -> () -> net.fretux.skillengine.client.ClientSkillEngineBridge.handleNodeUnlocked(
msg.id,
msg.newSkillPoints,
isAbility
msg.ability
)
);
});
@@ -13,7 +13,7 @@ import net.minecraftforge.network.simple.SimpleChannel;
public class PacketHandler {
private static final String PROTOCOL = "2";
private static final String PROTOCOL = "3";
public static final SimpleChannel CHANNEL = NetworkRegistry.ChannelBuilder
.named(ResourceLocation.fromNamespaceAndPath(SkillEngine.MODID, "network"))
.clientAcceptedVersions(PROTOCOL::equals)
@@ -44,7 +44,7 @@ public class ServerboundUnlockNodePacket {
PacketHandler.syncSkillsTo(player);
PacketHandler.CHANNEL.send(
PacketDistributor.PLAYER.with(() -> player),
new ClientboundNodeUnlockedPacket(node.getId(), data.getSkillPoints())
new ClientboundNodeUnlockedPacket(node.getId(), data.getSkillPoints(), false)
);
} else {
SkillEngine.LOGGER.info("[SKILLENGINE] Rejecting unlock for {}", node.getId());
@@ -57,7 +57,7 @@ public class ServerboundUnlockNodePacket {
PacketHandler.syncSkillsTo(player);
PacketHandler.CHANNEL.send(
PacketDistributor.PLAYER.with(() -> player),
new ClientboundNodeUnlockedPacket(ability.getId(), data.getSkillPoints())
new ClientboundNodeUnlockedPacket(ability.getId(), data.getSkillPoints(), true)
);
} else {
SkillEngine.LOGGER.info("[SKILLENGINE] Rejecting ability unlock for {}", ability.getId());