mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-11-23 02:55:45 +01:00
update to latest Bukkit event API
This commit is contained in:
parent
7d10c522ec
commit
5aec593858
Binary file not shown.
@ -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);
|
||||
}
|
||||
}
|
80
src/net/citizensnpcs/EventListen.java
Normal file
80
src/net/citizensnpcs/EventListen.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user