package com.Acrobot.ChestShop.Plugins;

import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.Protection.BuildPermissionEvent;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.internal.platform.WorldGuardPlatform;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.managers.RegionManager;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

/**
 * @author Acrobot
 */
public class WorldGuardBuilding implements Listener {
    private WorldGuardPlugin worldGuard;
    private WorldGuardPlatform worldGuardPlatform;

    public WorldGuardBuilding(WorldGuardPlugin plugin) {
        this.worldGuard = plugin;
        this.worldGuardPlatform = WorldGuard.getInstance().getPlatform();
    }

    @EventHandler
    public void canBuild(BuildPermissionEvent event) {
        ApplicableRegionSet regions = getApplicableRegions(event.getSign().getBlock().getLocation());

        if (regions != null && Properties.WORLDGUARD_USE_FLAG) {
            event.allow(regions.queryState(worldGuard.wrapPlayer(event.getPlayer()), WorldGuardFlags.ENABLE_SHOP) == StateFlag.State.ALLOW);
        } else {
            event.allow(regions == null || regions.size() != 0);
        }
    }

    private ApplicableRegionSet getApplicableRegions(Location location) {
        RegionManager regionManager = worldGuardPlatform.getRegionContainer().get(BukkitAdapter.adapt(location.getWorld()));
        if (regionManager == null) {
            return null;
        }
        return regionManager.getApplicableRegions(BukkitAdapter.adapt(location).toVector().toBlockPoint());
    }
}

@@ -1191,19 +1191,70 @@
 ons 
-!
+=
 = null
- && 
+) %7B%0A            event.allow(false);%0A        %7D else if (
 Prop
@@ -1476,35 +1476,16 @@
 ions
- == null %7C%7C regions
 .size() 
 != 0
@@ -1484,10 +1484,9 @@
 e() 
-!=
+%3E
  0);

Fix players being allowed to build outside regions (Fixes #278)