Search in sources :

Example 1 with RegionVisitor

use of com.sk89q.worldedit.function.visitor.RegionVisitor in project FastAsyncWorldEdit by IntellectualSites.

the class GenerationCommands method image.

@Command(name = "/img", aliases = { "/image", "image" }, desc = "Generate an image")
@CommandPermissions("worldedit.generation.image")
@Logging(PLACEMENT)
public void image(Actor actor, LocalSession session, EditSession editSession, @Arg(desc = "Image URL (imgur only)") String imageURL, @Arg(desc = "boolean", def = "true") boolean randomize, @Arg(desc = "TODO", def = "100") int threshold, @Arg(desc = "BlockVector2", def = "") BlockVector2 dimensions) throws WorldEditException, IOException {
    TextureUtil tu = Fawe.instance().getCachedTextureUtil(randomize, 0, threshold);
    URL url = new URL(imageURL);
    if (!url.getHost().equalsIgnoreCase("i.imgur.com")) {
        throw new IOException("Only i.imgur.com links are allowed!");
    }
    BufferedImage image = MainUtil.readImage(url);
    if (dimensions != null) {
        image = ImageUtil.getScaledInstance(image, dimensions.getBlockX(), dimensions.getBlockZ(), RenderingHints.VALUE_INTERPOLATION_BILINEAR, false);
    }
    BlockVector3 pos1 = session.getPlacementPosition(actor);
    BlockVector3 pos2 = pos1.add(image.getWidth() - 1, 0, image.getHeight() - 1);
    CuboidRegion region = new CuboidRegion(pos1, pos2);
    int[] count = new int[1];
    final BufferedImage finalImage = image;
    RegionVisitor visitor = new RegionVisitor(region, pos -> {
        int x = pos.getBlockX() - pos1.getBlockX();
        int z = pos.getBlockZ() - pos1.getBlockZ();
        int color = finalImage.getRGB(x, z);
        BlockType block = tu.getNearestBlock(color);
        count[0]++;
        if (block != null) {
            return editSession.setBlock(pos, block.getDefaultState());
        }
        return false;
    }, editSession);
    Operations.completeBlindly(visitor);
    actor.print(Caption.of("fawe.worldedit.visitor.visitor.block", editSession.getBlockChangeCount()));
}
Also used : RegionVisitor(com.sk89q.worldedit.function.visitor.RegionVisitor) TextureUtil(com.fastasyncworldedit.core.util.TextureUtil) BlockType(com.sk89q.worldedit.world.block.BlockType) IOException(java.io.IOException) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) BlockVector3(com.sk89q.worldedit.math.BlockVector3) URL(java.net.URL) BufferedImage(java.awt.image.BufferedImage) Logging(com.sk89q.worldedit.command.util.Logging) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 2 with RegionVisitor

use of com.sk89q.worldedit.function.visitor.RegionVisitor in project FastAsyncWorldEdit by IntellectualSites.

the class SelectionCommands method distr.

@Command(name = "/distr", desc = "Get the distribution of blocks in the selection")
@CommandPermissions("worldedit.analysis.distr")
public void distr(Actor actor, World world, LocalSession session, // FAWE start > add extent to RegionVisitor to allow chunk preloading
EditSession editSession, // FAWE end
@Switch(name = 'c', desc = "Get the distribution of the clipboard instead") boolean clipboardDistr, @Switch(name = 'd', desc = "Separate blocks by state") boolean separateStates, @ArgFlag(name = 'p', desc = "Gets page from a previous distribution.") Integer page) throws WorldEditException {
    List<Countable<BlockState>> distribution;
    if (page == null) {
        if (clipboardDistr) {
            // throws if missing
            Clipboard clipboard = session.getClipboard().getClipboard();
            BlockDistributionCounter count = new BlockDistributionCounter(clipboard, separateStates);
            // FAWE start > add extent to RegionVisitor to allow chunk preloading
            RegionVisitor visitor = new RegionVisitor(clipboard.getRegion(), count, editSession);
            // FAWE end
            Operations.completeBlindly(visitor);
            distribution = count.getDistribution();
        } else {
            distribution = editSession.getBlockDistribution(session.getSelection(world), separateStates);
        }
        session.setLastDistribution(distribution);
        page = 1;
    } else {
        distribution = session.getLastDistribution();
        if (distribution == null) {
            actor.print(Caption.of("worldedit.distr.no-previous"));
            return;
        }
    }
    if (distribution.isEmpty()) {
        // *Should* always be false
        actor.print(Caption.of("worldedit.distr.no-blocks"));
        return;
    }
    BlockDistributionResult res = new BlockDistributionResult(distribution, separateStates);
    if (!actor.isPlayer()) {
        res.formatForConsole();
    }
    actor.print(res.create(page));
}
Also used : Countable(com.sk89q.worldedit.util.Countable) RegionVisitor(com.sk89q.worldedit.function.visitor.RegionVisitor) BlockDistributionCounter(com.sk89q.worldedit.function.block.BlockDistributionCounter) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 3 with RegionVisitor

