Add client-side ability cooldowns, sync, and GUI updates
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package net.fretux.skillengine.network;
|
||||
|
||||
import net.fretux.skillengine.client.SkilltreeClientState;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ClientboundSyncCooldownPacket {
|
||||
|
||||
private final int slot;
|
||||
private final int cooldown;
|
||||
|
||||
public ClientboundSyncCooldownPacket(int slot, int cooldown) {
|
||||
this.slot = slot;
|
||||
this.cooldown = cooldown;
|
||||
}
|
||||
|
||||
public static void encode(ClientboundSyncCooldownPacket msg, FriendlyByteBuf buf) {
|
||||
buf.writeInt(msg.slot);
|
||||
buf.writeInt(msg.cooldown);
|
||||
}
|
||||
|
||||
public static ClientboundSyncCooldownPacket decode(FriendlyByteBuf buf) {
|
||||
return new ClientboundSyncCooldownPacket(buf.readInt(), buf.readInt());
|
||||
}
|
||||
|
||||
public static void handle(ClientboundSyncCooldownPacket msg, Supplier<NetworkEvent.Context> ctx) {
|
||||
ctx.get().enqueueWork(() -> SkilltreeClientState.updateCooldown(msg.slot, msg.cooldown));
|
||||
ctx.get().setPacketHandled(true);
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,16 @@ public class PacketHandler {
|
||||
.decoder(ServerboundBindAbilityPacket::decode)
|
||||
.consumerMainThread(ServerboundBindAbilityPacket::handle)
|
||||
.add();
|
||||
CHANNEL.messageBuilder(ClientboundSyncCooldownPacket.class, id++, NetworkDirection.PLAY_TO_CLIENT)
|
||||
.encoder(ClientboundSyncCooldownPacket::encode)
|
||||
.decoder(ClientboundSyncCooldownPacket::decode)
|
||||
.consumerMainThread(ClientboundSyncCooldownPacket::handle)
|
||||
.add();
|
||||
CHANNEL.messageBuilder(ServerboundActivateAbilityPacket.class, id++, NetworkDirection.PLAY_TO_SERVER)
|
||||
.encoder(ServerboundActivateAbilityPacket::encode)
|
||||
.decoder(ServerboundActivateAbilityPacket::decode)
|
||||
.consumerMainThread(ServerboundActivateAbilityPacket::handle)
|
||||
.add();
|
||||
}
|
||||
|
||||
public static void syncSkillsTo(ServerPlayer player) {
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package net.fretux.skillengine.network;
|
||||
|
||||
import net.fretux.skillengine.capability.SkillEngineCapabilities;
|
||||
import net.fretux.skillengine.skilltree.AbilityNode;
|
||||
import net.fretux.skillengine.skilltree.AbilityNodeRegistry;
|
||||
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 ServerboundActivateAbilityPacket {
|
||||
|
||||
private final int slot;
|
||||
|
||||
public ServerboundActivateAbilityPacket(int slot) {
|
||||
this.slot = slot;
|
||||
}
|
||||
|
||||
public static void encode(ServerboundActivateAbilityPacket msg, FriendlyByteBuf buf) {
|
||||
buf.writeInt(msg.slot);
|
||||
}
|
||||
|
||||
public static ServerboundActivateAbilityPacket decode(FriendlyByteBuf buf) {
|
||||
return new ServerboundActivateAbilityPacket(buf.readInt());
|
||||
}
|
||||
|
||||
public static void handle(ServerboundActivateAbilityPacket msg, Supplier<NetworkEvent.Context> ctx) {
|
||||
ctx.get().enqueueWork(() -> {
|
||||
|
||||
ServerPlayer player = ctx.get().getSender();
|
||||
if (player == null) return;
|
||||
|
||||
player.getCapability(SkillEngineCapabilities.PLAYER_SKILLS).ifPresent(data -> {
|
||||
|
||||
ResourceLocation abilityId = data.getAbilityInSlot(msg.slot);
|
||||
if (abilityId == null) return;
|
||||
|
||||
AbilityNode ability = AbilityNodeRegistry.get(abilityId);
|
||||
if (ability == null) return;
|
||||
|
||||
int cd = data.getCooldown(msg.slot);
|
||||
if (cd > 0) return;
|
||||
ability.execute(player);
|
||||
data.setCooldown(msg.slot, ability.getCooldown());
|
||||
PacketHandler.CHANNEL.send(
|
||||
PacketDistributor.PLAYER.with(() -> player),
|
||||
new ClientboundSyncCooldownPacket(msg.slot, ability.getCooldown())
|
||||
);
|
||||
});
|
||||
});
|
||||
ctx.get().setPacketHandled(true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user