Add support for copying entities between Extents.

This commit is contained in:
sk89q
2014-07-10 22:22:35 -07:00
parent 52f1a7d2d4
commit 0ce7954dc9
23 changed files with 768 additions and 108 deletions

View File

@ -0,0 +1,103 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.entity;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.EntityFunction;
import com.sk89q.worldedit.math.transform.Transform;
import com.sk89q.worldedit.util.Location;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Copies entities provided to the function to the provided destination
* {@code Extent}.
*/
public class ExtentEntityCopy implements EntityFunction {
private final Extent destination;
private final Vector from;
private final Vector to;
private final Transform transform;
private boolean removing;
/**
* Create a new instance.
*
* @param from the from position
* @param destination the destination {@code Extent}
* @param to the destination position
* @param transform the transformation to apply to both position and orientation
*/
public ExtentEntityCopy(Vector from, Extent destination, Vector to, Transform transform) {
checkNotNull(from);
checkNotNull(destination);
checkNotNull(to);
checkNotNull(transform);
this.destination = destination;
this.from = from;
this.to = to;
this.transform = transform;
}
/**
* Return whether entities that are copied should be removed.
*
* @return true if removing
*/
public boolean isRemoving() {
return removing;
}
/**
* Set whether entities that are copied should be removed.
*
* @param removing true if removing
*/
public void setRemoving(boolean removing) {
this.removing = removing;
}
@Override
public boolean apply(Entity entity) throws WorldEditException {
BaseEntity state = entity.getState();
if (state != null) {
Location location = entity.getLocation();
Vector newPosition = transform.apply(location.toVector().subtract(from));
Vector newDirection = transform.apply(location.getDirection()).subtract(transform.apply(Vector.ZERO)).normalize();
Location newLocation = new Location(destination, newPosition.add(to), newDirection);
boolean success = destination.createEntity(newLocation, state) != null;
// Remove
if (isRemoving() && success) {
entity.remove();
}
return success;
} else {
return false;
}
}
}

View File

@ -19,20 +19,25 @@
package com.sk89q.worldedit.function.operation;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.CombinedRegionFunction;
import com.sk89q.worldedit.function.RegionFunction;
import com.sk89q.worldedit.function.RegionMaskingFilter;
import com.sk89q.worldedit.function.block.ExtentBlockCopy;
import com.sk89q.worldedit.function.entity.ExtentEntityCopy;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.mask.Masks;
import com.sk89q.worldedit.function.visitor.EntityVisitor;
import com.sk89q.worldedit.function.visitor.RegionVisitor;
import com.sk89q.worldedit.math.transform.Identity;
import com.sk89q.worldedit.math.transform.Transform;
import com.sk89q.worldedit.regions.Region;
import java.util.List;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
@ -52,6 +57,7 @@ public class ForwardExtentCopy implements Operation {
private final Vector to;
private int repetitions = 1;
private Mask sourceMask = Masks.alwaysTrue();
private boolean removingEntities;
private RegionFunction sourceFunction = null;
private Transform transform = new Identity();
private Transform currentTransform = null;
@ -177,6 +183,24 @@ public class ForwardExtentCopy implements Operation {
this.repetitions = repetitions;
}
/**
* Return whether entities that are copied should be removed.
*
* @return true if removing
*/
public boolean isRemovingEntities() {
return removingEntities;
}
/**
* Set whether entities that are copied should be removed.
*
* @param removing true if removing
*/
public void setRemovingEntities(boolean removingEntities) {
this.removingEntities = removingEntities;
}
/**
* Get the number of affected objects.
*
@ -200,13 +224,19 @@ public class ForwardExtentCopy implements Operation {
currentTransform = transform;
}
ExtentBlockCopy copy = new ExtentBlockCopy(source, from, destination, to, currentTransform);
RegionMaskingFilter filter = new RegionMaskingFilter(sourceMask, copy);
ExtentBlockCopy blockCopy = new ExtentBlockCopy(source, from, destination, to, currentTransform);
RegionMaskingFilter filter = new RegionMaskingFilter(sourceMask, blockCopy);
RegionFunction function = sourceFunction != null ? new CombinedRegionFunction(filter, sourceFunction) : filter;
RegionVisitor visitor = new RegionVisitor(region, function);
lastVisitor = visitor;
RegionVisitor blockVisitor = new RegionVisitor(region, function);
ExtentEntityCopy entityCopy = new ExtentEntityCopy(from, destination, to, currentTransform);
entityCopy.setRemoving(removingEntities);
List<? extends Entity> entities = source.getEntities(region);
EntityVisitor entityVisitor = new EntityVisitor(entities.iterator(), entityCopy);
lastVisitor = blockVisitor;
currentTransform = currentTransform.combine(transform);
return new DelegateOperation(this, visitor);
return new DelegateOperation(this, new OperationQueue(blockVisitor, entityVisitor));
} else {
return null;
}

View File

@ -34,7 +34,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/
public class EntityVisitor implements Operation {
private final Iterator<Entity> iterator;
private final Iterator<? extends Entity> iterator;
private final EntityFunction function;
private int affected = 0;
@ -44,7 +44,7 @@ public class EntityVisitor implements Operation {
* @param iterator the iterator
* @param function the function
*/
public EntityVisitor(Iterator<Entity> iterator, EntityFunction function) {
public EntityVisitor(Iterator<? extends Entity> iterator, EntityFunction function) {
checkNotNull(iterator);
checkNotNull(function);
this.iterator = iterator;