From 70f05c950a023d1148e8c9586fdfc7c167a28c6d Mon Sep 17 00:00:00 2001 From: sk89q Date: Fri, 18 Jul 2014 11:43:18 -0700 Subject: [PATCH] Made BukkitEntity keep a weak ref to the entity and cleaned up code. --- .../sk89q/worldedit/bukkit/BukkitEntity.java | 68 ++++++++++--------- .../worldedit/bukkit/TameableAdapter.java | 40 ----------- .../java/com/sk89q/worldedit/EditSession.java | 2 +- .../worldedit/entity/metadata/Creature.java | 56 --------------- .../worldedit/entity/metadata/Tameable.java | 34 ---------- .../com/sk89q/worldedit/world/NullWorld.java | 15 ++++ 6 files changed, 52 insertions(+), 163 deletions(-) delete mode 100644 src/bukkit/java/com/sk89q/worldedit/bukkit/TameableAdapter.java delete mode 100644 src/main/java/com/sk89q/worldedit/entity/metadata/Creature.java delete mode 100644 src/main/java/com/sk89q/worldedit/entity/metadata/Tameable.java diff --git a/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitEntity.java b/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitEntity.java index 483b740eb..cfc49b01b 100644 --- a/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitEntity.java +++ b/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitEntity.java @@ -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 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 getMetaData(Class 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(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 getFacet(Class 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; diff --git a/src/bukkit/java/com/sk89q/worldedit/bukkit/TameableAdapter.java b/src/bukkit/java/com/sk89q/worldedit/bukkit/TameableAdapter.java deleted file mode 100644 index b514f1b29..000000000 --- a/src/bukkit/java/com/sk89q/worldedit/bukkit/TameableAdapter.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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.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 implements Tameable { - - TameableAdapter(org.bukkit.entity.Tameable entity) { - super(entity); - } - - @Override - public boolean isTamed() { - return getHandle().isTamed(); - } - -} diff --git a/src/main/java/com/sk89q/worldedit/EditSession.java b/src/main/java/com/sk89q/worldedit/EditSession.java index f97f002ea..e946f11d3 100644 --- a/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/src/main/java/com/sk89q/worldedit/EditSession.java @@ -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()); diff --git a/src/main/java/com/sk89q/worldedit/entity/metadata/Creature.java b/src/main/java/com/sk89q/worldedit/entity/metadata/Creature.java deleted file mode 100644 index 66ca80b17..000000000 --- a/src/main/java/com/sk89q/worldedit/entity/metadata/Creature.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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.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(); - -} diff --git a/src/main/java/com/sk89q/worldedit/entity/metadata/Tameable.java b/src/main/java/com/sk89q/worldedit/entity/metadata/Tameable.java deleted file mode 100644 index 6f288a00f..000000000 --- a/src/main/java/com/sk89q/worldedit/entity/metadata/Tameable.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.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(); - -} diff --git a/src/main/java/com/sk89q/worldedit/world/NullWorld.java b/src/main/java/com/sk89q/worldedit/world/NullWorld.java index cfb141a42..1edfaadb3 100644 --- a/src/main/java/com/sk89q/worldedit/world/NullWorld.java +++ b/src/main/java/com/sk89q/worldedit/world/NullWorld.java @@ -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; + } + }