Search in sources :

Example 16 with ProtectedRegion

use of com.sk89q.worldguard.protection.regions.ProtectedRegion in project AreaShop by NLthijs48.

the class Utils method getWorldEditRegionsInSelection.

/**
 * Get all WorldGuard regions intersecting with a WorldEdit selection.
 * @param selection The selection to check
 * @return A list with all the WorldGuard regions intersecting with the selection
 */
public static List<ProtectedRegion> getWorldEditRegionsInSelection(Selection selection) {
    // Get all regions inside or intersecting with the WorldEdit selection of the player
    World world = selection.getWorld();
    RegionManager regionManager = AreaShop.getInstance().getWorldGuard().getRegionManager(world);
    ArrayList<ProtectedRegion> result = new ArrayList<>();
    Location selectionMin = selection.getMinimumPoint();
    Location selectionMax = selection.getMaximumPoint();
    for (ProtectedRegion region : regionManager.getRegions().values()) {
        BlockVector regionMin = region.getMinimumPoint();
        BlockVector regionMax = region.getMaximumPoint();
        if ((// x part, resolves to true if the selection and region overlap anywhere on the x-axis
        (regionMin.getBlockX() <= selectionMax.getBlockX() && regionMin.getBlockX() >= selectionMin.getBlockX()) || (regionMax.getBlockX() <= selectionMax.getBlockX() && regionMax.getBlockX() >= selectionMin.getBlockX()) || (selectionMin.getBlockX() >= regionMin.getBlockX() && selectionMin.getBlockX() <= regionMax.getBlockX()) || (selectionMax.getBlockX() >= regionMin.getBlockX() && selectionMax.getBlockX() <= regionMax.getBlockX())) && (// Y part, resolves to true if the selection and region overlap anywhere on the y-axis
        (regionMin.getBlockY() <= selectionMax.getBlockY() && regionMin.getBlockY() >= selectionMin.getBlockY()) || (regionMax.getBlockY() <= selectionMax.getBlockY() && regionMax.getBlockY() >= selectionMin.getBlockY()) || (selectionMin.getBlockY() >= regionMin.getBlockY() && selectionMin.getBlockY() <= regionMax.getBlockY()) || (selectionMax.getBlockY() >= regionMin.getBlockY() && selectionMax.getBlockY() <= regionMax.getBlockY())) && (// Z part, resolves to true if the selection and region overlap anywhere on the z-axis
        (regionMin.getBlockZ() <= selectionMax.getBlockZ() && regionMin.getBlockZ() >= selectionMin.getBlockZ()) || (regionMax.getBlockZ() <= selectionMax.getBlockZ() && regionMax.getBlockZ() >= selectionMin.getBlockZ()) || (selectionMin.getBlockZ() >= regionMin.getBlockZ() && selectionMin.getBlockZ() <= regionMax.getBlockZ()) || (selectionMax.getBlockZ() >= regionMin.getBlockZ() && selectionMax.getBlockZ() <= regionMax.getBlockZ()))) {
            result.add(region);
        }
    }
    return result;
}
Also used : ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) ArrayList(java.util.ArrayList) RegionManager(com.sk89q.worldguard.protection.managers.RegionManager) BlockVector(com.sk89q.worldedit.BlockVector) World(org.bukkit.World) Location(org.bukkit.Location)

Example 17 with ProtectedRegion

use of com.sk89q.worldguard.protection.regions.ProtectedRegion in project AreaShop by NLthijs48.

the class GeneralRegion method saveRegionBlocks.

/**
 * Save all blocks in a region for restoring later.
 * @param fileName The name of the file to save to (extension and folder will be added)
 * @return true if the region has been saved properly, otherwise false
 */
public boolean saveRegionBlocks(String fileName) {
    // Check if the region is correct
    ProtectedRegion region = getRegion();
    if (region == null) {
        AreaShop.debug("Region '" + getName() + "' does not exist in WorldGuard, save failed");
        return false;
    }
    // The path to save the schematic
    File saveFile = new File(plugin.getFileManager().getSchematicFolder() + File.separator + fileName + AreaShop.schematicExtension);
    // Create parent directories
    File parent = saveFile.getParentFile();
    if (parent != null && !parent.exists()) {
        if (!parent.mkdirs()) {
            AreaShop.warn("Did not save region " + getName() + ", schematic directory could not be created: " + saveFile.getAbsolutePath());
            return false;
        }
    }
    boolean result = plugin.getWorldEditHandler().saveRegionBlocks(saveFile, this);
    if (result) {
        AreaShop.debug("Saved schematic for region " + getName());
    }
    return true;
}
Also used : ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) File(java.io.File)

