From 78d0d368d9a7dffe7312dd38b1dc0e994e527886 Mon Sep 17 00:00:00 2001 From: Jamalam Date: Mon, 21 Oct 2024 18:28:41 +0100 Subject: [PATCH] feat: add ClientPlayLifecycleEvents --- CHANGELOG.md | 4 +-- .../client/ClientPlayLifecycleEvents.java | 36 +++++++++++++++++++ .../event/ClientPacketListenerMixin.java | 27 ++++++++++++++ .../jamlib/mixin/event/ConnectionMixin.java | 35 ++++++++++++++++++ common/src/main/resources/jamlib.mixins.json | 17 +++++++++ fabric/src/main/resources/fabric.mod.json | 1 + gradle.properties | 2 +- .../resources/META-INF/neoforge.mods.toml | 3 ++ testmod-common/build.gradle | 1 + .../io/github/jamalam360/testmod/TestMod.java | 6 +++- testmod-fabric/build.gradle | 1 + 11 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 common/src/main/java/io/github/jamalam360/jamlib/events/client/ClientPlayLifecycleEvents.java create mode 100644 common/src/main/java/io/github/jamalam360/jamlib/mixin/event/ClientPacketListenerMixin.java create mode 100644 common/src/main/java/io/github/jamalam360/jamlib/mixin/event/ConnectionMixin.java create mode 100644 common/src/main/resources/jamlib.mixins.json diff --git a/CHANGELOG.md b/CHANGELOG.md index eeb93f8..2b4ad77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1 @@ -- (feat) config options with long names will now render in the screen as scrolling strings. -- (chore) various code cleanups -- (chore) use parchment mappings +- Add `ClientPlayLifecycleEvents`. diff --git a/common/src/main/java/io/github/jamalam360/jamlib/events/client/ClientPlayLifecycleEvents.java b/common/src/main/java/io/github/jamalam360/jamlib/events/client/ClientPlayLifecycleEvents.java new file mode 100644 index 0000000..c0d8996 --- /dev/null +++ b/common/src/main/java/io/github/jamalam360/jamlib/events/client/ClientPlayLifecycleEvents.java @@ -0,0 +1,36 @@ +package io.github.jamalam360.jamlib.events.client; + +import com.mojang.authlib.minecraft.client.MinecraftClient; +import dev.architectury.event.Event; +import dev.architectury.event.EventFactory; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.Minecraft; +import net.minecraft.world.entity.player.Player; + +/** + * Events for client-side player lifecycle events. + */ +@Environment(EnvType.CLIENT) +public class ClientPlayLifecycleEvents { + /** + * Called when the local player has joined a logical server. + */ + public static final Event JOIN = EventFactory.createLoop(Join.class); + /** + * Called when the local player leaves a logical server. + */ + public static final Event DISCONNECT = EventFactory.createLoop(Leave.class); + + @Environment(EnvType.CLIENT) + @FunctionalInterface + public interface Join { + void onJoin(Minecraft client); + } + + @Environment(EnvType.CLIENT) + @FunctionalInterface + public interface Leave { + void onLeave(Minecraft client); + } +} diff --git a/common/src/main/java/io/github/jamalam360/jamlib/mixin/event/ClientPacketListenerMixin.java b/common/src/main/java/io/github/jamalam360/jamlib/mixin/event/ClientPacketListenerMixin.java new file mode 100644 index 0000000..02b658f --- /dev/null +++ b/common/src/main/java/io/github/jamalam360/jamlib/mixin/event/ClientPacketListenerMixin.java @@ -0,0 +1,27 @@ +package io.github.jamalam360.jamlib.mixin.event; + +import io.github.jamalam360.jamlib.events.client.ClientPlayLifecycleEvents; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ClientPacketListener; +import net.minecraft.network.protocol.game.ClientboundLoginPacket; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(ClientPacketListener.class) +@Environment(EnvType.CLIENT) +public class ClientPacketListenerMixin { + /** + * Injection point taken from Fabric API - ... + */ + @Inject( + method = "handleLogin", + at = @At("RETURN") + ) + private void jamlib$joinServer(ClientboundLoginPacket packet, CallbackInfo ci) { + ClientPlayLifecycleEvents.JOIN.invoker().onJoin(Minecraft.getInstance()); + } +} diff --git a/common/src/main/java/io/github/jamalam360/jamlib/mixin/event/ConnectionMixin.java b/common/src/main/java/io/github/jamalam360/jamlib/mixin/event/ConnectionMixin.java new file mode 100644 index 0000000..3f99ca9 --- /dev/null +++ b/common/src/main/java/io/github/jamalam360/jamlib/mixin/event/ConnectionMixin.java @@ -0,0 +1,35 @@ +package io.github.jamalam360.jamlib.mixin.event; + +import io.github.jamalam360.jamlib.events.client.ClientPlayLifecycleEvents; +import io.netty.channel.ChannelHandlerContext; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.Minecraft; +import net.minecraft.network.Connection; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(Connection.class) +@Environment(EnvType.CLIENT) +public class ConnectionMixin { + @Inject( + method = "channelInactive", + at = @At("HEAD") + ) + private void jamlib$callDisconnectEvent(ChannelHandlerContext channelHandlerContext, CallbackInfo ci) { + ClientPlayLifecycleEvents.DISCONNECT.invoker().onLeave(Minecraft.getInstance()); + } + + @Inject( + method = "handleDisconnection", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/network/PacketListener;onDisconnect(Lnet/minecraft/network/DisconnectionDetails;)V" + ) + ) + private void jamlib$callDisconnectEvent(CallbackInfo ci) { + ClientPlayLifecycleEvents.DISCONNECT.invoker().onLeave(Minecraft.getInstance()); + } +} diff --git a/common/src/main/resources/jamlib.mixins.json b/common/src/main/resources/jamlib.mixins.json new file mode 100644 index 0000000..4b6b545 --- /dev/null +++ b/common/src/main/resources/jamlib.mixins.json @@ -0,0 +1,17 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "io.github.jamalam360.jamlib.mixin", + "compatibilityLevel": "JAVA_17", + "mixins": [ + ], + "client": [ + "event.ClientPacketListenerMixin", + "event.ConnectionMixin" + ], + "server": [ + ], + "injectors": { + "defaultRequire": 1 + } +} diff --git a/fabric/src/main/resources/fabric.mod.json b/fabric/src/main/resources/fabric.mod.json index cb713f4..c91345b 100644 --- a/fabric/src/main/resources/fabric.mod.json +++ b/fabric/src/main/resources/fabric.mod.json @@ -24,6 +24,7 @@ ] }, "mixins": [ + "jamlib.mixins.json" ], "depends": { "fabric": ">=${fabric_api_version}", diff --git a/gradle.properties b/gradle.properties index 07c9701..e563a02 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,7 +1,7 @@ org.gradle.jvmargs=-Xmx3G org.gradle.daemon=false org.gradle.parallel=true -version=1.0.11+1.21 +version=1.2.0+1.21 minecraft_version=1.21 branch=main group=io.github.jamalam360 diff --git a/neoforge/src/main/resources/META-INF/neoforge.mods.toml b/neoforge/src/main/resources/META-INF/neoforge.mods.toml index 03f67f2..1019f4f 100644 --- a/neoforge/src/main/resources/META-INF/neoforge.mods.toml +++ b/neoforge/src/main/resources/META-INF/neoforge.mods.toml @@ -32,3 +32,6 @@ type = "required" versionRange = "[${architectury_version},)" ordering = "AFTER" side = "BOTH" + +[[mixins]] +config = "jamlib.mixins.json" diff --git a/testmod-common/build.gradle b/testmod-common/build.gradle index 4833f0b..81bfa71 100644 --- a/testmod-common/build.gradle +++ b/testmod-common/build.gradle @@ -4,6 +4,7 @@ loom { dependencies { modImplementation libs.fabric.loader + modImplementation libs.architectury.common implementation project(path: ":common", configuration: "namedElements") } diff --git a/testmod-common/src/main/java/io/github/jamalam360/testmod/TestMod.java b/testmod-common/src/main/java/io/github/jamalam360/testmod/TestMod.java index 55c3fa7..0cffde8 100644 --- a/testmod-common/src/main/java/io/github/jamalam360/testmod/TestMod.java +++ b/testmod-common/src/main/java/io/github/jamalam360/testmod/TestMod.java @@ -2,6 +2,7 @@ import io.github.jamalam360.jamlib.JamLibPlatform; import io.github.jamalam360.jamlib.config.ConfigManager; +import io.github.jamalam360.jamlib.events.client.ClientPlayLifecycleEvents; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -16,8 +17,11 @@ public class TestMod { public static void init() { - LOGGER.info("Initializing JamLib Test Mod on " + JamLibPlatform.getPlatform()); + LOGGER.info("Initializing JamLib Test Mod on {}", JamLibPlatform.getPlatform()); System.out.println(CONFIG_MANAGER.get()); System.out.println(QCB_CONFIG.get()); + + ClientPlayLifecycleEvents.JOIN.register(client -> LOGGER.info("Joined server!")); + ClientPlayLifecycleEvents.DISCONNECT.register(client -> LOGGER.info("Left server!")); } } diff --git a/testmod-fabric/build.gradle b/testmod-fabric/build.gradle index 9885538..4636878 100644 --- a/testmod-fabric/build.gradle +++ b/testmod-fabric/build.gradle @@ -24,6 +24,7 @@ configurations { dependencies { modImplementation libs.fabric.loader modImplementation libs.fabric.api + modImplementation libs.architectury.fabric modRuntimeOnly libs.modmenu implementation project(path: ":fabric", configuration: "namedElements")