package eu.mhsl.minecraft.pixelblocks.listeners;

import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlockWorld;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;


public class QuitWhileInPixelBlockListener implements Listener {
    @EventHandler
    public void onLeave(PlayerQuitEvent event) {
        kickPlayerOutOfWorld(event.getPlayer());
    }

    public static void kickPlayerOutOfWorld(Player player) {
        World pixelBlockWorld = player.getLocation().getWorld();
        if(!PixelBlockWorld.isPixelWorld(pixelBlockWorld)) return;
        PixelBlock pixelBlock = PixelBlock.getPixelBlockFromBlockWorld(pixelBlockWorld);
        if(pixelBlock == null) return;
        pixelBlock.exitBlock(player);
    }
}
