From 4cd729ae20164ed106544e9052f56ffa5c546998 Mon Sep 17 00:00:00 2001 From: fullwall Date: Sun, 13 May 2012 19:41:21 +0800 Subject: [PATCH] Remove constructor parameter, add method in Util --- src/main/java/net/citizensnpcs/Citizens.java | 14 ++- .../java/net/citizensnpcs/EventListen.java | 8 +- .../net/citizensnpcs/npc/CitizensMobNPC.java | 4 +- .../net/citizensnpcs/npc/CitizensNPC.java | 6 +- .../citizensnpcs/npc/CitizensNPCManager.java | 24 +---- .../npc/entity/CitizensBlazeNPC.java | 5 +- .../npc/entity/CitizensCaveSpiderNPC.java | 5 +- .../npc/entity/CitizensChickenNPC.java | 5 +- .../npc/entity/CitizensCowNPC.java | 5 +- .../npc/entity/CitizensCreeperNPC.java | 5 +- .../npc/entity/CitizensEnderDragonNPC.java | 5 +- .../npc/entity/CitizensEndermanNPC.java | 5 +- .../npc/entity/CitizensGhastNPC.java | 5 +- .../npc/entity/CitizensGiantNPC.java | 5 +- .../npc/entity/CitizensHumanNPC.java | 5 +- .../npc/entity/CitizensIronGolemNPC.java | 5 +- .../npc/entity/CitizensMagmaCubeNPC.java | 5 +- .../npc/entity/CitizensMushroomCowNPC.java | 5 +- .../npc/entity/CitizensOcelotNPC.java | 5 +- .../npc/entity/CitizensPigNPC.java | 5 +- .../npc/entity/CitizensPigZombieNPC.java | 5 +- .../npc/entity/CitizensSheepNPC.java | 5 +- .../npc/entity/CitizensSilverfishNPC.java | 5 +- .../npc/entity/CitizensSkeletonNPC.java | 5 +- .../npc/entity/CitizensSlimeNPC.java | 5 +- .../npc/entity/CitizensSnowmanNPC.java | 5 +- .../npc/entity/CitizensSpiderNPC.java | 5 +- .../npc/entity/CitizensSquidNPC.java | 5 +- .../npc/entity/CitizensVillagerNPC.java | 5 +- .../npc/entity/CitizensWolfNPC.java | 5 +- .../npc/entity/CitizensZombieNPC.java | 5 +- src/main/java/net/citizensnpcs/util/Util.java | 94 +++++++++++++++++++ 32 files changed, 172 insertions(+), 108 deletions(-) diff --git a/src/main/java/net/citizensnpcs/Citizens.java b/src/main/java/net/citizensnpcs/Citizens.java index 965dbdee2..01212c5bd 100644 --- a/src/main/java/net/citizensnpcs/Citizens.java +++ b/src/main/java/net/citizensnpcs/Citizens.java @@ -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 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())); diff --git a/src/main/java/net/citizensnpcs/EventListen.java b/src/main/java/net/citizensnpcs/EventListen.java index 83319b62f..e90dd3dc2 100644 --- a/src/main/java/net/citizensnpcs/EventListen.java +++ b/src/main/java/net/citizensnpcs/EventListen.java @@ -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)); } } diff --git a/src/main/java/net/citizensnpcs/npc/CitizensMobNPC.java b/src/main/java/net/citizensnpcs/npc/CitizensMobNPC.java index 1b7a9b641..331e6ac2f 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensMobNPC.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensMobNPC.java @@ -17,8 +17,8 @@ import org.bukkit.craftbukkit.CraftWorld; public abstract class CitizensMobNPC extends CitizensNPC { private final Constructor constructor; - protected CitizensMobNPC(CitizensNPCManager manager, int id, String name, Class clazz) { - super(manager, id, name); + protected CitizensMobNPC(int id, String name, Class clazz) { + super(id, name); try { this.constructor = clazz.getConstructor(World.class, NPC.class); } catch (Exception ex) { diff --git a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java index f4d4c9880..e7c6b050f 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java @@ -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(); } diff --git a/src/main/java/net/citizensnpcs/npc/CitizensNPCManager.java b/src/main/java/net/citizensnpcs/npc/CitizensNPCManager.java index f6a89de63..7223dea70 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensNPCManager.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensNPCManager.java @@ -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,21 +160,7 @@ 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 itr = this.iterator(); - while (itr.hasNext()) { - NPC npc = itr.next(); - itr.remove(); - npc.despawn(); + saves.getKey("npc").removeKey(String.valueOf(npc.getId())); } } @@ -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; } diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensBlazeNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensBlazeNPC.java index 22e63c404..06aa9f2b9 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensBlazeNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensBlazeNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensCaveSpiderNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensCaveSpiderNPC.java index 51c6f808e..4176a7a33 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensCaveSpiderNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensCaveSpiderNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensChickenNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensChickenNPC.java index 5ffd9d8cb..f5b4fa2e4 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensChickenNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensChickenNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensCowNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensCowNPC.java index 1fc28f418..4b0ee4309 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensCowNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensCowNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensCreeperNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensCreeperNPC.java index 7a7028378..79d83edb6 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensCreeperNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensCreeperNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensEnderDragonNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensEnderDragonNPC.java index ae2500ad1..651b6a54a 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensEnderDragonNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensEnderDragonNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensEndermanNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensEndermanNPC.java index 00ebb4262..3d1344116 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensEndermanNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensEndermanNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensGhastNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensGhastNPC.java index 1b5496bcb..27d074c62 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensGhastNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensGhastNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensGiantNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensGiantNPC.java index db717651b..33d622fb0 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensGiantNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensGiantNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensHumanNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensHumanNPC.java index 2ecf3adaa..f0b7969a8 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensHumanNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensHumanNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensIronGolemNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensIronGolemNPC.java index d95ef56c6..20d1b2d38 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensIronGolemNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensIronGolemNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensMagmaCubeNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensMagmaCubeNPC.java index 3877e20da..905c88245 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensMagmaCubeNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensMagmaCubeNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensMushroomCowNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensMushroomCowNPC.java index 8a78a596f..7a3bfedd0 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensMushroomCowNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensMushroomCowNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensOcelotNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensOcelotNPC.java index 536eb9c73..9e692b93b 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensOcelotNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensOcelotNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensPigNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensPigNPC.java index 1b3fef5d6..bce59f966 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensPigNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensPigNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensPigZombieNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensPigZombieNPC.java index 933d27aa6..e350a6857 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensPigZombieNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensPigZombieNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensSheepNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensSheepNPC.java index 425838290..218a5c110 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensSheepNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensSheepNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensSilverfishNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensSilverfishNPC.java index d0fd3a3e8..692872fed 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensSilverfishNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensSilverfishNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensSkeletonNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensSkeletonNPC.java index b283ccf87..f4ae68741 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensSkeletonNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensSkeletonNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensSlimeNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensSlimeNPC.java index 38a7c698c..341470990 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensSlimeNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensSlimeNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensSnowmanNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensSnowmanNPC.java index 3780dffe8..4cd639416 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensSnowmanNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensSnowmanNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensSpiderNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensSpiderNPC.java index 2987ea1f2..50dd406eb 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensSpiderNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensSpiderNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensSquidNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensSquidNPC.java index 2de2457ea..d7bba73dc 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensSquidNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensSquidNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensVillagerNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensVillagerNPC.java index 2b02f960d..62ab5a899 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensVillagerNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensVillagerNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensWolfNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensWolfNPC.java index 1b4dc7e11..f76ac35d1 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensWolfNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensWolfNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensZombieNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensZombieNPC.java index d004b81dd..630678b97 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensZombieNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensZombieNPC.java @@ -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 diff --git a/src/main/java/net/citizensnpcs/util/Util.java b/src/main/java/net/citizensnpcs/util/Util.java index 31a3c707b..95e209f56 100644 --- a/src/main/java/net/citizensnpcs/util/Util.java +++ b/src/main/java/net/citizensnpcs/util/Util.java @@ -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 createInstance(Class 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 createInstance0(Class clazz, Object[] params) throws InstantiationException, + IllegalAccessException, IllegalArgumentException, InvocationTargetException { + + @SuppressWarnings("unchecked") + Constructor[] constructors = (Constructor[]) clazz.getConstructors(); + Arrays.sort(constructors, new Comparator>() { + @Override + public int compare(Constructor o1, Constructor o2) { + return o2.getParameterTypes().length - o1.getParameterTypes().length; + } + }); + constructorLoop: for (Constructor 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> 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("*"))