Initial commit

This commit is contained in:
2025-11-24 23:10:25 +01:00
commit 6f06da5f31
41 changed files with 3443 additions and 0 deletions
@@ -0,0 +1,33 @@
package net.fretux.skillengine.network;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.network.NetworkEvent;
import net.fretux.skillengine.client.SkilltreeClientState;
import java.util.function.Supplier;
public class ClientboundNodeUnlockedPacket {
private final ResourceLocation id;
public ClientboundNodeUnlockedPacket(ResourceLocation id) {
this.id = id;
}
public static void encode(ClientboundNodeUnlockedPacket msg, FriendlyByteBuf buf) {
buf.writeResourceLocation(msg.id);
}
public static ClientboundNodeUnlockedPacket decode(FriendlyByteBuf buf) {
return new ClientboundNodeUnlockedPacket(buf.readResourceLocation());
}
public static void handle(ClientboundNodeUnlockedPacket msg, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
SkilltreeClientState.unlockNode(msg.id);
});
ctx.get().setPacketHandled(true);
}
}
@@ -0,0 +1,42 @@
package net.fretux.skillengine.network;
import net.fretux.skillengine.client.SkilltreeClientState;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.network.NetworkEvent;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Supplier;
public class ClientboundSyncSkillsPacket {
private final Set<ResourceLocation> unlocked;
public ClientboundSyncSkillsPacket(Set<ResourceLocation> unlocked) {
this.unlocked = unlocked;
}
public static void encode(ClientboundSyncSkillsPacket msg, FriendlyByteBuf buf) {
buf.writeInt(msg.unlocked.size());
for (ResourceLocation id : msg.unlocked) {
buf.writeResourceLocation(id);
}
}
public static ClientboundSyncSkillsPacket decode(FriendlyByteBuf buf) {
int size = buf.readInt();
Set<ResourceLocation> result = new HashSet<>();
for (int i = 0; i < size; i++) {
result.add(buf.readResourceLocation());
}
return new ClientboundSyncSkillsPacket(result);
}
public static void handle(ClientboundSyncSkillsPacket msg, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
SkilltreeClientState.setUnlocked(msg.unlocked);
});
ctx.get().setPacketHandled(true);
}
}
@@ -0,0 +1,38 @@
package net.fretux.skillengine.network;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.NetworkRegistry;
import net.fretux.skillengine.SkillEngine;
import net.minecraftforge.network.simple.SimpleChannel;
public class PacketHandler {
private static final String PROTOCOL = "1";
public static final SimpleChannel CHANNEL = NetworkRegistry.ChannelBuilder
.named(new ResourceLocation(SkillEngine.MODID, "network"))
.clientAcceptedVersions(PROTOCOL::equals)
.serverAcceptedVersions(PROTOCOL::equals)
.networkProtocolVersion(() -> PROTOCOL)
.simpleChannel();
private static int id = 0;
public static void register() {
CHANNEL.messageBuilder(ServerboundUnlockNodePacket.class, id++, NetworkDirection.PLAY_TO_SERVER)
.encoder(ServerboundUnlockNodePacket::encode)
.decoder(ServerboundUnlockNodePacket::decode)
.consumerMainThread(ServerboundUnlockNodePacket::handle)
.add();
CHANNEL.messageBuilder(ClientboundNodeUnlockedPacket.class, id++, NetworkDirection.PLAY_TO_CLIENT)
.encoder(ClientboundNodeUnlockedPacket::encode)
.decoder(ClientboundNodeUnlockedPacket::decode)
.consumerMainThread(ClientboundNodeUnlockedPacket::handle)
.add();
CHANNEL.messageBuilder(ClientboundSyncSkillsPacket.class, id++, NetworkDirection.PLAY_TO_CLIENT)
.encoder(ClientboundSyncSkillsPacket::encode)
.decoder(ClientboundSyncSkillsPacket::decode)
.consumerMainThread(ClientboundSyncSkillsPacket::handle)
.add();
}
}
@@ -0,0 +1,66 @@
package net.fretux.skillengine.network;
import net.fretux.skillengine.SkillEngine;
import net.fretux.skillengine.capability.SkillEngineCapabilities;
import net.fretux.skillengine.skilltree.SkillLogic;
import net.fretux.skillengine.skilltree.SkillNode;
import net.fretux.skillengine.skilltree.SkillNodeRegistry;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.network.PacketDistributor;
import java.util.function.Supplier;
public class ServerboundUnlockNodePacket {
private final ResourceLocation nodeId;
public ServerboundUnlockNodePacket(ResourceLocation nodeId) {
this.nodeId = nodeId;
}
public static void encode(ServerboundUnlockNodePacket msg, FriendlyByteBuf buf) {
buf.writeResourceLocation(msg.nodeId);
}
public static ServerboundUnlockNodePacket decode(FriendlyByteBuf buf) {
return new ServerboundUnlockNodePacket(buf.readResourceLocation());
}
public static void handle(ServerboundUnlockNodePacket msg, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
ServerPlayer player = ctx.get().getSender();
if (player == null) {
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());
if (SkillLogic.canUnlock(data, node)) {
data.unlockNode(node);
SkillEngine.LOGGER.info("Node {} unlocked for {}", msg.nodeId, player.getGameProfile().getName());
PacketHandler.CHANNEL.send(
PacketDistributor.PLAYER.with(() -> player),
new ClientboundNodeUnlockedPacket(node.getId())
);
} else {
SkillEngine.LOGGER.info("Cannot unlock node {} for {} (canUnlock=false)",
msg.nodeId, player.getGameProfile().getName());
}
});
});
ctx.get().setPacketHandled(true);
}
}