update to latest Bukkit event API

This commit is contained in:
aPunch 2012-01-19 01:38:40 -06:00
parent 7d10c522ec
commit 5aec593858
5 changed files with 82 additions and 91 deletions

Binary file not shown.

View File

@ -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);
}
}

View File

@ -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<Integer> toRespawn = new HashSet<Integer>();
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();
}
}
}
}

View File

@ -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());
}
}

View File

@ -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<Integer> toRespawn = new HashSet<Integer>();
@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();
}
}
}
}