mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-12-27 03:28:45 +01:00
added some events
This commit is contained in:
parent
4aafac4604
commit
b923781a2c
@ -3,11 +3,13 @@ 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 {
|
||||
@ -23,7 +25,7 @@ public class Citizens extends JavaPlugin {
|
||||
npcManager = new CitizensNPCManager();
|
||||
CitizensAPI.setNPCManager(npcManager);
|
||||
|
||||
registerEvents();
|
||||
registerEvents(getServer().getPluginManager());
|
||||
|
||||
Messaging.log("v" + getDescription().getVersion() + " enabled.");
|
||||
|
||||
@ -46,7 +48,8 @@ public class Citizens extends JavaPlugin {
|
||||
Messaging.log("Loaded " + npcManager.getNPCs().size() + " NPCs.");
|
||||
}
|
||||
|
||||
private void registerEvents() {
|
||||
getServer().getPluginManager().registerEvents(new WorldListen(), this);
|
||||
private void registerEvents(PluginManager pm) {
|
||||
pm.registerEvents(new EntityListen(), this);
|
||||
pm.registerEvents(new WorldListen(), this);
|
||||
}
|
||||
}
|
40
src/net/citizensnpcs/listener/EntityListen.java
Normal file
40
src/net/citizensnpcs/listener/EntityListen.java
Normal 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());
|
||||
}
|
||||
}
|
@ -1,5 +1,12 @@
|
||||
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;
|
||||
@ -7,14 +14,30 @@ 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