Search in sources :

Example 1 with Clipboard

use of com.sk89q.worldedit.extent.clipboard.Clipboard in project Skree by Skelril.

the class WEDecorator method pasteAt.

public <T> ZoneWorldBoundingBox pasteAt(WorldResolver world, Vector3i origin, String resourceName, Function<ZoneWorldBoundingBox, T> initMapper, Consumer<T> callback) {
    EditSession transaction = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world.getWorldEditWorld(), -1);
    transaction.setFastMode(true);
    transaction.getChangeSet().setRecordChanges(false);
    hashRefMap.computeIfAbsent(resourceName, (a) -> {
        HashRef ref = new HashRef();
        try {
            ref.holder = getHolder(resourceName, world.getWorldEditWorld().getWorldData());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ref;
    });
    HashRef ref = hashRefMap.get(resourceName);
    if (ref == null) {
        callback.accept(null);
        return null;
    }
    ++ref.refCount;
    Clipboard clipboard = ref.holder.getClipboard();
    Region clipReg = clipboard.getRegion();
    clipboard.setOrigin(clipReg.getMinimumPoint());
    Operation operation = ref.holder.createPaste(transaction, transaction.getWorld().getWorldData()).to(new Vector(origin.getX(), origin.getY(), origin.getZ())).build();
    Vector dimensions = clipboard.getDimensions();
    ZoneWorldBoundingBox region = new ZoneWorldBoundingBox(world.getSpongeWorld(), origin, new Vector3i(dimensions.getX(), dimensions.getY(), dimensions.getZ()));
    T returnVal = initMapper.apply(region);
    RunManager.runOperation(operation, () -> {
        RunManager.runOperation(transaction.commit(), () -> {
            region.getChunks().forEach(chunk -> {
                tf(chunk).resetRelightChecks();
                tf(chunk).generateSkylightMap();
                tf(chunk).checkLight();
            });
            callback.accept(returnVal);
            if (--ref.refCount == 0) {
                hashRefMap.remove(resourceName);
                System.gc();
            }
        });
    });
    return region;
}
Also used : Vector3i(com.flowpowered.math.vector.Vector3i) Region(com.sk89q.worldedit.regions.Region) EditSession(com.sk89q.worldedit.EditSession) IOException(java.io.IOException) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) Operation(com.sk89q.worldedit.function.operation.Operation) Vector(com.sk89q.worldedit.Vector) ZoneWorldBoundingBox(com.skelril.skree.service.internal.zone.ZoneWorldBoundingBox)

Example 2 with Clipboard

use of com.sk89q.worldedit.extent.clipboard.Clipboard in project RedProtect by FabioZumbi12.

the class WEListener method pasteWithWE.

public static void pasteWithWE(Player p, File f) throws DataException {
    SpongePlayer sp = SpongeWorldEdit.inst().wrapPlayer(p);
    SpongeWorld ws = SpongeWorldEdit.inst().getWorld(p.getWorld());
    LocalSession session = SpongeWorldEdit.inst().getSession(p);
    Closer closer = Closer.create();
    try {
        ClipboardFormat format = ClipboardFormat.findByAlias("schematic");
        FileInputStream fis = closer.register(new FileInputStream(f));
        BufferedInputStream bis = closer.register(new BufferedInputStream(fis));
        ClipboardReader reader = format.getReader(bis);
        WorldData worldData = ws.getWorldData();
        Clipboard clipboard = reader.read(ws.getWorldData());
        session.setClipboard(new ClipboardHolder(clipboard, worldData));
        ClipboardHolder holder = session.getClipboard();
        Operation op = holder.createPaste(session.createEditSession(sp), ws.getWorldData()).to(session.getPlacementPosition(sp)).build();
        Operations.completeLegacy(op);
    } catch (IOException | MaxChangedBlocksException | EmptyClipboardException | IncompleteRegionException e) {
        e.printStackTrace();
    }
}
Also used : Closer(com.sk89q.worldedit.util.io.Closer) SpongePlayer(com.sk89q.worldedit.sponge.SpongePlayer) ClipboardHolder(com.sk89q.worldedit.session.ClipboardHolder) LocalSession(com.sk89q.worldedit.LocalSession) IncompleteRegionException(com.sk89q.worldedit.IncompleteRegionException) WorldData(com.sk89q.worldedit.world.registry.WorldData) Operation(com.sk89q.worldedit.function.operation.Operation) IOException(java.io.IOException) ClipboardFormat(com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat) FileInputStream(java.io.FileInputStream) MaxChangedBlocksException(com.sk89q.worldedit.MaxChangedBlocksException) EmptyClipboardException(com.sk89q.worldedit.EmptyClipboardException) BufferedInputStream(java.io.BufferedInputStream) SpongeWorld(com.sk89q.worldedit.sponge.SpongeWorld) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) ClipboardReader(com.sk89q.worldedit.extent.clipboard.io.ClipboardReader)

