package tk.allele.protection.methods;

import com.griefcraft.lwc.LWC;
import com.griefcraft.lwc.LWCPlugin;
import com.griefcraft.model.Protection;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import tk.allele.protection.ProtectionMethod;

import java.util.List;

/**
 * Support for LWC by Hidendra.
 *
 * @see <a href="http://forums.bukkit.org/threads/967/">LWC</a>
 */
public class LWCMethod implements ProtectionMethod {
    private LWC lwc;

    public LWCMethod(Plugin plugin) {
        Plugin lwcPlugin = plugin.getServer().getPluginManager().getPlugin("LWC");
        if(lwcPlugin != null && lwcPlugin instanceof LWCPlugin) {
            lwc = ((LWCPlugin) lwcPlugin).getLWC();
        }
    }

    @Override
    public boolean isEnabled() {
        return (lwc != null);
    }

    @Override
    public String toString() {
        return "LWC";
    }

    @Override
    public boolean canAccess(String playerName, Block block) {
        // Use getProtectionSet() to grab both parts of a double chest
        List<Block> blocksProtected = lwc.getProtectionSet(block.getWorld(), block.getX(), block.getY(), block.getZ());
        for(Block chestBlock : blocksProtected) {
            Protection protection = lwc.findProtection(chestBlock);
            // Check if the player is the owner
            if(protection != null) {
                return protection.getOwner().equalsIgnoreCase(playerName);
            }
        }
        // If none of the blocks are protected, anyone can access it
        return true;
    }

    @Override
    public boolean canAccess(Player player, Block block) {
        return canAccess(player.getName(), block);
    }
}


@@ -128,26 +128,112 @@
 t.model.
-Protection
+AccessRight;%0Aimport com.griefcraft.model.Protection;%0Aimport com.griefcraft.model.ProtectionTypes
 ;%0Aimport
@@ -982,32 +982,861 @@
 n %22LWC%22;%0A    %7D%0A%0A
+    // Ripped off %3Chttps://github.com/Hidendra/LWC/blob/master/src/main/java/com/griefcraft/lwc/LWC.java#L340%3E%0A    private boolean canAccessProtection(String playerName, Protection protection) %7B%0A        switch(protection.getType()) %7B%0A            case ProtectionTypes.PUBLIC:%0A                return true;%0A%0A            case ProtectionTypes.PASSWORD:%0A                return lwc.getMemoryDatabase().hasAccess(playerName, protection);%0A%0A            case ProtectionTypes.PRIVATE:%0A                if(playerName.equalsIgnoreCase(protection.getOwner())) %7B%0A                    return true;%0A                %7D%0A%0A                if(protection.getAccess(AccessRight.PLAYER, playerName) %3E= 0) %7B%0A                    return true;%0A                %7D%0A%0A                return false;%0A%0A            default:%0A                return false;%0A        %7D%0A    %7D%0A%0A
     @Override%0A  
@@ -2221,59 +2221,66 @@
     
-// Check if the player is the owner%0A            if(
+if(protection != null && !canAccessProtection(playerName, 
 prot
@@ -2285,24 +2285,17 @@
 otection
- != null
+)
 ) %7B%0A    
@@ -2317,58 +2317,13 @@
 urn 
-protection.getOwner().equalsIgnoreCase(playerName)
+false
 ;%0A  

Check LWC protections more thoroughly