Search in sources :

Example 16 with BlockVector2

use of com.sk89q.worldedit.math.BlockVector2 in project FastAsyncWorldEdit by IntellectualSites.

the class SnapshotRestore method checkAndAddBlock.

private void checkAndAddBlock(BlockVector3 pos) {
    if (editSession.getMask() != null && !editSession.getMask().test(pos)) {
        return;
    }
    BlockVector2 chunkPos = ChunkStore.toChunk(pos);
    // Unidentified chunk
    if (!neededChunks.containsKey(chunkPos)) {
        neededChunks.put(chunkPos, new LocalBlockVectorSet());
    }
    neededChunks.get(chunkPos).add(pos);
}
Also used : LocalBlockVectorSet(com.fastasyncworldedit.core.math.LocalBlockVectorSet) BlockVector2(com.sk89q.worldedit.math.BlockVector2)

Example 17 with BlockVector2

use of com.sk89q.worldedit.math.BlockVector2 in project FastAsyncWorldEdit by IntellectualSites.

the class EditSession method regenerate.

public boolean regenerate(Region region, BiomeType biome, Long seed) {
    // TODO Optimize - avoid Vector2D creation (make mutable)
    final AbstractChangeSet fcs = (AbstractChangeSet) this.getChangeSet();
    this.setChangeSet(null);
    final FaweRegionExtent fe = this.getRegionExtent();
    final boolean cuboid = region instanceof CuboidRegion;
    if (fe != null && cuboid) {
        BlockVector3 max = region.getMaximumPoint();
        BlockVector3 min = region.getMinimumPoint();
        if (!fe.contains(max.getBlockX(), max.getBlockY(), max.getBlockZ()) && !fe.contains(min.getBlockX(), min.getBlockY(), min.getBlockZ())) {
            throw FaweCache.OUTSIDE_REGION;
        }
    }
    final Set<BlockVector2> chunks = region.getChunks();
    MutableBlockVector3 mutable = new MutableBlockVector3();
    MutableBlockVector3 mutable2 = new MutableBlockVector3();
    MutableBlockVector2 mutable2D = new MutableBlockVector2();
    for (BlockVector2 chunk : chunks) {
        final int cx = chunk.getBlockX();
        final int cz = chunk.getBlockZ();
        final int bx = cx << 4;
        final int bz = cz << 4;
        final BlockVector3 cmin = BlockVector3.at(bx, 0, bz);
        final BlockVector3 cmax = cmin.add(15, maxY, 15);
        final boolean containsBot1 = fe == null || fe.contains(cmin.getBlockX(), cmin.getBlockY(), cmin.getBlockZ());
        final boolean containsBot2 = region.contains(cmin);
        final boolean containsTop1 = fe == null || fe.contains(cmax.getBlockX(), cmax.getBlockY(), cmax.getBlockZ());
        final boolean containsTop2 = region.contains(cmax);
        if (containsBot2 && containsTop2 && !containsBot1 && !containsTop1) {
            continue;
        }
        boolean conNextX = chunks.contains(mutable2D.setComponents(cx + 1, cz));
        boolean conNextZ = chunks.contains(mutable2D.setComponents(cx, cz + 1));
        boolean containsAny = false;
        if (cuboid && containsBot1 && containsBot2 && containsTop1 && containsTop2 && conNextX && conNextZ) {
            containsAny = true;
            if (fcs != null) {
                for (int x = 0; x < 16; x++) {
                    int xx = x + bx;
                    for (int z = 0; z < 16; z++) {
                        int zz = z + bz;
                        for (int y = minY; y < maxY + 1; y++) {
                            BaseBlock block = getFullBlock(mutable.setComponents(xx, y, zz));
                            fcs.add(mutable, block, BlockTypes.AIR.getDefaultState().toBaseBlock());
                        }
                    }
                }
            }
        } else {
            if (!conNextX) {
                setExistingBlocks(mutable.setComponents(bx + 16, 0, bz), mutable2.setComponents(bx + 31, maxY, bz + 15));
            }
            if (!conNextZ) {
                setExistingBlocks(mutable.setComponents(bx, 0, bz + 16), mutable2.setComponents(bx + 15, maxY, bz + 31));
            }
            if (!chunks.contains(mutable2D.setComponents(cx + 1, cz + 1)) && !conNextX && !conNextZ) {
                setExistingBlocks(mutable.setComponents(bx + 16, 0, bz + 16), mutable2.setComponents(bx + 31, maxY, bz + 31));
            }
            for (int x = 0; x < 16; x++) {
                int xx = x + bx;
                mutable.mutX(xx);
                for (int z = 0; z < 16; z++) {
                    int zz = z + bz;
                    mutable.mutZ(zz);
                    for (int y = minY; y < maxY + 1; y++) {
                        mutable.mutY(y);
                        boolean contains = (fe == null || fe.contains(xx, y, zz)) && region.contains(mutable);
                        if (contains) {
                            containsAny = true;
                            if (fcs != null) {
                                BaseBlock block = getFullBlock(mutable);
                                fcs.add(mutable, block, BlockTypes.AIR.getDefaultState().toBaseBlock());
                            }
                        } else {
                            BaseBlock block = getFullBlock(mutable);
                            try {
                                setBlock(mutable, block);
                            } catch (MaxChangedBlocksException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    }
                }
            }
        }
        if (containsAny) {
            changes++;
            TaskManager.taskManager().sync(new RunnableVal<Object>() {

                @Override
                public void run(Object value) {
                    regenerateChunk(cx, cz, biome, seed);
                }
            });
        }
    }
    if (changes != 0) {
        flushQueue();
        return true;
    }
    return false;
}
Also used : AbstractChangeSet(com.fastasyncworldedit.core.history.changeset.AbstractChangeSet) FaweRegionExtent(com.fastasyncworldedit.core.extent.FaweRegionExtent) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) MutableBlockVector3(com.fastasyncworldedit.core.math.MutableBlockVector3) BlockVector3(com.sk89q.worldedit.math.BlockVector3) MutableBlockVector2(com.fastasyncworldedit.core.math.MutableBlockVector2) BlockVector2(com.sk89q.worldedit.math.BlockVector2) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) MutableBlockVector2(com.fastasyncworldedit.core.math.MutableBlockVector2) MutableBlockVector3(com.fastasyncworldedit.core.math.MutableBlockVector3)

