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

@ -23,7 +23,10 @@ import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.internal.helper.MCDirections;
import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.util.Location;
import net.minecraft.entity.EntityHanging;
import net.minecraft.entity.EntityList;
import net.minecraft.nbt.NBTTagCompound;
@ -40,17 +43,42 @@ class ForgeEntity implements Entity {
@Override
public BaseEntity getState() {
NBTTagCompound tag = new NBTTagCompound();
entity.writeToNBT(tag);
return new BaseEntity(EntityList.getEntityString(entity), NBTConverter.fromNative(tag));
String id = EntityList.getEntityString(entity);
if (id != null) {
NBTTagCompound tag = new NBTTagCompound();
entity.writeToNBT(tag);
return new BaseEntity(id, NBTConverter.fromNative(tag));
} else {
return null;
}
}
@Override
public Location getLocation() {
return new Location(
ForgeAdapter.adapt(entity.worldObj),
new Vector(entity.posX, entity.posY, entity.posZ),
ForgeAdapter.adapt(entity.getLookVec()));
Vector position;
float pitch;
float yaw;
if (entity instanceof EntityHanging) {
EntityHanging hanging = (EntityHanging) entity;
position = new Vector(hanging.xPosition, hanging.yPosition, hanging.zPosition);
Direction direction = MCDirections.fromHanging(hanging.hangingDirection);
if (direction != null) {
yaw = direction.toVector().toYaw();
pitch = direction.toVector().toPitch();
} else {
yaw = (float) Math.toRadians(entity.rotationYaw);
pitch = (float) Math.toRadians(entity.rotationPitch);
}
} else {
position = new Vector(entity.posX, entity.posY, entity.posZ);
yaw = (float) Math.toRadians(entity.rotationYaw);
pitch = (float) Math.toRadians(entity.rotationPitch);
}
return new Location(ForgeAdapter.adapt(entity.worldObj), position, yaw, pitch);
}
@Override

View File

@ -32,7 +32,10 @@ import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.blocks.LazyBlock;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.internal.helper.MCDirections;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.util.Direction.Flag;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.TreeGenerator.TreeType;
import com.sk89q.worldedit.world.AbstractWorld;
@ -500,7 +503,29 @@ public class ForgeWorld extends AbstractWorld {
}
@Override
public List<Entity> getEntities() {
@SuppressWarnings("unchecked")
public List<? extends Entity> getEntities(Region region) {
List<Entity> entities = new ArrayList<Entity>();
World world = getWorld();
for (Vector2D pt : region.getChunks()) {
if (!world.getChunkProvider().chunkExists(pt.getBlockX(), pt.getBlockZ())) {
continue;
}
Chunk chunk = world.getChunkProvider().provideChunk(pt.getBlockX(), pt.getBlockZ());
for (List<net.minecraft.entity.Entity> entitySubList : chunk.entityLists) {
for (net.minecraft.entity.Entity entity : entitySubList) {
if (region.contains(new Vector(entity.posX, entity.posY, entity.posZ))) {
entities.add(new ForgeEntity(entity));
}
}
}
}
return entities;
}
@Override
public List<? extends Entity> getEntities() {
List<Entity> entities = new ArrayList<Entity>();
for (Object entity : getWorld().getLoadedEntityList()) {
entities.add(new ForgeEntity((net.minecraft.entity.Entity) entity));
@ -518,6 +543,19 @@ public class ForgeWorld extends AbstractWorld {
if (tag != null) {
createdEntity.readFromNBT(NBTConverter.toNative(entity.getNbtData()));
}
createdEntity.setLocationAndAngles(location.getX(), location.getY(), location.getZ(), (float) Math.toDegrees(location.getYaw()), (float) Math.toDegrees(location.getPitch()));
// Special handling for hanging entities
if (createdEntity instanceof EntityHanging) {
EntityHanging hanging = (EntityHanging) createdEntity;
hanging.xPosition = location.getBlockX();
hanging.yPosition = location.getBlockY();
hanging.zPosition = location.getBlockZ();
Direction direction = Direction.findClosest(location.getDirection(), Flag.CARDINAL);
hanging.setDirection(MCDirections.toHanging(direction));
}
world.spawnEntityInWorld(createdEntity);
return new ForgeEntity(createdEntity);
} else {