Example 3 with Clipboard

use of com.sk89q.worldedit.extent.clipboard.Clipboard in project Skree by Skelril.

the class WEDecorator method getHolder.

private ClipboardHolder getHolder(String resourceName, WorldData worldData) throws IOException {
    try (InputStream bis = Files.newInputStream(getFile(resourceName))) {
        ClipboardReader reader = ClipboardFormat.SCHEMATIC.getReader(bis);
        Clipboard clipboard = reader.read(worldData);
        return new ClipboardHolder(clipboard, worldData);
    }
}
Also used : ClipboardHolder(com.sk89q.worldedit.session.ClipboardHolder) InputStream(java.io.InputStream) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) ClipboardReader(com.sk89q.worldedit.extent.clipboard.io.ClipboardReader)

Example 4 with Clipboard

use of com.sk89q.worldedit.extent.clipboard.Clipboard in project AreaShop by NLthijs48.

the class WorldEditHandler6 method restoreRegionBlocks.

@Override
public boolean restoreRegionBlocks(File file, GeneralRegionInterface regionInterface) {
    com.sk89q.worldedit.world.World world = null;
    if (regionInterface.getName() != null) {
        world = LocalWorldAdapter.adapt(new BukkitWorld(regionInterface.getWorld()));
    }
    if (world == null) {
        pluginInterface.getLogger().info("Did not restore region " + regionInterface.getName() + ", world not found: " + regionInterface.getWorldName());
        return false;
    }
    EditSession editSession = pluginInterface.getWorldEdit().getWorldEdit().getEditSessionFactory().getEditSession(world, pluginInterface.getConfig().getInt("maximumBlocks"));
    editSession.enableQueue();
    ProtectedRegion region = regionInterface.getRegion();
    // Get the origin and size of the region
    Vector origin = new Vector(region.getMinimumPoint().getBlockX(), region.getMinimumPoint().getBlockY(), region.getMinimumPoint().getBlockZ());
    // Read the schematic and paste it into the world
    try (Closer closer = Closer.create()) {
        FileInputStream fis = closer.register(new FileInputStream(file));
        BufferedInputStream bis = closer.register(new BufferedInputStream(fis));
        ClipboardReader reader = ClipboardFormat.SCHEMATIC.getReader(bis);
        WorldData worldData = world.getWorldData();
        LocalSession session = new LocalSession(pluginInterface.getWorldEdit().getLocalConfiguration());
        Clipboard clipboard = reader.read(worldData);
        if (clipboard.getDimensions().getY() != regionInterface.getHeight() || clipboard.getDimensions().getX() != regionInterface.getWidth() || clipboard.getDimensions().getZ() != regionInterface.getDepth()) {
            pluginInterface.getLogger().warning("Size of the region " + regionInterface.getName() + " is not the same as the schematic to restore!");
            pluginInterface.debugI("schematic|region, x:" + clipboard.getDimensions().getX() + "|" + regionInterface.getWidth() + ", y:" + clipboard.getDimensions().getY() + "|" + regionInterface.getHeight() + ", z:" + clipboard.getDimensions().getZ() + "|" + regionInterface.getDepth());
        }
        clipboard.setOrigin(clipboard.getMinimumPoint());
        ClipboardHolder clipboardHolder = new ClipboardHolder(clipboard, worldData);
        session.setBlockChangeLimit(pluginInterface.getConfig().getInt("maximumBlocks"));
        session.setClipboard(clipboardHolder);
        // Build operation
        BlockTransformExtent extent = new BlockTransformExtent(clipboardHolder.getClipboard(), clipboardHolder.getTransform(), editSession.getWorld().getWorldData().getBlockRegistry());
        ForwardExtentCopy copy = new ForwardExtentCopy(extent, clipboard.getRegion(), clipboard.getOrigin(), editSession, origin);
        copy.setTransform(clipboardHolder.getTransform());
        // TODO make this more efficient (especially for polygon regions)
        if (region.getType() != RegionType.CUBOID) {
            copy.setSourceMask(new Mask() {

                @Override
                public boolean test(Vector vector) {
                    return region.contains(vector);
                }

                @Nullable
                @Override
                public Mask2D toMask2D() {
                    return null;
                }
            });
        }
        Operations.completeLegacy(copy);
    } catch (MaxChangedBlocksException e) {
        pluginInterface.getLogger().warning("Exeeded the block limit while restoring schematic of " + regionInterface.getName() + ", limit in exception: " + e.getBlockLimit() + ", limit passed by AreaShop: " + pluginInterface.getConfig().getInt("maximumBlocks"));
        return false;
    } catch (IOException e) {
        pluginInterface.getLogger().warning("An error occured while restoring schematic of " + regionInterface.getName() + ", enable debug to see the complete stacktrace");
        pluginInterface.debugI(ExceptionUtils.getStackTrace(e));
        return false;
    }
    editSession.flushQueue();
    return true;
}
Also used : Closer(com.sk89q.worldedit.util.io.Closer) BlockTransformExtent(com.sk89q.worldedit.extent.transform.BlockTransformExtent) ClipboardHolder(com.sk89q.worldedit.session.ClipboardHolder) LocalSession(com.sk89q.worldedit.LocalSession) Mask(com.sk89q.worldedit.function.mask.Mask) BukkitWorld(com.sk89q.worldedit.bukkit.BukkitWorld) WorldData(com.sk89q.worldedit.world.registry.WorldData) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ForwardExtentCopy(com.sk89q.worldedit.function.operation.ForwardExtentCopy) Mask2D(com.sk89q.worldedit.function.mask.Mask2D) MaxChangedBlocksException(com.sk89q.worldedit.MaxChangedBlocksException) BufferedInputStream(java.io.BufferedInputStream) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) EditSession(com.sk89q.worldedit.EditSession) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) ClipboardReader(com.sk89q.worldedit.extent.clipboard.io.ClipboardReader) Vector(com.sk89q.worldedit.Vector) Nullable(javax.annotation.Nullable)

