package com.Acrobot.ChestShop.Listeners.Player;

import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Security;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.block.DoubleChest;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryType;

/**
 * @author Acrobot
 */
public class PlayerInventory implements Listener {
    @EventHandler
    public static void onInventoryOpen(InventoryOpenEvent event) {
        if (event.getInventory().getType() != InventoryType.CHEST) {
            return;
        }

        if (!Properties.TURN_OFF_DEFAULT_PROTECTION_WHEN_PROTECTED_EXTERNALLY) {
            return;
        }

        HumanEntity entity = event.getPlayer();

        if (!(entity instanceof Player) || (!(event.getInventory().getHolder() instanceof Chest) && !(event.getInventory().getHolder() instanceof DoubleChest))) {
            return;
        }

        Player player = (Player) entity;
        Block chest;

        if (event.getInventory().getHolder() instanceof Chest) {
            chest = ((BlockState) event.getInventory().getHolder()).getBlock();
        } else {
            chest = ((DoubleChest) event.getInventory().getHolder()).getLocation().getBlock();
        }

        if (!Security.canAccess(player, chest)) {
            player.sendMessage(Messages.prefix(Messages.ACCESS_DENIED));
            event.setCancelled(true);
        }
    }
}

@@ -185,24 +185,74 @@
 p.Security;%0A
+import com.Acrobot.ChestShop.Signs.ChestShopSign;%0A
 import org.b
@@ -310,39 +310,8 @@
 te;%0A
-import org.bukkit.block.Chest;%0A
 impo
@@ -347,46 +347,8 @@
 st;%0A
-import org.bukkit.entity.HumanEntity;%0A
 impo
@@ -512,38 +512,32 @@
 port org.bukkit.
-event.
 inventory.Invent
@@ -539,20 +539,22 @@
 nventory
-Type
+Holder
 ;%0A%0A/**%0A 
@@ -715,108 +715,8 @@
 ) %7B%0A
-        if (event.getInventory().getType() != InventoryType.CHEST) %7B%0A            return;%0A        %7D%0A%0A
     
@@ -835,207 +835,194 @@
     
-HumanEntity entity = event.getPlayer();%0A%0A        if (!(entity instanceof Player) %7C%7C (!(event.getInventory().getHolder() instanceof Chest) && !(event.getInventory().getHolder() instanceof DoubleChest)
+if (!(event.getPlayer() instanceof Player)) %7B%0A            return;%0A        %7D%0A%0A        InventoryHolder holder = event.getInventory().getHolder();%0A        if (!(holder instanceof BlockState
 )) %7B
@@ -1087,21 +1087,32 @@
 layer) e
-ntity
+vent.getPlayer()
 ;%0A      
@@ -1120,20 +1120,24 @@
  Block c
-hest
+ontainer
 ;%0A%0A     
@@ -1147,40 +1147,14 @@
 if (
-event.getInventory().getH
+h
 older
-()
  ins
@@ -1161,16 +1161,22 @@
 tanceof 
+Double
 Chest) %7B
@@ -1193,185 +1193,225 @@
    c
-hest = ((BlockState) event.getInventory().getHolder()).getBlock();%0A        %7D else %7B%0A            chest = ((DoubleChest) event.getInventory().getH
+ontainer = ((DoubleChest) holder).getLocation().getBlock();%0A        %7D else %7B%0A            container = ((BlockState) h
 older
-()
 ).get
-Location().getBlock()
+Block();%0A        %7D%0A%0A        if (!ChestShopSign.isShopBlock(container)) %7B%0A            return
 ;%0A  
@@ -1460,20 +1460,24 @@
 layer, c
-hest
+ontainer
 )) %7B%0A   

Fix InventoryOpenEvent listener not applying to non chest containers