From d0575e3e22e6170bfd6c20ab65e41ada348138af Mon Sep 17 00:00:00 2001 From: fullwall Date: Mon, 12 Mar 2012 18:45:36 +0800 Subject: [PATCH] Make NPC handles accessible from nms Entity (increase speed of NPCManager.getNPC), make Text trait respect despawned state, despawn NPCs on death event --- src/main/java/net/citizensnpcs/Citizens.java | 4 ++-- src/main/java/net/citizensnpcs/EventListen.java | 9 +++++++++ .../java/net/citizensnpcs/npc/CitizensMobNPC.java | 5 +++-- .../net/citizensnpcs/npc/CitizensNPCManager.java | 11 +++++++++-- .../java/net/citizensnpcs/npc/ai/NPCHandle.java | 7 +++++++ .../citizensnpcs/npc/entity/CitizensBlazeNPC.java | 14 +++++++++++--- .../npc/entity/CitizensCaveSpiderNPC.java | 14 +++++++++++--- .../npc/entity/CitizensChickenNPC.java | 14 +++++++++++--- .../citizensnpcs/npc/entity/CitizensCowNPC.java | 14 +++++++++++--- .../npc/entity/CitizensCreeperNPC.java | 14 +++++++++++--- .../npc/entity/CitizensEnderDragonNPC.java | 14 +++++++++++--- .../npc/entity/CitizensEndermanNPC.java | 14 +++++++++++--- .../citizensnpcs/npc/entity/CitizensGhastNPC.java | 14 +++++++++++--- .../citizensnpcs/npc/entity/CitizensGiantNPC.java | 14 +++++++++++--- .../npc/entity/CitizensIronGolemNPC.java | 14 +++++++++++--- .../npc/entity/CitizensMagmaCubeNPC.java | 14 +++++++++++--- .../npc/entity/CitizensMushroomCowNPC.java | 14 +++++++++++--- .../citizensnpcs/npc/entity/CitizensOcelotNPC.java | 14 +++++++++++--- .../citizensnpcs/npc/entity/CitizensPigNPC.java | 14 +++++++++++--- .../npc/entity/CitizensPigZombieNPC.java | 14 +++++++++++--- .../citizensnpcs/npc/entity/CitizensSheepNPC.java | 14 +++++++++++--- .../npc/entity/CitizensSilverfishNPC.java | 14 +++++++++++--- .../npc/entity/CitizensSkeletonNPC.java | 14 +++++++++++--- .../citizensnpcs/npc/entity/CitizensSlimeNPC.java | 14 +++++++++++--- .../npc/entity/CitizensSnowmanNPC.java | 14 +++++++++++--- .../citizensnpcs/npc/entity/CitizensSpiderNPC.java | 14 +++++++++++--- .../citizensnpcs/npc/entity/CitizensSquidNPC.java | 14 +++++++++++--- .../npc/entity/CitizensVillagerNPC.java | 14 +++++++++++--- .../citizensnpcs/npc/entity/CitizensWolfNPC.java | 14 +++++++++++--- .../citizensnpcs/npc/entity/CitizensZombieNPC.java | 14 +++++++++++--- .../java/net/citizensnpcs/trait/text/Text.java | 2 ++ 31 files changed, 307 insertions(+), 81 deletions(-) create mode 100644 src/main/java/net/citizensnpcs/npc/ai/NPCHandle.java diff --git a/src/main/java/net/citizensnpcs/Citizens.java b/src/main/java/net/citizensnpcs/Citizens.java index 49c030d1d..28c1ab73a 100644 --- a/src/main/java/net/citizensnpcs/Citizens.java +++ b/src/main/java/net/citizensnpcs/Citizens.java @@ -249,8 +249,8 @@ public class Citizens extends JavaPlugin { if (!key.keyExists("name")) throw new NPCLoadException("Could not find a name for the NPC with ID '" + id + "'."); - NPC npc = npcManager.createNPC(EntityType.valueOf(key.getString("traits.type").toUpperCase()), id, key - .getString("name"), null); + NPC npc = npcManager.createNPC(EntityType.valueOf(key.getString("traits.type").toUpperCase()), id, + key.getString("name"), null); try { ((CitizensNPC) npc).load(key); } catch (NPCException ex) { diff --git a/src/main/java/net/citizensnpcs/EventListen.java b/src/main/java/net/citizensnpcs/EventListen.java index 21f389d53..ea215e30e 100644 --- a/src/main/java/net/citizensnpcs/EventListen.java +++ b/src/main/java/net/citizensnpcs/EventListen.java @@ -22,6 +22,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.event.entity.EntityTargetEvent; import org.bukkit.event.entity.EntityTargetEvent.TargetReason; import org.bukkit.event.player.PlayerChangedWorldEvent; @@ -138,6 +139,14 @@ public class EventListen implements Listener { npc.getCharacter().onRightClick(npc, player); } + @EventHandler + public void onEntityDeath(EntityDeathEvent event) { + if (!npcManager.isNPC(event.getEntity())) + return; + NPC npc = npcManager.getNPC(event.getEntity()); + npc.despawn(); + } + /* * Player events */ diff --git a/src/main/java/net/citizensnpcs/npc/CitizensMobNPC.java b/src/main/java/net/citizensnpcs/npc/CitizensMobNPC.java index 876ce9be1..1b7a9b641 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensMobNPC.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensMobNPC.java @@ -4,6 +4,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.util.Map; +import net.citizensnpcs.api.npc.NPC; import net.minecraft.server.Entity; import net.minecraft.server.EntityLiving; import net.minecraft.server.EntityTypes; @@ -19,7 +20,7 @@ public abstract class CitizensMobNPC extends CitizensNPC { protected CitizensMobNPC(CitizensNPCManager manager, int id, String name, Class clazz) { super(manager, id, name); try { - this.constructor = clazz.getConstructor(World.class); + this.constructor = clazz.getConstructor(World.class, NPC.class); } catch (Exception ex) { throw new IllegalStateException("unable to find an entity constructor"); } @@ -29,7 +30,7 @@ public abstract class CitizensMobNPC extends CitizensNPC { private EntityLiving createEntityFromClass(World world) { try { - return constructor.newInstance(world); + return constructor.newInstance(world, this); } catch (Exception ex) { ex.printStackTrace(); return null; diff --git a/src/main/java/net/citizensnpcs/npc/CitizensNPCManager.java b/src/main/java/net/citizensnpcs/npc/CitizensNPCManager.java index 6fad9a1f4..fc7874a77 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensNPCManager.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensNPCManager.java @@ -13,10 +13,12 @@ import net.citizensnpcs.api.npc.NPCManager; import net.citizensnpcs.api.npc.character.Character; import net.citizensnpcs.api.util.Storage; import net.citizensnpcs.editor.Editor; +import net.citizensnpcs.npc.ai.NPCHandle; import net.citizensnpcs.util.ByIdArray; import net.citizensnpcs.util.NPCBuilder; import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.entity.CraftEntity; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; @@ -66,9 +68,14 @@ public class CitizensNPCManager implements NPCManager { @Override public NPC getNPC(Entity entity) { - for (NPC npc : npcs) - if (npc.isSpawned() && npc.getBukkitEntity().getEntityId() == entity.getEntityId()) + net.minecraft.server.Entity handle = ((CraftEntity) entity).getHandle(); + if (handle instanceof NPCHandle) + return ((NPCHandle) handle).getNPC(); + for (NPC npc : npcs) { // fall back to linear search + if (npc.isSpawned() && npc.getBukkitEntity().getEntityId() == entity.getEntityId()) { return npc; + } + } return null; } diff --git a/src/main/java/net/citizensnpcs/npc/ai/NPCHandle.java b/src/main/java/net/citizensnpcs/npc/ai/NPCHandle.java new file mode 100644 index 000000000..183b1078a --- /dev/null +++ b/src/main/java/net/citizensnpcs/npc/ai/NPCHandle.java @@ -0,0 +1,7 @@ +package net.citizensnpcs.npc.ai; + +import net.citizensnpcs.api.npc.NPC; + +public interface NPCHandle { + public NPC getNPC(); +} diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensBlazeNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensBlazeNPC.java index 0e3bc91ac..e34939995 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensBlazeNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensBlazeNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntityBlaze; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensBlazeNPC extends CitizensMobNPC { return (Blaze) getHandle().getBukkitEntity(); } - public static class EntityBlazeNPC extends EntityBlaze { + public static class EntityBlazeNPC extends EntityBlaze implements NPCHandle { + private final NPC npc; - public EntityBlazeNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityBlazeNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensCaveSpiderNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensCaveSpiderNPC.java index f156a03b3..9fd3f5026 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensCaveSpiderNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensCaveSpiderNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntityCaveSpider; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensCaveSpiderNPC extends CitizensMobNPC { return (CaveSpider) getHandle().getBukkitEntity(); } - public static class EntityCaveSpiderNPC extends EntityCaveSpider { + public static class EntityCaveSpiderNPC extends EntityCaveSpider implements NPCHandle { + private final NPC npc; - public EntityCaveSpiderNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityCaveSpiderNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensChickenNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensChickenNPC.java index 9684a7dd9..f25b91029 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensChickenNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensChickenNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntityChicken; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensChickenNPC extends CitizensMobNPC { return (Chicken) getHandle().getBukkitEntity(); } - public static class EntityChickenNPC extends EntityChicken { + public static class EntityChickenNPC extends EntityChicken implements NPCHandle { + private final NPC npc; - public EntityChickenNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityChickenNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensCowNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensCowNPC.java index f3c2c7d88..4d0da468b 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensCowNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensCowNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntityCow; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensCowNPC extends CitizensMobNPC { return (Cow) getHandle().getBukkitEntity(); } - public static class EntityCowNPC extends EntityCow { + public static class EntityCowNPC extends EntityCow implements NPCHandle { + private final NPC npc; - public EntityCowNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityCowNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensCreeperNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensCreeperNPC.java index 0f85712a8..16db04915 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensCreeperNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensCreeperNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntityCreeper; import net.minecraft.server.EntityWeatherLighting; import net.minecraft.server.PathfinderGoalSelector; @@ -21,10 +22,17 @@ public class CitizensCreeperNPC extends CitizensMobNPC { return (Creeper) getHandle().getBukkitEntity(); } - public static class EntityCreeperNPC extends EntityCreeper { + public static class EntityCreeperNPC extends EntityCreeper implements NPCHandle { + private final NPC npc; - public EntityCreeperNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityCreeperNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensEnderDragonNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensEnderDragonNPC.java index 7cfcd7bff..7955267d6 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensEnderDragonNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensEnderDragonNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntityEnderDragon; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensEnderDragonNPC extends CitizensMobNPC { return (EnderDragon) getHandle().getBukkitEntity(); } - public static class EntityEnderDragonNPC extends EntityEnderDragon { + public static class EntityEnderDragonNPC extends EntityEnderDragon implements NPCHandle { + private final NPC npc; - public EntityEnderDragonNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityEnderDragonNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensEndermanNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensEndermanNPC.java index fcc5d11a9..3277cb039 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensEndermanNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensEndermanNPC.java @@ -1,11 +1,12 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.trait.trait.Equipment; import net.citizensnpcs.editor.Equipable; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; +import net.citizensnpcs.npc.ai.NPCHandle; import net.citizensnpcs.util.Messaging; - import net.minecraft.server.EntityEnderman; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -58,10 +59,17 @@ public class CitizensEndermanNPC extends CitizensMobNPC implements Equipable { getTrait(Equipment.class).set(0, set); } - public static class EntityEndermanNPC extends EntityEnderman { + public static class EntityEndermanNPC extends EntityEnderman implements NPCHandle { + private final NPC npc; - public EntityEndermanNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityEndermanNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensGhastNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensGhastNPC.java index 57261fed6..d19fc4967 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensGhastNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensGhastNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntityGhast; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensGhastNPC extends CitizensMobNPC { return (Ghast) getHandle().getBukkitEntity(); } - public static class EntityGhastNPC extends EntityGhast { + public static class EntityGhastNPC extends EntityGhast implements NPCHandle { + private final NPC npc; - public EntityGhastNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityGhastNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensGiantNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensGiantNPC.java index 21243e0ba..32e2363bd 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensGiantNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensGiantNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntityGiantZombie; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensGiantNPC extends CitizensMobNPC { return (Giant) getHandle().getBukkitEntity(); } - public static class EntityGiantNPC extends EntityGiantZombie { + public static class EntityGiantNPC extends EntityGiantZombie implements NPCHandle { + private final NPC npc; - public EntityGiantNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityGiantNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensIronGolemNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensIronGolemNPC.java index 8e8158126..1c6738b79 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensIronGolemNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensIronGolemNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntityIronGolem; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensIronGolemNPC extends CitizensMobNPC { return (IronGolem) getHandle().getBukkitEntity(); } - public static class EntityIronGolemNPC extends EntityIronGolem { + public static class EntityIronGolemNPC extends EntityIronGolem implements NPCHandle { + private final NPC npc; - public EntityIronGolemNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityIronGolemNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensMagmaCubeNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensMagmaCubeNPC.java index b40e6b2be..8d774adc8 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensMagmaCubeNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensMagmaCubeNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntityMagmaCube; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensMagmaCubeNPC extends CitizensMobNPC { return (MagmaCube) getHandle().getBukkitEntity(); } - public static class EntityMagmaCubeNPC extends EntityMagmaCube { + public static class EntityMagmaCubeNPC extends EntityMagmaCube implements NPCHandle { + private final NPC npc; - public EntityMagmaCubeNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityMagmaCubeNPC(World world, NPC npc) { super(world); + this.npc = npc; setSize(3); goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensMushroomCowNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensMushroomCowNPC.java index 8b0e04b2d..a8eb1a413 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensMushroomCowNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensMushroomCowNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntityMushroomCow; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensMushroomCowNPC extends CitizensMobNPC { return (MushroomCow) getHandle().getBukkitEntity(); } - public static class EntityMushroomCowNPC extends EntityMushroomCow { + public static class EntityMushroomCowNPC extends EntityMushroomCow implements NPCHandle { + private final NPC npc; - public EntityMushroomCowNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityMushroomCowNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensOcelotNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensOcelotNPC.java index ee500cbfd..e6e2ae73b 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensOcelotNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensOcelotNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntityOcelot; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensOcelotNPC extends CitizensMobNPC { return (Ocelot) getHandle().getBukkitEntity(); } - public static class EntityOcelotNPC extends EntityOcelot { + public static class EntityOcelotNPC extends EntityOcelot implements NPCHandle { + private final NPC npc; - public EntityOcelotNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityOcelotNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensPigNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensPigNPC.java index bd064a889..9d7ffdf8e 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensPigNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensPigNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntityPig; import net.minecraft.server.EntityWeatherLighting; import net.minecraft.server.PathfinderGoalSelector; @@ -21,10 +22,17 @@ public class CitizensPigNPC extends CitizensMobNPC { return (Pig) getHandle().getBukkitEntity(); } - public static class EntityPigNPC extends EntityPig { + public static class EntityPigNPC extends EntityPig implements NPCHandle { + private final NPC npc; - public EntityPigNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityPigNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensPigZombieNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensPigZombieNPC.java index 59a9f7261..bdbdcb589 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensPigZombieNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensPigZombieNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntityPigZombie; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensPigZombieNPC extends CitizensMobNPC { return (PigZombie) getHandle().getBukkitEntity(); } - public static class EntityPigZombieNPC extends EntityPigZombie { + public static class EntityPigZombieNPC extends EntityPigZombie implements NPCHandle { + private final NPC npc; - public EntityPigZombieNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityPigZombieNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensSheepNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensSheepNPC.java index b819fe563..13f6e9002 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensSheepNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensSheepNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntitySheep; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensSheepNPC extends CitizensMobNPC { return (Sheep) getHandle().getBukkitEntity(); } - public static class EntitySheepNPC extends EntitySheep { + public static class EntitySheepNPC extends EntitySheep implements NPCHandle { + private final NPC npc; - public EntitySheepNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntitySheepNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensSilverfishNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensSilverfishNPC.java index 3e843c0a1..1b45e7dce 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensSilverfishNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensSilverfishNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntitySilverfish; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensSilverfishNPC extends CitizensMobNPC { return (Silverfish) getHandle().getBukkitEntity(); } - public static class EntitySilverfishNPC extends EntitySilverfish { + public static class EntitySilverfishNPC extends EntitySilverfish implements NPCHandle { + private final NPC npc; - public EntitySilverfishNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntitySilverfishNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensSkeletonNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensSkeletonNPC.java index 3d7923923..ea2414bdd 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensSkeletonNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensSkeletonNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntitySkeleton; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensSkeletonNPC extends CitizensMobNPC { return (Skeleton) getHandle().getBukkitEntity(); } - public static class EntitySkeletonNPC extends EntitySkeleton { + public static class EntitySkeletonNPC extends EntitySkeleton implements NPCHandle { + private final NPC npc; - public EntitySkeletonNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntitySkeletonNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensSlimeNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensSlimeNPC.java index 50f085cd8..4561b66ca 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensSlimeNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensSlimeNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntitySlime; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensSlimeNPC extends CitizensMobNPC { return (Slime) getHandle().getBukkitEntity(); } - public static class EntitySlimeNPC extends EntitySlime { + public static class EntitySlimeNPC extends EntitySlime implements NPCHandle { + private final NPC npc; - public EntitySlimeNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntitySlimeNPC(World world, NPC npc) { super(world); + this.npc = npc; setSize(3); goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensSnowmanNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensSnowmanNPC.java index 53a6e8823..1823b7ae3 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensSnowmanNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensSnowmanNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntitySnowman; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensSnowmanNPC extends CitizensMobNPC { return (Snowman) getHandle().getBukkitEntity(); } - public static class EntitySnowmanNPC extends EntitySnowman { + public static class EntitySnowmanNPC extends EntitySnowman implements NPCHandle { + private final NPC npc; - public EntitySnowmanNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntitySnowmanNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensSpiderNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensSpiderNPC.java index 035ae034e..3af73739d 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensSpiderNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensSpiderNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntitySpider; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensSpiderNPC extends CitizensMobNPC { return (Spider) getHandle().getBukkitEntity(); } - public static class EntitySpiderNPC extends EntitySpider { + public static class EntitySpiderNPC extends EntitySpider implements NPCHandle { + private final NPC npc; - public EntitySpiderNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntitySpiderNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensSquidNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensSquidNPC.java index d77a7e276..b3c44c535 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensSquidNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensSquidNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntitySquid; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensSquidNPC extends CitizensMobNPC { return (Squid) getHandle().getBukkitEntity(); } - public static class EntitySquidNPC extends EntitySquid { + public static class EntitySquidNPC extends EntitySquid implements NPCHandle { + private final NPC npc; - public EntitySquidNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntitySquidNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensVillagerNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensVillagerNPC.java index 152cbf878..be7cea715 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensVillagerNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensVillagerNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntityVillager; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensVillagerNPC extends CitizensMobNPC { return (Villager) getHandle().getBukkitEntity(); } - public static class EntityVillagerNPC extends EntityVillager { + public static class EntityVillagerNPC extends EntityVillager implements NPCHandle { + private final NPC npc; - public EntityVillagerNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityVillagerNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensWolfNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensWolfNPC.java index 141a93bb3..acc467346 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensWolfNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensWolfNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntityWolf; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensWolfNPC extends CitizensMobNPC { return (Wolf) getHandle().getBukkitEntity(); } - public static class EntityWolfNPC extends EntityWolf { + public static class EntityWolfNPC extends EntityWolf implements NPCHandle { + private final NPC npc; - public EntityWolfNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityWolfNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensZombieNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensZombieNPC.java index 43ac3beea..a8407a648 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensZombieNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensZombieNPC.java @@ -1,8 +1,9 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; - +import net.citizensnpcs.npc.ai.NPCHandle; import net.minecraft.server.EntityZombie; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; @@ -20,10 +21,17 @@ public class CitizensZombieNPC extends CitizensMobNPC { return (Zombie) getHandle().getBukkitEntity(); } - public static class EntityZombieNPC extends EntityZombie { + public static class EntityZombieNPC extends EntityZombie implements NPCHandle { + private final NPC npc; - public EntityZombieNPC(World world) { + @Override + public NPC getNPC() { + return this.npc; + } + + public EntityZombieNPC(World world, NPC npc) { super(world); + this.npc = npc; goalSelector = new PathfinderGoalSelector(); targetSelector = new PathfinderGoalSelector(); } diff --git a/src/main/java/net/citizensnpcs/trait/text/Text.java b/src/main/java/net/citizensnpcs/trait/text/Text.java index ef867afc2..79e839ea2 100644 --- a/src/main/java/net/citizensnpcs/trait/text/Text.java +++ b/src/main/java/net/citizensnpcs/trait/text/Text.java @@ -70,6 +70,8 @@ public class Text extends Trait implements Runnable, Toggleable, ConversationAba @Override public void run() { + if (!npc.isSpawned()) + return; EntityHuman search = null; EntityLiving handle = ((CitizensNPC) npc).getHandle(); if ((search = handle.world.findNearbyPlayer(handle, 5)) != null && talkClose) {