Search in sources :

Example 1 with Mask2D

use of com.sk89q.worldedit.function.mask.Mask2D 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)

Aggregations

EditSession (com.sk89q.worldedit.EditSession)1 LocalSession (com.sk89q.worldedit.LocalSession)1 MaxChangedBlocksException (com.sk89q.worldedit.MaxChangedBlocksException)1 Vector (com.sk89q.worldedit.Vector)1 BukkitWorld (com.sk89q.worldedit.bukkit.BukkitWorld)1 BlockArrayClipboard (com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard)1 Clipboard (com.sk89q.worldedit.extent.clipboard.Clipboard)1 ClipboardReader (com.sk89q.worldedit.extent.clipboard.io.ClipboardReader)1 BlockTransformExtent (com.sk89q.worldedit.extent.transform.BlockTransformExtent)1 Mask (com.sk89q.worldedit.function.mask.Mask)1 Mask2D (com.sk89q.worldedit.function.mask.Mask2D)1 ForwardExtentCopy (com.sk89q.worldedit.function.operation.ForwardExtentCopy)1 ClipboardHolder (com.sk89q.worldedit.session.ClipboardHolder)1 Closer (com.sk89q.worldedit.util.io.Closer)1 WorldData (com.sk89q.worldedit.world.registry.WorldData)1 ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Nullable (javax.annotation.Nullable)1