package com.trafalcraft.anti_redstone_clock.util.worldGuard;


import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
import com.sk89q.worldguard.protection.managers.RegionManager;
import org.bukkit.Location;
import org.bukkit.World;

public class WorldGuard_7 implements IWorldGuard {
    @Override
    public boolean isAllowedRegion(Location loc) {
        World world = loc.getWorld();
        if (world == null) return false;
        com.sk89q.worldguard.protection.regions.RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
        RegionManager regionManager = container.get(BukkitAdapter.adapt(world));
        if (regionManager == null) return false;
        BlockVector3 bv = BukkitAdapter.asBlockVector(loc);
        ApplicableRegionSet regionSet = regionManager.getApplicableRegions(bv);
        // Prüfe das antiredstoneclock-Flag in allen Regionen an dieser Location
            StateFlag flag = (StateFlag) WorldGuard.getInstance().getFlagRegistry().get("antiredstoneclock");
            if (flag == null) return false;
            // testState gibt true zurück, wenn das Flag auf ALLOW steht (Schutz aktiv), false bei DENY (Schutz ignorieren)
            return !regionSet.testState(null, flag);
    }

    @Override
    public String getVersion() {
        return "7.x";
    }

    @Override
    public boolean registerFlag() {
        try {
            FlagRegistry registry = WorldGuard.getInstance().getFlagRegistry();
            StateFlag flag = new StateFlag("antiredstoneclock", true);
            Flag<?> existing = registry.get("antiredstoneclock");
            if (existing == null) {
                registry.register(flag);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
