extend skill unlock packet to include ability type and improve logout handling logic
This commit is contained in:
@@ -13,20 +13,19 @@ import net.minecraftforge.network.PacketDistributor;
|
|||||||
|
|
||||||
@Mod.EventBusSubscriber(modid = SkillEngine.MODID)
|
@Mod.EventBusSubscriber(modid = SkillEngine.MODID)
|
||||||
public class AbilityCooldownHandler {
|
public class AbilityCooldownHandler {
|
||||||
private static int syncCounter = 0;
|
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public static void onServerTick(TickEvent.PlayerTickEvent event) {
|
public static void onServerTick(TickEvent.PlayerTickEvent event) {
|
||||||
if (event.phase != TickEvent.Phase.END) return;
|
if (event.phase != TickEvent.Phase.END) return;
|
||||||
if (event.player.level().isClientSide) return;
|
if (event.player.level().isClientSide) return;
|
||||||
|
if (!(event.player instanceof ServerPlayer player)) return;
|
||||||
event.player.getCapability(SkillEngineCapabilities.PLAYER_SKILLS).ifPresent(data -> {
|
event.player.getCapability(SkillEngineCapabilities.PLAYER_SKILLS).ifPresent(data -> {
|
||||||
data.tickCooldowns();
|
data.tickCooldowns();
|
||||||
syncCounter++;
|
data.tickTagCooldowns();
|
||||||
if (syncCounter >= 10) {
|
if (player.tickCount % 10 == 0) {
|
||||||
syncCounter = 0;
|
|
||||||
for (int i = 1; i <= 3; i++) {
|
for (int i = 1; i <= 3; i++) {
|
||||||
PacketHandler.CHANNEL.send(
|
PacketHandler.CHANNEL.send(
|
||||||
PacketDistributor.PLAYER.with(() -> (ServerPlayer) event.player),
|
PacketDistributor.PLAYER.with(() -> player),
|
||||||
new ClientboundSyncCooldownPacket(i, data.getCooldown(i))
|
new ClientboundSyncCooldownPacket(i, data.getCooldown(i))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,6 +69,7 @@ public final class SkillEngineAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean consumeSkillPoints(Player player, int amount) {
|
public static boolean consumeSkillPoints(Player player, int amount) {
|
||||||
|
if (amount <= 0) return false;
|
||||||
PlayerSkillData data = getPlayerData(player);
|
PlayerSkillData data = getPlayerData(player);
|
||||||
if (data != null && data.getSkillPoints() >= amount) {
|
if (data != null && data.getSkillPoints() >= amount) {
|
||||||
data.addSkillPoints(-amount);
|
data.addSkillPoints(-amount);
|
||||||
|
|||||||
@@ -33,6 +33,12 @@ public class AscendCompatHandler {
|
|||||||
public static void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event) {
|
public static void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event) {
|
||||||
initAscendLevel(event.getEntity());
|
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
|
@SubscribeEvent
|
||||||
public static void onPlayerClone(PlayerEvent.Clone event) {
|
public static void onPlayerClone(PlayerEvent.Clone event) {
|
||||||
@@ -68,4 +74,4 @@ public class AscendCompatHandler {
|
|||||||
LAST_ASCEND_LEVEL.put(player.getUUID(), currentLevel);
|
LAST_ASCEND_LEVEL.put(player.getUUID(), currentLevel);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import net.fretux.skillengine.network.PacketHandler;
|
|||||||
import net.minecraft.server.level.ServerPlayer;
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
import net.minecraft.world.entity.player.Player;
|
import net.minecraft.world.entity.player.Player;
|
||||||
import net.minecraftforge.event.TickEvent;
|
import net.minecraftforge.event.TickEvent;
|
||||||
|
import net.minecraftforge.event.entity.player.PlayerEvent;
|
||||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||||
import net.minecraftforge.fml.common.Mod;
|
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) {
|
private static void resetSkillEngine(PlayerSkillData data, Player player) {
|
||||||
int refund = data.getTotalSkillCost();
|
int refund = data.getTotalSkillCost();
|
||||||
data.addSkillPoints(refund);
|
data.addSkillPoints(refund);
|
||||||
@@ -60,4 +69,4 @@ public class AscendResetDetector {
|
|||||||
PacketHandler.syncSkillsTo(serverPlayer);
|
PacketHandler.syncSkillsTo(serverPlayer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package net.fretux.skillengine.network;
|
package net.fretux.skillengine.network;
|
||||||
|
|
||||||
import net.fretux.skillengine.skilltree.AbilityNodeRegistry;
|
|
||||||
import net.minecraft.network.FriendlyByteBuf;
|
import net.minecraft.network.FriendlyByteBuf;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
import net.minecraft.resources.ResourceLocation;
|
||||||
import net.minecraftforge.api.distmarker.Dist;
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
@@ -12,32 +11,35 @@ import java.util.function.Supplier;
|
|||||||
public class ClientboundNodeUnlockedPacket {
|
public class ClientboundNodeUnlockedPacket {
|
||||||
private final ResourceLocation id;
|
private final ResourceLocation id;
|
||||||
private final int newSkillPoints;
|
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.id = id;
|
||||||
this.newSkillPoints = newSkillPoints;
|
this.newSkillPoints = newSkillPoints;
|
||||||
|
this.ability = ability;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void encode(ClientboundNodeUnlockedPacket msg, FriendlyByteBuf buf) {
|
public static void encode(ClientboundNodeUnlockedPacket msg, FriendlyByteBuf buf) {
|
||||||
buf.writeResourceLocation(msg.id);
|
buf.writeResourceLocation(msg.id);
|
||||||
buf.writeInt(msg.newSkillPoints);
|
buf.writeInt(msg.newSkillPoints);
|
||||||
|
buf.writeBoolean(msg.ability);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ClientboundNodeUnlockedPacket decode(FriendlyByteBuf buf) {
|
public static ClientboundNodeUnlockedPacket decode(FriendlyByteBuf buf) {
|
||||||
ResourceLocation id = buf.readResourceLocation();
|
ResourceLocation id = buf.readResourceLocation();
|
||||||
int points = buf.readInt();
|
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) {
|
public static void handle(ClientboundNodeUnlockedPacket msg, Supplier<NetworkEvent.Context> ctx) {
|
||||||
ctx.get().enqueueWork(() -> {
|
ctx.get().enqueueWork(() -> {
|
||||||
boolean isAbility = AbilityNodeRegistry.get(msg.id) != null;
|
|
||||||
DistExecutor.unsafeRunWhenOn(
|
DistExecutor.unsafeRunWhenOn(
|
||||||
Dist.CLIENT,
|
Dist.CLIENT,
|
||||||
() -> () -> net.fretux.skillengine.client.ClientSkillEngineBridge.handleNodeUnlocked(
|
() -> () -> net.fretux.skillengine.client.ClientSkillEngineBridge.handleNodeUnlocked(
|
||||||
msg.id,
|
msg.id,
|
||||||
msg.newSkillPoints,
|
msg.newSkillPoints,
|
||||||
isAbility
|
msg.ability
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import net.minecraftforge.network.simple.SimpleChannel;
|
|||||||
|
|
||||||
public class PacketHandler {
|
public class PacketHandler {
|
||||||
|
|
||||||
private static final String PROTOCOL = "2";
|
private static final String PROTOCOL = "3";
|
||||||
public static final SimpleChannel CHANNEL = NetworkRegistry.ChannelBuilder
|
public static final SimpleChannel CHANNEL = NetworkRegistry.ChannelBuilder
|
||||||
.named(ResourceLocation.fromNamespaceAndPath(SkillEngine.MODID, "network"))
|
.named(ResourceLocation.fromNamespaceAndPath(SkillEngine.MODID, "network"))
|
||||||
.clientAcceptedVersions(PROTOCOL::equals)
|
.clientAcceptedVersions(PROTOCOL::equals)
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ public class ServerboundUnlockNodePacket {
|
|||||||
PacketHandler.syncSkillsTo(player);
|
PacketHandler.syncSkillsTo(player);
|
||||||
PacketHandler.CHANNEL.send(
|
PacketHandler.CHANNEL.send(
|
||||||
PacketDistributor.PLAYER.with(() -> player),
|
PacketDistributor.PLAYER.with(() -> player),
|
||||||
new ClientboundNodeUnlockedPacket(node.getId(), data.getSkillPoints())
|
new ClientboundNodeUnlockedPacket(node.getId(), data.getSkillPoints(), false)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
SkillEngine.LOGGER.info("[SKILLENGINE] Rejecting unlock for {}", node.getId());
|
SkillEngine.LOGGER.info("[SKILLENGINE] Rejecting unlock for {}", node.getId());
|
||||||
@@ -57,7 +57,7 @@ public class ServerboundUnlockNodePacket {
|
|||||||
PacketHandler.syncSkillsTo(player);
|
PacketHandler.syncSkillsTo(player);
|
||||||
PacketHandler.CHANNEL.send(
|
PacketHandler.CHANNEL.send(
|
||||||
PacketDistributor.PLAYER.with(() -> player),
|
PacketDistributor.PLAYER.with(() -> player),
|
||||||
new ClientboundNodeUnlockedPacket(ability.getId(), data.getSkillPoints())
|
new ClientboundNodeUnlockedPacket(ability.getId(), data.getSkillPoints(), true)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
SkillEngine.LOGGER.info("[SKILLENGINE] Rejecting ability unlock for {}", ability.getId());
|
SkillEngine.LOGGER.info("[SKILLENGINE] Rejecting ability unlock for {}", ability.getId());
|
||||||
|
|||||||
Reference in New Issue
Block a user