/* * WorldEdit, a Minecraft world manipulation toolkit * Copyright (C) sk89q * Copyright (C) WorldEdit team and contributors * * 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. * * 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. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ 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; } @Override public Vector getCenter() { return getMinimumPoint().add(getMaximumPoint()).divide(2); } /** * Get the iterator. * * @return iterator of points inside the region */ @Override public Iterator 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); } @Override public AbstractRegion clone() { try { return (AbstractRegion) super.clone(); } catch (CloneNotSupportedException exc) { return null; } } @Override public List 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 points = new ArrayList(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 getChunks() { final Set chunks = new HashSet(); 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 getChunkCubes() { final Set chunks = new HashSet(); 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; } }