From c6ae73c1220af34fc517690cb0c61390bb2a2191 Mon Sep 17 00:00:00 2001 From: ktpatient <167013520+ktpatient@users.noreply.github.com> Date: Mon, 9 Sep 2024 13:16:13 +0100 Subject: [PATCH 1/2] Add onCooldown to augments --- .../modjam/content/augments/Augment.java | 5 +++++ .../content/augments/DisallowBreakingAugment.java | 12 ------------ .../modjam/content/augments/GiveDiamondAugment.java | 1 - .../modjam/content/augments/StaticAugment.java | 1 + .../content/augments/ThrowSnowballAugment.java | 7 ++----- 5 files changed, 8 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/portingdeadmods/modjam/content/augments/Augment.java b/src/main/java/com/portingdeadmods/modjam/content/augments/Augment.java index 8955a5b9..1b863f8b 100644 --- a/src/main/java/com/portingdeadmods/modjam/content/augments/Augment.java +++ b/src/main/java/com/portingdeadmods/modjam/content/augments/Augment.java @@ -32,4 +32,9 @@ public void handleKeybindPress(Slot slot, Player player) { public int getId() { return -1; } + + @Override + public boolean onCooldown(Slot slot, Player player) { + return (AugmentHelper.getCooldown(player, slot)>=0); + } } diff --git a/src/main/java/com/portingdeadmods/modjam/content/augments/DisallowBreakingAugment.java b/src/main/java/com/portingdeadmods/modjam/content/augments/DisallowBreakingAugment.java index 3d8e690a..21203c1d 100644 --- a/src/main/java/com/portingdeadmods/modjam/content/augments/DisallowBreakingAugment.java +++ b/src/main/java/com/portingdeadmods/modjam/content/augments/DisallowBreakingAugment.java @@ -20,16 +20,4 @@ public int getId() { public void breakBlock(Slot slot, BlockEvent.BreakEvent event) { event.setCanceled(true); } - - @Override - public void clientTick(Slot slot, PlayerTickEvent.Post event) { - if (InputUtils.isKeyDown(InputConstants.KEY_Y)){ - PacketDistributor.sendToServer(new KeyPressedPayload(getId(),slot.slotId)); - } - } - - @Override - public void handleKeybindPress(Slot slot, Player player) { - player.addItem(Items.EMERALD.getDefaultInstance()); - } } diff --git a/src/main/java/com/portingdeadmods/modjam/content/augments/GiveDiamondAugment.java b/src/main/java/com/portingdeadmods/modjam/content/augments/GiveDiamondAugment.java index ced3b1cf..935ecbc2 100644 --- a/src/main/java/com/portingdeadmods/modjam/content/augments/GiveDiamondAugment.java +++ b/src/main/java/com/portingdeadmods/modjam/content/augments/GiveDiamondAugment.java @@ -21,7 +21,6 @@ public int getId() { @Override public void clientTick(Slot slot, PlayerTickEvent.Post event) { - // ModJam.LOGGER.info("ClientTick for id {}", getId()); if (InputUtils.isKeyDown(InputConstants.KEY_Y)) { PacketDistributor.sendToServer(new KeyPressedPayload(getId(), slot.slotId)); } diff --git a/src/main/java/com/portingdeadmods/modjam/content/augments/StaticAugment.java b/src/main/java/com/portingdeadmods/modjam/content/augments/StaticAugment.java index fa230e01..4e612ee2 100644 --- a/src/main/java/com/portingdeadmods/modjam/content/augments/StaticAugment.java +++ b/src/main/java/com/portingdeadmods/modjam/content/augments/StaticAugment.java @@ -11,4 +11,5 @@ public interface StaticAugment { void serverTick(Slot slot, PlayerTickEvent.Post event); void handleKeybindPress(Slot slot, Player player); int getId(); + boolean onCooldown(Slot slot, Player player); } diff --git a/src/main/java/com/portingdeadmods/modjam/content/augments/ThrowSnowballAugment.java b/src/main/java/com/portingdeadmods/modjam/content/augments/ThrowSnowballAugment.java index 1f8628da..166dde1a 100644 --- a/src/main/java/com/portingdeadmods/modjam/content/augments/ThrowSnowballAugment.java +++ b/src/main/java/com/portingdeadmods/modjam/content/augments/ThrowSnowballAugment.java @@ -20,10 +20,7 @@ public int getId() { @Override public void clientTick(Slot slot, PlayerTickEvent.Post event) { - // ModJam.LOGGER.info("ClientTick for id {}", getId()); - - if (InputUtils.isKeyDown(InputConstants.KEY_Y) && (AugmentHelper.getCooldown(event.getEntity(), slot)<=0)) { - // ModJam.LOGGER.info("Snow"); + if (InputUtils.isKeyDown(InputConstants.KEY_Y) && !onCooldown(slot, event.getEntity())) { PacketDistributor.sendToServer(new KeyPressedPayload(getId(), slot.slotId)); } } @@ -34,6 +31,6 @@ public void handleKeybindPress(Slot slot, Player player) { snowball.setItem(Items.SNOWBALL.getDefaultInstance()); snowball.shootFromRotation(player, player.getXRot(), player.getYRot(), 0.0F, 1.5F, 1.0F); player.level().addFreshEntity(snowball); - AugmentHelper.setCooldownAndUpdate(player, slot, 20); + AugmentHelper.setCooldownAndUpdate(player, slot, 20); // Set the cooldown, which decrements by 1 every tick } } From ff02f81d56ee48e574657c699e9b47098f06a7f1 Mon Sep 17 00:00:00 2001 From: ktpatient <167013520+ktpatient@users.noreply.github.com> Date: Mon, 9 Sep 2024 13:16:36 +0100 Subject: [PATCH 2/2] Remove Payload actions --- .../modjam/network/KeyPressedPayload.java | 17 +++++++ .../modjam/network/NetworkEvents.java | 6 +-- .../modjam/network/PayloadActions.java | 45 +++++++++---------- .../modjam/network/SetAugmentDataPayload.java | 8 ++++ .../modjam/network/SetCooldownPayload.java | 9 ++++ 5 files changed, 58 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/portingdeadmods/modjam/network/KeyPressedPayload.java b/src/main/java/com/portingdeadmods/modjam/network/KeyPressedPayload.java index 68b8ebc9..db9c21d6 100644 --- a/src/main/java/com/portingdeadmods/modjam/network/KeyPressedPayload.java +++ b/src/main/java/com/portingdeadmods/modjam/network/KeyPressedPayload.java @@ -1,11 +1,16 @@ package com.portingdeadmods.modjam.network; import com.portingdeadmods.modjam.ModJam; +import com.portingdeadmods.modjam.capabilities.augmentation.Slot; +import com.portingdeadmods.modjam.content.augments.AugmentHelper; import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.chat.Component; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.entity.player.Player; +import net.neoforged.neoforge.network.handling.IPayloadContext; public record KeyPressedPayload(int augmentId, int slot) implements CustomPacketPayload { public static final Type TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(ModJam.MODID, "key_pressesd_paylad")); @@ -21,4 +26,16 @@ public record KeyPressedPayload(int augmentId, int slot) implements CustomPacket public Type type() { return TYPE; } + + public static void keyPressedAction(KeyPressedPayload payload, IPayloadContext context){ + context.enqueueWork(()->{ + Player player = context.player(); + int augmentId = payload.augmentId(); + AugmentHelper.getAugment(augmentId).handleKeybindPress(Slot.GetValue(payload.slot()), player); + }).exceptionally(e->{ + context.disconnect(Component.literal("action failed: "+ e.getMessage())); + return null; + }); + + } } diff --git a/src/main/java/com/portingdeadmods/modjam/network/NetworkEvents.java b/src/main/java/com/portingdeadmods/modjam/network/NetworkEvents.java index 93a1663a..8e8a5999 100644 --- a/src/main/java/com/portingdeadmods/modjam/network/NetworkEvents.java +++ b/src/main/java/com/portingdeadmods/modjam/network/NetworkEvents.java @@ -14,17 +14,17 @@ public static void registerPayloads(final RegisterPayloadHandlersEvent event) { registrar.playToServer( KeyPressedPayload.TYPE, KeyPressedPayload.STREAM_CODEC, - PayloadActions::keyPressedAction + KeyPressedPayload::keyPressedAction ); registrar.playBidirectional( SetAugmentDataPayload.TYPE, SetAugmentDataPayload.STREAM_CODEC, - PayloadActions::setAugmentDataAction + SetAugmentDataPayload::setAugmentDataAction ); registrar.playBidirectional( SetCooldownPayload.TYPE, SetCooldownPayload.STREAM_CODEC, - PayloadActions::setCooldownAction + SetCooldownPayload::setCooldownAction ); } } diff --git a/src/main/java/com/portingdeadmods/modjam/network/PayloadActions.java b/src/main/java/com/portingdeadmods/modjam/network/PayloadActions.java index 0a599e50..876da2fd 100644 --- a/src/main/java/com/portingdeadmods/modjam/network/PayloadActions.java +++ b/src/main/java/com/portingdeadmods/modjam/network/PayloadActions.java @@ -9,28 +9,25 @@ public class PayloadActions { - public static void keyPressedAction(KeyPressedPayload payload, IPayloadContext context){ - context.enqueueWork(()->{ - Player player = context.player(); - int augmentId = payload.augmentId(); - - // ModJam.LOGGER.info("Sent with packetId: {}", augmentId); - AugmentHelper.getAugment(augmentId).handleKeybindPress(Slot.GetValue(payload.slot()), player); - }).exceptionally(e->{ - context.disconnect(Component.literal("action failed: "+ e.getMessage())); - return null; - }); - - } - public static void setAugmentDataAction(SetAugmentDataPayload payload, IPayloadContext context){ - context.enqueueWork(()->{ - AugmentHelper.setId(context.player(), Slot.GetValue(payload.slot()), payload.augmentId()); - // ModJam.LOGGER.debug("Syncing, id: {}",payload.augmentId()); - }); - } - public static void setCooldownAction(SetCooldownPayload payload, IPayloadContext context){ - context.enqueueWork(()->{ - AugmentHelper.setCooldown(context.player(), Slot.GetValue(payload.slot()), payload.cooldown()); - }); - } +// public static void keyPressedAction(KeyPressedPayload payload, IPayloadContext context){ +// context.enqueueWork(()->{ +// Player player = context.player(); +// int augmentId = payload.augmentId(); +// AugmentHelper.getAugment(augmentId).handleKeybindPress(Slot.GetValue(payload.slot()), player); +// }).exceptionally(e->{ +// context.disconnect(Component.literal("action failed: "+ e.getMessage())); +// return null; +// }); +// +// } +// public static void setAugmentDataAction(SetAugmentDataPayload payload, IPayloadContext context){ +// context.enqueueWork(()->{ +// AugmentHelper.setId(context.player(), Slot.GetValue(payload.slot()), payload.augmentId()); +// }); +// } +// public static void setCooldownAction(SetCooldownPayload payload, IPayloadContext context){ +// context.enqueueWork(()->{ +// AugmentHelper.setCooldown(context.player(), Slot.GetValue(payload.slot()), payload.cooldown()); +// }); +// } } diff --git a/src/main/java/com/portingdeadmods/modjam/network/SetAugmentDataPayload.java b/src/main/java/com/portingdeadmods/modjam/network/SetAugmentDataPayload.java index 7b11fe36..70039726 100644 --- a/src/main/java/com/portingdeadmods/modjam/network/SetAugmentDataPayload.java +++ b/src/main/java/com/portingdeadmods/modjam/network/SetAugmentDataPayload.java @@ -1,11 +1,14 @@ package com.portingdeadmods.modjam.network; import com.portingdeadmods.modjam.ModJam; +import com.portingdeadmods.modjam.capabilities.augmentation.Slot; +import com.portingdeadmods.modjam.content.augments.AugmentHelper; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; +import net.neoforged.neoforge.network.handling.IPayloadContext; import org.jetbrains.annotations.NotNull; public record SetAugmentDataPayload(int augmentId, int slot) implements CustomPacketPayload { @@ -23,4 +26,9 @@ public record SetAugmentDataPayload(int augmentId, int slot) implements CustomPa public @NotNull Type type() { return TYPE; } + public static void setAugmentDataAction(SetAugmentDataPayload payload, IPayloadContext context){ + context.enqueueWork(()->{ + AugmentHelper.setId(context.player(), Slot.GetValue(payload.slot()), payload.augmentId()); + }); + } } diff --git a/src/main/java/com/portingdeadmods/modjam/network/SetCooldownPayload.java b/src/main/java/com/portingdeadmods/modjam/network/SetCooldownPayload.java index 71fbdaaf..6d220bdc 100644 --- a/src/main/java/com/portingdeadmods/modjam/network/SetCooldownPayload.java +++ b/src/main/java/com/portingdeadmods/modjam/network/SetCooldownPayload.java @@ -1,11 +1,14 @@ package com.portingdeadmods.modjam.network; import com.portingdeadmods.modjam.ModJam; +import com.portingdeadmods.modjam.capabilities.augmentation.Slot; +import com.portingdeadmods.modjam.content.augments.AugmentHelper; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; +import net.neoforged.neoforge.network.handling.IPayloadContext; import org.jetbrains.annotations.NotNull; public record SetCooldownPayload (int cooldown, int slot)implements CustomPacketPayload { @@ -22,4 +25,10 @@ public record SetCooldownPayload (int cooldown, int slot)implements CustomPacket public @NotNull Type type() { return TYPE; } + + public static void setCooldownAction(SetCooldownPayload payload, IPayloadContext context){ + context.enqueueWork(()->{ + AugmentHelper.setCooldown(context.player(), Slot.GetValue(payload.slot()), payload.cooldown()); + }); + } }