Skip to content

Commit

Permalink
feat: add ClientPlayLifecycleEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamalam360 committed Oct 21, 2024
1 parent 7be2adc commit 78d0d36
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 5 deletions.
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
Original file line number Diff line number Diff line change
@@ -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> JOIN = EventFactory.createLoop(Join.class);
/**
* Called when the local player leaves a logical server.
*/
public static final Event<Leave> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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 - <a href="https://github.com/FabricMC/fabric/blob/1.21.2/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientPlayNetworkHandlerMixin.java#L54">...</a>
*/
@Inject(
method = "handleLogin",
at = @At("RETURN")
)
private void jamlib$joinServer(ClientboundLoginPacket packet, CallbackInfo ci) {
ClientPlayLifecycleEvents.JOIN.invoker().onJoin(Minecraft.getInstance());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
17 changes: 17 additions & 0 deletions common/src/main/resources/jamlib.mixins.json
Original file line number Diff line number Diff line change
@@ -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
}
}
1 change: 1 addition & 0 deletions fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
]
},
"mixins": [
"jamlib.mixins.json"
],
"depends": {
"fabric": ">=${fabric_api_version}",
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions neoforge/src/main/resources/META-INF/neoforge.mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ type = "required"
versionRange = "[${architectury_version},)"
ordering = "AFTER"
side = "BOTH"

[[mixins]]
config = "jamlib.mixins.json"
1 change: 1 addition & 0 deletions testmod-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ loom {

dependencies {
modImplementation libs.fabric.loader
modImplementation libs.architectury.common
implementation project(path: ":common", configuration: "namedElements")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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!"));
}
}
1 change: 1 addition & 0 deletions testmod-fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 78d0d36

Please sign in to comment.