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 f33a8084..8955a5b9 100644 --- a/src/main/java/com/portingdeadmods/modjam/content/augments/Augment.java +++ b/src/main/java/com/portingdeadmods/modjam/content/augments/Augment.java @@ -1,5 +1,6 @@ package com.portingdeadmods.modjam.content.augments; +import com.portingdeadmods.modjam.capabilities.augmentation.Slot; import net.minecraft.world.entity.player.Player; import net.neoforged.neoforge.event.level.BlockEvent; import net.neoforged.neoforge.event.tick.PlayerTickEvent; @@ -8,22 +9,22 @@ public class Augment implements StaticAugment { @Override - public void breakBlock(BlockEvent.BreakEvent event) { + public void breakBlock(Slot slot, BlockEvent.BreakEvent event) { } @Override - public void clientTick(PlayerTickEvent.Post event) { + public void clientTick(Slot slot, PlayerTickEvent.Post event) { } @Override - public void serverTick(PlayerTickEvent.Post event) { + public void serverTick(Slot slot, PlayerTickEvent.Post event) { } @Override - public void handleKeybindPress(Player player) { + public void handleKeybindPress(Slot slot, Player player) { } diff --git a/src/main/java/com/portingdeadmods/modjam/content/augments/AugmentHelper.java b/src/main/java/com/portingdeadmods/modjam/content/augments/AugmentHelper.java index 71bc6426..7491b168 100644 --- a/src/main/java/com/portingdeadmods/modjam/content/augments/AugmentHelper.java +++ b/src/main/java/com/portingdeadmods/modjam/content/augments/AugmentHelper.java @@ -3,6 +3,7 @@ import com.portingdeadmods.modjam.ModJam; import com.portingdeadmods.modjam.capabilities.augmentation.Slot; import com.portingdeadmods.modjam.network.SetAugmentDataPayload; +import com.portingdeadmods.modjam.network.SetCooldownPayload; import com.portingdeadmods.modjam.registries.MJDataAttachments; import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerPlayer; @@ -27,7 +28,29 @@ public static StaticAugment getAugment(Player player, Slot slot) { return augmentHashMap.get(id); } - public static Supplier> getAttachment(Slot slot) { + public static Supplier> getDataAttachment(Slot slot){ + return switch (slot) { + case HEAD -> MJDataAttachments.HEAD_AUGMENTATION_DATA; + case BODY -> MJDataAttachments.BODY_AUGMENTATION_DATA; + case LEGS -> MJDataAttachments.LEGS_AUGMENTATION_DATA; + case ARMS -> MJDataAttachments.ARMS_AUGMENTATION_DATA; + case HEART -> MJDataAttachments.HEART_AUGMENTATION_DATA; + case NONE -> null; + }; + } + + public static Supplier> getCooldownAttachment(Slot slot){ + return switch (slot) { + case HEAD -> MJDataAttachments.HEAD_AUGMENTATION_COOLDOWN; + case BODY -> MJDataAttachments.BODY_AUGMENTATION_COOLDOWN; + case LEGS -> MJDataAttachments.LEGS_AUGMENTATION_COOLDOWN; + case ARMS -> MJDataAttachments.ARMS_AUGMENTATION_COOLDOWN; + case HEART -> MJDataAttachments.HEART_AUGMENTATION_COOLDOWN; + case NONE -> null; + }; + } + + private static Supplier> getAttachment(Slot slot) { return switch (slot) { case HEAD -> MJDataAttachments.HEAD_AUGMENTATION; case BODY -> MJDataAttachments.BODY_AUGMENTATION; @@ -40,12 +63,23 @@ public static Supplier> getAttachment(Slot slot) { public static int getId(Player player, Slot slot) { return player.getData(getAttachment(slot).get()); - // return -2; + } + public static int getCooldown(Player player, Slot slot) { + return player.getData(getCooldownAttachment(slot).get()); + } + public static int getData(Player player, Slot slot) { + return player.getData(getDataAttachment(slot).get()); } public static void setId(Player player, Slot slot, int id) { player.setData(getAttachment(slot).get(), id); } + public static void setCooldown(Player player, Slot slot, int cooldown) { + player.setData(getCooldownAttachment(slot).get(), cooldown); + } + public static void setData(Player player, Slot slot, int data){ + player.setData(getDataAttachment(slot).get(),data); + } public static void setIdAndUpdate(Player player, Slot slot, int id) { if (player.level().isClientSide()) { @@ -55,6 +89,15 @@ public static void setIdAndUpdate(Player player, Slot slot, int id) { } setId(player, slot, id); } + public static void setCooldownAndUpdate(Player player, Slot slot, int cooldown) { + ModJam.LOGGER.info("Setting Cooldown to {}", cooldown); + if (player.level().isClientSide()) { + PacketDistributor.sendToServer(new SetCooldownPayload(cooldown, slot.slotId)); + } else { + PacketDistributor.sendToPlayer((ServerPlayer) player, new SetCooldownPayload(cooldown, slot.slotId)); + } + setCooldown(player, slot, cooldown); + } public static void incId(Player player, Slot slot) { setIdAndUpdate(player, slot, AugmentHelper.getId(player, slot) + 1); 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 2c3d205f..3d8e690a 100644 --- a/src/main/java/com/portingdeadmods/modjam/content/augments/DisallowBreakingAugment.java +++ b/src/main/java/com/portingdeadmods/modjam/content/augments/DisallowBreakingAugment.java @@ -1,6 +1,7 @@ package com.portingdeadmods.modjam.content.augments; import com.mojang.blaze3d.platform.InputConstants; +import com.portingdeadmods.modjam.capabilities.augmentation.Slot; import com.portingdeadmods.modjam.network.KeyPressedPayload; import com.portingdeadmods.modjam.utils.InputUtils; import net.minecraft.world.entity.player.Player; @@ -16,19 +17,19 @@ public int getId() { } @Override - public void breakBlock(BlockEvent.BreakEvent event) { + public void breakBlock(Slot slot, BlockEvent.BreakEvent event) { event.setCanceled(true); } @Override - public void clientTick(PlayerTickEvent.Post event) { + public void clientTick(Slot slot, PlayerTickEvent.Post event) { if (InputUtils.isKeyDown(InputConstants.KEY_Y)){ - PacketDistributor.sendToServer(new KeyPressedPayload(getId())); + PacketDistributor.sendToServer(new KeyPressedPayload(getId(),slot.slotId)); } } @Override - public void handleKeybindPress(Player player) { + 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 9a5f91f8..ced3b1cf 100644 --- a/src/main/java/com/portingdeadmods/modjam/content/augments/GiveDiamondAugment.java +++ b/src/main/java/com/portingdeadmods/modjam/content/augments/GiveDiamondAugment.java @@ -2,6 +2,7 @@ import com.mojang.blaze3d.platform.InputConstants; import com.portingdeadmods.modjam.ModJam; +import com.portingdeadmods.modjam.capabilities.augmentation.Slot; import com.portingdeadmods.modjam.network.KeyPressedPayload; import com.portingdeadmods.modjam.utils.InputUtils; import net.minecraft.client.Minecraft; @@ -19,15 +20,15 @@ public int getId() { } @Override - public void clientTick(PlayerTickEvent.Post event) { + 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())); + PacketDistributor.sendToServer(new KeyPressedPayload(getId(), slot.slotId)); } } @Override - public void handleKeybindPress(Player player) { + public void handleKeybindPress(Slot slot, Player player) { player.addItem(Items.DIAMOND.getDefaultInstance()); } } 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 b308f6dc..fa230e01 100644 --- a/src/main/java/com/portingdeadmods/modjam/content/augments/StaticAugment.java +++ b/src/main/java/com/portingdeadmods/modjam/content/augments/StaticAugment.java @@ -1,13 +1,14 @@ package com.portingdeadmods.modjam.content.augments; +import com.portingdeadmods.modjam.capabilities.augmentation.Slot; import net.minecraft.world.entity.player.Player; import net.neoforged.neoforge.event.level.BlockEvent; import net.neoforged.neoforge.event.tick.PlayerTickEvent; public interface StaticAugment { - void breakBlock(BlockEvent.BreakEvent event); - void clientTick(PlayerTickEvent.Post event); - void serverTick(PlayerTickEvent.Post event); - void handleKeybindPress(Player player); + void breakBlock(Slot slot, BlockEvent.BreakEvent event); + void clientTick(Slot slot, PlayerTickEvent.Post event); + void serverTick(Slot slot, PlayerTickEvent.Post event); + void handleKeybindPress(Slot slot, Player player); int getId(); } 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 6fb42f5e..1f8628da 100644 --- a/src/main/java/com/portingdeadmods/modjam/content/augments/ThrowSnowballAugment.java +++ b/src/main/java/com/portingdeadmods/modjam/content/augments/ThrowSnowballAugment.java @@ -2,6 +2,7 @@ import com.mojang.blaze3d.platform.InputConstants; import com.portingdeadmods.modjam.ModJam; +import com.portingdeadmods.modjam.capabilities.augmentation.Slot; import com.portingdeadmods.modjam.network.KeyPressedPayload; import com.portingdeadmods.modjam.utils.InputUtils; import net.minecraft.world.entity.player.Player; @@ -18,20 +19,21 @@ public int getId() { } @Override - public void clientTick(PlayerTickEvent.Post event) { + public void clientTick(Slot slot, PlayerTickEvent.Post event) { // ModJam.LOGGER.info("ClientTick for id {}", getId()); - if (InputUtils.isKeyDown(InputConstants.KEY_Y)) { + if (InputUtils.isKeyDown(InputConstants.KEY_Y) && (AugmentHelper.getCooldown(event.getEntity(), slot)<=0)) { // ModJam.LOGGER.info("Snow"); - PacketDistributor.sendToServer(new KeyPressedPayload(getId())); + PacketDistributor.sendToServer(new KeyPressedPayload(getId(), slot.slotId)); } } @Override - public void handleKeybindPress(Player player) { + public void handleKeybindPress(Slot slot, Player player) { Snowball snowball = new Snowball(player.level(), 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); } } diff --git a/src/main/java/com/portingdeadmods/modjam/content/items/AugmentDebugItem.java b/src/main/java/com/portingdeadmods/modjam/content/items/AugmentDebugItem.java index b93aac9e..5735f99a 100644 --- a/src/main/java/com/portingdeadmods/modjam/content/items/AugmentDebugItem.java +++ b/src/main/java/com/portingdeadmods/modjam/content/items/AugmentDebugItem.java @@ -38,6 +38,8 @@ public boolean onDroppedByPlayer(@NotNull ItemStack item, Player player) { AugmentHelper.decId(player, Slot.HEAD); } else if (clickedBlock == Blocks.STONE) { AugmentHelper.incId(player, Slot.BODY); + } else if (clickedBlock == Blocks.SAND) { + AugmentHelper.setCooldownAndUpdate(player, Slot.HEAD, 60); } } diff --git a/src/main/java/com/portingdeadmods/modjam/events/AugmentEvents.java b/src/main/java/com/portingdeadmods/modjam/events/AugmentEvents.java index 827f74d5..ede9e49f 100644 --- a/src/main/java/com/portingdeadmods/modjam/events/AugmentEvents.java +++ b/src/main/java/com/portingdeadmods/modjam/events/AugmentEvents.java @@ -1,12 +1,15 @@ package com.portingdeadmods.modjam.events; import com.portingdeadmods.modjam.ModJam; +import com.portingdeadmods.modjam.capabilities.augmentation.Slot; import com.portingdeadmods.modjam.content.augments.AugmentHelper; import com.portingdeadmods.modjam.content.augments.StaticAugment; +import net.minecraft.world.entity.player.Player; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.neoforge.event.level.BlockEvent; import net.neoforged.neoforge.event.tick.PlayerTickEvent; +import org.checkerframework.checker.units.qual.A; @SuppressWarnings("unused") @EventBusSubscriber(modid = ModJam.MODID) @@ -17,7 +20,7 @@ public static void breakEvent(BlockEvent.BreakEvent event){ StaticAugment[] augments = AugmentHelper.getAugments(event.getPlayer()); for (int i = 0; i < augments.length; i++) { if (augments[i] != null){ - augments[i].breakBlock(event); + augments[i].breakBlock(Slot.GetValue(i),event); } } } @@ -26,13 +29,19 @@ public static void breakEvent(BlockEvent.BreakEvent event){ @SubscribeEvent public static void playerTick(PlayerTickEvent.Post event){ StaticAugment[] augments = AugmentHelper.getAugments(event.getEntity()); - for (StaticAugment augment : augments) { + for (int i = 0; i < 5; i++) { + StaticAugment augment = augments[i]; if (augment != null) { - if (event.getEntity().level().isClientSide) { - augment.clientTick(event); - // ModJam.LOGGER.debug("AAAA"); + Slot slot = Slot.GetValue(i); + Player player = event.getEntity(); + + if (player.level().isClientSide) { + augment.clientTick(slot,event); + if (AugmentHelper.getCooldown(player, slot) >= 0){ + AugmentHelper.setCooldownAndUpdate(player, slot, AugmentHelper.getCooldown(player, slot) - 1); + } } else { - augment.serverTick(event); + augment.serverTick(slot,event); } } } diff --git a/src/main/java/com/portingdeadmods/modjam/network/KeyPressedPayload.java b/src/main/java/com/portingdeadmods/modjam/network/KeyPressedPayload.java index f1cb6dc0..68b8ebc9 100644 --- a/src/main/java/com/portingdeadmods/modjam/network/KeyPressedPayload.java +++ b/src/main/java/com/portingdeadmods/modjam/network/KeyPressedPayload.java @@ -7,11 +7,13 @@ import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; -public record KeyPressedPayload(int augmentId) implements CustomPacketPayload { +public record KeyPressedPayload(int augmentId, int slot) implements CustomPacketPayload { public static final Type TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(ModJam.MODID, "key_pressesd_paylad")); public static final StreamCodec STREAM_CODEC = StreamCodec.composite( ByteBufCodecs.INT, KeyPressedPayload::augmentId, + ByteBufCodecs.INT, + KeyPressedPayload::slot, KeyPressedPayload::new ); diff --git a/src/main/java/com/portingdeadmods/modjam/network/NetworkEvents.java b/src/main/java/com/portingdeadmods/modjam/network/NetworkEvents.java index 97c35123..93a1663a 100644 --- a/src/main/java/com/portingdeadmods/modjam/network/NetworkEvents.java +++ b/src/main/java/com/portingdeadmods/modjam/network/NetworkEvents.java @@ -21,5 +21,10 @@ public static void registerPayloads(final RegisterPayloadHandlersEvent event) { SetAugmentDataPayload.STREAM_CODEC, PayloadActions::setAugmentDataAction ); + registrar.playBidirectional( + SetCooldownPayload.TYPE, + SetCooldownPayload.STREAM_CODEC, + PayloadActions::setCooldownAction + ); } } diff --git a/src/main/java/com/portingdeadmods/modjam/network/PayloadActions.java b/src/main/java/com/portingdeadmods/modjam/network/PayloadActions.java index f089a2b2..0a599e50 100644 --- a/src/main/java/com/portingdeadmods/modjam/network/PayloadActions.java +++ b/src/main/java/com/portingdeadmods/modjam/network/PayloadActions.java @@ -13,8 +13,9 @@ public static void keyPressedAction(KeyPressedPayload payload, IPayloadContext c context.enqueueWork(()->{ Player player = context.player(); int augmentId = payload.augmentId(); - ModJam.LOGGER.info("Sent with packetId: {}", augmentId); - AugmentHelper.getAugment(augmentId).handleKeybindPress(player); + + // 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; @@ -24,7 +25,12 @@ public static void keyPressedAction(KeyPressedPayload payload, IPayloadContext c 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()); + // 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()); }); } } diff --git a/src/main/java/com/portingdeadmods/modjam/network/SetCooldownPayload.java b/src/main/java/com/portingdeadmods/modjam/network/SetCooldownPayload.java new file mode 100644 index 00000000..71fbdaaf --- /dev/null +++ b/src/main/java/com/portingdeadmods/modjam/network/SetCooldownPayload.java @@ -0,0 +1,25 @@ +package com.portingdeadmods.modjam.network; + +import com.portingdeadmods.modjam.ModJam; +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 org.jetbrains.annotations.NotNull; + +public record SetCooldownPayload (int cooldown, int slot)implements CustomPacketPayload { + public static final Type TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(ModJam.MODID, "augment_cooldown_payload")); + public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + ByteBufCodecs.INT, + SetCooldownPayload::cooldown, + ByteBufCodecs.INT, + SetCooldownPayload::slot, + SetCooldownPayload::new + ); + + @Override + public @NotNull Type type() { + return TYPE; + } +} diff --git a/src/main/java/com/portingdeadmods/modjam/registries/MJDataAttachments.java b/src/main/java/com/portingdeadmods/modjam/registries/MJDataAttachments.java index 8079b146..9683cee8 100644 --- a/src/main/java/com/portingdeadmods/modjam/registries/MJDataAttachments.java +++ b/src/main/java/com/portingdeadmods/modjam/registries/MJDataAttachments.java @@ -18,18 +18,48 @@ public final class MJDataAttachments { public static final Supplier> HEAD_AUGMENTATION = ATTACHMENTS.register( "head_augment_id", ()-> AttachmentType.builder(()->0).serialize(Codec.INT).copyOnDeath().build() ); + public static final Supplier> HEAD_AUGMENTATION_COOLDOWN = ATTACHMENTS.register( + "head_augment_cooldown", ()-> AttachmentType.builder(()->0).serialize(Codec.INT).copyOnDeath().build() + ); + public static final Supplier> HEAD_AUGMENTATION_DATA = ATTACHMENTS.register( + "head_augment_data", ()-> AttachmentType.builder(()->0).serialize(Codec.INT).copyOnDeath().build() + ); public static final Supplier> BODY_AUGMENTATION = ATTACHMENTS.register( "body_augment_id", ()-> AttachmentType.builder(()->0).serialize(Codec.INT).copyOnDeath().build() ); + public static final Supplier> BODY_AUGMENTATION_COOLDOWN = ATTACHMENTS.register( + "body_augment_cooldown", ()-> AttachmentType.builder(()->0).serialize(Codec.INT).copyOnDeath().build() + ); + public static final Supplier> BODY_AUGMENTATION_DATA = ATTACHMENTS.register( + "body_augment_data", ()-> AttachmentType.builder(()->0).serialize(Codec.INT).copyOnDeath().build() + ); public static final Supplier> ARMS_AUGMENTATION = ATTACHMENTS.register( "arms_augment_id", ()-> AttachmentType.builder(()->0).serialize(Codec.INT).copyOnDeath().build() ); + public static final Supplier> ARMS_AUGMENTATION_COOLDOWN = ATTACHMENTS.register( + "arms_augment_cooldown", ()-> AttachmentType.builder(()->0).serialize(Codec.INT).copyOnDeath().build() + ); + public static final Supplier> ARMS_AUGMENTATION_DATA = ATTACHMENTS.register( + "arms_augment_data", ()-> AttachmentType.builder(()->0).serialize(Codec.INT).copyOnDeath().build() + ); public static final Supplier> LEGS_AUGMENTATION = ATTACHMENTS.register( "legs_augment_id", ()-> AttachmentType.builder(()->0).serialize(Codec.INT).copyOnDeath().build() ); + public static final Supplier> LEGS_AUGMENTATION_COOLDOWN = ATTACHMENTS.register( + "legs_augment_cooldown", ()-> AttachmentType.builder(()->0).serialize(Codec.INT).copyOnDeath().build() + ); + public static final Supplier> LEGS_AUGMENTATION_DATA = ATTACHMENTS.register( + "legs_augment_data", ()-> AttachmentType.builder(()->0).serialize(Codec.INT).copyOnDeath().build() + ); public static final Supplier> HEART_AUGMENTATION = ATTACHMENTS.register( "heart_augment_id", ()-> AttachmentType.builder(()->0).serialize(Codec.INT).copyOnDeath().build() ); + public static final Supplier> HEART_AUGMENTATION_COOLDOWN = ATTACHMENTS.register( + "heart_augment_cooldown", ()-> AttachmentType.builder(()->0).serialize(Codec.INT).copyOnDeath().build() + ); + public static final Supplier> HEART_AUGMENTATION_DATA = ATTACHMENTS.register( + "heart_augment_data", ()-> AttachmentType.builder(()->0).serialize(Codec.INT).copyOnDeath().build() + ); // Lasers in a chunk public static final Supplier>> CHUNK_LASERS = ATTACHMENTS.register(