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.Entity;
import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.entity.metadata.EntityType; import com.sk89q.worldedit.entity.metadata.EntityType;
import com.sk89q.worldedit.entity.metadata.Tameable;
import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.NullWorld;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
@ -37,7 +38,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/ */
class BukkitEntity implements Entity { class BukkitEntity implements Entity {
private final org.bukkit.entity.Entity entity; private final WeakReference<org.bukkit.entity.Entity> entityRef;
/** /**
* Create a new instance. * Create a new instance.
@ -46,39 +47,33 @@ class BukkitEntity implements Entity {
*/ */
BukkitEntity(org.bukkit.entity.Entity entity) { BukkitEntity(org.bukkit.entity.Entity entity) {
checkNotNull(entity); checkNotNull(entity);
this.entity = entity; this.entityRef = new WeakReference<org.bukkit.entity.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;
}
} }
@Override @Override
public Extent getExtent() { 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 @Override
public Location getLocation() { 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 @Override
public BaseEntity getState() { public BaseEntity getState() {
org.bukkit.entity.Entity entity = entityRef.get();
if (entity != null) {
if (entity instanceof Player) { if (entity instanceof Player) {
return null; return null;
} }
@ -89,19 +84,28 @@ class BukkitEntity implements Entity {
} else { } else {
return null; return null;
} }
} else {
return null;
}
} }
@Override @Override
public boolean remove() { public boolean remove() {
org.bukkit.entity.Entity entity = entityRef.get();
if (entity != null) {
entity.remove(); entity.remove();
return entity.isDead(); return entity.isDead();
} else {
return true;
}
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Nullable @Nullable
@Override @Override
public <T> T getFacet(Class<? extends T> cls) { 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); return (T) new BukkitEntityType(entity);
} else { } else {
return null; 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();
}
}

View File

@ -190,7 +190,7 @@ public class EditSession implements Extent {
this.bypassNone = extent; this.bypassNone = extent;
} else { } else {
Extent extent = new NullExtent(); Extent extent = new NullExtent();
extent = survivalExtent = new SurvivalModeExtent(extent, new NullWorld()); extent = survivalExtent = new SurvivalModeExtent(extent, NullWorld.getInstance());
extent = blockBagExtent = new BlockBagExtent(extent, blockBag); extent = blockBagExtent = new BlockBagExtent(extent, blockBag);
extent = reorderExtent = new MultiStageReorder(extent, false); extent = reorderExtent = new MultiStageReorder(extent, false);
extent = maskingExtent = new MaskingExtent(extent, Masks.alwaysTrue()); extent = maskingExtent = new MaskingExtent(extent, Masks.alwaysTrue());

View File

@ -1,56 +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.entity.metadata;
/**
* Describes a creature.
*/
public interface Creature {
/**
* Returns whether the creature is a non-player character, such as
* a NPC human or a villager.
*
* @return true if the creature is an NPC
*/
boolean isNpc();
/**
* Returns whether the creature can be tamed.
*
* @return true if the creature can be tamed
*/
boolean isTameable();
/**
* Returns whether the creature is hostile.
*
* @return true if the creature is hostile
*/
boolean isHostile();
/**
* Returns whether the creature is passive.
*
* @return true if the creature is passive
*/
boolean isPassive();
}

View File

@ -1,34 +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.entity.metadata;
/**
* Indicates a creature that can be tamed.
*/
public interface Tameable {
/**
* Returns whether the creature is tamed.
*
* @return true if the creature is tamed
*/
boolean isTamed();
}

View File

@ -46,6 +46,11 @@ import java.util.List;
*/ */
public class NullWorld extends AbstractWorld { public class NullWorld extends AbstractWorld {
private static final NullWorld INSTANCE = new NullWorld();
protected NullWorld() {
}
@Override @Override
public String getName() { public String getName() {
return "null"; return "null";
@ -120,4 +125,14 @@ public class NullWorld extends AbstractWorld {
public Entity createEntity(Location location, BaseEntity entity) { public Entity createEntity(Location location, BaseEntity entity) {
return null; return null;
} }
/**
* Return an instance of this null world.
*
* @return a null world
*/
public static NullWorld getInstance() {
return INSTANCE;
}
} }