Search in sources :

Example 1 with YAMLNode

use of com.sk89q.util.yaml.YAMLNode in project WorldGuard by EngineHub.

the class YamlRegionFile method loadAll.

@Override
public Set<ProtectedRegion> loadAll(FlagRegistry flagRegistry) throws StorageException {
    Map<String, ProtectedRegion> loaded = new HashMap<>();
    YAMLProcessor config = createYamlProcessor(file);
    try {
        config.load();
    } catch (FileNotFoundException e) {
        return new HashSet<>(loaded.values());
    } catch (IOException | ParserException e) {
        throw new StorageException("Failed to load region data from '" + file + "'", e);
    }
    Map<String, YAMLNode> regionData = config.getNodes("regions");
    if (regionData == null) {
        // No regions are even configured
        return Collections.emptySet();
    }
    Map<ProtectedRegion, String> parentSets = new LinkedHashMap<>();
    for (Map.Entry<String, YAMLNode> entry : regionData.entrySet()) {
        String id = entry.getKey();
        YAMLNode node = entry.getValue();
        String type = node.getString("type");
        ProtectedRegion region;
        try {
            if (type == null) {
                log.warning("Undefined region type for region '" + id + "'!\n" + "Here is what the region data looks like:\n\n" + toYamlOutput(entry.getValue().getMap()) + "\n");
                continue;
            } else if (type.equals("cuboid")) {
                Vector3 pt1 = checkNotNull(node.getVector("min"));
                Vector3 pt2 = checkNotNull(node.getVector("max"));
                BlockVector3 min = pt1.getMinimum(pt2).toBlockPoint();
                BlockVector3 max = pt1.getMaximum(pt2).toBlockPoint();
                region = new ProtectedCuboidRegion(id, min, max);
            } else if (type.equals("poly2d")) {
                Integer minY = checkNotNull(node.getInt("min-y"));
                Integer maxY = checkNotNull(node.getInt("max-y"));
                List<BlockVector2> points = node.getBlockVector2List("points", null);
                region = new ProtectedPolygonalRegion(id, points, minY, maxY);
            } else if (type.equals("global")) {
                region = new GlobalProtectedRegion(id);
            } else {
                log.warning("Unknown region type for region '" + id + "'!\n" + "Here is what the region data looks like:\n\n" + toYamlOutput(entry.getValue().getMap()) + "\n");
                continue;
            }
            Integer priority = checkNotNull(node.getInt("priority"));
            region.setPriority(priority);
            setFlags(flagRegistry, region, node.getNode("flags"));
            region.setOwners(parseDomain(node.getNode("owners")));
            region.setMembers(parseDomain(node.getNode("members")));
            loaded.put(id, region);
            String parentId = node.getString("parent");
            if (parentId != null) {
                parentSets.put(region, parentId);
            }
        } catch (NullPointerException e) {
            log.log(Level.WARNING, "Unexpected NullPointerException encountered during parsing for the region '" + id + "'!\n" + "Here is what the region data looks like:\n\n" + toYamlOutput(entry.getValue().getMap()) + "\n\nNote: This region will disappear as a result!", e);
        }
    }
    // Relink parents
    RegionDatabaseUtils.relinkParents(loaded, parentSets);
    return new HashSet<>(loaded.values());
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FileNotFoundException(java.io.FileNotFoundException) LinkedHashMap(java.util.LinkedHashMap) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) GlobalProtectedRegion(com.sk89q.worldguard.protection.regions.GlobalProtectedRegion) ProtectedPolygonalRegion(com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion) YAMLProcessor(com.sk89q.util.yaml.YAMLProcessor) HashSet(java.util.HashSet) ParserException(org.yaml.snakeyaml.parser.ParserException) BlockVector3(com.sk89q.worldedit.math.BlockVector3) Vector3(com.sk89q.worldedit.math.Vector3) IOException(java.io.IOException) BlockVector3(com.sk89q.worldedit.math.BlockVector3) BlockVector2(com.sk89q.worldedit.math.BlockVector2) GlobalProtectedRegion(com.sk89q.worldguard.protection.regions.GlobalProtectedRegion) ProtectedCuboidRegion(com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion) StorageException(com.sk89q.worldguard.protection.managers.storage.StorageException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) YAMLNode(com.sk89q.util.yaml.YAMLNode)

