Implement mutual exclusives
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package net.fretux.skillengine.network;
|
||||
|
||||
import net.fretux.skillengine.skilltree.AbilityNodeRegistry;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
@@ -30,8 +31,10 @@ public class ClientboundNodeUnlockedPacket {
|
||||
|
||||
public static void handle(ClientboundNodeUnlockedPacket msg, Supplier<NetworkEvent.Context> ctx) {
|
||||
ctx.get().enqueueWork(() -> {
|
||||
// Update client-side unlocked set and points immediately so UI can reflect changes this frame
|
||||
SkilltreeClientState.unlockNode(msg.id);
|
||||
if (AbilityNodeRegistry.get(msg.id) != null)
|
||||
SkilltreeClientState.unlockAbility(msg.id);
|
||||
else
|
||||
SkilltreeClientState.unlockNode(msg.id);
|
||||
SkilltreeClientState.setCurrentSkillPoints(msg.newSkillPoints);
|
||||
});
|
||||
ctx.get().setPacketHandled(true);
|
||||
|
||||
@@ -12,35 +12,61 @@ import java.util.function.Supplier;
|
||||
public class ClientboundSyncSkillsPacket {
|
||||
|
||||
private final Set<ResourceLocation> unlocked;
|
||||
private final Set<ResourceLocation> unlockedAbilities;
|
||||
private final int skillPoints;
|
||||
private final ResourceLocation[] abilitySlots;
|
||||
|
||||
public ClientboundSyncSkillsPacket(Set<ResourceLocation> unlocked, int skillPoints) {
|
||||
public ClientboundSyncSkillsPacket(Set<ResourceLocation> unlocked,
|
||||
Set<ResourceLocation> unlockedAbilities,
|
||||
int skillPoints,
|
||||
ResourceLocation[] abilitySlots) {
|
||||
this.unlocked = unlocked;
|
||||
this.unlockedAbilities = unlockedAbilities;
|
||||
this.skillPoints = skillPoints;
|
||||
this.abilitySlots = abilitySlots;
|
||||
}
|
||||
|
||||
public static void encode(ClientboundSyncSkillsPacket msg, FriendlyByteBuf buf) {
|
||||
buf.writeInt(msg.unlocked.size());
|
||||
for (ResourceLocation id : msg.unlocked) {
|
||||
buf.writeResourceLocation(id);
|
||||
}
|
||||
for (ResourceLocation id : msg.unlocked) buf.writeResourceLocation(id);
|
||||
buf.writeInt(msg.unlockedAbilities.size());
|
||||
for (ResourceLocation id : msg.unlockedAbilities) buf.writeResourceLocation(id);
|
||||
buf.writeInt(msg.skillPoints);
|
||||
// ability slots (nullable entries)
|
||||
int count = msg.abilitySlots != null ? msg.abilitySlots.length : 0;
|
||||
buf.writeInt(count);
|
||||
if (count > 0) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
ResourceLocation id = msg.abilitySlots[i];
|
||||
buf.writeBoolean(id != null);
|
||||
if (id != null) 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());
|
||||
}
|
||||
Set<ResourceLocation> unlocked = new HashSet<>();
|
||||
for (int i = 0; i < size; i++) unlocked.add(buf.readResourceLocation());
|
||||
int abilitySize = buf.readInt();
|
||||
Set<ResourceLocation> unlockedAbilities = new HashSet<>();
|
||||
for (int i = 0; i < abilitySize; i++) unlockedAbilities.add(buf.readResourceLocation());
|
||||
int points = buf.readInt();
|
||||
return new ClientboundSyncSkillsPacket(result, points);
|
||||
int slotsCount = buf.readInt();
|
||||
ResourceLocation[] slots = new ResourceLocation[Math.max(slotsCount, 0)];
|
||||
for (int i = 0; i < slots.length; i++) {
|
||||
boolean present = buf.readBoolean();
|
||||
slots[i] = present ? buf.readResourceLocation() : null;
|
||||
}
|
||||
return new ClientboundSyncSkillsPacket(unlocked, unlockedAbilities, points, slots);
|
||||
}
|
||||
|
||||
|
||||
public static void handle(ClientboundSyncSkillsPacket msg, Supplier<NetworkEvent.Context> ctx) {
|
||||
ctx.get().enqueueWork(() -> {
|
||||
SkilltreeClientState.setUnlocked(msg.unlocked);
|
||||
SkilltreeClientState.setUnlockedAbilities(msg.unlockedAbilities);
|
||||
SkilltreeClientState.setCurrentSkillPoints(msg.skillPoints);
|
||||
SkilltreeClientState.setAbilitySlots(msg.abilitySlots);
|
||||
});
|
||||
ctx.get().setPacketHandled(true);
|
||||
}
|
||||
|
||||
@@ -37,12 +37,22 @@ public class PacketHandler {
|
||||
.decoder(ClientboundSyncSkillsPacket::decode)
|
||||
.consumerMainThread(ClientboundSyncSkillsPacket::handle)
|
||||
.add();
|
||||
CHANNEL.messageBuilder(ServerboundBindAbilityPacket.class, id++, NetworkDirection.PLAY_TO_SERVER)
|
||||
.encoder(ServerboundBindAbilityPacket::encode)
|
||||
.decoder(ServerboundBindAbilityPacket::decode)
|
||||
.consumerMainThread(ServerboundBindAbilityPacket::handle)
|
||||
.add();
|
||||
}
|
||||
|
||||
public static void syncSkillsTo(ServerPlayer player) {
|
||||
player.getCapability(SkillEngineCapabilities.PLAYER_SKILLS).ifPresent(data -> {
|
||||
ClientboundSyncSkillsPacket packet =
|
||||
new ClientboundSyncSkillsPacket(data.getUnlockedNodes(), data.getSkillPoints());
|
||||
new ClientboundSyncSkillsPacket(
|
||||
data.getUnlockedNodes(),
|
||||
data.getUnlockedAbilities(),
|
||||
data.getSkillPoints(),
|
||||
data.getAbilitySlots()
|
||||
);
|
||||
CHANNEL.send(PacketDistributor.PLAYER.with(() -> player), packet);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package net.fretux.skillengine.network;
|
||||
|
||||
import net.fretux.skillengine.capability.SkillEngineCapabilities;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.fretux.skillengine.network.PacketHandler;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ServerboundBindAbilityPacket {
|
||||
|
||||
private final ResourceLocation abilityId;
|
||||
private final int slot;
|
||||
|
||||
public ServerboundBindAbilityPacket(ResourceLocation id, int slot) {
|
||||
this.abilityId = id;
|
||||
this.slot = slot;
|
||||
}
|
||||
|
||||
public static void encode(ServerboundBindAbilityPacket msg, FriendlyByteBuf buf) {
|
||||
buf.writeResourceLocation(msg.abilityId);
|
||||
buf.writeInt(msg.slot);
|
||||
}
|
||||
|
||||
public static ServerboundBindAbilityPacket decode(FriendlyByteBuf buf) {
|
||||
return new ServerboundBindAbilityPacket(buf.readResourceLocation(), buf.readInt());
|
||||
}
|
||||
|
||||
public static void handle(ServerboundBindAbilityPacket msg, Supplier<NetworkEvent.Context> ctx) {
|
||||
ctx.get().enqueueWork(() -> {
|
||||
ServerPlayer player = ctx.get().getSender();
|
||||
if (player == null) return;
|
||||
|
||||
player.getCapability(SkillEngineCapabilities.PLAYER_SKILLS).ifPresent(data ->
|
||||
data.bindAbility(msg.slot, msg.abilityId)
|
||||
);
|
||||
// Sync back to client so UI reflects updated bindings
|
||||
PacketHandler.syncSkillsTo(player);
|
||||
});
|
||||
ctx.get().setPacketHandled(true);
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,7 @@ 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.fretux.skillengine.skilltree.*;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
@@ -37,27 +35,45 @@ public class ServerboundUnlockNodePacket {
|
||||
return;
|
||||
}
|
||||
SkillNode node = SkillNodeRegistry.get(msg.nodeId);
|
||||
if (node == null) {
|
||||
SkillEngine.LOGGER.warn("Unlock packet for unknown node {}", 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 -> {
|
||||
SkillEngine.LOGGER.info("Received unlock request for {} from {} (points={})",
|
||||
msg.nodeId, player.getGameProfile().getName(), data.getSkillPoints());
|
||||
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);
|
||||
|
||||
if (SkillLogic.canUnlock(data, player, 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(), data.getSkillPoints())
|
||||
);
|
||||
} else {
|
||||
SkillEngine.LOGGER.info("Cannot unlock node {} for {} (canUnlock=false)",
|
||||
msg.nodeId, player.getGameProfile().getName());
|
||||
PacketHandler.CHANNEL.send(
|
||||
PacketDistributor.PLAYER.with(() -> player),
|
||||
new ClientboundNodeUnlockedPacket(node.getId(), data.getSkillPoints())
|
||||
);
|
||||
} else {
|
||||
SkillEngine.LOGGER.info("Cannot unlock skill node {} for {}",
|
||||
msg.nodeId, player.getGameProfile().getName());
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (ability != null) {
|
||||
SkillEngine.LOGGER.info("Received ability unlock request for {} from {} (points={})",
|
||||
msg.nodeId, player.getGameProfile().getName(), data.getSkillPoints());
|
||||
if (!data.isAbilityUnlocked(ability.getId())) {
|
||||
data.unlockAbility(ability);
|
||||
PacketHandler.CHANNEL.send(
|
||||
PacketDistributor.PLAYER.with(() -> player),
|
||||
new ClientboundNodeUnlockedPacket(ability.getId(), data.getSkillPoints())
|
||||
);
|
||||
} else {
|
||||
SkillEngine.LOGGER.info("Ability {} already unlocked for {}",
|
||||
msg.nodeId, player.getGameProfile().getName());
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
ctx.get().setPacketHandled(true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user