package eu.mhsl.minenet.minigames.handler.global;

import eu.mhsl.minenet.minigames.instance.Spawnable;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Player;
import net.minestom.server.event.EventListener;
import net.minestom.server.event.instance.AddEntityToInstanceEvent;
import net.minestom.server.potion.Potion;
import net.minestom.server.potion.PotionEffect;
import net.minestom.server.timer.ExecutionType;
import org.jetbrains.annotations.NotNull;

public class AddEntityToInstanceEventListener implements EventListener<AddEntityToInstanceEvent> {
    @Override
    public @NotNull Class eventType() {
        return AddEntityToInstanceEvent.class;
    }

    @Override
    public @NotNull Result run(@NotNull AddEntityToInstanceEvent event) {
        if(event.getEntity() instanceof Player p) {
            MinecraftServer.getSchedulerManager().scheduleNextTick(p::refreshCommands, ExecutionType.TICK_END);

            if(event.getInstance() instanceof Spawnable instance) {
                p.setRespawnPoint(instance.getSpawn());
            }

            p.addEffect(new Potion(PotionEffect.BLINDNESS, (byte) 1, 20));
        }

        return Result.SUCCESS;
    }
}
