Added incomplete entity support to all Extents.

The Bukkit implementation supports the new entity API, but it has
not yet been tested. The Forge implementation does not support the
entity API yet.

At the moment, an UnsupportedOperationException is thrown for
Entity.getState() in some implementations, but use of an exception
should probably not be allowed.

BaseEntity is now an interface. It should not be possible to create
instances of BaseEntity because it may be implementation-specific.
This commit is contained in:
sk89q 2014-04-27 23:34:39 -07:00
parent a5c3238876
commit c4c19017bb
17 changed files with 336 additions and 79 deletions

View File

@ -20,9 +20,11 @@
package com.sk89q.worldedit.bukkit;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.Vectors;
import com.sk89q.worldedit.world.World;
import org.bukkit.Bukkit;
import static com.google.common.base.Preconditions.checkNotNull;
@ -45,6 +47,26 @@ final class BukkitAdapter {
return new BukkitWorld(world);
}
/**
* Create a Bukkit world from a WorldEdit world.
*
* @param world the WorldEdit world
* @return a Bukkit world
*/
public static org.bukkit.World adapt(World world) {
checkNotNull(world);
if (world instanceof BukkitWorld) {
return ((BukkitWorld) world).getWorld();
} else {
org.bukkit.World match = Bukkit.getServer().getWorld(world.getName());
if (match != null) {
return match;
} else {
throw new IllegalArgumentException("Can't find a Bukkit world for " + world);
}
}
}
/**
* Create a WorldEdit location from a Bukkit location.
*
@ -58,4 +80,36 @@ final class BukkitAdapter {
return new com.sk89q.worldedit.util.Location(adapt(location.getWorld()), position, direction);
}
/**
* Create a Bukkit location from a WorldEdit location.
*
* @param location the WorldEdit location
* @return a Bukkit location
*/
public static org.bukkit.Location adapt(Location location) {
checkNotNull(location);
Vector position = location.toVector();
Vector direction = location.getDirection();
final double eyeX = direction.getX();
final double eyeZ = direction.getZ();
final float yaw = (float) Math.toDegrees(Math.atan2(-eyeX, eyeZ));
final double length = Math.sqrt(eyeX * eyeX + eyeZ * eyeZ);
final float pitch = (float) Math.toDegrees(Math.atan2(-direction.getY(), length));
return new org.bukkit.Location(
adapt(location.getWorld()),
position.getX(), position.getY(), position.getZ(),
yaw, pitch);
}
/**
* Create a WorldEdit entity from a Bukkit entity.
*
* @param entity the Bukkit entity
* @return a WorldEdit entity
*/
public static Entity adapt(org.bukkit.entity.Entity entity) {
checkNotNull(entity);
return new BukkitEntity(entity);
}
}

View File

@ -0,0 +1,44 @@
/*
* 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.bukkit;
import com.sk89q.worldedit.entity.AbstractBaseEntity;
import com.sk89q.worldedit.entity.BaseEntity;
import org.bukkit.entity.EntityType;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* An implementation of a {@link BaseEntity} for Bukkit.
*/
public class BukkitBaseEntity extends AbstractBaseEntity {
private final EntityType type;
public BukkitBaseEntity(EntityType type) {
checkNotNull(type);
this.type = type;
}
public EntityType getBukkitType() {
return type;
}
}

View File

