Implement detection for resetting points and add remaining skillpoints in tree screen

This commit is contained in:
2025-11-25 22:23:14 +01:00
parent ec49eb4385
commit dd15eb9327
9 changed files with 116 additions and 8 deletions
@@ -12,9 +12,11 @@ import java.util.function.Supplier;
public class ClientboundSyncSkillsPacket {
private final Set<ResourceLocation> unlocked;
private final int skillPoints;
public ClientboundSyncSkillsPacket(Set<ResourceLocation> unlocked) {
public ClientboundSyncSkillsPacket(Set<ResourceLocation> unlocked, int skillPoints) {
this.unlocked = unlocked;
this.skillPoints = skillPoints;
}
public static void encode(ClientboundSyncSkillsPacket msg, FriendlyByteBuf buf) {
@@ -22,6 +24,7 @@ public class ClientboundSyncSkillsPacket {
for (ResourceLocation id : msg.unlocked) {
buf.writeResourceLocation(id);
}
buf.writeInt(msg.skillPoints);
}
public static ClientboundSyncSkillsPacket decode(FriendlyByteBuf buf) {
@@ -30,13 +33,15 @@ public class ClientboundSyncSkillsPacket {
for (int i = 0; i < size; i++) {
result.add(buf.readResourceLocation());
}
return new ClientboundSyncSkillsPacket(result);
int points = buf.readInt();
return new ClientboundSyncSkillsPacket(result, points);
}
public static void handle(ClientboundSyncSkillsPacket msg, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
SkilltreeClientState.setUnlocked(msg.unlocked);
SkilltreeClientState.setCurrentSkillPoints(msg.skillPoints);
});
ctx.get().setPacketHandled(true);
}
}
}
@@ -1,9 +1,12 @@
package net.fretux.skillengine.network;
import net.fretux.skillengine.capability.SkillEngineCapabilities;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.NetworkRegistry;
import net.fretux.skillengine.SkillEngine;
import net.minecraftforge.network.PacketDistributor;
import net.minecraftforge.network.simple.SimpleChannel;
public class PacketHandler {
@@ -35,4 +38,12 @@ public class PacketHandler {
.consumerMainThread(ClientboundSyncSkillsPacket::handle)
.add();
}
public static void syncSkillsTo(ServerPlayer player) {
player.getCapability(SkillEngineCapabilities.PLAYER_SKILLS).ifPresent(data -> {
ClientboundSyncSkillsPacket packet =
new ClientboundSyncSkillsPacket(data.getUnlockedNodes(), data.getSkillPoints());
CHANNEL.send(PacketDistributor.PLAYER.with(() -> player), packet);
});
}
}
@@ -36,13 +36,11 @@ public class ServerboundUnlockNodePacket {
SkillEngine.LOGGER.warn("Unlock packet received but sender was null");
return;
}
SkillNode node = SkillNodeRegistry.get(msg.nodeId);
if (node == null) {
SkillEngine.LOGGER.warn("Unlock packet for unknown node {}", msg.nodeId);
return;
}
player.getCapability(SkillEngineCapabilities.PLAYER_SKILLS).ifPresent(data -> {
SkillEngine.LOGGER.info("Received unlock request for {} from {} (points={})",
msg.nodeId, player.getGameProfile().getName(), data.getSkillPoints());