package eu.mhsl.minecraft.pixelblocks.listeners;

import eu.mhsl.minecraft.pixelblocks.Main;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlockWorld;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPortalEvent;
import org.bukkit.event.player.PlayerPortalEvent;


public class ExitPixelWorldListener implements Listener {
    @EventHandler
    public void onPlayerPortal(PlayerPortalEvent event) {
        World pixelBlockWorld = event.getFrom().getWorld();
        if(!PixelBlockWorld.isPixelWorld(pixelBlockWorld)) return;

        event.setCancelled(true);
        PixelBlock pixelBlock = PixelBlock.getPixelBlockFromBlockWorld(pixelBlockWorld);
        if(pixelBlock == null) {
            Main.logger().warning("Player used a portal in an unknown pixel world: " + pixelBlockWorld.getName());
            return;
        }
        pixelBlock.exitBlock(event.getPlayer());
    }

    @EventHandler
    public void onEntityPortal(EntityPortalEvent event) {
        World pixelBlockWorld = event.getFrom().getWorld();
        if(!PixelBlockWorld.isPixelWorld(pixelBlockWorld)) return;

        event.setCancelled(true);
    }
}
