package com.elmakers.mine.bukkit.protection;

import com.elmakers.mine.bukkit.api.spell.SpellCategory;
import com.elmakers.mine.bukkit.api.spell.SpellTemplate;
import com.mewin.WGCustomFlags.WGCustomFlagsPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.association.RegionAssociable;
import com.sk89q.worldguard.protection.flags.RegionGroup;
import com.sk89q.worldguard.protection.flags.SetFlag;
import com.sk89q.worldguard.protection.flags.StringFlag;

import org.bukkit.plugin.Plugin;

import java.util.Set;

public class WGCustomFlagsManager {

    private final WGCustomFlagsPlugin customFlags;

    public static SetFlag<String> ALLOWED_SPELLS = new SetFlag<String>("allowed-spells", RegionGroup.ALL, new StringFlag(null));
    public static SetFlag<String> BLOCKED_SPELLS = new SetFlag<String>("blocked-spells", RegionGroup.ALL, new StringFlag(null));
    public static SetFlag<String> ALLOWED_SPELL_CATEGORIES = new SetFlag<String>("allowed-spell-categories", RegionGroup.ALL, new StringFlag(null));
    public static SetFlag<String> BLOCKED_SPELL_CATEGORIES = new SetFlag<String>("blocked-spell-categories", RegionGroup.ALL, new StringFlag(null));

    public WGCustomFlagsManager(Plugin wgCustomFlags) {
        customFlags = (WGCustomFlagsPlugin)wgCustomFlags;
        customFlags.addCustomFlag(ALLOWED_SPELLS);
        customFlags.addCustomFlag(BLOCKED_SPELLS);
        customFlags.addCustomFlag(ALLOWED_SPELL_CATEGORIES);
        customFlags.addCustomFlag(BLOCKED_SPELL_CATEGORIES);
    }

    public Boolean getCastPermission(RegionAssociable source, ApplicableRegionSet checkSet, SpellTemplate spell) {
        String spellKey = spell.getSpellKey().getBaseKey();
        SpellCategory category = spell.getCategory();

        Set<String> blocked = checkSet.queryValue(source, BLOCKED_SPELLS);
        if (blocked != null && blocked.contains(spellKey)) return false;
        Set<String> blockedCategories = checkSet.queryValue(source, BLOCKED_SPELL_CATEGORIES);
        if (blockedCategories != null && category != null && blockedCategories.contains(category.getKey())) return false;

        Set<String> allowed = checkSet.queryValue(source, ALLOWED_SPELLS);
        if (allowed != null && (allowed.contains("*") || allowed.contains(spellKey))) return true;

        Set<String> allowedCategories = checkSet.queryValue(source, ALLOWED_SPELL_CATEGORIES);
        if (allowedCategories != null && category != null && allowedCategories.contains(category.getKey())) return true;

        if (blocked != null && blocked.contains("*")) return false;

        return null;
    }
}

@@ -205,16 +205,68 @@
 Plugin;%0A
+import com.mewin.WGCustomFlags.flags.CustomSetFlag;%0A
 import c
@@ -317,16 +317,16 @@
 ionSet;%0A
-
 import c
@@ -448,62 +448,8 @@
 up;%0A
-import com.sk89q.worldguard.protection.flags.SetFlag;%0A
 impo
@@ -658,32 +658,38 @@
   public static 
+Custom
 SetFlag%3CString%3E 
@@ -701,32 +701,38 @@
 ED_SPELLS = new 
+Custom
 SetFlag%3CString%3E(
@@ -799,32 +799,38 @@
   public static 
+Custom
 SetFlag%3CString%3E 
@@ -842,32 +842,38 @@
 ED_SPELLS = new 
+Custom
 SetFlag%3CString%3E(
@@ -940,32 +940,38 @@
   public static 
+Custom
 SetFlag%3CString%3E 
@@ -993,32 +993,38 @@
 ATEGORIES = new 
+Custom
 SetFlag%3CString%3E(
@@ -1109,16 +1109,22 @@
  static 
+Custom
 SetFlag%3C
@@ -1162,16 +1162,22 @@
 S = new 
+Custom
 SetFlag%3C

Use CustomSetFlag as recommended on WGCustomFlags page.