Example 2 with YAMLNode

use of com.sk89q.util.yaml.YAMLNode in project WorldGuard by EngineHub.

the class YamlRegionFile method saveAll.

@Override
public void saveAll(Set<ProtectedRegion> regions) throws StorageException {
    checkNotNull(regions);
    File tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
    YAMLProcessor config = createYamlProcessor(tempFile);
    config.clear();
    YAMLNode regionsNode = config.addNode("regions");
    Map<String, Object> map = regionsNode.getMap();
    for (ProtectedRegion region : regions) {
        Map<String, Object> nodeMap = new HashMap<>();
        map.put(region.getId(), nodeMap);
        YAMLNode node = new YAMLNode(nodeMap, false);
        if (region instanceof ProtectedCuboidRegion) {
            ProtectedCuboidRegion cuboid = (ProtectedCuboidRegion) region;
            node.setProperty("type", "cuboid");
            node.setProperty("min", cuboid.getMinimumPoint());
            node.setProperty("max", cuboid.getMaximumPoint());
        } else if (region instanceof ProtectedPolygonalRegion) {
            ProtectedPolygonalRegion poly = (ProtectedPolygonalRegion) region;
            node.setProperty("type", "poly2d");
            node.setProperty("min-y", poly.getMinimumPoint().getBlockY());
            node.setProperty("max-y", poly.getMaximumPoint().getBlockY());
            List<Map<String, Object>> points = new ArrayList<>();
            for (BlockVector2 point : poly.getPoints()) {
                Map<String, Object> data = new HashMap<>();
                data.put("x", point.getBlockX());
                data.put("z", point.getBlockZ());
                points.add(data);
            }
            node.setProperty("points", points);
        } else if (region instanceof GlobalProtectedRegion) {
            node.setProperty("type", "global");
        } else {
            node.setProperty("type", region.getClass().getCanonicalName());
        }
        node.setProperty("priority", region.getPriority());
        node.setProperty("flags", getFlagData(region));
        node.setProperty("owners", getDomainData(region.getOwners()));
        node.setProperty("members", getDomainData(region.getMembers()));
        ProtectedRegion parent = region.getParent();
        if (parent != null) {
            node.setProperty("parent", parent.getId());
        }
    }
    config.setHeader(FILE_HEADER);
    config.save();
    // noinspection ResultOfMethodCallIgnored
    file.delete();
    if (!tempFile.renameTo(file)) {
        throw new StorageException("Failed to rename temporary regions file to " + file.getAbsolutePath());
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) BlockVector2(com.sk89q.worldedit.math.BlockVector2) GlobalProtectedRegion(com.sk89q.worldguard.protection.regions.GlobalProtectedRegion) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) GlobalProtectedRegion(com.sk89q.worldguard.protection.regions.GlobalProtectedRegion) ProtectedPolygonalRegion(com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion) YAMLProcessor(com.sk89q.util.yaml.YAMLProcessor) ProtectedCuboidRegion(com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) StorageException(com.sk89q.worldguard.protection.managers.storage.StorageException) YAMLNode(com.sk89q.util.yaml.YAMLNode)

Aggregations

YAMLNode (com.sk89q.util.yaml.YAMLNode)2 YAMLProcessor (com.sk89q.util.yaml.YAMLProcessor)2 BlockVector2 (com.sk89q.worldedit.math.BlockVector2)2 StorageException (com.sk89q.worldguard.protection.managers.storage.StorageException)2 GlobalProtectedRegion (com.sk89q.worldguard.protection.regions.GlobalProtectedRegion)2 ProtectedCuboidRegion (com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion)2 ProtectedPolygonalRegion (com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion)2 ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)1 Vector3 (com.sk89q.worldedit.math.Vector3)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ParserException (org.yaml.snakeyaml.parser.ParserException)1