feat: add player join and leave events
Some checks failed
Test and Deploy / deploy (17, ubuntu-latest) (push) Blocked by required conditions
Test and Deploy / build (17, ubuntu-latest) (push) Has been cancelled

This commit is contained in:
Quentin Legot 2024-08-20 18:38:23 +02:00
parent 204198f143
commit d6515d9cbb
8 changed files with 101 additions and 5 deletions

View File

@ -0,0 +1,37 @@
package fr.altarik.toolbox.core.event;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.ActionResult;
public interface PlayerLifecycleCallback {
Event<PlayerJoin> PLAYER_JOIN = EventFactory.createArrayBacked(PlayerJoin.class, listeners -> player -> {
for(PlayerJoin listener : listeners) {
ActionResult result = listener.onPlayerJoin(player);
if (result != ActionResult.PASS)
return result;
}
return ActionResult.PASS;
});
Event<PlayerLeave> PLAYER_LEAVE = EventFactory.createArrayBacked(PlayerLeave.class, listeners -> player -> {
for(PlayerLeave listener : listeners) {
ActionResult result = listener.onPlayerLeave(player);
if (result != ActionResult.PASS)
return result;
}
return ActionResult.PASS;
});
@FunctionalInterface
interface PlayerJoin {
ActionResult onPlayerJoin(ServerPlayerEntity player);
}
@FunctionalInterface
interface PlayerLeave {
ActionResult onPlayerLeave(ServerPlayerEntity player);
}
}

View File

@ -0,0 +1,20 @@
package fr.altarik.toolbox.core.mixin;
import fr.altarik.toolbox.core.event.PlayerLifecycleCallback;
import net.minecraft.network.ClientConnection;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ConnectedClientData;
import net.minecraft.server.network.ServerPlayerEntity;
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(PlayerManager.class)
public class PlayerJoinEvent {
@Inject(method = "onPlayerConnect", at = @At(value = "RETURN"))
private void onPlayerConnect(ClientConnection connection, ServerPlayerEntity player, ConnectedClientData clientData, CallbackInfo ci) {
PlayerLifecycleCallback.PLAYER_JOIN.invoker().onPlayerJoin(player);
}
}

View File

@ -0,0 +1,23 @@
package fr.altarik.toolbox.core.mixin;
import fr.altarik.toolbox.core.event.PlayerLifecycleCallback;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ServerPlayNetworkHandler.class)
public class PlayerLeaveEvent {
@Shadow
public ServerPlayerEntity player;
@Inject(at = @At(value = "HEAD"), method = "onDisconnected")
private void onPlayerQuit(Text reason, CallbackInfo ci) {
PlayerLifecycleCallback.PLAYER_LEAVE.invoker().onPlayerLeave(player);
}
}

View File

@ -0,0 +1,14 @@
{
"required": true,
"minVersion": "0.8",
"package": "fr.altarik.toolbox.core.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"PlayerJoinEvent",
"PlayerLeaveEvent"
],
"verbose": false,
"injectors": {
"defaultRequire": 1
}
}

View File

@ -19,7 +19,9 @@
"entrypoints": { "entrypoints": {
"main": [] "main": []
}, },
"mixins": [], "mixins": [
"Core.mixins.json"
],
"depends": { "depends": {
"fabricloader": "^${loaderVersion}", "fabricloader": "^${loaderVersion}",
"fabric-api": "*", "fabric-api": "*",

View File

@ -2,7 +2,7 @@ import fr.altarik.CreateTag
import fr.altarik.ReportDiscord import fr.altarik.ReportDiscord
plugins { plugins {
id 'fabric-loom' version '1.5-SNAPSHOT' apply false id 'fabric-loom' version '1.6-SNAPSHOT' apply false
} }
Properties local = new Properties() Properties local = new Properties()

View File

@ -5,10 +5,10 @@ junit_version=5.9.0
minecraft_version=1.20.4 minecraft_version=1.20.4
yarn_mappings=1.20.4+build.3 yarn_mappings=1.20.4+build.3
loader_version=0.15.6 loader_version=0.15.6
fabric_version=0.95.4+1.20.4 fabric_version=0.97.1+1.20.4
maven_group=fr.altarik.toolbox maven_group=fr.altarik.toolbox
maven_version=5.0.0 maven_version=5.1.0
git_owner=quentinlegot git_owner=quentinlegot
git_repo=Toolbox git_repo=Toolbox

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists