Search in sources :

Example 1 with StorageException

use of com.sk89q.worldguard.protection.managers.storage.StorageException in project ProtectionStones by espidev.

the class LegacyUpgrade method fixBase64HeadRegions.

// for one day when we switch to proper base64 generation (no hashcode, use nameuuidfrombytes)
// problem is, currently I don't know how to convert all items to use this uuid
public static void fixBase64HeadRegions() {
    HashMap<String, String> oldToNew = new HashMap<>();
    for (PSProtectBlock b : ProtectionStones.getInstance().getConfiguredBlocks()) {
        if (b.type.startsWith("PLAYER_HEAD:") && b.type.split(":").length > 1) {
            String base64 = b.type.split(":")[1];
            oldToNew.put(new UUID(base64.hashCode(), base64.hashCode()).toString(), UUID.nameUUIDFromBytes(base64.getBytes()).toString());
        }
    }
    for (World world : Bukkit.getWorlds()) {
        RegionManager rm = WGUtils.getRegionManagerWithWorld(world);
        for (ProtectedRegion r : rm.getRegions().values()) {
            if (ProtectionStones.isPSRegion(r)) {
                PSRegion psr = PSRegion.fromWGRegion(world, r);
                if (psr instanceof PSGroupRegion) {
                    PSGroupRegion psgr = (PSGroupRegion) psr;
                    for (PSMergedRegion psmr : psgr.getMergedRegions()) {
                        String type = psmr.getType();
                        if (oldToNew.containsKey(type)) {
                            Set<String> flag = psmr.getGroupRegion().getWGRegion().getFlag(FlagHandler.PS_MERGED_REGIONS_TYPES);
                            String original = null;
                            for (String s : flag) {
                                String[] spl = s.split(" ");
                                String id = spl[0];
                                if (id.equals(psmr.getId())) {
                                    original = s;
                                    break;
                                }
                            }
                            if (original != null) {
                                flag.remove(original);
                                flag.add(psmr.getId() + " " + oldToNew.get(type));
                            }
                        }
                    }
                }
                if (oldToNew.containsKey(psr.getType())) {
                    psr.getWGRegion().setFlag(FlagHandler.PS_BLOCK_MATERIAL, oldToNew.get(psr.getType()));
                }
            }
        }
        try {
            rm.save();
        } catch (StorageException e) {
            e.printStackTrace();
        }
    }
}
Also used : World(org.bukkit.World) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) RegionManager(com.sk89q.worldguard.protection.managers.RegionManager) StorageException(com.sk89q.worldguard.protection.managers.storage.StorageException)

Example 2 with StorageException

use of com.sk89q.worldguard.protection.managers.storage.StorageException 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 3 with StorageException

use of com.sk89q.worldguard.protection.managers.storage.StorageException 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)

Example 4 with StorageException

use of com.sk89q.worldguard.protection.managers.storage.StorageException in project WorldGuard by EngineHub.

the class DriverMigration method write.

private void write(String name, Set<ProtectedRegion> regions) throws MigrationException {
    log.info("Saving the data for '" + name + "' with the new driver...");
    RegionDatabase store = target.get(name);
    try {
        store.saveAll(regions);
    } catch (StorageException e) {
        throw new MigrationException("Failed to save region data for '" + store.getName() + "' to the new driver", e);
    }
}
Also used : RegionDatabase(com.sk89q.worldguard.protection.managers.storage.RegionDatabase) StorageException(com.sk89q.worldguard.protection.managers.storage.StorageException)

Example 5 with StorageException

use of com.sk89q.worldguard.protection.managers.storage.StorageException in project WorldGuard by EngineHub.

the class RegionContainerImpl method load.

/**
 * Load the {@code RegionManager} for the world with the given name,
 * creating a new instance for the world if one does not exist yet.
 *
 * @param name the name of the world
 * @return a region manager, or {@code null} if loading failed
 */
@Nullable
public RegionManager load(String name) {
    checkNotNull(name);
    Normal normal = Normal.normal(name);
    synchronized (lock) {
        RegionManager manager = mapping.get(normal);
        if (manager != null) {
            return manager;
        } else {
            try {
                manager = createAndLoad(name);
                mapping.put(normal, manager);
                failingLoads.remove(normal);
                return manager;
            } catch (StorageException e) {
                log.log(Level.WARNING, "Failed to load the region data for '" + name + "' (periodic attempts will be made to load the data until success)", e);
                failingLoads.add(normal);
                return null;
            }
        }
    }
}
Also used : Normal(com.sk89q.worldguard.util.Normal) StorageException(com.sk89q.worldguard.protection.managers.storage.StorageException) Nullable(javax.annotation.Nullable)

Aggregations

StorageException (com.sk89q.worldguard.protection.managers.storage.StorageException)31 RegionManager (com.sk89q.worldguard.protection.managers.RegionManager)19 BukkitWorld (com.sk89q.worldedit.bukkit.BukkitWorld)16 ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)15 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)9 DefaultDomain (com.sk89q.worldguard.domains.DefaultDomain)9 ProtectedCuboidRegion (com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion)9 Flag (com.sk89q.worldguard.protection.flags.Flag)6 StateFlag (com.sk89q.worldguard.protection.flags.StateFlag)6 Closer (com.sk89q.worldguard.util.io.Closer)5 SQLException (java.sql.SQLException)5 GlobalProtectedRegion (com.sk89q.worldguard.protection.regions.GlobalProtectedRegion)3 ProtectedPolygonalRegion (com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion)3 HashMap (java.util.HashMap)3 World (org.bukkit.World)3 YAMLNode (com.sk89q.util.yaml.YAMLNode)2 YAMLProcessor (com.sk89q.util.yaml.YAMLProcessor)2 BlockVector2 (com.sk89q.worldedit.math.BlockVector2)2 ApplicableRegionSet (com.sk89q.worldguard.protection.ApplicableRegionSet)2 RegionDatabase (com.sk89q.worldguard.protection.managers.storage.RegionDatabase)2