Slightly hackish workaround for mob NPC and spawn egg bug. This fixes CITIZENS-59.

This commit is contained in:
aPunch 2012-05-03 16:38:23 -05:00
parent affb84f6cd
commit a9105c624e
22 changed files with 203 additions and 107 deletions

View File

@ -2,8 +2,10 @@ package net.citizensnpcs.npc;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.citizensnpcs.Citizens;
import net.citizensnpcs.api.event.NPCSelectEvent;
@ -13,8 +15,33 @@ 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.npc.entity.CitizensBlazeNPC;
import net.citizensnpcs.npc.entity.CitizensCaveSpiderNPC;
import net.citizensnpcs.npc.entity.CitizensChickenNPC;
import net.citizensnpcs.npc.entity.CitizensCowNPC;
import net.citizensnpcs.npc.entity.CitizensCreeperNPC;
import net.citizensnpcs.npc.entity.CitizensEnderDragonNPC;
import net.citizensnpcs.npc.entity.CitizensEndermanNPC;
import net.citizensnpcs.npc.entity.CitizensGhastNPC;
import net.citizensnpcs.npc.entity.CitizensGiantNPC;
import net.citizensnpcs.npc.entity.CitizensHumanNPC;
import net.citizensnpcs.npc.entity.CitizensIronGolemNPC;
import net.citizensnpcs.npc.entity.CitizensMagmaCubeNPC;
import net.citizensnpcs.npc.entity.CitizensMushroomCowNPC;
import net.citizensnpcs.npc.entity.CitizensOcelotNPC;
import net.citizensnpcs.npc.entity.CitizensPigNPC;
import net.citizensnpcs.npc.entity.CitizensPigZombieNPC;
import net.citizensnpcs.npc.entity.CitizensSheepNPC;
import net.citizensnpcs.npc.entity.CitizensSilverfishNPC;
import net.citizensnpcs.npc.entity.CitizensSkeletonNPC;
import net.citizensnpcs.npc.entity.CitizensSlimeNPC;
import net.citizensnpcs.npc.entity.CitizensSnowmanNPC;
import net.citizensnpcs.npc.entity.CitizensSpiderNPC;
import net.citizensnpcs.npc.entity.CitizensSquidNPC;
import net.citizensnpcs.npc.entity.CitizensVillagerNPC;
import net.citizensnpcs.npc.entity.CitizensWolfNPC;
import net.citizensnpcs.npc.entity.CitizensZombieNPC;
import net.citizensnpcs.util.ByIdArray;
import net.citizensnpcs.util.NPCBuilder;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.entity.CraftEntity;
@ -26,20 +53,48 @@ import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
public class CitizensNPCManager implements NPCManager {
private final NPCBuilder npcBuilder = new NPCBuilder();
private final ByIdArray<NPC> npcs = new ByIdArray<NPC>();
private final Citizens plugin;
private final Storage saves;
private final Map<EntityType, Class<? extends CitizensNPC>> types = new EnumMap<EntityType, Class<? extends CitizensNPC>>(
EntityType.class);
public CitizensNPCManager(Citizens plugin, Storage saves) {
this.plugin = plugin;
this.saves = saves;
types.put(EntityType.BLAZE, CitizensBlazeNPC.class);
types.put(EntityType.CAVE_SPIDER, CitizensCaveSpiderNPC.class);
types.put(EntityType.CHICKEN, CitizensChickenNPC.class);
types.put(EntityType.COW, CitizensCowNPC.class);
types.put(EntityType.CREEPER, CitizensCreeperNPC.class);
types.put(EntityType.ENDER_DRAGON, CitizensEnderDragonNPC.class);
types.put(EntityType.ENDERMAN, CitizensEndermanNPC.class);
types.put(EntityType.GHAST, CitizensGhastNPC.class);
types.put(EntityType.GIANT, CitizensGiantNPC.class);
types.put(EntityType.IRON_GOLEM, CitizensIronGolemNPC.class);
types.put(EntityType.MAGMA_CUBE, CitizensMagmaCubeNPC.class);
types.put(EntityType.MUSHROOM_COW, CitizensMushroomCowNPC.class);
types.put(EntityType.OCELOT, CitizensOcelotNPC.class);
types.put(EntityType.PIG, CitizensPigNPC.class);
types.put(EntityType.PIG_ZOMBIE, CitizensPigZombieNPC.class);
types.put(EntityType.PLAYER, CitizensHumanNPC.class);
types.put(EntityType.SHEEP, CitizensSheepNPC.class);
types.put(EntityType.SILVERFISH, CitizensSilverfishNPC.class);
types.put(EntityType.SKELETON, CitizensSkeletonNPC.class);
types.put(EntityType.SLIME, CitizensSlimeNPC.class);
types.put(EntityType.SNOWMAN, CitizensSnowmanNPC.class);
types.put(EntityType.SPIDER, CitizensSpiderNPC.class);
types.put(EntityType.SQUID, CitizensSquidNPC.class);
types.put(EntityType.VILLAGER, CitizensVillagerNPC.class);
types.put(EntityType.WOLF, CitizensWolfNPC.class);
types.put(EntityType.ZOMBIE, CitizensZombieNPC.class);
}
public NPC createNPC(EntityType type, int id, String name, Character character) {
CitizensNPC npc = npcBuilder.getByType(type, this, id, name);
CitizensNPC npc = getByType(type, id, name);
if (npc == null)
throw new IllegalStateException("could not create npc");
throw new IllegalStateException("Could not create NPC.");
if (character != null)
npc.setCharacter(character);
npcs.put(npc.getId(), npc);
@ -161,4 +216,16 @@ public class CitizensNPCManager implements NPCManager {
// Call selection event
player.getServer().getPluginManager().callEvent(new NPCSelectEvent(npc, player));
}
private CitizensNPC getByType(EntityType type, int id, String name) {
Class<? extends CitizensNPC> npcClass = types.get(type);
if (npcClass == null)
throw new IllegalArgumentException("Invalid EntityType: " + type);
try {
return npcClass.getConstructor(CitizensNPCManager.class, int.class, String.class).newInstance(this, id,
name);
} catch (Exception ex) {
return null;
}
}
}

View File

@ -25,6 +25,10 @@ public class CitizensBlazeNPC extends CitizensMobNPC {
public static class EntityBlazeNPC extends EntityBlaze implements NPCHandle {
private final CitizensNPC npc;
public EntityBlazeNPC(World world) {
this(world, null);
}
public EntityBlazeNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -34,7 +38,10 @@ public class CitizensBlazeNPC extends CitizensMobNPC {
@Override
public void d_() {
npc.update();
if (npc != null)
npc.update();
else
super.d_();
}
@Override

View File

@ -25,6 +25,10 @@ public class CitizensChickenNPC extends CitizensMobNPC {
public static class EntityChickenNPC extends EntityChicken implements NPCHandle {
private final CitizensNPC npc;
public EntityChickenNPC(World world) {
this(world, null);
}
public EntityChickenNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -35,7 +39,8 @@ public class CitizensChickenNPC extends CitizensMobNPC {
@Override
public void z_() {
super.z_();
npc.update();
if (npc != null)
npc.update();
}
@Override

View File

@ -25,6 +25,10 @@ public class CitizensCowNPC extends CitizensMobNPC {
public static class EntityCowNPC extends EntityCow implements NPCHandle {
private final CitizensNPC npc;
public EntityCowNPC(World world) {
this(world, null);
}
public EntityCowNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -35,7 +39,8 @@ public class CitizensCowNPC extends CitizensMobNPC {
@Override
public void z_() {
super.z_();
npc.update();
if (npc != null)
npc.update();
}
@Override

View File

@ -26,6 +26,10 @@ public class CitizensCreeperNPC extends CitizensMobNPC {
public static class EntityCreeperNPC extends EntityCreeper implements NPCHandle {
private final CitizensNPC npc;
public EntityCreeperNPC(World world) {
this(world, null);
}
public EntityCreeperNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -40,7 +44,8 @@ public class CitizensCreeperNPC extends CitizensMobNPC {
@Override
public void z_() {
super.z_();
npc.update();
if (npc != null)
npc.update();
}
@Override

View File

@ -63,6 +63,10 @@ public class CitizensEndermanNPC extends CitizensMobNPC implements Equipable {
public static class EntityEndermanNPC extends EntityEnderman implements NPCHandle {
private final CitizensNPC npc;
public EntityEndermanNPC(World world) {
this(world, null);
}
public EntityEndermanNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -76,7 +80,10 @@ public class CitizensEndermanNPC extends CitizensMobNPC implements Equipable {
@Override
public void e() {
npc.update();
if (npc != null)
npc.update();
else
super.e();
}
@Override

View File

@ -25,6 +25,10 @@ public class CitizensGhastNPC extends CitizensMobNPC {
public static class EntityGhastNPC extends EntityGhast implements NPCHandle {
private final CitizensNPC npc;
public EntityGhastNPC(World world) {
this(world, null);
}
public EntityGhastNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -34,7 +38,10 @@ public class CitizensGhastNPC extends CitizensMobNPC {
@Override
public void d_() {
npc.update();
if (npc != null)
npc.update();
else
super.d_();
}
@Override

View File

@ -25,6 +25,10 @@ public class CitizensMagmaCubeNPC extends CitizensMobNPC {
public static class EntityMagmaCubeNPC extends EntityMagmaCube implements NPCHandle {
private final CitizensNPC npc;
public EntityMagmaCubeNPC(World world) {
this(world, null);
}
public EntityMagmaCubeNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -35,7 +39,10 @@ public class CitizensMagmaCubeNPC extends CitizensMobNPC {
@Override
public void d_() {
npc.update();
if (npc != null)
npc.update();
else
super.d_();
}
@Override

View File

@ -25,6 +25,10 @@ public class CitizensMushroomCowNPC extends CitizensMobNPC {
public static class EntityMushroomCowNPC extends EntityMushroomCow implements NPCHandle {
private final CitizensNPC npc;
public EntityMushroomCowNPC(World world) {
this(world, null);
}
public EntityMushroomCowNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -35,7 +39,8 @@ public class CitizensMushroomCowNPC extends CitizensMobNPC {
@Override
public void z_() {
super.z_();
npc.update();
if (npc != null)
npc.update();
}
@Override

View File

@ -25,6 +25,10 @@ public class CitizensOcelotNPC extends CitizensMobNPC {
public static class EntityOcelotNPC extends EntityOcelot implements NPCHandle {
private final CitizensNPC npc;
public EntityOcelotNPC(World world) {
this(world, null);
}
public EntityOcelotNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -35,7 +39,8 @@ public class CitizensOcelotNPC extends CitizensMobNPC {
@Override
public void z_() {
super.z_();
npc.update();
if (npc != null)
npc.update();
}
@Override

View File

@ -52,6 +52,10 @@ public class CitizensPigNPC extends CitizensMobNPC implements Equipable {
public static class EntityPigNPC extends EntityPig implements NPCHandle {
private final CitizensNPC npc;
public EntityPigNPC(World world) {
this(world, null);
}
public EntityPigNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -66,7 +70,8 @@ public class CitizensPigNPC extends CitizensMobNPC implements Equipable {
@Override
public void z_() {
super.z_();
npc.update();
if (npc != null)
npc.update();
}
@Override

View File

@ -25,6 +25,10 @@ public class CitizensPigZombieNPC extends CitizensMobNPC {
public static class EntityPigZombieNPC extends EntityPigZombie implements NPCHandle {
private final CitizensNPC npc;
public EntityPigZombieNPC(World world) {
this(world, null);
}
public EntityPigZombieNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -34,7 +38,10 @@ public class CitizensPigZombieNPC extends CitizensMobNPC {
@Override
public void d_() {
npc.update();
if (npc != null)
npc.update();
else
super.d_();
}
@Override

View File

@ -62,6 +62,10 @@ public class CitizensSheepNPC extends CitizensMobNPC implements Equipable {
public static class EntitySheepNPC extends EntitySheep implements NPCHandle {
private final CitizensNPC npc;
public EntitySheepNPC(World world) {
this(world, null);
}
public EntitySheepNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -72,7 +76,8 @@ public class CitizensSheepNPC extends CitizensMobNPC implements Equipable {
@Override
public void z_() {
super.z_();
npc.update();
if (npc != null)
npc.update();
}
@Override

View File

@ -25,6 +25,10 @@ public class CitizensSilverfishNPC extends CitizensMobNPC {
public static class EntitySilverfishNPC extends EntitySilverfish implements NPCHandle {
private final CitizensNPC npc;
public EntitySilverfishNPC(World world) {
this(world, null);
}
public EntitySilverfishNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -35,7 +39,8 @@ public class CitizensSilverfishNPC extends CitizensMobNPC {
@Override
public void z_() {
super.z_();
npc.update();
if (npc != null)
npc.update();
}
@Override

View File

@ -25,6 +25,10 @@ public class CitizensSkeletonNPC extends CitizensMobNPC {
public static class EntitySkeletonNPC extends EntitySkeleton implements NPCHandle {
private final CitizensNPC npc;
public EntitySkeletonNPC(World world) {
this(world, null);
}
public EntitySkeletonNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -35,7 +39,8 @@ public class CitizensSkeletonNPC extends CitizensMobNPC {
@Override
public void z_() {
super.z_();
npc.update();
if (npc != null)
npc.update();
}
@Override

View File

@ -25,6 +25,10 @@ public class CitizensSlimeNPC extends CitizensMobNPC {
public static class EntitySlimeNPC extends EntitySlime implements NPCHandle {
private final CitizensNPC npc;
public EntitySlimeNPC(World world) {
this(world, null);
}
public EntitySlimeNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -36,7 +40,8 @@ public class CitizensSlimeNPC extends CitizensMobNPC {
@Override
public void z_() {
super.z_();
npc.update();
if (npc != null)
npc.update();
}
@Override

View File

@ -25,6 +25,10 @@ public class CitizensSpiderNPC extends CitizensMobNPC {
public static class EntitySpiderNPC extends EntitySpider implements NPCHandle {
private final CitizensNPC npc;
public EntitySpiderNPC(World world) {
this(world, null);
}
public EntitySpiderNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -35,7 +39,8 @@ public class CitizensSpiderNPC extends CitizensMobNPC {
@Override
public void z_() {
super.z_();
npc.update();
if (npc != null)
npc.update();
}
@Override

View File

@ -25,6 +25,10 @@ public class CitizensSquidNPC extends CitizensMobNPC {
public static class EntitySquidNPC extends EntitySquid implements NPCHandle {
private final CitizensNPC npc;
public EntitySquidNPC(World world) {
this(world, null);
}
public EntitySquidNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -34,7 +38,10 @@ public class CitizensSquidNPC extends CitizensMobNPC {
@Override
public void d_() {
npc.update();
if (npc != null)
npc.update();
else
super.d_();
}
@Override

View File

@ -25,6 +25,10 @@ public class CitizensVillagerNPC extends CitizensMobNPC {
public static class EntityVillagerNPC extends EntityVillager implements NPCHandle {
private final CitizensNPC npc;
public EntityVillagerNPC(World world) {
this(world, null);
}
public EntityVillagerNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -35,7 +39,8 @@ public class CitizensVillagerNPC extends CitizensMobNPC {
@Override
public void z_() {
super.z_();
npc.update();
if (npc != null)
npc.update();
}
@Override

View File

@ -25,6 +25,10 @@ public class CitizensWolfNPC extends CitizensMobNPC {
public static class EntityWolfNPC extends EntityWolf implements NPCHandle {
private final CitizensNPC npc;
public EntityWolfNPC(World world) {
this(world, null);
}
public EntityWolfNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -35,7 +39,8 @@ public class CitizensWolfNPC extends CitizensMobNPC {
@Override
public void z_() {
super.z_();
npc.update();
if (npc != null)
npc.update();
}
@Override

View File

@ -25,6 +25,10 @@ public class CitizensZombieNPC extends CitizensMobNPC {
public static class EntityZombieNPC extends EntityZombie implements NPCHandle {
private final CitizensNPC npc;
public EntityZombieNPC(World world) {
this(world, null);
}
public EntityZombieNPC(World world, NPC npc) {
super(world);
this.npc = (CitizensNPC) npc;
@ -35,7 +39,8 @@ public class CitizensZombieNPC extends CitizensMobNPC {
@Override
public void z_() {
super.z_();
npc.update();
if (npc != null)
npc.update();
}
@Override

View File

@ -1,83 +0,0 @@
package net.citizensnpcs.util;
import java.util.EnumMap;
import java.util.Map;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.entity.CitizensBlazeNPC;
import net.citizensnpcs.npc.entity.CitizensCaveSpiderNPC;
import net.citizensnpcs.npc.entity.CitizensChickenNPC;
import net.citizensnpcs.npc.entity.CitizensCowNPC;
import net.citizensnpcs.npc.entity.CitizensCreeperNPC;
import net.citizensnpcs.npc.entity.CitizensEnderDragonNPC;
import net.citizensnpcs.npc.entity.CitizensEndermanNPC;
import net.citizensnpcs.npc.entity.CitizensGhastNPC;
import net.citizensnpcs.npc.entity.CitizensGiantNPC;
import net.citizensnpcs.npc.entity.CitizensHumanNPC;
import net.citizensnpcs.npc.entity.CitizensIronGolemNPC;
import net.citizensnpcs.npc.entity.CitizensMagmaCubeNPC;
import net.citizensnpcs.npc.entity.CitizensMushroomCowNPC;
import net.citizensnpcs.npc.entity.CitizensOcelotNPC;
import net.citizensnpcs.npc.entity.CitizensPigNPC;
import net.citizensnpcs.npc.entity.CitizensPigZombieNPC;
import net.citizensnpcs.npc.entity.CitizensSheepNPC;
import net.citizensnpcs.npc.entity.CitizensSilverfishNPC;
import net.citizensnpcs.npc.entity.CitizensSkeletonNPC;
import net.citizensnpcs.npc.entity.CitizensSlimeNPC;
import net.citizensnpcs.npc.entity.CitizensSnowmanNPC;
import net.citizensnpcs.npc.entity.CitizensSpiderNPC;
import net.citizensnpcs.npc.entity.CitizensSquidNPC;
import net.citizensnpcs.npc.entity.CitizensVillagerNPC;
import net.citizensnpcs.npc.entity.CitizensWolfNPC;
import net.citizensnpcs.npc.entity.CitizensZombieNPC;
import org.bukkit.entity.EntityType;
public class NPCBuilder {
// TODO: convert this into solely a lookup class.
public CitizensNPC getByType(EntityType type, CitizensNPCManager npcManager, int id, String name) {
Class<? extends CitizensNPC> npcClass = types.get(type);
if (npcClass == null)
return null;
try {
return npcClass.getConstructor(CitizensNPCManager.class, int.class, String.class).newInstance(npcManager,
id, name);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
private static final Map<EntityType, Class<? extends CitizensNPC>> types = new EnumMap<EntityType, Class<? extends CitizensNPC>>(
EntityType.class);
static {
types.put(EntityType.BLAZE, CitizensBlazeNPC.class);
types.put(EntityType.CAVE_SPIDER, CitizensCaveSpiderNPC.class);
types.put(EntityType.CHICKEN, CitizensChickenNPC.class);
types.put(EntityType.COW, CitizensCowNPC.class);
types.put(EntityType.CREEPER, CitizensCreeperNPC.class);
types.put(EntityType.ENDER_DRAGON, CitizensEnderDragonNPC.class);
types.put(EntityType.ENDERMAN, CitizensEndermanNPC.class);
types.put(EntityType.GHAST, CitizensGhastNPC.class);
types.put(EntityType.GIANT, CitizensGiantNPC.class);
types.put(EntityType.IRON_GOLEM, CitizensIronGolemNPC.class);
types.put(EntityType.MAGMA_CUBE, CitizensMagmaCubeNPC.class);
types.put(EntityType.MUSHROOM_COW, CitizensMushroomCowNPC.class);
types.put(EntityType.OCELOT, CitizensOcelotNPC.class);
types.put(EntityType.PIG, CitizensPigNPC.class);
types.put(EntityType.PIG_ZOMBIE, CitizensPigZombieNPC.class);
types.put(EntityType.PLAYER, CitizensHumanNPC.class);
types.put(EntityType.SHEEP, CitizensSheepNPC.class);
types.put(EntityType.SILVERFISH, CitizensSilverfishNPC.class);
types.put(EntityType.SKELETON, CitizensSkeletonNPC.class);
types.put(EntityType.SLIME, CitizensSlimeNPC.class);
types.put(EntityType.SNOWMAN, CitizensSnowmanNPC.class);
types.put(EntityType.SPIDER, CitizensSpiderNPC.class);
types.put(EntityType.SQUID, CitizensSquidNPC.class);
types.put(EntityType.VILLAGER, CitizensVillagerNPC.class);
types.put(EntityType.WOLF, CitizensWolfNPC.class);
types.put(EntityType.ZOMBIE, CitizensZombieNPC.class);
}
}