Example 18 with ProtectedRegion

use of com.sk89q.worldguard.protection.regions.ProtectedRegion in project AreaShop by NLthijs48.

the class GeneralRegion method resetRegionFlags.

/**
 * Reset all flags of the region.
 */
public void resetRegionFlags() {
    ProtectedRegion region = getRegion();
    if (region != null) {
        region.setFlag(DefaultFlag.GREET_MESSAGE, null);
        region.setFlag(DefaultFlag.FAREWELL_MESSAGE, null);
    }
}
Also used : ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion)

Example 19 with ProtectedRegion

use of com.sk89q.worldguard.protection.regions.ProtectedRegion in project AreaShop by NLthijs48.

the class GeneralRegion method calculateVolume.

/**
 * Calculate the volume of the region (could be expensive for polygon regions).
 * @return Number of blocks in the region
 */
private long calculateVolume() {
    // Use own calculation for polygon regions, as WorldGuard does not implement it and returns 0
    ProtectedRegion region = getRegion();
    if (region instanceof ProtectedPolygonalRegion) {
        BlockVector min = region.getMinimumPoint();
        BlockVector max = region.getMaximumPoint();
        // Exact, but slow algorithm
        if (getWidth() * getDepth() < 100) {
            long surface = 0;
            for (int x = min.getBlockX(); x <= max.getBlockX(); x++) {
                for (int z = min.getBlockZ(); z <= max.getBlockZ(); z++) {
                    if (region.contains(x, min.getBlockY(), z)) {
                        surface++;
                    }
                }
            }
            return surface * getHeight();
        } else // Estimate, but quick algorithm
        {
            List<BlockVector2D> points = region.getPoints();
            int numPoints = points.size();
            if (numPoints < 3) {
                return 0;
            }
            double area = 0;
            int x1, x2, z1, z2;
            for (int i = 0; i <= numPoints - 2; i++) {
                x1 = points.get(i).getBlockX();
                z1 = points.get(i).getBlockZ();
                x2 = points.get(i + 1).getBlockX();
                z2 = points.get(i + 1).getBlockZ();
                area = area + ((z1 + z2) * (x1 - x2));
            }
            x1 = points.get(numPoints - 1).getBlockX();
            z1 = points.get(numPoints - 1).getBlockZ();
            x2 = points.get(0).getBlockX();
            z2 = points.get(0).getBlockZ();
            area = area + ((z1 + z2) * (x1 - x2));
            area = Math.ceil(Math.abs(area) / 2);
            return (long) (area * getHeight());
        }
    } else {
        return region.volume();
    }
}
Also used : BlockVector2D(com.sk89q.worldedit.BlockVector2D) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) ProtectedPolygonalRegion(com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion) BlockVector(com.sk89q.worldedit.BlockVector)

Example 20 with ProtectedRegion

use of com.sk89q.worldguard.protection.regions.ProtectedRegion 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

ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)29 ArrayList (java.util.ArrayList)7 World (org.bukkit.World)7 RegionManager (com.sk89q.worldguard.protection.managers.RegionManager)6 OwnedLand (biz.princeps.landlord.util.OwnedLand)5 BuyRegion (me.wiefferink.areashop.regions.BuyRegion)5 GeneralRegion (me.wiefferink.areashop.regions.GeneralRegion)5 RentRegion (me.wiefferink.areashop.regions.RentRegion)5 Vector (com.sk89q.worldedit.Vector)4 HashSet (java.util.HashSet)4 Location (org.bukkit.Location)4 Player (org.bukkit.entity.Player)4 LandlordCommand (biz.princeps.landlord.commands.LandlordCommand)3 BlockVector (com.sk89q.worldedit.BlockVector)3 Flag (com.sk89q.worldguard.protection.flags.Flag)3 StateFlag (com.sk89q.worldguard.protection.flags.StateFlag)3 List (java.util.List)3 Landlord (biz.princeps.landlord.Landlord)2 ManageGUIAll (biz.princeps.landlord.guis.ManageGUIAll)2 EditSession (com.sk89q.worldedit.EditSession)2