001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.util; 020 021import com.plotsquared.core.location.Location; 022import com.sk89q.worldedit.math.BlockVector3; 023import com.sk89q.worldedit.regions.CuboidRegion; 024import org.checkerframework.checker.nullness.qual.NonNull; 025 026import java.util.Collection; 027import java.util.Iterator; 028 029public class RegionUtil { 030 031 public static @NonNull Location[] getCorners( 032 final @NonNull String world, 033 final @NonNull CuboidRegion region 034 ) { 035 final BlockVector3 min = region.getMinimumPoint(); 036 final BlockVector3 max = region.getMaximumPoint(); 037 return new Location[]{Location.at(world, min), Location.at(world, max)}; 038 } 039 040 public static @NonNull Location[] getCorners(String world, Collection<CuboidRegion> regions) { 041 CuboidRegion aabb = getAxisAlignedBoundingBox(regions); 042 return getCorners(world, aabb); 043 } 044 045 /** 046 * Create a minimum {@link CuboidRegion} containing all given regions. 047 * 048 * @param regions The regions the bounding box should contain. 049 * @return a CuboidRegion that contains all given regions. 050 */ 051 public static @NonNull CuboidRegion getAxisAlignedBoundingBox(Iterable<CuboidRegion> regions) { 052 Iterator<CuboidRegion> iterator = regions.iterator(); 053 if (!iterator.hasNext()) { 054 throw new IllegalArgumentException("No regions given"); 055 } 056 CuboidRegion next = iterator.next(); 057 BlockVector3 min = next.getMinimumPoint(); 058 BlockVector3 max = next.getMaximumPoint(); 059 060 while (iterator.hasNext()) { 061 next = iterator.next(); 062 // as max >= min, this is enough to check 063 min = min.getMinimum(next.getMinimumPoint()); 064 max = max.getMaximum(next.getMaximumPoint()); 065 } 066 return new CuboidRegion(min, max); 067 } 068 069 public static CuboidRegion createRegion( 070 int pos1x, int pos2x, int pos1y, int pos2y, int pos1z, 071 int pos2z 072 ) { 073 BlockVector3 pos1 = BlockVector3.at(pos1x, pos1y, pos1z); 074 BlockVector3 pos2 = BlockVector3.at(pos2x, pos2y, pos2z); 075 return new CuboidRegion(pos1, pos2); 076 } 077 078 public static boolean contains(CuboidRegion region, int x, int z) { 079 BlockVector3 min = region.getMinimumPoint(); 080 BlockVector3 max = region.getMaximumPoint(); 081 return x >= min.getX() && x <= max.getX() && z >= min.getZ() && z <= max.getZ(); 082 } 083 084 public static boolean contains(CuboidRegion region, int x, int y, int z) { 085 BlockVector3 min = region.getMinimumPoint(); 086 BlockVector3 max = region.getMaximumPoint(); 087 return x >= min.getX() && x <= max.getX() && z >= min.getZ() && z <= max.getZ() && y >= min 088 .getY() && y <= max.getY(); 089 } 090 091 // Because WorldEdit (not FastAsyncWorldEdit) lack this for CuboidRegion 092 public static boolean intersects(CuboidRegion region, CuboidRegion other) { 093 BlockVector3 regionMin = region.getMinimumPoint(); 094 BlockVector3 regionMax = region.getMaximumPoint(); 095 096 BlockVector3 otherMin = other.getMinimumPoint(); 097 BlockVector3 otherMax = other.getMaximumPoint(); 098 099 return otherMin.getX() <= regionMax.getX() && otherMax.getX() >= regionMin.getX() 100 && otherMin.getZ() <= regionMax.getZ() && otherMax.getZ() >= regionMin.getZ(); 101 } 102 103}