Search in sources :

Example 11 with EditSession

use of com.sk89q.worldedit.EditSession in project RedProtect by FabioZumbi12.

the class AWEListener method regenRegion.

public static void regenRegion(final br.net.fabiozumbi12.RedProtect.Bukkit.Region r, final World w, final Location p1, final Location p2, final int delay, final CommandSender sender, final boolean remove) {
    Bukkit.getScheduler().scheduleSyncDelayedTask(RedProtect.get(), () -> {
        if (RPUtil.stopRegen) {
            return;
        }
        CuboidSelection csel = new CuboidSelection(w, p1, p2);
        Region wreg;
        try {
            wreg = csel.getRegionSelector().getRegion();
        } catch (IncompleteRegionException e1) {
            e1.printStackTrace();
            return;
        }
        AsyncWorldEditBukkit aweMain = (AsyncWorldEditBukkit) Bukkit.getPluginManager().getPlugin("AsyncWorldEdit");
        IBlockPlacer bPlacer = aweMain.getBlockPlacer();
        final IBlockPlacerListener listener = new IBlockPlacerListener() {

            @Override
            public void jobAdded(IJobEntry job) {
            /*if (job.getPlayer().getName().equals("redprotect")){
                        String name = job.getName();
                        //job.addStateChangedListener(stateListener);
                        RedProtect.get().logger.warning("JobAdded: " + name + " of region " + r.getName() + " - " + job.getStatus() + ": " + job.isTaskDone());
                    }*/
            }

            @Override
            public void jobRemoved(IJobEntry job) {
                if (job.getPlayer().getName().equals("redprotect")) {
                    String name = job.getName();
                    // job.addStateChangedListener(stateListener);
                    if (RPConfig.getBool("purge.regen.awe-logs") && job.isTaskDone()) {
                        RedProtect.get().logger.sucess("JobDone: " + name + " of region " + r.getName() + " - " + job.getStatus() + ": " + job.getStatusString());
                    }
                }
            }
        };
        bPlacer.addListener(listener);
        AsyncEditSessionFactory factory = (AsyncEditSessionFactory) WorldEdit.getInstance().getEditSessionFactory();
        EditSession ess = factory.getEditSession(wreg.getWorld(), -1);
        eSessions.put(r.getID(), ess);
        int delayCount = 1 + delay / 10;
        if (sender != null) {
            if (AsyncWorld.wrap(wreg.getWorld(), new PlayerManager(aweMain).createFakePlayer("redprotect", UUID.randomUUID())).regenerate(wreg, ess)) {
                RPLang.sendMessage(sender, "[" + delayCount + "]" + " &aRegion " + r.getID().split("@")[0] + " regenerated with success!");
            } else {
                RPLang.sendMessage(sender, "[" + delayCount + "]" + " &cTheres an error when regen the region " + r.getID().split("@")[0] + "!");
            }
        } else {
            if (AsyncWorld.wrap(wreg.getWorld(), new PlayerManager(aweMain).createFakePlayer("redprotect", UUID.randomUUID())).regenerate(wreg, ess)) {
                RedProtect.get().logger.warning("[" + delayCount + "]" + " &aRegion " + r.getID().split("@")[0] + " regenerated with success!");
            } else {
                RedProtect.get().logger.warning("[" + delayCount + "]" + " &cTheres an error when regen the region " + r.getID().split("@")[0] + "!");
            }
        }
        if (remove) {
            RedProtect.get().rm.remove(r, RedProtect.get().serv.getWorld(r.getWorld()));
        }
        if (RPConfig.getInt("purge.regen.stop-server-every") > 0 && delayCount > RPConfig.getInt("purge.regen.stop-server-every")) {
            Bukkit.getScheduler().cancelTasks(RedProtect.get());
            RedProtect.get().rm.saveAll();
            Bukkit.getServer().shutdown();
        }
    }, delay);
}
Also used : AsyncWorldEditBukkit(org.primesoft.asyncworldedit.AsyncWorldEditBukkit) CuboidSelection(com.sk89q.worldedit.bukkit.selections.CuboidSelection) PlayerManager(org.primesoft.asyncworldedit.playerManager.PlayerManager) IBlockPlacer(org.primesoft.asyncworldedit.api.blockPlacer.IBlockPlacer) IBlockPlacerListener(org.primesoft.asyncworldedit.api.blockPlacer.IBlockPlacerListener) IJobEntry(org.primesoft.asyncworldedit.api.blockPlacer.entries.IJobEntry) IncompleteRegionException(com.sk89q.worldedit.IncompleteRegionException) Region(com.sk89q.worldedit.regions.Region) EditSession(com.sk89q.worldedit.EditSession) AsyncEditSessionFactory(org.primesoft.asyncworldedit.worldedit.AsyncEditSessionFactory)

