Skip to content

Commit

Permalink
setup networking
Browse files Browse the repository at this point in the history
  • Loading branch information
Thepigcat76 committed Jul 16, 2024
1 parent b0c1846 commit 64bcfe0
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ configurations {
dependencies {
implementation "net.neoforged:neoforge:${neo_version}"
implementation "curse.maven:curios-1037991:5441959"
runtimeOnly("mezz.jei:jei-1.21-neoforge:19.0.0.7")
runtimeOnly("mezz.jei:jei-1.21-neoforge:${jei_version}")
}


Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ neo_version_range=[21.0.10-beta,)

loader_version_range=[2,)

jei_version = 19.4.0.28

mod_id=modular_angelring

mod_name=Modular Angel Ring
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public static void stopDrift(PlayerTickEvent.Pre event) {
}
}


@SubscribeEvent(priority = EventPriority.LOW)
public static void newFlightSpeed(PlayerInteractEvent.RightClickItem event) {
final float MAX_FLY_SPEED = 0.15F;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.leclowndu93150.modular_angelring.networking;

import com.leclowndu93150.modular_angelring.AngelRingMain;
import com.leclowndu93150.modular_angelring.render.AngelRingRenderer;
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 KeyPressedPayload(int key) implements CustomPacketPayload {
public static final Type<KeyPressedPayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(AngelRingMain.MODID, "keypressed_payload"));
public static final StreamCodec<RegistryFriendlyByteBuf, KeyPressedPayload> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.INT,
KeyPressedPayload::key,
KeyPressedPayload::new
);

@Override
public @NotNull Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.leclowndu93150.modular_angelring.networking;

import com.leclowndu93150.modular_angelring.AngelRingMain;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
import net.neoforged.neoforge.network.registration.PayloadRegistrar;

@EventBusSubscriber(modid = AngelRingMain.MODID, bus = EventBusSubscriber.Bus.MOD)
public final class NetworkingEvents {
@SubscribeEvent
public static void registerPayloads(RegisterPayloadHandlersEvent event) {
PayloadRegistrar registrar = event.registrar(AngelRingMain.MODID);
registrar.playToServer(KeyPressedPayload.TYPE, KeyPressedPayload.STREAM_CODEC, PayloadActions::keyPressedAction);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.leclowndu93150.modular_angelring.networking;

import com.leclowndu93150.modular_angelring.AngelRingMain;
import com.leclowndu93150.modular_angelring.registry.KeyBindRegistry;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.network.handling.IPayloadContext;

public final class PayloadActions {
public static void keyPressedAction(KeyPressedPayload payload, IPayloadContext ctx) {
// SERVER-SIDE
Player player = ctx.player();
Level level = player.level();
int key = payload.key();
if (key == KeyBindRegistry.INERTIA_MODULE.get().getKey().getValue()) {
// do stuff if inertia module key was pressed
System.out.println("Inertia pressed");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.leclowndu93150.modular_angelring.common.AngelRingItem;
import com.leclowndu93150.modular_angelring.common.AngelRingModules;
import com.leclowndu93150.modular_angelring.networking.KeyPressedPayload;
import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.ChatFormatting;
import net.minecraft.client.KeyMapping;
Expand All @@ -18,6 +19,7 @@
import net.neoforged.neoforge.client.event.RegisterKeyMappingsEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.common.util.Lazy;
import net.neoforged.neoforge.network.PacketDistributor;
import org.lwjgl.glfw.GLFW;
import top.theillusivec4.curios.api.CuriosApi;
import top.theillusivec4.curios.api.SlotResult;
Expand Down Expand Up @@ -54,6 +56,8 @@ public static void onKey(InputEvent.Key event) {
ItemStack angelRingStack = slotResult.get().stack();
Level level = player.level();
if (INERTIA_MODULE.get().consumeClick() && AngelRingModules.getInertiaModifier(angelRingStack)) {
// Send payload to server
PacketDistributor.sendToServer(new KeyPressedPayload(INERTIA_MODULE.get().getKey().getValue()));
inertiaEnabled = !inertiaEnabled;
if (inertiaEnabled) {
player.displayClientMessage(Component.literal("Inertia Module: Enabled").withStyle(ChatFormatting.GREEN), true);
Expand Down

0 comments on commit 64bcfe0

Please sign in to comment.