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

214 lines
5.8 KiB
Java
Raw Normal View History

/*
2014-04-04 22:03:18 +00:00
* 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/>.
2014-04-04 22:03:18 +00:00
*/
package com.sk89q.worldedit.regions;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.regions.iterator.RegionIterator;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.storage.ChunkStore;
import java.util.*;
public abstract class AbstractRegion implements Region {
/**
* Stores the world.
*/
protected World world;
public AbstractRegion(World world) {
this.world = world;
}
2012-08-29 08:00:26 +00:00
@Override
public Vector getCenter() {
return getMinimumPoint().add(getMaximumPoint()).divide(2);
}
/**
* Get the iterator.
*
* @return iterator of points inside the region
*/
@Override
public Iterator<BlockVector> iterator() {
return new RegionIterator(this);
}
@Override
public World getWorld() {
return world;
}
@Override
public void setWorld(LocalWorld world) {
setWorld((World) world);
}
@Override
public void setWorld(World world) {
this.world = world;
}
@Override
public void shift(Vector change) throws RegionOperationException {
expand(change);
contract(change);
}
2012-03-20 14:14:41 +00:00
@Override
2012-03-20 14:14:41 +00:00
public AbstractRegion clone() {
try {
return (AbstractRegion) super.clone();
} catch (CloneNotSupportedException exc) {
return null;
}
}
@Override
public List<BlockVector2D> polygonize(int maxPoints) {
if (maxPoints >= 0 && maxPoints < 4) {
throw new IllegalArgumentException("Cannot polygonize an AbstractRegion with no overridden polygonize method into less than 4 points.");
}
final BlockVector min = getMinimumPoint().toBlockVector();
final BlockVector max = getMaximumPoint().toBlockVector();
final List<BlockVector2D> points = new ArrayList<BlockVector2D>(4);
points.add(new BlockVector2D(min.getX(), min.getZ()));
points.add(new BlockVector2D(min.getX(), max.getZ()));
points.add(new BlockVector2D(max.getX(), max.getZ()));
points.add(new BlockVector2D(max.getX(), min.getZ()));
return points;
}
/**
* Get the number of blocks in the region.
*
* @return number of blocks
*/
@Override
public int getArea() {
Vector min = getMinimumPoint();
Vector max = getMaximumPoint();
return (int)((max.getX() - min.getX() + 1) *
(max.getY() - min.getY() + 1) *
(max.getZ() - min.getZ() + 1));
}
/**
* Get X-size.
*
* @return width
*/
@Override
public int getWidth() {
Vector min = getMinimumPoint();
Vector max = getMaximumPoint();
return (int) (max.getX() - min.getX() + 1);
}
/**
* Get Y-size.
*
* @return height
*/
@Override
public int getHeight() {
Vector min = getMinimumPoint();
Vector max = getMaximumPoint();
return (int) (max.getY() - min.getY() + 1);
}
/**
* Get Z-size.
*
* @return length
*/
@Override
public int getLength() {
Vector min = getMinimumPoint();
Vector max = getMaximumPoint();
return (int) (max.getZ() - min.getZ() + 1);
}
/**
* Get a list of chunks.
*
* @return
*/
@Override
public Set<Vector2D> getChunks() {
final Set<Vector2D> chunks = new HashSet<Vector2D>();
final Vector min = getMinimumPoint();
final Vector max = getMaximumPoint();
final int minY = min.getBlockY();
for (int x = min.getBlockX(); x <= max.getBlockX(); ++x) {
for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) {
if (!contains(new Vector(x, minY, z))) {
continue;
}
chunks.add(new BlockVector2D(
x >> ChunkStore.CHUNK_SHIFTS,
z >> ChunkStore.CHUNK_SHIFTS
));
}
}
return chunks;
}
@Override
public Set<Vector> getChunkCubes() {
final Set<Vector> chunks = new HashSet<Vector>();
final Vector min = getMinimumPoint();
final Vector max = getMaximumPoint();
for (int x = min.getBlockX(); x <= max.getBlockX(); ++x) {
for (int y = min.getBlockY(); y <= max.getBlockY(); ++y) {
for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) {
if (!contains(new Vector(x, y, z))) {
continue;
}
chunks.add(new BlockVector(
x >> ChunkStore.CHUNK_SHIFTS,
y >> ChunkStore.CHUNK_SHIFTS,
z >> ChunkStore.CHUNK_SHIFTS
));
}
}
}
return chunks;
}
}