2020-04-16 04:52:39 +02:00
|
|
|
/*
|
|
|
|
* _____ _ _ _____ _
|
|
|
|
* | __ \| | | | / ____| | |
|
|
|
|
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
|
|
|
|
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
|
|
|
|
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
|
|
|
|
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
|
|
|
|
* | |
|
|
|
|
* |_|
|
|
|
|
* PlotSquared plot management system for Minecraft
|
|
|
|
* Copyright (C) 2020 IntellectualSites
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-08-15 14:59:29 +02:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-04-16 04:52:39 +02:00
|
|
|
*/
|
2020-04-15 21:26:54 +02:00
|
|
|
package com.plotsquared.core.util;
|
2019-11-04 22:08:33 +01:00
|
|
|
|
2020-07-19 14:23:55 +02:00
|
|
|
import com.plotsquared.core.location.Location;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.plot.Plot;
|
2020-05-21 20:06:04 +02:00
|
|
|
import com.sk89q.worldedit.math.BlockVector2;
|
2019-11-04 22:08:33 +01:00
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
|
|
|
import com.sk89q.worldedit.regions.CuboidRegion;
|
|
|
|
|
2020-07-17 17:24:45 +02:00
|
|
|
import javax.annotation.Nonnull;
|
2020-05-25 15:13:31 +02:00
|
|
|
import java.awt.geom.Rectangle2D;
|
2020-07-19 14:23:55 +02:00
|
|
|
import java.util.Collection;
|
2020-05-25 15:13:31 +02:00
|
|
|
|
2019-11-04 22:08:33 +01:00
|
|
|
public class RegionUtil {
|
2020-07-19 14:23:55 +02:00
|
|
|
|
|
|
|
@Nonnull public static Location[] getCorners(@Nonnull final String world,
|
|
|
|
@Nonnull final CuboidRegion region) {
|
|
|
|
final BlockVector3 min = region.getMinimumPoint();
|
|
|
|
final BlockVector3 max = region.getMaximumPoint();
|
|
|
|
return new Location[] {Location.at(world, min), Location.at(world, max)};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull public static Location[] getCorners(String world, Collection<CuboidRegion> regions) {
|
|
|
|
Location min = null;
|
|
|
|
Location max = null;
|
|
|
|
for (CuboidRegion region : regions) {
|
|
|
|
Location[] corners = getCorners(world, region);
|
|
|
|
if (min == null) {
|
|
|
|
min = corners[0];
|
|
|
|
max = corners[1];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Location pos1 = corners[0];
|
|
|
|
Location pos2 = corners[1];
|
|
|
|
if (pos2.getX() > max.getX()) {
|
|
|
|
max = max.withX(pos2.getX());
|
|
|
|
}
|
|
|
|
if (pos1.getX() < min.getX()) {
|
|
|
|
min = min.withX(pos1.getX());
|
|
|
|
}
|
|
|
|
if (pos2.getZ() > max.getZ()) {
|
|
|
|
max = max.withZ(pos2.getZ());
|
|
|
|
}
|
|
|
|
if (pos1.getZ() < min.getZ()) {
|
|
|
|
min = min.withZ(pos1.getZ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Location[] {min, max};
|
|
|
|
}
|
|
|
|
|
2019-11-04 22:08:33 +01:00
|
|
|
public static CuboidRegion createRegion(int pos1x, int pos2x, int pos1z, int pos2z) {
|
|
|
|
return createRegion(pos1x, pos2x, 0, Plot.MAX_HEIGHT - 1, pos1z, pos2z);
|
|
|
|
}
|
|
|
|
|
2020-02-24 18:42:02 +01:00
|
|
|
public static CuboidRegion createRegion(int pos1x, int pos2x, int pos1y, int pos2y, int pos1z,
|
|
|
|
int pos2z) {
|
2019-11-04 22:08:33 +01:00
|
|
|
BlockVector3 pos1 = BlockVector3.at(pos1x, pos1y, pos1z);
|
|
|
|
BlockVector3 pos2 = BlockVector3.at(pos2x, pos2y, pos2z);
|
|
|
|
return new CuboidRegion(pos1, pos2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean contains(CuboidRegion region, int x, int z) {
|
|
|
|
BlockVector3 min = region.getMinimumPoint();
|
|
|
|
BlockVector3 max = region.getMaximumPoint();
|
|
|
|
return x >= min.getX() && x <= max.getX() && z >= min.getZ() && z <= max.getZ();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean contains(CuboidRegion region, int x, int y, int z) {
|
|
|
|
BlockVector3 min = region.getMinimumPoint();
|
|
|
|
BlockVector3 max = region.getMaximumPoint();
|
2020-02-24 18:42:02 +01:00
|
|
|
return x >= min.getX() && x <= max.getX() && z >= min.getZ() && z <= max.getZ() && y >= min
|
|
|
|
.getY() && y <= max.getY();
|
2019-11-04 22:08:33 +01:00
|
|
|
}
|
|
|
|
|
2020-07-14 18:49:40 +02:00
|
|
|
@Nonnull public static Rectangle2D toRectangle(@Nonnull final CuboidRegion region) {
|
2020-05-21 20:06:04 +02:00
|
|
|
final BlockVector2 min = region.getMinimumPoint().toBlockVector2();
|
|
|
|
final BlockVector2 max = region.getMaximumPoint().toBlockVector2();
|
2020-05-25 15:13:31 +02:00
|
|
|
return new Rectangle2D.Double(min.getX(), min.getZ(), max.getX(), max.getZ());
|
2020-05-21 20:06:04 +02:00
|
|
|
}
|
|
|
|
|
2019-11-04 22:08:33 +01:00
|
|
|
// Because WE (not fawe) lack this for CuboidRegion
|
|
|
|
public static boolean intersects(CuboidRegion region, CuboidRegion other) {
|
|
|
|
BlockVector3 regionMin = region.getMinimumPoint();
|
|
|
|
BlockVector3 regionMax = region.getMaximumPoint();
|
|
|
|
|
|
|
|
BlockVector3 otherMin = other.getMinimumPoint();
|
|
|
|
BlockVector3 otherMax = other.getMaximumPoint();
|
|
|
|
|
2020-02-24 18:42:02 +01:00
|
|
|
return otherMin.getX() <= regionMax.getX() && otherMax.getX() >= regionMin.getX()
|
|
|
|
&& otherMin.getZ() <= regionMax.getZ() && otherMax.getZ() >= regionMin.getZ();
|
2019-11-04 22:08:33 +01:00
|
|
|
}
|
|
|
|
}
|