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

View File

@ -1,6 +1,7 @@
package net.citizensnpcs; package net.citizensnpcs;
import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.event.NPCDamageByEntityEvent;
import net.citizensnpcs.api.event.NPCDamageEvent; import net.citizensnpcs.api.event.NPCDamageEvent;
import net.citizensnpcs.api.event.NPCLeftClickEvent; import net.citizensnpcs.api.event.NPCLeftClickEvent;
import net.citizensnpcs.api.event.NPCRightClickEvent; import net.citizensnpcs.api.event.NPCRightClickEvent;
@ -86,11 +87,11 @@ public class EventListen implements Listener {
if (!npcManager.isNPC(event.getEntity())) if (!npcManager.isNPC(event.getEntity()))
return; return;
NPC npc = npcManager.getNPC(event.getEntity());
if (event instanceof EntityDamageByEntityEvent) { if (event instanceof EntityDamageByEntityEvent) {
EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) event; 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); Bukkit.getPluginManager().callEvent(event);
if (!damageEvent.isCancelled() || !(e.getDamager() instanceof Player)) if (!damageEvent.isCancelled() || !(e.getDamager() instanceof Player))
@ -105,7 +106,8 @@ public class EventListen implements Listener {
if (npc.getCharacter() != null) if (npc.getCharacter() != null)
npc.getCharacter().onLeftClick(npc, damager); 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 { public abstract class CitizensMobNPC extends CitizensNPC {
private final Constructor<? extends EntityLiving> constructor; private final Constructor<? extends EntityLiving> constructor;
protected CitizensMobNPC(CitizensNPCManager manager, int id, String name, Class<? extends EntityLiving> clazz) { protected CitizensMobNPC(int id, String name, Class<? extends EntityLiving> clazz) {
super(manager, id, name); super(id, name);
try { try {
this.constructor = clazz.getConstructor(World.class, NPC.class); this.constructor = clazz.getConstructor(World.class, NPC.class);
} catch (Exception ex) { } catch (Exception ex) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,11 @@
package net.citizensnpcs.util; 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.citizensnpcs.Settings.Setting;
import net.minecraft.server.Packet; import net.minecraft.server.Packet;
@ -12,12 +18,100 @@ import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
public class Util { public class Util {
// Static class for small (emphasis small) utility methods // Static class for small (emphasis small) utility methods
private Util() { 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) { public static boolean isSettingFulfilled(Player player, Setting setting) {
String parts = setting.asString(); String parts = setting.asString();
if (parts.contains("*")) if (parts.contains("*"))