use of com.sk89q.worldedit.function.visitor.RegionVisitor in project FastAsyncWorldEdit by IntellectualSites.

the class Extent method countBlocks.

/**
 * Count the number of blocks of a list of types in a region.
 *
 * @param region     the region
 * @param searchMask mask to match
 * @return the number of blocks that matched the mask
 */
default int countBlocks(Region region, Mask searchMask) {
    // FAWE start > use slightly more performant RegionVisitor
    RegionVisitor visitor = new RegionVisitor(region, searchMask::test, this);
    // FAWE end
    Operations.completeBlindly(visitor);
    return visitor.getAffected();
}
Also used : RegionVisitor(com.sk89q.worldedit.function.visitor.RegionVisitor)

Example 4 with RegionVisitor

use of com.sk89q.worldedit.function.visitor.RegionVisitor in project FastAsyncWorldEdit by IntellectualSites.

the class ForwardExtentCopy method resume.

@Override
public Operation resume(RunContext run) throws WorldEditException {
    // FAWE start
    if (currentTransform == null) {
        currentTransform = transform;
    }
    // FAWE end
    if (lastBiomeVisitor != null) {
        affectedBiomeCols += lastBiomeVisitor.getAffected();
        lastBiomeVisitor = null;
    }
    if (lastEntityVisitor != null) {
        affectedEntities += lastEntityVisitor.getAffected();
        lastEntityVisitor = null;
    }
    // FAWE start
    Extent finalDest = destination;
    BlockVector3 translation = to.subtract(from);
    if (!translation.equals(BlockVector3.ZERO)) {
        finalDest = new BlockTranslateExtent(finalDest, translation.getBlockX(), translation.getBlockY(), translation.getBlockZ());
    }
    // FAWE end
    // FAWE start - RegionVisitor > ExtentBlockCopy
    RegionFunction copy;
    RegionVisitor blockCopy = null;
    PositionTransformExtent transExt = null;
    if (!currentTransform.isIdentity()) {
        if (!(currentTransform instanceof AffineTransform) || ((AffineTransform) currentTransform).isOffAxis()) {
            transExt = new PositionTransformExtent(source, currentTransform.inverse());
            transExt.setOrigin(from);
            copy = new SimpleBlockCopy(transExt, finalDest);
            if (this.filterFunction != null) {
                copy = new IntersectRegionFunction(filterFunction, copy);
            }
            if (sourceFunction != null) {
                copy = CombinedRegionFunction.combine(copy, sourceFunction);
            }
            if (sourceMask != Masks.alwaysTrue()) {
                new MaskTraverser(sourceMask).reset(transExt);
                copy = new RegionMaskingFilter(source, sourceMask, copy);
            }
            if (copyingBiomes && (source.isWorld() || region instanceof FlatRegion)) {
                copy = CombinedRegionFunction.combine(copy, new BiomeCopy(source, finalDest));
            }
            blockCopy = new BackwardsExtentBlockCopy(region, from, transform, copy);
        } else {
            transExt = new PositionTransformExtent(finalDest, currentTransform);
            transExt.setOrigin(from);
            finalDest = transExt;
        }
    }
    if (blockCopy == null) {
        RegionFunction maskFunc = null;
        if (sourceFunction != null) {
            BlockVector3 disAbs = translation.abs();
            BlockVector3 size = region.getMaximumPoint().subtract(region.getMinimumPoint()).add(1, 1, 1);
            boolean overlap = (disAbs.getBlockX() < size.getBlockX() && disAbs.getBlockY() < size.getBlockY() && disAbs.getBlockZ() < size.getBlockZ());
            RegionFunction copySrcFunc = sourceFunction;
            if (overlap && translation.length() != 0) {
                int x = translation.getBlockX();
                int y = translation.getBlockY();
                int z = translation.getBlockZ();
                maskFunc = position -> {
                    BlockVector3 bv = BlockVector3.at(position.getBlockX() + x, position.getBlockY() + y, position.getBlockZ() + z);
                    if (region.contains(bv)) {
                        return sourceFunction.apply(bv);
                    }
                    return false;
                };
                copySrcFunc = position -> {
                    BlockVector3 bv = BlockVector3.at(position.getBlockX() - x, position.getBlockY() - y, position.getBlockZ() - z);
                    if (!region.contains(bv)) {
                        return sourceFunction.apply(position);
                    }
                    return false;
                };
            }
            copy = new CombinedBlockCopy(source, finalDest, copySrcFunc);
        } else {
            copy = new SimpleBlockCopy(source, finalDest);
        }
        if (this.filterFunction != null) {
            copy = new IntersectRegionFunction(filterFunction, copy);
        }
        if (sourceMask != Masks.alwaysTrue()) {
            if (maskFunc != null) {
                copy = new RegionMaskTestFunction(sourceMask, copy, maskFunc);
            } else {
                copy = new RegionMaskingFilter(source, sourceMask, copy);
            }
        }
        if (copyingBiomes && (source.isWorld() || region instanceof FlatRegion)) {
            copy = CombinedRegionFunction.combine(copy, new BiomeCopy(source, finalDest));
        }
        ExtentTraverser<ParallelQueueExtent> queueTraverser = new ExtentTraverser<>(finalDest).find(ParallelQueueExtent.class);
        Extent preloader = queueTraverser != null ? queueTraverser.get() : source;
        blockCopy = new RegionVisitor(region, copy, preloader);
    }
    List<? extends Entity> entities;
    if (copyingEntities) {
        // filter players since they can't be copied
        entities = source.getEntities(region);
        entities.removeIf(entity -> {
            EntityProperties properties = entity.getFacet(EntityProperties.class);
            return properties != null && !properties.isPasteable();
        });
    } else {
        entities = Collections.emptyList();
    }
    for (int i = 0; i < repetitions; i++) {
        Operations.completeBlindly(blockCopy);
        if (!entities.isEmpty()) {
            ExtentEntityCopy entityCopy = new ExtentEntityCopy(source, from.toVector3(), destination, to.toVector3(), currentTransform);
            entityCopy.setRemoving(removingEntities);
            List<? extends Entity> entities2 = Lists.newArrayList(source.getEntities(region));
            entities2.removeIf(entity -> {
                EntityProperties properties = entity.getFacet(EntityProperties.class);
                return properties != null && !properties.isPasteable();
            });
            EntityVisitor entityVisitor = new EntityVisitor(entities.iterator(), entityCopy);
            Operations.completeBlindly(entityVisitor);
            affectedEntities += entityVisitor.getAffected();
        }
        if (transExt != null) {
            currentTransform = currentTransform.combine(transform);
            transExt.setTransform(currentTransform);
        }
    }
    affectedBlocks += blockCopy.getAffected();
    // FAWE end
    return null;
}
Also used : PositionTransformExtent(com.fastasyncworldedit.core.extent.PositionTransformExtent) ParallelQueueExtent(com.fastasyncworldedit.core.queue.implementation.ParallelQueueExtent) BlockTranslateExtent(com.fastasyncworldedit.core.extent.BlockTranslateExtent) Extent(com.sk89q.worldedit.extent.Extent) RegionMaskTestFunction(com.fastasyncworldedit.core.function.RegionMaskTestFunction) EntityVisitor(com.sk89q.worldedit.function.visitor.EntityVisitor) SimpleBlockCopy(com.fastasyncworldedit.core.function.block.SimpleBlockCopy) CombinedBlockCopy(com.fastasyncworldedit.core.function.block.CombinedBlockCopy) RegionVisitor(com.sk89q.worldedit.function.visitor.RegionVisitor) ExtentEntityCopy(com.sk89q.worldedit.function.entity.ExtentEntityCopy) BiomeCopy(com.fastasyncworldedit.core.function.block.BiomeCopy) PositionTransformExtent(com.fastasyncworldedit.core.extent.PositionTransformExtent) EntityProperties(com.sk89q.worldedit.entity.metadata.EntityProperties) ParallelQueueExtent(com.fastasyncworldedit.core.queue.implementation.ParallelQueueExtent) RegionMaskingFilter(com.sk89q.worldedit.function.RegionMaskingFilter) RegionFunction(com.sk89q.worldedit.function.RegionFunction) CombinedRegionFunction(com.sk89q.worldedit.function.CombinedRegionFunction) IntersectRegionFunction(com.fastasyncworldedit.core.function.visitor.IntersectRegionFunction) FlatRegion(com.sk89q.worldedit.regions.FlatRegion) MaskTraverser(com.fastasyncworldedit.core.util.MaskTraverser) BlockVector3(com.sk89q.worldedit.math.BlockVector3) AffineTransform(com.sk89q.worldedit.math.transform.AffineTransform) IntersectRegionFunction(com.fastasyncworldedit.core.function.visitor.IntersectRegionFunction) BlockTranslateExtent(com.fastasyncworldedit.core.extent.BlockTranslateExtent)

