Skip to content

Commit

Permalink
Augment Network syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
ktpatient committed Sep 8, 2024
1 parent 442f266 commit 9c0cdc8
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ public enum Slot {
BODY(1),
ARMS(2),
LEGS(3),
HEART(4);
HEART(4),
NONE(-1);

public int slotId;
Slot(int id){
this.slotId = id;
}
public boolean Compare(int i){return slotId == i;}
public static Slot GetValue(int _id)
{
Slot[] Slots = Slot.values();
for(int i = 0; i < Slots.length; i++)
{
if(Slots[i].Compare(_id))
return Slots[i];
}
return Slot.NONE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import com.portingdeadmods.modjam.ModJam;
import com.portingdeadmods.modjam.capabilities.augmentation.Slot;
import com.portingdeadmods.modjam.network.SetAugmentDataPayload;
import com.portingdeadmods.modjam.registries.MJDataAttachments;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.neoforged.neoforge.attachment.AttachmentType;
import net.neoforged.neoforge.network.PacketDistributor;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -43,25 +46,35 @@ private static Supplier<AttachmentType<Integer>> getAttachment(Slot slot){
ModJam.LOGGER.warn("Error parsing slot {} (I thought this was unreachable)", slot.name());
return MJDataAttachments.HEAD_AUGMENTATION;
}

public static int getId(Player player, Slot slot){
return player.getData(getAttachment(slot));
// return -2;
}

public static void setId(Player player, Slot slot , int id){
player.setData(getAttachment(slot), id);
if (player.level().isClientSide){
PacketDistributor.sendToServer(new SetAugmentDataPayload(id , slot.slotId));
} else {
PacketDistributor.sendToPlayer((ServerPlayer) player, new SetAugmentDataPayload(id, slot.slotId));
}
}

public static void incId(Player player, Slot slot){
setId(player, slot, AugmentHelper.getId(player, slot) + 1);
player.sendSystemMessage(Component.literal("Incremented to Id "+getId(player, slot)+" for slot "+slot.name()));

}

public static void decId(Player player, Slot slot){
setId(player, slot, AugmentHelper.getId(player, slot) - 1);
player.sendSystemMessage(Component.literal("Decremented to Id "+getId(player, slot)+" for slot "+slot.name()));
}

public static StaticAugment getAugment(int id){
return augmentHashMap.get(id);
}

public static StaticAugment[] getAugments(Player player) {
List<StaticAugment> augments = new ArrayList<>();
augments.add(getAugment(player, Slot.HEAD));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.portingdeadmods.modjam.content.augments;

import com.mojang.blaze3d.platform.InputConstants;
import com.portingdeadmods.modjam.ModJam;
import com.portingdeadmods.modjam.capabilities.MJCapabilities;
import com.portingdeadmods.modjam.events.MJClientEvents;
import com.portingdeadmods.modjam.network.KeyPressedPayload;
import com.portingdeadmods.modjam.utils.InputUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Items;
import net.neoforged.neoforge.event.level.BlockEvent;
Expand All @@ -26,10 +22,7 @@ public void breakBlock(BlockEvent.BreakEvent event) {

@Override
public void clientTick(PlayerTickEvent.Post event) {
ModJam.LOGGER.info("ClientTick for id {}", getId());

if (MJClientEvents.ClientBus.AUGMENT_TEST_KEYMAP.get().consumeClick()){
ModJam.LOGGER.debug("Key down");
if (InputUtils.isKeyDown(InputConstants.KEY_Y)){
PacketDistributor.sendToServer(new KeyPressedPayload(getId()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public int getId() {

@Override
public void clientTick(PlayerTickEvent.Post event) {
ModJam.LOGGER.info("ClientTick for id {}", getId());
// ModJam.LOGGER.info("ClientTick for id {}", getId());
if (InputUtils.isKeyDown(InputConstants.KEY_Y)) {
PacketDistributor.sendToAllPlayers(new KeyPressedPayload(getId()));
PacketDistributor.sendToServer(new KeyPressedPayload(getId()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public int getId() {

@Override
public void clientTick(PlayerTickEvent.Post event) {
ModJam.LOGGER.info("ClientTick for id {}", getId());
// ModJam.LOGGER.info("ClientTick for id {}", getId());

if (InputUtils.isKeyDown(InputConstants.KEY_Y)) {
ModJam.LOGGER.info("Snow");
PacketDistributor.sendToAllPlayers(new KeyPressedPayload(getId()));
// ModJam.LOGGER.info("Snow");
PacketDistributor.sendToServer(new KeyPressedPayload(getId()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.mojang.blaze3d.platform.InputConstants;
import com.portingdeadmods.modjam.capabilities.augmentation.Slot;
import com.portingdeadmods.modjam.content.augments.AugmentHelper;
import com.portingdeadmods.modjam.network.SetAugmentDataPayload;
import com.portingdeadmods.modjam.utils.InputUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
Expand All @@ -16,6 +18,7 @@
import net.minecraft.world.level.block.Blocks;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.NotNull;

public class AugmentDebugItem extends Item {
Expand Down Expand Up @@ -44,7 +47,7 @@ public boolean onDroppedByPlayer(@NotNull ItemStack item, Player player) {
AugmentHelper.incId(player, Slot.BODY);
}

return InteractionResult.SUCCESS;
return super.useOn(context);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
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 com.portingdeadmods.modjam.network.AugmentDataPayload;
import com.portingdeadmods.modjam.network.KeyPressedPayload;
import com.portingdeadmods.modjam.network.SetAugmentDataPayload;
import net.minecraft.server.level.ServerPlayer;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
Expand All @@ -17,10 +15,6 @@
@SuppressWarnings("unused")
@EventBusSubscriber(modid = ModJam.MODID)
public class AugmentEvents {
public static void logInEvent(PlayerEvent.PlayerLoggedInEvent event){
if (event.getEntity().level().isClientSide) return;
PacketDistributor.sendToPlayer((ServerPlayer) event.getEntity(), new AugmentDataPayload(1,1));
}

@SubscribeEvent
public static void breakEvent(BlockEvent.BreakEvent event){
Expand All @@ -40,7 +34,7 @@ public static void playerTick(PlayerTickEvent.Post event){
if (augment != null) {
if (event.getEntity().level().isClientSide) {
augment.clientTick(event);
ModJam.LOGGER.debug("AAAA");
// ModJam.LOGGER.debug("AAAA");
} else {
augment.serverTick(event);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.portingdeadmods.modjam.events;
package com.portingdeadmods.modjam.network;

import com.portingdeadmods.modjam.ModJam;
import com.portingdeadmods.modjam.network.AugmentDataPayload;
import com.portingdeadmods.modjam.network.KeyPressedPayload;
import com.portingdeadmods.modjam.network.PayloadActions;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
import net.neoforged.neoforge.network.handling.DirectionalPayloadHandler;
import net.neoforged.neoforge.network.registration.PayloadRegistrar;

@EventBusSubscriber(modid = ModJam.MODID, bus = EventBusSubscriber.Bus.MOD)
Expand All @@ -20,10 +16,10 @@ public static void registerPayloads(final RegisterPayloadHandlersEvent event) {
KeyPressedPayload.STREAM_CODEC,
PayloadActions::keyPressedAction
);
registrar.playToServer(
AugmentDataPayload.TYPE,
AugmentDataPayload.STREAM_CODEC,
PayloadActions::augmentDataAction
registrar.playBidirectional(
SetAugmentDataPayload.TYPE,
SetAugmentDataPayload.STREAM_CODEC,
PayloadActions::setAugmentDataAction
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@
import com.portingdeadmods.modjam.ModJam;
import com.portingdeadmods.modjam.capabilities.augmentation.Slot;
import com.portingdeadmods.modjam.content.augments.AugmentHelper;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;
import net.neoforged.neoforge.network.handling.IPayloadContext;

public class PayloadActions {

public static void keyPressedAction(KeyPressedPayload payload, IPayloadContext context){
ModJam.LOGGER.debug("Key pressed");
context.enqueueWork(()->{
Player player = context.player();
int augmentId = payload.augmentId();
ModJam.LOGGER.info("Sent with packetId: {}", augmentId);
AugmentHelper.getAugment(augmentId).handleKeybindPress(player);
}).exceptionally(e->{
context.disconnect(Component.literal("action failed: "+ e.getMessage()));
return null;
});

}
public static void augmentDataAction(AugmentDataPayload payload, IPayloadContext context){
public static void setAugmentDataAction(SetAugmentDataPayload payload, IPayloadContext context){
context.enqueueWork(()->{
AugmentHelper.setId(context.player(), Slot.GetValue(payload.slot()), payload.augmentId());
ModJam.LOGGER.info("Syncing data {}", payload.slot());
});

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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 SetAugmentDataPayload(int augmentId, int slot) implements CustomPacketPayload {

public static final Type<SetAugmentDataPayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(ModJam.MODID, "augment_data_payload"));
public static final StreamCodec<RegistryFriendlyByteBuf, SetAugmentDataPayload> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.INT,
SetAugmentDataPayload::augmentId,
ByteBufCodecs.INT,
SetAugmentDataPayload::slot,
SetAugmentDataPayload::new
);

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

0 comments on commit 9c0cdc8

Please sign in to comment.