From d50cd89005477f5508bc747d04a57304ff2fc1c4 Mon Sep 17 00:00:00 2001 From: sk89q Date: Thu, 10 Jul 2014 02:59:09 -0700 Subject: [PATCH] Add Entity visitor and function interfaces. --- .../worldedit/function/EntityFunction.java | 39 ++++++++++ .../function/visitor/EntityVisitor.java | 78 +++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 src/main/java/com/sk89q/worldedit/function/EntityFunction.java create mode 100644 src/main/java/com/sk89q/worldedit/function/visitor/EntityVisitor.java diff --git a/src/main/java/com/sk89q/worldedit/function/EntityFunction.java b/src/main/java/com/sk89q/worldedit/function/EntityFunction.java new file mode 100644 index 000000000..f7048cd1a --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/function/EntityFunction.java @@ -0,0 +1,39 @@ +/* + * 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.function; + +import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.entity.Entity; + +/** + * Applies a function to entities. + */ +public interface EntityFunction { + + /** + * Apply the function to the entity. + * + * @param entity the entity + * @return true if something was changed + * @throws WorldEditException thrown on an error + */ + public boolean apply(Entity entity) throws WorldEditException; + +} diff --git a/src/main/java/com/sk89q/worldedit/function/visitor/EntityVisitor.java b/src/main/java/com/sk89q/worldedit/function/visitor/EntityVisitor.java new file mode 100644 index 000000000..14509bd8c --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/function/visitor/EntityVisitor.java @@ -0,0 +1,78 @@ +/* + * 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.function.visitor; + +import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.entity.Entity; +import com.sk89q.worldedit.function.EntityFunction; +import com.sk89q.worldedit.function.operation.Operation; +import com.sk89q.worldedit.function.operation.RunContext; + +import java.util.Iterator; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Visits entities as provided by an {@code Iterator}. + */ +public class EntityVisitor implements Operation { + + private final Iterator iterator; + private final EntityFunction function; + private int affected = 0; + + /** + * Create a new instance. + * + * @param iterator the iterator + * @param function the function + */ + public EntityVisitor(Iterator iterator, EntityFunction function) { + checkNotNull(iterator); + checkNotNull(function); + this.iterator = iterator; + this.function = function; + } + + /** + * Get the number of affected objects. + * + * @return the number of affected + */ + public int getAffected() { + return affected; + } + + @Override + public Operation resume(RunContext run) throws WorldEditException { + while (iterator.hasNext()) { + if (function.apply(iterator.next())) { + affected++; + } + } + + return null; + } + + @Override + public void cancel() { + } + +}