Search in sources :

Example 1 with RegionGroupFlag

use of com.sk89q.worldguard.protection.flags.RegionGroupFlag in project AreaShop by NLthijs48.

the class WorldGuardRegionFlagsFeature method updateRegionFlags.

/**
 * Set the region flags/options to the values of a ConfigurationSection.
 * @param region The region to update the flags for
 * @param flags  The flags to apply
 * @return true if the flags have been set correctly, otherwise false
 */
private boolean updateRegionFlags(GeneralRegion region, ConfigurationSection flags) {
    boolean result = true;
    Set<String> flagNames = flags.getKeys(false);
    WorldGuardPlugin worldGuard = plugin.getWorldGuard();
    // Get the region
    ProtectedRegion worldguardRegion = region.getRegion();
    if (worldguardRegion == null) {
        AreaShop.debug("Region '" + region.getName() + "' does not exist, setting flags failed");
        return false;
    }
    // Loop through all flags that are set in the config
    for (String flagName : flagNames) {
        String value = Message.fromString(flags.getString(flagName)).replacements(region).getPlain();
        // In the config normal Bukkit color codes are used, those only need to be translated on 5.X WorldGuard versions
        if (plugin.getWorldGuard().getDescription().getVersion().startsWith("5.")) {
            value = translateBukkitToWorldGuardColors(value);
        }
        if (flagName.equalsIgnoreCase("members")) {
            plugin.getWorldGuardHandler().setMembers(worldguardRegion, parseAccessSet(value));
        // AreaShop.debug("  Flag " + flagName + " set: " + members.toUserFriendlyString());
        } else if (flagName.equalsIgnoreCase("owners")) {
            plugin.getWorldGuardHandler().setOwners(worldguardRegion, parseAccessSet(value));
        // AreaShop.debug("  Flag " + flagName + " set: " + owners.toUserFriendlyString());
        } else if (flagName.equalsIgnoreCase("priority")) {
            try {
                int priority = Integer.parseInt(value);
                if (worldguardRegion.getPriority() != priority) {
                    worldguardRegion.setPriority(priority);
                }
            // AreaShop.debug("  Flag " + flagName + " set: " + value);
            } catch (NumberFormatException e) {
                AreaShop.warn("The value of flag " + flagName + " is not a number");
                result = false;
            }
        } else if (flagName.equalsIgnoreCase("parent")) {
            if (region.getWorld() == null || worldGuard.getRegionManager(region.getWorld()) == null) {
                continue;
            }
            ProtectedRegion parentRegion = worldGuard.getRegionManager(region.getWorld()).getRegion(value);
            if (parentRegion != null) {
                if (!parentRegion.equals(worldguardRegion.getParent())) {
                    try {
                        worldguardRegion.setParent(parentRegion);
                    // AreaShop.debug("  Flag " + flagName + " set: " + value);
                    } catch (ProtectedRegion.CircularInheritanceException e) {
                        AreaShop.warn("The parent set in the config is not correct (circular inheritance)");
                    }
                }
            } else {
                AreaShop.warn("The parent set in the config is not correct (region does not exist)");
            }
        } else {
            // Parse all other normal flags (groups are also handled)
            String flagSetting = null;
            com.sk89q.worldguard.protection.flags.RegionGroup groupValue = null;
            Flag<?> foundFlag = plugin.getWorldGuardHandler().fuzzyMatchFlag(flagName);
            if (foundFlag == null) {
                AreaShop.warn("Found wrong flag in flagProfiles section: " + flagName + ", check if that is the correct WorldGuard flag");
                continue;
            }
            RegionGroupFlag groupFlag = foundFlag.getRegionGroupFlag();
            if (value == null || value.isEmpty()) {
                if (worldguardRegion.getFlag(foundFlag) != null) {
                    worldguardRegion.setFlag(foundFlag, null);
                }
                if (groupFlag != null && worldguardRegion.getFlag(groupFlag) != null) {
                    worldguardRegion.setFlag(groupFlag, null);
                }
            // AreaShop.debug("  Flag " + flagName + " reset (+ possible group of flag)");
            } else {
                if (groupFlag == null) {
                    flagSetting = value;
                } else {
                    for (String part : value.split(" ")) {
                        if (part.startsWith("g:")) {
                            if (part.length() > 2) {
                                try {
                                    groupValue = plugin.getWorldGuardHandler().parseFlagGroupInput(groupFlag, part.substring(2));
                                } catch (InvalidFlagFormat e) {
                                    AreaShop.warn("Found wrong group value for flag " + flagName);
                                }
                            }
                        } else {
                            if (flagSetting == null) {
                                flagSetting = part;
                            } else {
                                flagSetting += " " + part;
                            }
                        }
                    }
                }
                if (flagSetting != null) {
                    try {
                        setFlag(worldguardRegion, foundFlag, flagSetting);
                    // AreaShop.debug("  Flag " + flagName + " set: " + flagSetting);
                    } catch (InvalidFlagFormat e) {
                        AreaShop.warn("Found wrong value for flag " + flagName);
                    }
                }
                if (groupValue != null) {
                    if (groupValue == groupFlag.getDefault()) {
                        worldguardRegion.setFlag(groupFlag, null);
                    // AreaShop.debug("    Group of flag " + flagName + " set to default: " + groupValue);
                    } else {
                        worldguardRegion.setFlag(groupFlag, groupValue);
                    // AreaShop.debug("    Group of flag " + flagName + " set: " + groupValue);
                    }
                }
            }
        }
    }
    // Indicate that the regions needs to be saved
    // TODO do we still need this? maybe only for old WorldGuard?
    plugin.getFileManager().saveIsRequiredForRegionWorld(region.getWorldName());
    return result;
}
Also used : RegionGroupFlag(com.sk89q.worldguard.protection.flags.RegionGroupFlag) Flag(com.sk89q.worldguard.protection.flags.Flag) RegionGroupFlag(com.sk89q.worldguard.protection.flags.RegionGroupFlag) WorldGuardPlugin(com.sk89q.worldguard.bukkit.WorldGuardPlugin) InvalidFlagFormat(com.sk89q.worldguard.protection.flags.InvalidFlagFormat) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion)

Aggregations

WorldGuardPlugin (com.sk89q.worldguard.bukkit.WorldGuardPlugin)1 Flag (com.sk89q.worldguard.protection.flags.Flag)1 InvalidFlagFormat (com.sk89q.worldguard.protection.flags.InvalidFlagFormat)1 RegionGroupFlag (com.sk89q.worldguard.protection.flags.RegionGroupFlag)1 ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)1