Example 5 with RegionVisitor

use of com.sk89q.worldedit.function.visitor.RegionVisitor in project FastAsyncWorldEdit by IntellectualSites.

the class BiomeCommands method setBiome.

@Command(name = "/setbiome", desc = "Sets the biome of your current block or region.", descFooter = "By default, uses all the blocks in your selection")
@Logging(REGION)
@Preload(Preload.PreloadCheck.PRELOAD)
@Confirm(Confirm.Processor.REGION)
@CommandPermissions("worldedit.biome.set")
public void setBiome(Actor actor, World world, LocalSession session, EditSession editSession, @Arg(desc = "Biome type.") BiomeType target, @Switch(name = 'p', desc = "Use your current position") boolean atPosition) throws WorldEditException {
    Region region;
    Mask mask = editSession.getMask();
    if (atPosition) {
        if (actor instanceof Locatable) {
            final BlockVector3 pos = ((Locatable) actor).getLocation().toVector().toBlockPoint();
            region = new CuboidRegion(pos, pos);
        } else {
            actor.print(Caption.of("worldedit.setbiome.not-locatable"));
            return;
        }
    } else {
        region = session.getSelection(world);
    }
    RegionFunction replace = new BiomeReplace(editSession, target);
    if (mask != null) {
        replace = new RegionMaskingFilter(editSession, mask, replace);
    }
    RegionVisitor visitor = new RegionVisitor(region, replace);
    Operations.completeLegacy(visitor);
    actor.print(Caption.of("worldedit.setbiome.changed", TextComponent.of(visitor.getAffected() / (editSession.getMaxY() - editSession.getMinY()))));
}
Also used : RegionFunction(com.sk89q.worldedit.function.RegionFunction) RegionVisitor(com.sk89q.worldedit.function.visitor.RegionVisitor) BiomeReplace(com.sk89q.worldedit.function.biome.BiomeReplace) Mask(com.sk89q.worldedit.function.mask.Mask) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) Region(com.sk89q.worldedit.regions.Region) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) BlockVector3(com.sk89q.worldedit.math.BlockVector3) RegionMaskingFilter(com.sk89q.worldedit.function.RegionMaskingFilter) Locatable(com.sk89q.worldedit.extension.platform.Locatable) Logging(com.sk89q.worldedit.command.util.Logging) Preload(com.sk89q.worldedit.command.util.annotation.Preload) Command(org.enginehub.piston.annotation.Command) Confirm(com.sk89q.worldedit.command.util.annotation.Confirm) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Aggregations

