Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Thepigcat76 committed Sep 9, 2024
2 parents fb4d277 + a9c0387 commit b7d5065
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +28,29 @@ public static StaticAugment getAugment(Player player, Slot slot) {
return augmentHashMap.get(id);
}

public static Supplier<AttachmentType<Integer>> getAttachment(Slot slot) {
public static Supplier<AttachmentType<Integer>> 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<AttachmentType<Integer>> 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<AttachmentType<Integer>> getAttachment(Slot slot) {
return switch (slot) {
case HEAD -> MJDataAttachments.HEAD_AUGMENTATION;
case BODY -> MJDataAttachments.BODY_AUGMENTATION;
Expand All @@ -40,12 +63,23 @@ public static Supplier<AttachmentType<Integer>> 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()) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
21 changes: 15 additions & 6 deletions src/main/java/com/portingdeadmods/modjam/events/AugmentEvents.java
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyPressedPayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(ModJam.MODID, "key_pressesd_paylad"));
public static final StreamCodec<RegistryFriendlyByteBuf, KeyPressedPayload> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.INT,
KeyPressedPayload::augmentId,
ByteBufCodecs.INT,
KeyPressedPayload::slot,
KeyPressedPayload::new
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
});
}
}
Original file line number Diff line number Diff line change
@@ -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<SetCooldownPayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(ModJam.MODID, "augment_cooldown_payload"));
public static final StreamCodec<RegistryFriendlyByteBuf, SetCooldownPayload> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.INT,
SetCooldownPayload::cooldown,
ByteBufCodecs.INT,
SetCooldownPayload::slot,
SetCooldownPayload::new
);

@Override
public @NotNull Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}
Loading

0 comments on commit b7d5065

Please sign in to comment.