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);
    }
}
