5.1.0: add player life cycle events #33

Merged
quentinlegot merged 2 commits from dev into master 2024-08-20 18:52:18 +02:00
9 changed files with 109 additions and 5 deletions

View File

@ -33,6 +33,14 @@ jobs:
with: with:
java-version: ${{ matrix.java }} java-version: ${{ matrix.java }}
distribution: 'oracle' distribution: 'oracle'
- uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: make gradle wrapper executable - name: make gradle wrapper executable
if: ${{ runner.os != 'Windows' }} if: ${{ runner.os != 'Windows' }}
run: | run: |

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