diff --git a/lib/bukkit-1.1-R1-SNAPSHOT.jar b/lib/bukkit-1.1-R1-SNAPSHOT.jar index 446b0b180..d8c78a554 100644 Binary files a/lib/bukkit-1.1-R1-SNAPSHOT.jar and b/lib/bukkit-1.1-R1-SNAPSHOT.jar differ diff --git a/src/net/citizensnpcs/Citizens.java b/src/net/citizensnpcs/Citizens.java index 056bb7da4..46b6bb2bf 100644 --- a/src/net/citizensnpcs/Citizens.java +++ b/src/net/citizensnpcs/Citizens.java @@ -3,13 +3,10 @@ package net.citizensnpcs; import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.trait.trait.LocationTrait; -import net.citizensnpcs.listener.EntityListen; -import net.citizensnpcs.listener.WorldListen; import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.util.Messaging; import org.bukkit.Bukkit; -import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; public class Citizens extends JavaPlugin { @@ -25,7 +22,8 @@ public class Citizens extends JavaPlugin { npcManager = new CitizensNPCManager(); CitizensAPI.setNPCManager(npcManager); - registerEvents(getServer().getPluginManager()); + // Register events + new EventListen(this); Messaging.log("v" + getDescription().getVersion() + " enabled."); @@ -49,9 +47,4 @@ public class Citizens extends JavaPlugin { } Messaging.log("Loaded " + npcManager.getNPCs().size() + " NPCs."); } - - private void registerEvents(PluginManager pm) { - pm.registerEvents(new EntityListen(), this); - pm.registerEvents(new WorldListen(), this); - } } \ No newline at end of file diff --git a/src/net/citizensnpcs/EventListen.java b/src/net/citizensnpcs/EventListen.java new file mode 100644 index 000000000..4a7638c73 --- /dev/null +++ b/src/net/citizensnpcs/EventListen.java @@ -0,0 +1,80 @@ +package net.citizensnpcs; + +import java.util.HashSet; +import java.util.Set; + +import net.citizensnpcs.api.CitizensAPI; +import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.api.npc.trait.trait.LocationTrait; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityTargetEvent; +import org.bukkit.event.world.ChunkLoadEvent; +import org.bukkit.event.world.ChunkUnloadEvent; + +public class EventListen implements Listener { + private Set toRespawn = new HashSet(); + + public EventListen(Citizens plugin) { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + /* + * Entity events + */ + @EventHandler() + public void onEntityDamage(EntityDamageEvent event) { + if (event.isCancelled() || !CitizensAPI.getNPCManager().isNPC(event.getEntity())) + return; + + if (event instanceof EntityDamageByEntityEvent) { + EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) event; + if (e.getDamager() instanceof Player) { + NPC npc = CitizensAPI.getNPCManager().getNPC(event.getEntity()); + npc.getCharacter().onLeftClick(npc, (Player) e.getDamager()); + } + } + } + + @EventHandler() + public void onEntityTarget(EntityTargetEvent event) { + if (event.isCancelled() || !CitizensAPI.getNPCManager().isNPC(event.getEntity()) + || !(event.getTarget() instanceof Player)) + return; + + NPC npc = CitizensAPI.getNPCManager().getNPC(event.getEntity()); + npc.getCharacter().onRightClick(npc, (Player) event.getTarget()); + } + + /* + * World events + */ + @EventHandler() + public void onChunkLoad(ChunkLoadEvent event) { + for (int id : toRespawn) { + NPC npc = CitizensAPI.getNPCManager().getNPC(id); + npc.spawn(((LocationTrait) npc.getTrait("location")).getLocation()); + toRespawn.remove(id); + } + } + + @EventHandler() + public void onChunkUnload(ChunkUnloadEvent event) { + if (event.isCancelled()) + return; + + for (NPC npc : CitizensAPI.getNPCManager().getNPCs()) { + LocationTrait loc = (LocationTrait) npc.getTrait("location"); + if (event.getWorld().equals(loc.getLocation().getWorld()) + && event.getChunk().getX() == loc.getLocation().getChunk().getX() + && event.getChunk().getZ() == loc.getLocation().getChunk().getZ()) { + toRespawn.add(npc.getId()); + npc.despawn(); + } + } + } +} \ No newline at end of file diff --git a/src/net/citizensnpcs/listener/EntityListen.java b/src/net/citizensnpcs/listener/EntityListen.java deleted file mode 100644 index 2ce413b3b..000000000 --- a/src/net/citizensnpcs/listener/EntityListen.java +++ /dev/null @@ -1,39 +0,0 @@ -package net.citizensnpcs.listener; - -import net.citizensnpcs.api.CitizensAPI; -import net.citizensnpcs.api.npc.NPC; - -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.entity.EntityDamageByEntityEvent; -import org.bukkit.event.entity.EntityDamageEvent; -import org.bukkit.event.entity.EntityTargetEvent; - -public class EntityListen implements Listener { - - @EventHandler(event = EntityDamageEvent.class, priority = EventPriority.NORMAL) - public void onEntityDamage(EntityDamageEvent event) { - if (event.isCancelled() || !CitizensAPI.getNPCManager().isNPC(event.getEntity())) - return; - - if (event instanceof EntityDamageByEntityEvent) { - EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) event; - if (e.getDamager() instanceof Player) { - NPC npc = CitizensAPI.getNPCManager().getNPC(event.getEntity()); - npc.getCharacter().onLeftClick(npc, (Player) e.getDamager()); - } - } - } - - @EventHandler(event = EntityTargetEvent.class, priority = EventPriority.NORMAL) - public void onEntityTarget(EntityTargetEvent event) { - if (event.isCancelled() || !CitizensAPI.getNPCManager().isNPC(event.getEntity()) - || !(event.getTarget() instanceof Player)) - return; - - NPC npc = CitizensAPI.getNPCManager().getNPC(event.getEntity()); - npc.getCharacter().onRightClick(npc, (Player) event.getTarget()); - } -} \ No newline at end of file diff --git a/src/net/citizensnpcs/listener/WorldListen.java b/src/net/citizensnpcs/listener/WorldListen.java deleted file mode 100644 index 4c1f318ce..000000000 --- a/src/net/citizensnpcs/listener/WorldListen.java +++ /dev/null @@ -1,43 +0,0 @@ -package net.citizensnpcs.listener; - -import java.util.HashSet; -import java.util.Set; - -import net.citizensnpcs.api.CitizensAPI; -import net.citizensnpcs.api.npc.NPC; -import net.citizensnpcs.api.npc.trait.trait.LocationTrait; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.world.ChunkLoadEvent; -import org.bukkit.event.world.ChunkUnloadEvent; - -public class WorldListen implements Listener { - private Set toRespawn = new HashSet(); - - @EventHandler(event = ChunkLoadEvent.class, priority = EventPriority.NORMAL) - public void onChunkLoad(ChunkLoadEvent event) { - for (int id : toRespawn) { - NPC npc = CitizensAPI.getNPCManager().getNPC(id); - npc.spawn(((LocationTrait) npc.getTrait("location")).getLocation()); - toRespawn.remove(id); - } - } - - @EventHandler(event = ChunkLoadEvent.class, priority = EventPriority.NORMAL) - public void onChunkUnload(ChunkUnloadEvent event) { - if (event.isCancelled()) - return; - - for (NPC npc : CitizensAPI.getNPCManager().getNPCs()) { - LocationTrait loc = (LocationTrait) npc.getTrait("location"); - if (event.getWorld().equals(loc.getLocation().getWorld()) - && event.getChunk().getX() == loc.getLocation().getChunk().getX() - && event.getChunk().getZ() == loc.getLocation().getChunk().getZ()) { - toRespawn.add(npc.getId()); - npc.despawn(); - } - } - } -} \ No newline at end of file