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;
}
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;
}
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);
}
}
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();
}
}
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;
}
Aggregations