package com.Acrobot.ChestShop.Plugins;

import com.Acrobot.ChestShop.Events.Protection.ProtectionCheckEvent;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.internal.permission.RegionPermissionModel;
import com.sk89q.worldguard.internal.platform.WorldGuardPlatform;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.managers.RegionManager;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;

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

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

    @EventHandler
    public void onProtectionCheck(ProtectionCheckEvent event) {
        if (event.getResult() == Event.Result.DENY) {
            return;
        }

        Block block = event.getBlock();
        Player player = event.getPlayer();

        BlockVector3 blockPos = BukkitAdapter.adapt(block.getLocation()).toVector().toBlockPoint();
        RegionManager manager = worldGuardPlatform.getRegionContainer().get(BukkitAdapter.adapt(block.getWorld()));
        if (manager == null) {
            return;
        }
        ApplicableRegionSet set = manager.getApplicableRegions(blockPos);

        LocalPlayer localPlayer = worldGuard.wrapPlayer(player);

        if (!canAccess(localPlayer, block, set)) {
            event.setResult(Event.Result.DENY);
        }
    }

    private boolean canAccess(LocalPlayer player, Block block, ApplicableRegionSet set) {
        return new RegionPermissionModel(player).mayIgnoreRegionProtection(BukkitAdapter.adapt(block.getWorld()))
                || set.testState(player, Flags.BUILD)
                || set.testState(player, Flags.CHEST_ACCESS);
    }
}

@@ -182,25 +182,61 @@
 dit.
-math.BlockVector3
+util.Location;%0Aimport com.sk89q.worldedit.world.World
 ;%0Aim
@@ -314,16 +314,77 @@
 dGuard;%0A
+import com.sk89q.worldguard.bukkit.BukkitWorldConfiguration;%0A
 import c
@@ -1549,25 +1549,24 @@
 ayer();%0A
-%0A
         
 BlockVec
@@ -1561,99 +1561,270 @@
     
-BlockVector3 blockPos = BukkitAdapter.adapt(block.getLocation()).toVector().toBlockPoint();
+LocalPlayer localPlayer = worldGuard.wrapPlayer(player);%0A        Location location = BukkitAdapter.adapt(block.getLocation());%0A%0A        if (!canAccess(localPlayer, block, location)) %7B%0A            event.setResult(Event.Result.DENY);%0A            return;%0A        %7D%0A
 %0A   
@@ -1900,44 +1900,35 @@
 get(
-BukkitAdapter.adapt(block.getWorld()
+(World) location.getExtent(
 ));%0A
@@ -2055,16 +2055,42 @@
 ons(
-b
+location.toVector().toB
 lockPo
-s
+int()
 );%0A%0A
@@ -2101,79 +2101,159 @@
     
-LocalPlayer localPlayer = worldGuard.wrapPlayer(player);%0A
+if (!canAccess(localPlayer, (World) location.getExtent(), set)) %7B%0A            event.setResult(Event.Result.DENY);%0A        %7D
 %0A    
+%7D%0A%0A
     
-if (!
+private boolean 
 canA
@@ -2250,33 +2250,33 @@
 olean canAccess(
-l
+L
 ocalPlayer, bloc
@@ -2273,82 +2273,411 @@
 ayer
-, block, set)) %7B%0A            event.setResult(Event.Result.DENY);%0A        %7D
+ player, Block block, Location location) %7B%0A        BukkitWorldConfiguration wcfg = (BukkitWorldConfiguration) worldGuardPlatform.getGlobalStateManager().get((World) location.getExtent());%0A        return !wcfg.signChestProtection%0A                %7C%7C !wcfg.getChestProtection().isChest(BukkitAdapter.asBlockType(block.getType()))%0A                %7C%7C !wcfg.getChestProtection().isProtected(location, player);
 %0A   
@@ -2730,27 +2730,27 @@
 player, 
-Block block
+World world
 , Applic
@@ -2849,45 +2849,13 @@
 ion(
-BukkitAdapter.adapt(block.getW
+w
 orld
-())
 )%0A  

Check WorldGuard's chest protection too