Example 5 with Clipboard

use of com.sk89q.worldedit.extent.clipboard.Clipboard in project FunnyGuilds by FunnyGuilds.

the class SchematicHelper method pasteSchematic.

public static boolean pasteSchematic(File schematicFile, Location location, boolean withAir) {
    try {
        Vector pasteLocation = new Vector(location.getX(), location.getY(), location.getZ());
        World pasteWorld = new BukkitWorld(location.getWorld());
        WorldData pasteWorldData = pasteWorld.getWorldData();
        Clipboard clipboard = ClipboardFormat.SCHEMATIC.getReader(new FileInputStream(schematicFile)).read(pasteWorldData);
        Region pasteRegion = clipboard.getRegion();
        Extent pasteExtent = WorldEdit.getInstance().getEditSessionFactory().getEditSession(pasteWorld, -1);
        AffineTransform transform = new AffineTransform();
        ForwardExtentCopy copy = new ForwardExtentCopy(pasteExtent, pasteRegion, clipboard.getOrigin(), pasteExtent, pasteLocation);
        if (!transform.isIdentity()) {
            copy.setTransform(transform);
        }
        if (!withAir) {
            copy.setSourceMask(new ExistingBlockMask(clipboard));
        }
        Operations.completeLegacy(copy);
        return true;
    } catch (IOException | MaxChangedBlocksException e) {
        FunnyLogger.exception(e);
        return false;
    }
}
Also used : Extent(com.sk89q.worldedit.extent.Extent) BukkitWorld(com.sk89q.worldedit.bukkit.BukkitWorld) WorldData(com.sk89q.worldedit.world.registry.WorldData) ExistingBlockMask(com.sk89q.worldedit.function.mask.ExistingBlockMask) IOException(java.io.IOException) World(com.sk89q.worldedit.world.World) BukkitWorld(com.sk89q.worldedit.bukkit.BukkitWorld) FileInputStream(java.io.FileInputStream) ForwardExtentCopy(com.sk89q.worldedit.function.operation.ForwardExtentCopy) MaxChangedBlocksException(com.sk89q.worldedit.MaxChangedBlocksException) Region(com.sk89q.worldedit.regions.Region) AffineTransform(com.sk89q.worldedit.math.transform.AffineTransform) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) Vector(com.sk89q.worldedit.Vector)

Aggregations

Clipboard (com.sk89q.worldedit.extent.clipboard.Clipboard)5 IOException (java.io.IOException)4 MaxChangedBlocksException (com.sk89q.worldedit.MaxChangedBlocksException)3 Vector (com.sk89q.worldedit.Vector)3 ClipboardReader (com.sk89q.worldedit.extent.clipboard.io.ClipboardReader)3 ClipboardHolder (com.sk89q.worldedit.session.ClipboardHolder)3 WorldData (com.sk89q.worldedit.world.registry.WorldData)3 FileInputStream (java.io.FileInputStream)3 EditSession (com.sk89q.worldedit.EditSession)2 LocalSession (com.sk89q.worldedit.LocalSession)2 BukkitWorld (com.sk89q.worldedit.bukkit.BukkitWorld)2 ForwardExtentCopy (com.sk89q.worldedit.function.operation.ForwardExtentCopy)2 Operation (com.sk89q.worldedit.function.operation.Operation)2 Region (com.sk89q.worldedit.regions.Region)2 Closer (com.sk89q.worldedit.util.io.Closer)2 BufferedInputStream (java.io.BufferedInputStream)2 Vector3i (com.flowpowered.math.vector.Vector3i)1 EmptyClipboardException (com.sk89q.worldedit.EmptyClipboardException)1 IncompleteRegionException (com.sk89q.worldedit.IncompleteRegionException)1 Extent (com.sk89q.worldedit.extent.Extent)1