Add easy way to control whether to protect NPCs

This commit is contained in:
fullwall 2012-09-01 23:28:56 +08:00
parent a0573c8997
commit 6d0086d54e
2 changed files with 16 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package net.citizensnpcs;
import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.EntityTargetNPCEvent;
import net.citizensnpcs.api.event.NPCCombustByBlockEvent; import net.citizensnpcs.api.event.NPCCombustByBlockEvent;
import net.citizensnpcs.api.event.NPCCombustByEntityEvent; import net.citizensnpcs.api.event.NPCCombustByEntityEvent;
import net.citizensnpcs.api.event.NPCCombustEvent; import net.citizensnpcs.api.event.NPCCombustEvent;
@ -37,7 +38,6 @@ import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityTargetEvent; import org.bukkit.event.entity.EntityTargetEvent;
import org.bukkit.event.entity.EntityTargetEvent.TargetReason;
import org.bukkit.event.player.PlayerChangedWorldEvent; import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent; import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
@ -92,6 +92,7 @@ public class EventListen implements Listener {
NPC npc = npcRegistry.getNPC(event.getEntity()); NPC npc = npcRegistry.getNPC(event.getEntity());
if (npc == null) if (npc == null)
return; return;
event.setCancelled(npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true));
if (event instanceof EntityCombustByEntityEvent) { if (event instanceof EntityCombustByEntityEvent) {
Bukkit.getPluginManager().callEvent( Bukkit.getPluginManager().callEvent(
new NPCCombustByEntityEvent((EntityCombustByEntityEvent) event, npc)); new NPCCombustByEntityEvent((EntityCombustByEntityEvent) event, npc));
@ -109,6 +110,7 @@ public class EventListen implements Listener {
return; return;
NPC npc = npcRegistry.getNPC(event.getEntity()); NPC npc = npcRegistry.getNPC(event.getEntity());
event.setCancelled(npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true));
if (event instanceof EntityDamageByEntityEvent) { if (event instanceof EntityDamageByEntityEvent) {
NPCDamageByEntityEvent damageEvent = new NPCDamageByEntityEvent(npc, NPCDamageByEntityEvent damageEvent = new NPCDamageByEntityEvent(npc,
(EntityDamageByEntityEvent) event); (EntityDamageByEntityEvent) event);
@ -145,13 +147,11 @@ public class EventListen implements Listener {
@EventHandler @EventHandler
public void onEntityTarget(EntityTargetEvent event) { public void onEntityTarget(EntityTargetEvent event) {
if (npcRegistry.isNPC(event.getEntity()) && event.getTarget() instanceof Player) { if (npcRegistry.isNPC(event.getTarget())) {
NPC npc = npcRegistry.getNPC(event.getEntity()); NPC npc = npcRegistry.getNPC(event.getTarget());
Player player = (Player) event.getTarget();
// Call right-click event event.setCancelled(npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true));
NPCRightClickEvent rightClickEvent = new NPCRightClickEvent(npc, player); Bukkit.getPluginManager().callEvent(new EntityTargetNPCEvent(event, npc));
Bukkit.getPluginManager().callEvent(rightClickEvent);
} }
} }
@ -195,14 +195,17 @@ public class EventListen implements Listener {
} }
} }
@EventHandler(ignoreCancelled = true) @EventHandler
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if (!npcRegistry.isNPC(event.getRightClicked())) NPC npc = npcRegistry.getNPC(event.getRightClicked());
if (npc == null)
return; return;
// Call target event for NPCs Player player = event.getPlayer();
Bukkit.getPluginManager().callEvent(
new EntityTargetEvent(event.getRightClicked(), event.getPlayer(), TargetReason.CUSTOM)); // Call right-click event
NPCRightClickEvent rightClickEvent = new NPCRightClickEvent(npc, player);
Bukkit.getPluginManager().callEvent(rightClickEvent);
} }
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)

View File

@ -35,6 +35,7 @@ public class Util {
public static NPCPushEvent callPushEvent(NPC npc, Vector vector) { public static NPCPushEvent callPushEvent(NPC npc, Vector vector) {
NPCPushEvent event = new NPCPushEvent(npc, vector); NPCPushEvent event = new NPCPushEvent(npc, vector);
event.setCancelled(npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true));
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
return event; return event;
} }