Example 12 with EditSession

use of com.sk89q.worldedit.EditSession in project AreaShop by NLthijs48.

the class WorldEditHandler6 method saveRegionBlocks.

@Override
public boolean saveRegionBlocks(File file, GeneralRegionInterface regionInterface) {
    com.sk89q.worldedit.world.World world = null;
    if (regionInterface.getWorld() != null) {
        world = LocalWorldAdapter.adapt(new BukkitWorld(regionInterface.getWorld()));
    }
    if (world == null) {
        pluginInterface.getLogger().warning("Did not save region " + regionInterface.getName() + ", world not found: " + regionInterface.getWorldName());
        return false;
    }
    EditSession editSession = pluginInterface.getWorldEdit().getWorldEdit().getEditSessionFactory().getEditSession(world, pluginInterface.getConfig().getInt("maximumBlocks"));
    // Create a clipboard
    CuboidRegion selection = new CuboidRegion(world, regionInterface.getRegion().getMinimumPoint(), regionInterface.getRegion().getMaximumPoint());
    BlockArrayClipboard clipboard = new BlockArrayClipboard(selection);
    clipboard.setOrigin(regionInterface.getRegion().getMinimumPoint());
    ForwardExtentCopy copy = new ForwardExtentCopy(editSession, new CuboidRegion(world, regionInterface.getRegion().getMinimumPoint(), regionInterface.getRegion().getMaximumPoint()), clipboard, regionInterface.getRegion().getMinimumPoint());
    try {
        Operations.completeLegacy(copy);
    } catch (MaxChangedBlocksException e) {
        pluginInterface.getLogger().warning("Exeeded the block limit while saving schematic of " + regionInterface.getName() + ", limit in exception: " + e.getBlockLimit() + ", limit passed by AreaShop: " + pluginInterface.getConfig().getInt("maximumBlocks"));
        return false;
    }
    try (Closer closer = Closer.create()) {
        FileOutputStream fos = closer.register(new FileOutputStream(file));
        BufferedOutputStream bos = closer.register(new BufferedOutputStream(fos));
        ClipboardWriter writer = closer.register(ClipboardFormat.SCHEMATIC.getWriter(bos));
        writer.write(clipboard, world.getWorldData());
    } catch (IOException e) {
        pluginInterface.getLogger().warning("An error occured while saving schematic of " + regionInterface.getName() + ", enable debug to see the complete stacktrace");
        pluginInterface.debugI(ExceptionUtils.getStackTrace(e));
        return false;
    }
    return true;
}
Also used : Closer(com.sk89q.worldedit.util.io.Closer) ClipboardWriter(com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) BukkitWorld(com.sk89q.worldedit.bukkit.BukkitWorld) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) IOException(java.io.IOException) ForwardExtentCopy(com.sk89q.worldedit.function.operation.ForwardExtentCopy) MaxChangedBlocksException(com.sk89q.worldedit.MaxChangedBlocksException) FileOutputStream(java.io.FileOutputStream) EditSession(com.sk89q.worldedit.EditSession) BufferedOutputStream(java.io.BufferedOutputStream)

