Search in sources :

Example 1 with NPCDamageByBlockEvent

use of net.citizensnpcs.api.event.NPCDamageByBlockEvent in project Citizens2 by CitizensDev.

the class EventListen method onEntityDamage.

@EventHandler
public void onEntityDamage(EntityDamageEvent event) {
    NPC npc = npcRegistry.getNPC(event.getEntity());
    if (npc == null) {
        if (event instanceof EntityDamageByEntityEvent) {
            npc = npcRegistry.getNPC(((EntityDamageByEntityEvent) event).getDamager());
            if (npc == null)
                return;
            event.setCancelled(!npc.data().get(NPC.DAMAGE_OTHERS_METADATA, true));
            NPCDamageEntityEvent damageEvent = new NPCDamageEntityEvent(npc, (EntityDamageByEntityEvent) event);
            Bukkit.getPluginManager().callEvent(damageEvent);
        }
        return;
    }
    event.setCancelled(npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true));
    if (event instanceof EntityDamageByEntityEvent) {
        NPCDamageByEntityEvent damageEvent = new NPCDamageByEntityEvent(npc, (EntityDamageByEntityEvent) event);
        Bukkit.getPluginManager().callEvent(damageEvent);
        if (!damageEvent.isCancelled() || !(damageEvent.getDamager() instanceof Player))
            return;
        Player damager = (Player) damageEvent.getDamager();
        NPCLeftClickEvent leftClickEvent = new NPCLeftClickEvent(npc, damager);
        Bukkit.getPluginManager().callEvent(leftClickEvent);
    } else if (event instanceof EntityDamageByBlockEvent) {
        Bukkit.getPluginManager().callEvent(new NPCDamageByBlockEvent(npc, (EntityDamageByBlockEvent) event));
    } else {
        Bukkit.getPluginManager().callEvent(new NPCDamageEvent(npc, event));
    }
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) NPCDamageByEntityEvent(net.citizensnpcs.api.event.NPCDamageByEntityEvent) NPCLeftClickEvent(net.citizensnpcs.api.event.NPCLeftClickEvent) NPCDamageEvent(net.citizensnpcs.api.event.NPCDamageEvent) Player(org.bukkit.entity.Player) NPCDamageEntityEvent(net.citizensnpcs.api.event.NPCDamageEntityEvent) NPCDamageByBlockEvent(net.citizensnpcs.api.event.NPCDamageByBlockEvent) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) EntityDamageByBlockEvent(org.bukkit.event.entity.EntityDamageByBlockEvent) EventHandler(org.bukkit.event.EventHandler)

Aggregations

NPCDamageByBlockEvent (net.citizensnpcs.api.event.NPCDamageByBlockEvent)1 NPCDamageByEntityEvent (net.citizensnpcs.api.event.NPCDamageByEntityEvent)1 NPCDamageEntityEvent (net.citizensnpcs.api.event.NPCDamageEntityEvent)1 NPCDamageEvent (net.citizensnpcs.api.event.NPCDamageEvent)1 NPCLeftClickEvent (net.citizensnpcs.api.event.NPCLeftClickEvent)1 NPC (net.citizensnpcs.api.npc.NPC)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 EntityDamageByBlockEvent (org.bukkit.event.entity.EntityDamageByBlockEvent)1 EntityDamageByEntityEvent (org.bukkit.event.entity.EntityDamageByEntityEvent)1