package com.massivecraft.factions.listeners;

import com.massivecraft.factions.Conf;
import com.massivecraft.factions.util.XMaterial;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.player.PlayerTeleportEvent;


public class FactionsExploitListener implements Listener {

    /**
     * @author FactionsUUID Team
     */

    public static boolean clippingThrough(Location target, Location from, double thickness) {
        return ((from.getX() > target.getX() && (from.getX() - target.getX() < thickness)) || (target.getX() > from.getX() && (target.getX() - from.getX() < thickness)) || (from.getZ() > target.getZ() && (from.getZ() - target.getZ() < thickness)) || (target.getZ() > from.getZ() && (target.getZ() - from.getZ() < thickness)));
    }

    @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
    public void obsidianGenerator(BlockFromToEvent event) {
        if (!Conf.handleExploitObsidianGenerators) {
            return;
        }

        // thanks to ObGenBlocker and WorldGuard for this method
        Block block = event.getToBlock();

        Material source = event.getBlock().getType();
        Material target = block.getType();
        if ((target == XMaterial.REDSTONE_WIRE.parseMaterial() || target == XMaterial.TRIPWIRE.parseMaterial()) && (source == XMaterial.AIR.parseMaterial() || source == XMaterial.LAVA.parseMaterial() || source.toString().equalsIgnoreCase("STATIONARY_LAVA"))) {
            block.setType(XMaterial.AIR.parseMaterial());
        }
    }

    @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
    public void enderPearlTeleport(PlayerTeleportEvent event) {
        if (!Conf.handleExploitEnderPearlClipping) {
            return;
        }
        if (event.getCause() != PlayerTeleportEvent.TeleportCause.ENDER_PEARL) {
            return;
        }

        // this exploit works when the target location is within 0.31 blocks or so of a door or glass block or similar...
        Location target = event.getTo();
        Location from = event.getFrom();

        // blocks who occupy less than 1 block width or length wise need to be handled differently
        Material mat = event.getTo().getBlock().getType();
        if (((mat == XMaterial.GLASS_PANE.parseMaterial()
                || mat == XMaterial.IRON_BARS.parseMaterial()) && clippingThrough(target, from, 0.65))
                || ((mat == XMaterial.OAK_FENCE.parseMaterial() || mat == XMaterial.NETHER_BRICK_FENCE.parseMaterial()) && clippingThrough(target, from, 0.45))) {
            event.setTo(from);
            return;
        }

        // simple fix otherwise: ender pearl target locations are standardized to be in the center (X/Z) of the target block, not at the edges
        target.setX(target.getBlockX() + 0.5);
        target.setZ(target.getBlockZ() + 0.5);
        event.setTo(target);

    }
}
