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) {