Example 18 with BlockVector2

use of com.sk89q.worldedit.math.BlockVector2 in project FastAsyncWorldEdit by IntellectualSites.

the class ChunkCommands method deleteChunks.

@Command(name = "delchunks", aliases = { "/delchunks" }, desc = "Delete chunks that your selection includes")
@CommandPermissions("worldedit.delchunks")
@Logging(REGION)
public void deleteChunks(Actor actor, World world, LocalSession session, @ArgFlag(name = 'o', desc = "Only delete chunks older than the specified time.") ZonedDateTime beforeTime) throws WorldEditException {
    Path worldDir = world.getStoragePath();
    if (worldDir == null) {
        throw new StopExecutionException(TextComponent.of("Couldn't find world folder for this world."));
    }
    Path chunkPath = worldEdit.getWorkingDirectoryPath(DELCHUNKS_FILE_NAME);
    ChunkDeletionInfo currentInfo = null;
    if (Files.exists(chunkPath)) {
        try {
            currentInfo = ChunkDeleter.readInfo(chunkPath);
        } catch (IOException e) {
            throw new StopExecutionException(TextComponent.of("Error reading existing chunk file."));
        }
    }
    if (currentInfo == null) {
        currentInfo = new ChunkDeletionInfo();
        currentInfo.batches = new ArrayList<>();
    }
    ChunkDeletionInfo.ChunkBatch newBatch = new ChunkDeletionInfo.ChunkBatch();
    newBatch.worldPath = worldDir.toAbsolutePath().normalize().toString();
    newBatch.backup = true;
    final Region selection = session.getSelection(world);
    if (selection instanceof CuboidRegion) {
        newBatch.minChunk = selection.getMinimumPoint().shr(4).toBlockVector2();
        newBatch.maxChunk = selection.getMaximumPoint().shr(4).toBlockVector2();
    } else {
        // this has a possibility to OOM for very large selections still
        Set<BlockVector2> chunks = selection.getChunks();
        newBatch.chunks = new ArrayList<>(chunks);
    }
    if (beforeTime != null) {
        newBatch.deletionPredicates = new ArrayList<>();
        ChunkDeletionInfo.DeletionPredicate timePred = new ChunkDeletionInfo.DeletionPredicate();
        timePred.property = "modification";
        timePred.comparison = "<";
        timePred.value = String.valueOf((int) beforeTime.toOffsetDateTime().toEpochSecond());
        newBatch.deletionPredicates.add(timePred);
    }
    currentInfo.batches.add(newBatch);
    try {
        ChunkDeleter.writeInfo(currentInfo, chunkPath);
    } catch (IOException | JsonIOException e) {
        throw new StopExecutionException(TextComponent.of("Failed to write chunk list: " + e.getMessage()));
    }
    actor.print(TextComponent.of(String.format("%d chunk(s) have been marked for deletion the next time the server starts.", newBatch.getChunkCount())));
    if (currentInfo.batches.size() > 1) {
        actor.printDebug(TextComponent.of(String.format("%d chunks total marked for deletion. (May have overlaps).", currentInfo.batches.stream().mapToInt(ChunkDeletionInfo.ChunkBatch::getChunkCount).sum())));
    }
    actor.print(TextComponent.of("You can mark more chunks for deletion, or to stop now, run: ", TextColor.LIGHT_PURPLE).append(TextComponent.of("/stop", TextColor.AQUA).clickEvent(ClickEvent.of(ClickEvent.Action.SUGGEST_COMMAND, "/stop"))));
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) JsonIOException(com.google.gson.JsonIOException) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) BlockVector2(com.sk89q.worldedit.math.BlockVector2) ChunkDeletionInfo(com.sk89q.worldedit.internal.anvil.ChunkDeletionInfo) StopExecutionException(org.enginehub.piston.exception.StopExecutionException) JsonIOException(com.google.gson.JsonIOException) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) Region(com.sk89q.worldedit.regions.Region) Logging(com.sk89q.worldedit.command.util.Logging) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 19 with BlockVector2

use of com.sk89q.worldedit.math.BlockVector2 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 20 with BlockVector2

use of com.sk89q.worldedit.math.BlockVector2 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

BlockVector2 (com.sk89q.worldedit.math.BlockVector2)55 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)19 ArrayList (java.util.ArrayList)13 IOException (java.io.IOException)9 Map (java.util.Map)9 ProtectedPolygonalRegion (com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion)7 ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)6 LinkedHashMap (java.util.LinkedHashMap)6 GlobalProtectedRegion (com.sk89q.worldguard.protection.regions.GlobalProtectedRegion)5 Path (java.nio.file.Path)5 Iterator (java.util.Iterator)5 Set (java.util.Set)5 JsonIOException (com.google.gson.JsonIOException)4 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)4 Location (com.sk89q.worldedit.util.Location)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 List (java.util.List)4 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)3 Region (com.sk89q.worldedit.regions.Region)3