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