Example 13 with EditSession

use of com.sk89q.worldedit.EditSession 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 14 with EditSession

use of com.sk89q.worldedit.EditSession in project AreaShop by NLthijs48.

the class WorldEditHandler5 method restoreRegionBlocks.

@Override
public boolean restoreRegionBlocks(File file, GeneralRegionInterface regionInterface) {
    boolean result = true;
    EditSession editSession = pluginInterface.getWorldEdit().getWorldEdit().getEditSessionFactory().getEditSession(new BukkitWorld(regionInterface.getWorld()), pluginInterface.getConfig().getInt("maximumBlocks"));
    // Get the origin and size of the region
    Vector origin = new Vector(regionInterface.getRegion().getMinimumPoint().getBlockX(), regionInterface.getRegion().getMinimumPoint().getBlockY(), regionInterface.getRegion().getMinimumPoint().getBlockZ());
    editSession.enableQueue();
    Exception otherException = null;
    try {
        CuboidClipboard clipBoard = SchematicFormat.MCEDIT.load(file);
        if (clipBoard.getHeight() != regionInterface.getHeight() || clipBoard.getWidth() != regionInterface.getWidth() || clipBoard.getLength() != 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.getWidth() + "|" + regionInterface.getWidth() + ", y:" + clipBoard.getHeight() + "|" + regionInterface.getHeight() + ", z:" + clipBoard.getLength() + "|" + regionInterface.getDepth());
        }
        clipBoard.place(editSession, origin, false);
    } 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"));
        result = false;
    } catch (DataException | IOException e) {
        otherException = e;
    }
    if (otherException != null) {
        pluginInterface.getLogger().warning("Failed to restore schematic for region " + regionInterface.getName());
        pluginInterface.debugI(ExceptionUtils.getStackTrace(otherException));
        result = false;
    }
    editSession.flushQueue();
    return result;
}
Also used : DataException(com.sk89q.worldedit.data.DataException) CuboidClipboard(com.sk89q.worldedit.CuboidClipboard) BukkitWorld(com.sk89q.worldedit.bukkit.BukkitWorld) EditSession(com.sk89q.worldedit.EditSession) IOException(java.io.IOException) Vector(com.sk89q.worldedit.Vector) DataException(com.sk89q.worldedit.data.DataException) MaxChangedBlocksException(com.sk89q.worldedit.MaxChangedBlocksException) IOException(java.io.IOException) MaxChangedBlocksException(com.sk89q.worldedit.MaxChangedBlocksException)

Aggregations

EditSession (com.sk89q.worldedit.EditSession)14 MaxChangedBlocksException (com.sk89q.worldedit.MaxChangedBlocksException)7 Vector (com.sk89q.worldedit.Vector)7 Region (com.sk89q.worldedit.regions.Region)6 IOException (java.io.IOException)6 BukkitWorld (com.sk89q.worldedit.bukkit.BukkitWorld)5 IncompleteRegionException (com.sk89q.worldedit.IncompleteRegionException)4 CuboidSelection (com.sk89q.worldedit.bukkit.selections.CuboidSelection)4 EditSessionBuilder (com.boydti.fawe.util.EditSessionBuilder)3 CuboidClipboard (com.sk89q.worldedit.CuboidClipboard)3 Vector3i (com.flowpowered.math.vector.Vector3i)2 DataException (com.sk89q.worldedit.data.DataException)2 BlockArrayClipboard (com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard)2 Clipboard (com.sk89q.worldedit.extent.clipboard.Clipboard)2 ForwardExtentCopy (com.sk89q.worldedit.function.operation.ForwardExtentCopy)2 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)2 Closer (com.sk89q.worldedit.util.io.Closer)2 ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)2 Vector3d (com.flowpowered.math.vector.Vector3d)1 EditSessionFactory (com.sk89q.worldedit.EditSessionFactory)1