added some events

This commit is contained in:
aPunch 2012-01-18 04:35:15 -06:00
parent 4aafac4604
commit b923781a2c
3 changed files with 70 additions and 4 deletions

View File

@ -3,11 +3,13 @@ package net.citizensnpcs;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.trait.trait.LocationTrait; import net.citizensnpcs.api.npc.trait.trait.LocationTrait;
import net.citizensnpcs.listener.EntityListen;
import net.citizensnpcs.listener.WorldListen; import net.citizensnpcs.listener.WorldListen;
import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.Messaging;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public class Citizens extends JavaPlugin { public class Citizens extends JavaPlugin {
@ -23,7 +25,7 @@ public class Citizens extends JavaPlugin {
npcManager = new CitizensNPCManager(); npcManager = new CitizensNPCManager();
CitizensAPI.setNPCManager(npcManager); CitizensAPI.setNPCManager(npcManager);
registerEvents(); registerEvents(getServer().getPluginManager());
Messaging.log("v" + getDescription().getVersion() + " enabled."); Messaging.log("v" + getDescription().getVersion() + " enabled.");
@ -46,7 +48,8 @@ public class Citizens extends JavaPlugin {
Messaging.log("Loaded " + npcManager.getNPCs().size() + " NPCs."); Messaging.log("Loaded " + npcManager.getNPCs().size() + " NPCs.");
} }
private void registerEvents() { private void registerEvents(PluginManager pm) {
getServer().getPluginManager().registerEvents(new WorldListen(), this); pm.registerEvents(new EntityListen(), this);
pm.registerEvents(new WorldListen(), this);
} }
} }

View File

@ -0,0 +1,40 @@
package net.citizensnpcs.listener;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.entity.LivingEntity;
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((LivingEntity) event.getEntity()))
return;
if (event instanceof EntityDamageByEntityEvent) {
EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) event;
if (e.getDamager() instanceof Player) {
NPC npc = CitizensAPI.getNPCManager().getNPC((LivingEntity) 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((LivingEntity) event.getEntity())
|| !(event.getTarget() instanceof Player))
return;
NPC npc = CitizensAPI.getNPCManager().getNPC((LivingEntity) event.getEntity());
npc.getCharacter().onRightClick(npc, (Player) event.getTarget());
}
}

View File

@ -1,5 +1,12 @@
package net.citizensnpcs.listener; 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.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -7,14 +14,30 @@ import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent; import org.bukkit.event.world.ChunkUnloadEvent;
public class WorldListen implements Listener { public class WorldListen implements Listener {
private Set<Integer> toRespawn = new HashSet<Integer>();
@EventHandler(event = ChunkLoadEvent.class, priority = EventPriority.NORMAL) @EventHandler(event = ChunkLoadEvent.class, priority = EventPriority.NORMAL)
public void onChunkLoad(ChunkLoadEvent event) { 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) @EventHandler(event = ChunkLoadEvent.class, priority = EventPriority.NORMAL)
public void onChunkUnload(ChunkUnloadEvent event) { 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();
}
}
} }
} }