@ -20,6 +20,7 @@
package com.sk89q.worldedit.bukkit;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.util.Location;
import org.bukkit.command.CommandSender;
@ -92,6 +93,11 @@ public class BukkitCommandSender extends LocalPlayer {
throw new PlayerNeededException();
}
@Override
public BaseEntity getState() {
throw new UnsupportedOperationException("Cannot create a state from this object");
}
@Override
public Location getLocation() {
throw new PlayerNeededException();

View File

@ -19,16 +19,20 @@
package com.sk89q.worldedit.bukkit;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.entity.metadata.Tameable;
import com.sk89q.worldedit.internal.util.AbstractAdapter;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.World;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* An adapter to adapt a Bukkit entity into a WorldEdit one.
*/
class BukkitEntity extends AbstractAdapter<org.bukkit.entity.Entity> implements Entity {
class BukkitEntity implements Entity {
private final org.bukkit.entity.Entity entity;
/**
* Create a new instance.
@ -36,13 +40,23 @@ class BukkitEntity extends AbstractAdapter<org.bukkit.entity.Entity> implements
* @param entity the entity
*/
BukkitEntity(org.bukkit.entity.Entity entity) {
super(entity);
checkNotNull(entity);
this.entity = entity;
}
/**
* Get the underlying Bukkit entity.
*
* @return the Bukkit entity
*/
protected org.bukkit.entity.Entity getEntity() {
return entity;
}
@SuppressWarnings("unchecked")
<T> T getMetaData(Class<T> metaDataClass) {
if (metaDataClass == Tameable.class && getHandle() instanceof org.bukkit.entity.Tameable) {
return (T) new TameableAdapter((org.bukkit.entity.Tameable) getHandle());
if (metaDataClass == Tameable.class && getEntity() instanceof org.bukkit.entity.Tameable) {
return (T) new TameableAdapter((org.bukkit.entity.Tameable) getEntity());
} else {
return null;
}
@ -50,11 +64,17 @@ class BukkitEntity extends AbstractAdapter<org.bukkit.entity.Entity> implements
@Override
public World getWorld() {
return BukkitAdapter.adapt(getHandle().getWorld());
return BukkitAdapter.adapt(getEntity().getWorld());
}
@Override
public Location getLocation() {
return BukkitAdapter.adapt(getHandle().getLocation());
return BukkitAdapter.adapt(getEntity().getLocation());
}
@Override
public BaseEntity getState() {
return new BukkitBaseEntity(getEntity().getType());
}
}

View File

@ -21,6 +21,7 @@ package com.sk89q.worldedit.bukkit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.util.Vectors;
import org.bukkit.GameMode;
import org.bukkit.Location;
@ -170,6 +171,11 @@ public class BukkitPlayer extends LocalPlayer {
player.setFlying(true);
}
@Override
public BaseEntity getState() {
throw new UnsupportedOperationException("Cannot create a state from this object");
}
@Override
public com.sk89q.worldedit.util.Location getLocation() {
Location nativeLocation = player.getLocation();

View File

@ -26,7 +26,7 @@ import com.sk89q.worldedit.blocks.*;
import com.sk89q.worldedit.blocks.ContainerBlock;
import com.sk89q.worldedit.blocks.NoteBlock;
import com.sk89q.worldedit.bukkit.entity.BukkitEntity;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.*;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.util.TreeGenerator;
import com.sk89q.worldedit.world.mapping.NullResolver;
@ -36,6 +36,7 @@ import org.bukkit.Location;
import org.bukkit.block.*;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.*;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.DoubleChestInventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
@ -171,6 +172,27 @@ public class BukkitWorld extends LocalWorld {
}
}
@Override
public List<com.sk89q.worldedit.entity.Entity> getEntities() {
List<com.sk89q.worldedit.entity.Entity> list = new ArrayList<com.sk89q.worldedit.entity.Entity>();
for (Entity entity : getWorld().getEntities()) {
list.add(BukkitAdapter.adapt(entity));
}
return list;
}
@Nullable
@Override
public com.sk89q.worldedit.entity.Entity createEntity(com.sk89q.worldedit.util.Location location, BaseEntity entity) {
if (entity instanceof BukkitBaseEntity) {
BukkitBaseEntity bukkitBaseEntity = (BukkitBaseEntity) entity;
Entity nativeEntity = getWorld().spawnEntity(BukkitAdapter.adapt(location), bukkitBaseEntity.getBukkitType());
return BukkitAdapter.adapt(nativeEntity);
} else {
return null;
}
}
private class NmsBlockClassLoader extends ClassLoader {
public File searchDir;
public NmsBlockClassLoader(ClassLoader parent, File searchDir) {

View File

@ -24,6 +24,7 @@ import com.sk89q.worldedit.LocalPlayer;
import com.sk89q.worldedit.ServerInterface;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldVector;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.internal.LocalWorldAdapter;
import com.sk89q.worldedit.internal.cui.CUIEvent;
@ -51,6 +52,11 @@ public class ForgePlayer extends LocalPlayer {
return this.player.username;
}
@Override
public BaseEntity getState() {
throw new UnsupportedOperationException("Cannot create a state from this object");
}
@Override
public Location getLocation() {
Vector position = new Vector(this.player.posX, this.player.posY, this.player.posZ);

View File

@ -26,6 +26,7 @@ import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.blocks.LazyBlock;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.TreeGenerator.TreeType;
import com.sk89q.worldedit.world.AbstractWorld;
import com.sk89q.worldedit.world.mapping.NullResolver;
@ -54,6 +55,7 @@ import net.minecraft.world.gen.ChunkProviderServer;
import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
@ -501,6 +503,17 @@ public class ForgeWorld extends AbstractWorld {
return null;
}
@Override
public List<com.sk89q.worldedit.entity.Entity> getEntities() {
return Collections.emptyList();
}
@Nullable
@Override
public com.sk89q.worldedit.entity.Entity createEntity(Location location, BaseEntity entity) {
return null;
}
/**
* Thrown when the reference to the world is lost.
*/

View File

@ -22,6 +22,8 @@ package com.sk89q.worldedit;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.blocks.BlockType;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.event.extent.EditSessionEvent;
import com.sk89q.worldedit.extent.ChangeSetExtent;
import com.sk89q.worldedit.extent.Extent;
@ -70,8 +72,7 @@ import com.sk89q.worldedit.regions.shape.ArbitraryBiomeShape;
import com.sk89q.worldedit.regions.shape.ArbitraryShape;
import com.sk89q.worldedit.regions.shape.RegionShape;
import com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment;
import com.sk89q.worldedit.util.Countable;
import com.sk89q.worldedit.util.TreeGenerator;
import com.sk89q.worldedit.util.*;
import com.sk89q.worldedit.util.collection.DoubleArrayList;
import com.sk89q.worldedit.util.eventbus.EventBus;
import com.sk89q.worldedit.world.NullWorld;
@ -572,6 +573,17 @@ public class EditSession implements Extent {
return getBlock(position).isAir() && setBlock(position, block);
}
@Override
public List<Entity> getEntities() {
return world.getEntities();
}
@Override
@Nullable
public Entity createEntity(com.sk89q.worldedit.util.Location location, BaseEntity entity) {
return world.createEntity(location, entity);
}
/**
* Insert a contrived block change into the history.
*

View File

@ -0,0 +1,51 @@
/*
* 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.entity;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.world.DataException;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* An abstract implementation of {@link BaseEntity} that implementations can
* subclass to simplify implementation.
*/
public abstract class AbstractBaseEntity implements BaseEntity {
private CompoundTag nbtData;
@Override
public boolean hasNbtData() {
return getNbtData() != null;
}
@Override
public CompoundTag getNbtData() {
return nbtData;
}
@Override
public void setNbtData(CompoundTag nbtData) throws DataException {
checkNotNull(nbtData);
this.nbtData = nbtData;
}
}

View File

@ -19,79 +19,11 @@
package com.sk89q.worldedit.entity;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.world.DataException;
import com.sk89q.worldedit.world.NbtValued;
import java.util.HashMap;
import java.util.Map;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* A snapshot of an entity that can be reused and passed around.
*/
public class BaseEntity implements NbtValued {
private CompoundTag nbtData;
/**
* Create a new entity with the given entity ID.
*
* @param id the ID of the entity, which determines its type
*/
public BaseEntity(String id) {
checkNotNull(id);
Map<String, Tag> map = new HashMap<String, Tag>();
map.put("id", new StringTag("id", id));
this.nbtData = new CompoundTag("", map);
}
/**
* Create a new entity with the given NBT data.
*
* @param nbtData the NBT data
*/
public BaseEntity(CompoundTag nbtData) {
checkNotNull(nbtData);
this.nbtData = nbtData;
}
/**
* Get the ID of the entity, which determines the type of entity.
* An example of an entity ID would be "CaveSpider".
*
* @return the entity ID, which may be an empty string
*/
public String getEntityId() {
CompoundTag nbtData = getNbtData();
if (nbtData == null) {
return "";
}
Tag idTag = nbtData.getValue().get("id");
if (idTag != null && idTag instanceof StringTag) {
return ((StringTag) idTag).getValue();
} else {
return "";
}
}
@Override
public boolean hasNbtData() {
return getNbtData() != null;
}
@Override
public CompoundTag getNbtData() {
return nbtData;
}
@Override
public void setNbtData(CompoundTag nbtData) throws DataException {
checkNotNull(nbtData);
this.nbtData = nbtData;
}
public interface BaseEntity extends NbtValued {
}

View File

@ -35,6 +35,13 @@ import com.sk89q.worldedit.world.World;
*/
public interface Entity {
/**
* Get a copy of the entity's state.
*
* @return the entity's state
*/
BaseEntity getState();
/**
* Get the location of this entity.
*

View File

@ -22,11 +22,16 @@ package com.sk89q.worldedit.extent;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.OperationQueue;
import com.sk89q.worldedit.util.Location;
import javax.annotation.Nullable;
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
/**
@ -70,6 +75,17 @@ public abstract class AbstractDelegateExtent implements Extent {
return extent.setBlock(location, block);
}
@Override
@Nullable
public Entity createEntity(Location location, BaseEntity entity) {
return extent.createEntity(location, entity);
}
@Override
public List<Entity> getEntities() {
return extent.getEntities();
}
@Override
public Vector getMinimumPoint() {
return extent.getMinimumPoint();

View File

@ -20,6 +20,12 @@
package com.sk89q.worldedit.extent;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.util.Location;
import javax.annotation.Nullable;
import java.util.List;
/**
* A world, portion of a world, clipboard, or other object that can have blocks
@ -50,4 +56,24 @@ public interface Extent extends InputExtent, OutputExtent {
*/
Vector getMaximumPoint();
/**
* Get a list of all entities.
* </p>
* If the extent is not wholly loaded (i.e. a world being simulated in the
* game will not have every chunk loaded), then this list may not be
* incomplete.
*
* @return a list of entities
*/
List<Entity> getEntities();
/**
* Create an entity at the given location.
*
* @param entity the entity
* @param location the location
* @return a reference to the created entity, or null if the entity could not be created
*/
@Nullable Entity createEntity(Location location, BaseEntity entity);
}

View File

@ -22,9 +22,14 @@ package com.sk89q.worldedit.extent;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.util.Location;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
/**
* An extent that returns air blocks for all blocks and does not
@ -44,6 +49,17 @@ public class NullExtent implements Extent {
return nullPoint;
}
@Override
public List<Entity> getEntities() {
return Collections.emptyList();
}
@Nullable
@Override
public Entity createEntity(Location location, BaseEntity entity) {
return null;
}
@Override
public BaseBlock getBlock(Vector position) {
return new BaseBlock(0);

View File

@ -32,6 +32,7 @@ import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.mapping.Resolver;
import javax.annotation.Nullable;
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
@ -339,6 +340,17 @@ public class LocalWorldAdapter extends LocalWorld {
return world.getMetaData(entity, metaDataClass);
}
@Override
@Nullable
public Entity createEntity(com.sk89q.worldedit.util.Location location, BaseEntity entity) {
return world.createEntity(location, entity);
}
@Override
public List<Entity> getEntities() {
return world.getEntities();
}
public static LocalWorldAdapter wrap(World world) {
return new LocalWorldAdapter(world);
}

View File

@ -26,11 +26,14 @@ import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.TreeGenerator.TreeType;
import com.sk89q.worldedit.world.mapping.NullResolver;
import com.sk89q.worldedit.world.mapping.Resolver;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
/**
* A null implementation of {@link World} that drops all changes and
@ -128,4 +131,15 @@ public class NullWorld extends AbstractWorld {
public <T> T getMetaData(BaseEntity entity, Class<T> metaDataClass) {
return null;
}
@Override
public List<Entity> getEntities() {
return Collections.emptyList();
}
@Nullable
@Override
public Entity createEntity(Location location, BaseEntity entity) {
return null;
}
}