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();
}
}
}
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());
}
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());
}
}
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);
}
}
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;
}
}
}
}
Aggregations