package eu.mhsl.minenet.minigames.util;

import eu.mhsl.minenet.minigames.instance.Spawnable;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.timer.TaskSchedule;

import java.util.Set;

public class MoveInstance {
    public static void move(Set<Player> playerList, Spawnable destination) {
        playerList.forEach(player -> move(player, destination));
    }
    public static void move(Entity p, Spawnable destination) {
        if(p.getInstance() != destination) p.setInstance((Instance) destination, destination.getSpawn());
    }

    public static void forceCloseInstance(InstanceContainer instance) {
        instance.scheduler().scheduleTask(() -> {
            instance.getPlayers().forEach(player -> player.kick("you exceeded the switch timeout while an instance got closed"));
            MinecraftServer.getInstanceManager().unregisterInstance(instance);
        }, TaskSchedule.seconds(10), TaskSchedule.stop());
    }
}
