*   This file is part of Skript.
 *  Skript 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.
 *  Skript is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  GNU General Public License for more details.
 *  You should have received a copy of the GNU General Public License
 *  along with Skript.  If not, see <http://www.gnu.org/licenses/>.
 * Copyright 2011-2017 Peter G├╝ttinger and contributors
package ch.njol.skript.hooks.regions.expressions;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;

import org.bukkit.block.Block;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.hooks.regions.classes.Region;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.iterator.ArrayIterator;
import ch.njol.util.coll.iterator.EmptyIterator;

 * @author Peter G├╝ttinger
@Name("Blocks in Region")
@Description({"All blocks in a <a href='../classes.html#region'>region</a>.",
		"This expression requires a supported regions plugin to be installed."})
@Examples({"loop all blocks in the region {arena.%{faction.%player%}%}:",
		"	clear the loop-block"})
public class ExprBlocksInRegion extends SimpleExpression<Block> {
	static {
		Skript.registerExpression(ExprBlocksInRegion.class, Block.class, ExpressionType.COMBINED,
				"[(all|the)] blocks (in|of) [[the] region[s]] %regions%");
	private Expression<Region> regions;
	@SuppressWarnings({"unchecked", "null"})
	public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
		regions = (Expression<Region>) exprs[0];
		return true;
	protected Block[] get(final Event e) {
		final Iterator<Block> iter = iterator(e);
		final ArrayList<Block> r = new ArrayList<>();
		while (iter.hasNext())
		return r.toArray(new Block[r.size()]);
	public Iterator<Block> iterator(final Event e) {
		final Region[] rs = regions.getArray(e);
		if (rs.length == 0)
			return EmptyIterator.get();
		return new Iterator<Block>() {
			private Iterator<Block> current = rs[0].getBlocks();
			private final Iterator<Region> iter = new ArrayIterator<>(rs, 1);
			public boolean hasNext() {
				while (!current.hasNext() && iter.hasNext()) {
					final Region r = iter.next();
					if (r != null)
						current = r.getBlocks();
				return current.hasNext();
			public Block next() {
				if (!hasNext())
					throw new NoSuchElementException();
				return current.next();
			public void remove() {
				throw new UnsupportedOperationException();
	public boolean isSingle() {
		return false;
	public Class<? extends Block> getReturnType() {
		return Block.class;
	public String toString(final @Nullable Event e, final boolean debug) {
		return "all blocks in " + regions.toString(e, debug);