Plex-FAWE/worldedit-core/src/main/java/com/sk89q/worldedit/regions/RegionIntersection.java

150 lines
5.0 KiB
Java
Raw Normal View History

/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
2014-04-04 22:03:18 +00:00
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
2014-04-04 22:03:18 +00:00
* 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 Lesser General Public License
* for more details.
*
2014-04-04 22:03:18 +00:00
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.regions;
2018-06-16 05:29:48 +00:00
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
2019-05-04 17:58:26 +00:00
import static com.sk89q.worldedit.regions.Region.Contains.CHECKED;
import static com.sk89q.worldedit.regions.Region.Contains.FULL;
import static com.sk89q.worldedit.regions.Region.Contains.NONE;
import static com.sk89q.worldedit.regions.Region.Contains.PARTIAL;
2018-06-16 05:29:48 +00:00
import com.google.common.collect.Iterators;
2018-12-23 16:19:33 +00:00
import com.sk89q.worldedit.math.BlockVector3;
2019-05-04 17:58:26 +00:00
import com.sk89q.worldedit.math.MutableBlockVector3;
2018-06-16 05:29:48 +00:00
import com.sk89q.worldedit.world.World;
import java.util.ArrayList;
2014-07-29 18:04:04 +00:00
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* An intersection of several other regions. Any location that is contained in one
* of the child regions is considered as contained by this region.
2014-07-29 18:04:04 +00:00
*
* <p>{@link #iterator()} returns a special iterator that will iterate through
* the iterators of each region in an undefined sequence. Some positions may
* be repeated if the position is contained in more than one region, but this cannot
2014-07-29 18:04:04 +00:00
* be guaranteed to occur.</p>
*/
public class RegionIntersection extends AbstractRegion {
2018-06-16 06:36:55 +00:00
private final List<Region> regions = new ArrayList<>();
/**
* Create a new instance with the included list of regions.
*
* @param regions a list of regions, which is copied
*/
public RegionIntersection(List<Region> regions) {
this(null, regions);
}
/**
* Create a new instance with the included list of regions.
*
* @param regions a list of regions, which is copied
*/
public RegionIntersection(Region... regions) {
this(null, regions);
}
/**
* Create a new instance with the included list of regions.
*
* @param world the world
* @param regions a list of regions, which is copied
*/
2018-06-16 05:29:48 +00:00
public RegionIntersection(World world, List<Region> regions) {
super(world);
checkNotNull(regions);
2014-07-29 18:04:04 +00:00
checkArgument(!regions.isEmpty(), "empty region list is not supported");
this.regions.addAll(regions);
}
/**
* Create a new instance with the included list of regions.
*
* @param world the world
* @param regions an array of regions, which is copied
*/
2018-06-16 05:29:48 +00:00
public RegionIntersection(World world, Region... regions) {
super(world);
checkNotNull(regions);
checkArgument(regions.length > 0, "empty region list is not supported");
2014-07-29 18:04:04 +00:00
Collections.addAll(this.regions, regions);
}
@Override
2018-12-23 16:19:33 +00:00
public BlockVector3 getMinimumPoint() {
BlockVector3 minimum = regions.get(0).getMinimumPoint();
for (int i = 1; i < regions.size(); i++) {
2018-12-23 16:19:33 +00:00
minimum = regions.get(i).getMinimumPoint().getMinimum(minimum);
}
return minimum;
}
@Override
2018-12-23 16:19:33 +00:00
public BlockVector3 getMaximumPoint() {
BlockVector3 maximum = regions.get(0).getMaximumPoint();
for (int i = 1; i < regions.size(); i++) {
2018-12-23 16:19:33 +00:00
maximum = regions.get(i).getMaximumPoint().getMaximum(maximum);
}
return maximum;
}
@Override
2018-12-23 16:19:33 +00:00
public void expand(BlockVector3... changes) throws RegionOperationException {
checkNotNull(changes);
throw new RegionOperationException("Cannot expand a region intersection");
}
@Override
2018-12-23 16:19:33 +00:00
public void contract(BlockVector3... changes) throws RegionOperationException {
checkNotNull(changes);
throw new RegionOperationException("Cannot contract a region intersection");
}
@Override
2018-12-23 16:19:33 +00:00
public boolean contains(BlockVector3 position) {
2014-07-29 18:04:04 +00:00
checkNotNull(position);
for (Region region : regions) {
2014-07-29 18:04:04 +00:00
if (region.contains(position)) {
return true;
}
}
return false;
}
@SuppressWarnings({"unchecked"})
@Override
2018-12-23 16:19:33 +00:00
public Iterator<BlockVector3> iterator() {
Iterator<BlockVector3>[] iterators = (Iterator<BlockVector3>[]) new Iterator[regions.size()];
for (int i = 0; i < regions.size(); i++) {
iterators[i] = regions.get(i).iterator();
}
return Iterators.concat(iterators);
}
}