package top.r3944realms.lib39.mixin.init;

import com.mojang.datafixers.DataFixer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.ServerInterface;
import net.minecraft.server.Services;
import net.minecraft.server.WorldStem;
import net.minecraft.server.dedicated.DedicatedServer;
import net.minecraft.server.level.progress.ChunkProgressListenerFactory;
import net.minecraft.server.packs.repository.PackRepository;
import net.minecraft.world.level.storage.LevelStorageSource;
import net.minecraftforge.common.MinecraftForge;
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.CallbackInfoReturnable;
import top.r3944realms.lib39.api.event.MinecraftSetUpServiceEvent;

import java.net.Proxy;

/**
 * The type Mixin dedicate server.
 */
@Mixin(DedicatedServer.class)
public abstract class MixinDedicateServer extends MinecraftServer implements ServerInterface {
    /**
     * Instantiates a new Mixin dedicate server.
     *
     * @param serverThread            the server thread
     * @param storageSource           the storage source
     * @param packRepository          the pack repository
     * @param worldStem               the world stem
     * @param proxy                   the proxy
     * @param fixerUpper              the fixer upper
     * @param services                the services
     * @param progressListenerFactory the progress listener factory
     */
    public MixinDedicateServer(Thread serverThread, LevelStorageSource.LevelStorageAccess storageSource, PackRepository packRepository, WorldStem worldStem, Proxy proxy, DataFixer fixerUpper, Services services, ChunkProgressListenerFactory progressListenerFactory) {
        super(serverThread, storageSource, packRepository, worldStem, proxy, fixerUpper, services, progressListenerFactory);
    }

    @Inject(
            method = "initServer",
            at = @At(
                    value = "INVOKE",
                    target = "Lnet/minecraft/server/players/GameProfileCache;setUsesAuthentication(Z)V",
                    shift = At.Shift.AFTER
            )
    )
    private void initServer$setup(CallbackInfoReturnable<Boolean> cir) {
        MinecraftForge.EVENT_BUS.post(new MinecraftSetUpServiceEvent(this.services,this));
    }
}
