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

Dieser Commit ist enthalten in:
sk89q 2014-07-18 11:43:18 -07:00
Ursprung 781fc31d6f
Commit 70f05c950a
6 geänderte Dateien mit 52 neuen und 163 gelöschten Zeilen

Datei anzeigen

@ -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;

Datei anzeigen

@ -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();
}
}

Datei anzeigen

@ -190,7 +190,7 @@ public class EditSession implements Extent {
this.bypassNone = extent;
} else {
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 = reorderExtent = new MultiStageReorder(extent, false);
extent = maskingExtent = new MaskingExtent(extent, Masks.alwaysTrue());

Datei anzeigen

@ -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();
}

Datei anzeigen

@ -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();
}

Datei anzeigen

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