From c4c19017bb65c906612e4370a8fb5a6859862639 Mon Sep 17 00:00:00 2001
From: sk89q
Date: Sun, 27 Apr 2014 23:34:39 -0700
Subject: [PATCH] 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.
---
.../sk89q/worldedit/bukkit/BukkitAdapter.java | 54 ++++++++++++++
.../worldedit/bukkit/BukkitBaseEntity.java | 44 ++++++++++++
.../worldedit/bukkit/BukkitCommandSender.java | 6 ++
.../sk89q/worldedit/bukkit/BukkitEntity.java | 34 +++++++--
.../sk89q/worldedit/bukkit/BukkitPlayer.java | 6 ++
.../sk89q/worldedit/bukkit/BukkitWorld.java | 24 ++++++-
.../sk89q/worldedit/forge/ForgePlayer.java | 6 ++
.../com/sk89q/worldedit/forge/ForgeWorld.java | 13 ++++
.../java/com/sk89q/worldedit/EditSession.java | 16 ++++-
.../worldedit/entity/AbstractBaseEntity.java | 51 ++++++++++++++
.../sk89q/worldedit/entity/BaseEntity.java | 70 +------------------
.../com/sk89q/worldedit/entity/Entity.java | 7 ++
.../extent/AbstractDelegateExtent.java | 16 +++++
.../com/sk89q/worldedit/extent/Extent.java | 26 +++++++
.../sk89q/worldedit/extent/NullExtent.java | 16 +++++
.../worldedit/internal/LocalWorldAdapter.java | 12 ++++
.../com/sk89q/worldedit/world/NullWorld.java | 14 ++++
17 files changed, 336 insertions(+), 79 deletions(-)
create mode 100644 src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitBaseEntity.java
create mode 100644 src/main/java/com/sk89q/worldedit/entity/AbstractBaseEntity.java
diff --git a/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitAdapter.java b/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitAdapter.java
index 42b3eecbf..6ba9d5eaf 100644
--- a/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitAdapter.java
+++ b/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitAdapter.java
@@ -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);
+ }
+
}
diff --git a/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitBaseEntity.java b/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitBaseEntity.java
new file mode 100644
index 000000000..25fe7c4f4
--- /dev/null
+++ b/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitBaseEntity.java
@@ -0,0 +1,44 @@
+/*
+ * 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.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;
+ }
+
+}
diff --git a/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitCommandSender.java b/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitCommandSender.java
index e2067fd90..842eb4c55 100644
--- a/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitCommandSender.java
+++ b/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitCommandSender.java
@@ -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();
diff --git a/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitEntity.java b/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitEntity.java
index a2b749248..247135b47 100644
--- a/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitEntity.java
+++ b/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitEntity.java
@@ -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 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 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 getMetaData(Class 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 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());
+ }
+
}
diff --git a/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java b/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java
index 1499cd09f..7d3dbdeeb 100644
--- a/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java
+++ b/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java
@@ -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();
diff --git a/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitWorld.java b/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitWorld.java
index d8ac86841..7aab7e8fd 100644
--- a/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitWorld.java
+++ b/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitWorld.java
@@ -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 getEntities() {
+ List list = new ArrayList();
+ 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) {
diff --git a/src/forge/java/com/sk89q/worldedit/forge/ForgePlayer.java b/src/forge/java/com/sk89q/worldedit/forge/ForgePlayer.java
index 01e178f71..20ca43ad3 100644
--- a/src/forge/java/com/sk89q/worldedit/forge/ForgePlayer.java
+++ b/src/forge/java/com/sk89q/worldedit/forge/ForgePlayer.java
@@ -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);
diff --git a/src/forge/java/com/sk89q/worldedit/forge/ForgeWorld.java b/src/forge/java/com/sk89q/worldedit/forge/ForgeWorld.java
index 2b3a91eb6..ebb90d33d 100644
--- a/src/forge/java/com/sk89q/worldedit/forge/ForgeWorld.java
+++ b/src/forge/java/com/sk89q/worldedit/forge/ForgeWorld.java
@@ -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 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.
*/
diff --git a/src/main/java/com/sk89q/worldedit/EditSession.java b/src/main/java/com/sk89q/worldedit/EditSession.java
index 487395cd1..d246b3893 100644
--- a/src/main/java/com/sk89q/worldedit/EditSession.java
+++ b/src/main/java/com/sk89q/worldedit/EditSession.java
@@ -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 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.
*
diff --git a/src/main/java/com/sk89q/worldedit/entity/AbstractBaseEntity.java b/src/main/java/com/sk89q/worldedit/entity/AbstractBaseEntity.java
new file mode 100644
index 000000000..c1c4bca1b
--- /dev/null
+++ b/src/main/java/com/sk89q/worldedit/entity/AbstractBaseEntity.java
@@ -0,0 +1,51 @@
+/*
+ * 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.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;
+ }
+
+}
diff --git a/src/main/java/com/sk89q/worldedit/entity/BaseEntity.java b/src/main/java/com/sk89q/worldedit/entity/BaseEntity.java
index 2cb248a69..2b1fb2193 100644
--- a/src/main/java/com/sk89q/worldedit/entity/BaseEntity.java
+++ b/src/main/java/com/sk89q/worldedit/entity/BaseEntity.java
@@ -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 map = new HashMap();
- 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 {
}
diff --git a/src/main/java/com/sk89q/worldedit/entity/Entity.java b/src/main/java/com/sk89q/worldedit/entity/Entity.java
index 8f3c4be73..88473d6f8 100644
--- a/src/main/java/com/sk89q/worldedit/entity/Entity.java
+++ b/src/main/java/com/sk89q/worldedit/entity/Entity.java
@@ -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.
*
diff --git a/src/main/java/com/sk89q/worldedit/extent/AbstractDelegateExtent.java b/src/main/java/com/sk89q/worldedit/extent/AbstractDelegateExtent.java
index b9c4c1158..666c33872 100644
--- a/src/main/java/com/sk89q/worldedit/extent/AbstractDelegateExtent.java
+++ b/src/main/java/com/sk89q/worldedit/extent/AbstractDelegateExtent.java
@@ -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 getEntities() {
+ return extent.getEntities();
+ }
+
@Override
public Vector getMinimumPoint() {
return extent.getMinimumPoint();
diff --git a/src/main/java/com/sk89q/worldedit/extent/Extent.java b/src/main/java/com/sk89q/worldedit/extent/Extent.java
index 34d86d298..14bacb425 100644
--- a/src/main/java/com/sk89q/worldedit/extent/Extent.java
+++ b/src/main/java/com/sk89q/worldedit/extent/Extent.java
@@ -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.
+ *
+ * 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 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);
+
}
diff --git a/src/main/java/com/sk89q/worldedit/extent/NullExtent.java b/src/main/java/com/sk89q/worldedit/extent/NullExtent.java
index f291bc2f2..5f8f84644 100644
--- a/src/main/java/com/sk89q/worldedit/extent/NullExtent.java
+++ b/src/main/java/com/sk89q/worldedit/extent/NullExtent.java
@@ -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 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);
diff --git a/src/main/java/com/sk89q/worldedit/internal/LocalWorldAdapter.java b/src/main/java/com/sk89q/worldedit/internal/LocalWorldAdapter.java
index 9dc583878..9ed250c7f 100644
--- a/src/main/java/com/sk89q/worldedit/internal/LocalWorldAdapter.java
+++ b/src/main/java/com/sk89q/worldedit/internal/LocalWorldAdapter.java
@@ -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 getEntities() {
+ return world.getEntities();
+ }
+
public static LocalWorldAdapter wrap(World world) {
return new LocalWorldAdapter(world);
}
diff --git a/src/main/java/com/sk89q/worldedit/world/NullWorld.java b/src/main/java/com/sk89q/worldedit/world/NullWorld.java
index ba7b9d0d7..35cc1a863 100644
--- a/src/main/java/com/sk89q/worldedit/world/NullWorld.java
+++ b/src/main/java/com/sk89q/worldedit/world/NullWorld.java
@@ -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 getMetaData(BaseEntity entity, Class metaDataClass) {
return null;
}
+
+ @Override
+ public List getEntities() {
+ return Collections.emptyList();
+ }
+
+ @Nullable
+ @Override
+ public Entity createEntity(Location location, BaseEntity entity) {
+ return null;
+ }
}