Remove constructor parameter, add method in Util

This commit is contained in:
fullwall 2012-05-13 19:41:21 +08:00
parent 31dc1f813b
commit 5ea3f32f76
32 changed files with 172 additions and 108 deletions

View File

@ -3,6 +3,7 @@ package net.citizensnpcs;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.logging.Level;
import net.citizensnpcs.Settings.Setting;
@ -145,7 +146,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
// Don't bother with this part if MC versions are not compatible
if (compatible) {
save();
npcManager.safeRemove();
despawnNPCs();
npcManager = null;
getServer().getScheduler().cancelTasks(this);
}
@ -223,12 +224,21 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
public void reload() throws NPCLoadException {
Editor.leaveAll();
config.reload();
npcManager.safeRemove();
despawnNPCs();
setupNPCs();
getServer().getPluginManager().callEvent(new CitizensReloadEvent());
}
private void despawnNPCs() {
Iterator<NPC> itr = npcManager.iterator();
while (itr.hasNext()) {
NPC npc = itr.next();
itr.remove();
npc.despawn();
}
}
public void save() {
for (NPC npc : npcManager)
((CitizensNPC) npc).save(saves.getKey("npc." + npc.getId()));

View File

@ -1,6 +1,7 @@
package net.citizensnpcs;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.event.NPCDamageByEntityEvent;
import net.citizensnpcs.api.event.NPCDamageEvent;
import net.citizensnpcs.api.event.NPCLeftClickEvent;
import net.citizensnpcs.api.event.NPCRightClickEvent;
@ -86,11 +87,11 @@ public class EventListen implements Listener {
if (!npcManager.isNPC(event.getEntity()))
return;
NPC npc = npcManager.getNPC(event.getEntity());
if (event instanceof EntityDamageByEntityEvent) {
EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) event;
NPC npc = npcManager.getNPC(event.getEntity());
NPCDamageEvent damageEvent = new NPCDamageEvent(npc, e);
NPCDamageByEntityEvent damageEvent = new NPCDamageByEntityEvent(npc, e);
Bukkit.getPluginManager().callEvent(event);
if (!damageEvent.isCancelled() || !(e.getDamager() instanceof Player))
@ -105,7 +106,8 @@ public class EventListen implements Listener {
if (npc.getCharacter() != null)
npc.getCharacter().onLeftClick(npc, damager);
} else {
Bukkit.getPluginManager().callEvent(new NPCDamageEvent(npc, event));
}
}

View File

@ -17,8 +17,8 @@ import org.bukkit.craftbukkit.CraftWorld;
public abstract class CitizensMobNPC extends CitizensNPC {
private final Constructor<? extends EntityLiving> constructor;
protected CitizensMobNPC(CitizensNPCManager manager, int id, String name, Class<? extends EntityLiving> clazz) {
super(manager, id, name);
protected CitizensMobNPC(int id, String name, Class<? extends EntityLiving> clazz) {
super(id, name);
try {
this.constructor = clazz.getConstructor(World.class, NPC.class);
} catch (Exception ex) {

View File

@ -25,13 +25,11 @@ import org.bukkit.inventory.Inventory;
public abstract class CitizensNPC extends AbstractNPC {
private final CitizensAI ai = new CitizensAI(this);
private final CitizensNPCManager manager;
protected EntityLiving mcEntity;
private final CitizensTraitManager traitManager;
protected CitizensNPC(CitizensNPCManager manager, int id, String name) {
protected CitizensNPC(int id, String name) {
super(id, name);
this.manager = manager;
traitManager = (CitizensTraitManager) CitizensAPI.getTraitManager();
}
@ -140,7 +138,7 @@ public abstract class CitizensNPC extends AbstractNPC {
@Override
public void remove() {
super.remove();
manager.remove(this);
CitizensAPI.getNPCManager().deregister(this);
if (isSpawned())
despawn();
}

View File

@ -148,9 +148,10 @@ public class CitizensNPCManager implements NPCManager {
return npcs.iterator();
}
void remove(NPC npc) {
@Override
public void deregister(NPC npc) {
npcs.remove(npc.getId());
removeData(npc);
saves.getKey("npc").removeKey(String.valueOf(npc.getId()));
}
public void removeAll() {
@ -159,22 +160,8 @@ public class CitizensNPCManager implements NPCManager {
NPC npc = itr.next();
itr.remove();
npc.despawn();
removeData(npc);
}
}
private void removeData(NPC npc) {
saves.getKey("npc").removeKey(String.valueOf(npc.getId()));
}
public void safeRemove() {
// Destroy all NPCs everywhere besides storage
Iterator<NPC> itr = this.iterator();
while (itr.hasNext()) {
NPC npc = itr.next();
itr.remove();
npc.despawn();
}
}
private CitizensNPC getByType(EntityType type, int id, String name) {
@ -182,8 +169,7 @@ public class CitizensNPCManager implements NPCManager {
if (npcClass == null)
throw new IllegalArgumentException("Invalid EntityType: " + type);
try {
return npcClass.getConstructor(CitizensNPCManager.class, int.class, String.class).newInstance(this, id,
name);
return npcClass.getConstructor(int.class, String.class).newInstance(id, name);
} catch (Exception ex) {
return null;
}

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntityBlaze;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.Blaze;
public class CitizensBlazeNPC extends CitizensMobNPC {
public CitizensBlazeNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityBlazeNPC.class);
public CitizensBlazeNPC(int id, String name) {
super(id, name, EntityBlazeNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntityCaveSpider;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.CaveSpider;
public class CitizensCaveSpiderNPC extends CitizensMobNPC {
public CitizensCaveSpiderNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityCaveSpiderNPC.class);
public CitizensCaveSpiderNPC(int id, String name) {
super(id, name, EntityCaveSpiderNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntityChicken;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.Chicken;
public class CitizensChickenNPC extends CitizensMobNPC {
public CitizensChickenNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityChickenNPC.class);
public CitizensChickenNPC(int id, String name) {
super(id, name, EntityChickenNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntityCow;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.Cow;
public class CitizensCowNPC extends CitizensMobNPC {
public CitizensCowNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityCowNPC.class);
public CitizensCowNPC(int id, String name) {
super(id, name, EntityCowNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntityCreeper;
import net.minecraft.server.EntityWeatherLighting;
@ -14,8 +13,8 @@ import org.bukkit.entity.Creeper;
public class CitizensCreeperNPC extends CitizensMobNPC {
public CitizensCreeperNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityCreeperNPC.class);
public CitizensCreeperNPC(int id, String name) {
super(id, name, EntityCreeperNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntityEnderDragon;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.EnderDragon;
public class CitizensEnderDragonNPC extends CitizensMobNPC {
public CitizensEnderDragonNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityEnderDragonNPC.class);
public CitizensEnderDragonNPC(int id, String name) {
super(id, name, EntityEnderDragonNPC.class);
}
@Override

View File

@ -5,7 +5,6 @@ import net.citizensnpcs.api.trait.trait.Equipment;
import net.citizensnpcs.editor.Equipable;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.citizensnpcs.util.Messaging;
import net.minecraft.server.EntityEnderman;
@ -20,8 +19,8 @@ import org.bukkit.material.MaterialData;
public class CitizensEndermanNPC extends CitizensMobNPC implements Equipable {
public CitizensEndermanNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityEndermanNPC.class);
public CitizensEndermanNPC(int id, String name) {
super(id, name, EntityEndermanNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntityGhast;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.Ghast;
public class CitizensGhastNPC extends CitizensMobNPC {
public CitizensGhastNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityGhastNPC.class);
public CitizensGhastNPC(int id, String name) {
super(id, name, EntityGhastNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntityGiantZombie;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.Giant;
public class CitizensGiantNPC extends CitizensMobNPC {
public CitizensGiantNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityGiantNPC.class);
public CitizensGiantNPC(int id, String name) {
super(id, name, EntityGiantNPC.class);
}
@Override

View File

@ -4,7 +4,6 @@ import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.trait.trait.Equipment;
import net.citizensnpcs.editor.Equipable;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.StringHelper;
import net.minecraft.server.EntityLiving;
@ -20,8 +19,8 @@ import org.bukkit.inventory.ItemStack;
public class CitizensHumanNPC extends CitizensNPC implements Equipable {
public CitizensHumanNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name);
public CitizensHumanNPC(int id, String name) {
super(id, name);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntityIronGolem;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.IronGolem;
public class CitizensIronGolemNPC extends CitizensMobNPC {
public CitizensIronGolemNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityIronGolemNPC.class);
public CitizensIronGolemNPC(int id, String name) {
super(id, name, EntityIronGolemNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntityMagmaCube;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.MagmaCube;
public class CitizensMagmaCubeNPC extends CitizensMobNPC {
public CitizensMagmaCubeNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityMagmaCubeNPC.class);
public CitizensMagmaCubeNPC(int id, String name) {
super(id, name, EntityMagmaCubeNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntityMushroomCow;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.MushroomCow;
public class CitizensMushroomCowNPC extends CitizensMobNPC {
public CitizensMushroomCowNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityMushroomCowNPC.class);
public CitizensMushroomCowNPC(int id, String name) {
super(id, name, EntityMushroomCowNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntityOcelot;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.Ocelot;
public class CitizensOcelotNPC extends CitizensMobNPC {
public CitizensOcelotNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityOcelotNPC.class);
public CitizensOcelotNPC(int id, String name) {
super(id, name, EntityOcelotNPC.class);
}
@Override

View File

@ -4,7 +4,6 @@ import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.editor.Equipable;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.citizensnpcs.trait.Saddle;
import net.citizensnpcs.util.Messaging;
@ -21,8 +20,8 @@ import org.bukkit.inventory.ItemStack;
public class CitizensPigNPC extends CitizensMobNPC implements Equipable {
public CitizensPigNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityPigNPC.class);
public CitizensPigNPC(int id, String name) {
super(id, name, EntityPigNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntityPigZombie;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.PigZombie;
public class CitizensPigZombieNPC extends CitizensMobNPC {
public CitizensPigZombieNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityPigZombieNPC.class);
public CitizensPigZombieNPC(int id, String name) {
super(id, name, EntityPigZombieNPC.class);
}
@Override

View File

@ -4,7 +4,6 @@ import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.editor.Equipable;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.citizensnpcs.trait.Sheared;
import net.citizensnpcs.trait.WoolColor;
@ -22,8 +21,8 @@ import org.bukkit.inventory.ItemStack;
public class CitizensSheepNPC extends CitizensMobNPC implements Equipable {
public CitizensSheepNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntitySheepNPC.class);
public CitizensSheepNPC(int id, String name) {
super(id, name, EntitySheepNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntitySilverfish;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.Silverfish;
public class CitizensSilverfishNPC extends CitizensMobNPC {
public CitizensSilverfishNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntitySilverfishNPC.class);
public CitizensSilverfishNPC(int id, String name) {
super(id, name, EntitySilverfishNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntitySkeleton;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.Skeleton;
public class CitizensSkeletonNPC extends CitizensMobNPC {
public CitizensSkeletonNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntitySkeletonNPC.class);
public CitizensSkeletonNPC(int id, String name) {
super(id, name, EntitySkeletonNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntitySlime;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.Slime;
public class CitizensSlimeNPC extends CitizensMobNPC {
public CitizensSlimeNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntitySlimeNPC.class);
public CitizensSlimeNPC(int id, String name) {
super(id, name, EntitySlimeNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntitySnowman;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.Snowman;
public class CitizensSnowmanNPC extends CitizensMobNPC {
public CitizensSnowmanNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntitySnowmanNPC.class);
public CitizensSnowmanNPC(int id, String name) {
super(id, name, EntitySnowmanNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntitySpider;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.Spider;
public class CitizensSpiderNPC extends CitizensMobNPC {
public CitizensSpiderNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntitySpiderNPC.class);
public CitizensSpiderNPC(int id, String name) {
super(id, name, EntitySpiderNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntitySquid;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.Squid;
public class CitizensSquidNPC extends CitizensMobNPC {
public CitizensSquidNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntitySquidNPC.class);
public CitizensSquidNPC(int id, String name) {
super(id, name, EntitySquidNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntityVillager;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.Villager;
public class CitizensVillagerNPC extends CitizensMobNPC {
public CitizensVillagerNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityVillagerNPC.class);
public CitizensVillagerNPC(int id, String name) {
super(id, name, EntityVillagerNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntityWolf;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.Wolf;
public class CitizensWolfNPC extends CitizensMobNPC {
public CitizensWolfNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityWolfNPC.class);
public CitizensWolfNPC(int id, String name) {
super(id, name, EntityWolfNPC.class);
}
@Override

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.ai.NPCHandle;
import net.minecraft.server.EntityZombie;
import net.minecraft.server.PathfinderGoalSelector;
@ -13,8 +12,8 @@ import org.bukkit.entity.Zombie;
public class CitizensZombieNPC extends CitizensMobNPC {
public CitizensZombieNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityZombieNPC.class);
public CitizensZombieNPC(int id, String name) {
super(id, name, EntityZombieNPC.class);
}
@Override

View File

@ -1,5 +1,11 @@
package net.citizensnpcs.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;
import net.citizensnpcs.Settings.Setting;
import net.minecraft.server.Packet;
@ -12,12 +18,100 @@ import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Player;
import com.google.common.base.Splitter;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
public class Util {
// Static class for small (emphasis small) utility methods
private Util() {
}
/**
* Given a set of instantiation parameters, attempts to find a matching
* constructor with the greatest number of matching class parameters and
* invokes it.
*
* @param clazz
* @param params
* @return null if no instance could be created with the given parameters
*/
public static <T> T createInstance(Class<? extends T> clazz, Object... params) {
Validate.notNull(params);
Validate.noNullElements(params);
try {
if (params.length == 0) {
return clazz.newInstance();
}
return createInstance0(clazz, params);
} catch (Exception e) {
return null;
}
}
private static <T> T createInstance0(Class<? extends T> clazz, Object[] params) throws InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException {
@SuppressWarnings("unchecked")
Constructor<? extends T>[] constructors = (Constructor<? extends T>[]) clazz.getConstructors();
Arrays.sort(constructors, new Comparator<Constructor<?>>() {
@Override
public int compare(Constructor<?> o1, Constructor<?> o2) {
return o2.getParameterTypes().length - o1.getParameterTypes().length;
}
});
constructorLoop: for (Constructor<? extends T> constructor : constructors) {
Class<?>[] types = constructor.getParameterTypes();
if (Sets.newHashSet(types).size() != types.length)
continue;
// we can't resolve the order of the constructor parameters
if (types.length == 0)
return clazz.newInstance();
Object[] rebuild = new Object[types.length];
for (Object param : params) {
for (int i = 0; i < types.length; ++i) {
if (param.getClass() == types[i] || primitiveClassMap.get(param.getClass()) == types[i]
|| searchInterfaces(param.getClass(), types[i])) {
rebuild[i] = param;
}
}
}
for (Object constructorParam : rebuild) {
if (constructorParam == null)
continue constructorLoop;
}
return constructor.newInstance(rebuild);
}
return null;
}
private static boolean searchInterfaces(Class<?> class1, Class<?> class2) {
for (Class<?> test : class1.getInterfaces())
if (test == class2)
return true;
return false;
}
private static final Map<Class<?>, Class<?>> primitiveClassMap = Maps.newHashMap();
static {
primitiveClassMap.put(Boolean.class, boolean.class);
primitiveClassMap.put(Byte.class, byte.class);
primitiveClassMap.put(Short.class, short.class);
primitiveClassMap.put(Character.class, char.class);
primitiveClassMap.put(Integer.class, int.class);
primitiveClassMap.put(Long.class, long.class);
primitiveClassMap.put(Float.class, float.class);
primitiveClassMap.put(Double.class, double.class);
primitiveClassMap.put(boolean.class, Boolean.class);
primitiveClassMap.put(byte.class, Byte.class);
primitiveClassMap.put(short.class, Short.class);
primitiveClassMap.put(char.class, Character.class);
primitiveClassMap.put(int.class, Integer.class);
primitiveClassMap.put(long.class, Long.class);
primitiveClassMap.put(float.class, Float.class);
primitiveClassMap.put(double.class, Double.class);
}
public static boolean isSettingFulfilled(Player player, Setting setting) {
String parts = setting.asString();
if (parts.contains("*"))