RegionVisitor (com.sk89q.worldedit.function.visitor.RegionVisitor)8 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)4 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)3 RegionMaskingFilter (com.sk89q.worldedit.function.RegionMaskingFilter)3 Command (org.enginehub.piston.annotation.Command)3 Logging (com.sk89q.worldedit.command.util.Logging)2 RegionFunction (com.sk89q.worldedit.function.RegionFunction)2 FlatRegionVisitor (com.sk89q.worldedit.function.visitor.FlatRegionVisitor)2 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)2 FlatRegion (com.sk89q.worldedit.regions.FlatRegion)2 BlockType (com.sk89q.worldedit.world.block.BlockType)2 BlockTranslateExtent (com.fastasyncworldedit.core.extent.BlockTranslateExtent)1 PositionTransformExtent (com.fastasyncworldedit.core.extent.PositionTransformExtent)1 RegionMaskTestFunction (com.fastasyncworldedit.core.function.RegionMaskTestFunction)1 BiomeCopy (com.fastasyncworldedit.core.function.block.BiomeCopy)1 CombinedBlockCopy (com.fastasyncworldedit.core.function.block.CombinedBlockCopy)1 SimpleBlockCopy (com.fastasyncworldedit.core.function.block.SimpleBlockCopy)1 IntersectRegionFunction (com.fastasyncworldedit.core.function.visitor.IntersectRegionFunction)1 MutableBlockVector3 (com.fastasyncworldedit.core.math.MutableBlockVector3)1 MutableVector3 (com.fastasyncworldedit.core.math.MutableVector3)1