Made BukkitEntity keep a weak ref to the entity and cleaned up code.

This commit is contained in:
sk89q
2014-07-18 11:43:18 -07:00
parent 781fc31d6f
commit 70f05c950a
6 changed files with 52 additions and 163 deletions

View File

@ -24,11 +24,12 @@ import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.entity.metadata.EntityType;
import com.sk89q.worldedit.entity.metadata.Tameable;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.NullWorld;
import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
import static com.google.common.base.Preconditions.checkNotNull;
@ -37,7 +38,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/
class BukkitEntity implements Entity {
private final org.bukkit.entity.Entity entity;
private final WeakReference<org.bukkit.entity.Entity> entityRef;
/**
* Create a new instance.
@ -46,46 +47,43 @@ class BukkitEntity implements Entity {
*/
BukkitEntity(org.bukkit.entity.Entity 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 && getEntity() instanceof org.bukkit.entity.Tameable) {
return (T) new TameableAdapter((org.bukkit.entity.Tameable) getEntity());
} else {
return null;
}
this.entityRef = new WeakReference<org.bukkit.entity.Entity>(entity);
}
@Override
public Extent getExtent() {
return BukkitAdapter.adapt(getEntity().getWorld());
org.bukkit.entity.Entity entity = entityRef.get();
if (entity != null) {
return BukkitAdapter.adapt(entity.getWorld());
} else {
return NullWorld.getInstance();
}
}
@Override
public Location getLocation() {
return BukkitAdapter.adapt(getEntity().getLocation());
org.bukkit.entity.Entity entity = entityRef.get();
if (entity != null) {
return BukkitAdapter.adapt(entity.getLocation());
} else {
return new Location(NullWorld.getInstance());
}
}
@Override
public BaseEntity getState() {
if (entity instanceof Player) {
return null;
}
org.bukkit.entity.Entity entity = entityRef.get();
if (entity != null) {
if (entity instanceof Player) {
return null;
}
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
if (adapter != null) {
return adapter.getEntity(entity);
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
if (adapter != null) {
return adapter.getEntity(entity);
} else {
return null;
}
} else {
return null;
}
@ -93,15 +91,21 @@ class BukkitEntity implements Entity {
@Override
public boolean remove() {
entity.remove();
return entity.isDead();
org.bukkit.entity.Entity entity = entityRef.get();
if (entity != null) {
entity.remove();
return entity.isDead();
} else {
return true;
}
}
@SuppressWarnings("unchecked")
@Nullable
@Override
public <T> T getFacet(Class<? extends T> cls) {
if (EntityType.class.isAssignableFrom(cls)) {
org.bukkit.entity.Entity entity = entityRef.get();
if (entity != null && EntityType.class.isAssignableFrom(cls)) {
return (T) new BukkitEntityType(entity);
} else {
return null;

View File

@ -1,40 +0,0 @@
/*
* 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.metadata.Tameable;
import com.sk89q.worldedit.internal.util.AbstractAdapter;
/**
* Adapts a Bukkit {@link org.bukkit.entity.Tameable} into a WorldEdit
* equivalent.
*/
public class TameableAdapter extends AbstractAdapter<org.bukkit.entity.Tameable> implements Tameable {
TameableAdapter(org.bukkit.entity.Tameable entity) {
super(entity);
}
@Override
public boolean isTamed() {
return getHandle().isTamed();
}
}