Skip to content
Permalink
Browse files

reorganize events to event handler class

  • Loading branch information...
mcmonkey4eva committed Mar 23, 2019
1 parent d2d8cf4 commit 0f4828bf250d96902a50c1c9b3ea5add3e762c4a
@@ -4,11 +4,18 @@
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Projectile;
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.EntityDeathEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.projectiles.ProjectileSource;

import java.util.UUID;

public class SentinelEventHandler implements Listener {

@@ -26,18 +33,145 @@ public void run() {
if (!event.getPlayer().isOnline()) {
return;
}
for (NPC npc : CitizensAPI.getNPCRegistry()) {
if (npc.hasTrait(SentinelTrait.class)) {
SentinelTrait sentinel = npc.getTrait(SentinelTrait.class);
if (sentinel.allTargets.isEventTarget(event)) {
sentinel.targetingHelper.addTarget(event.getPlayer().getUniqueId());
}
if (sentinel.allAvoids.isEventTarget(event)) {
sentinel.targetingHelper.addAvoid(event.getPlayer().getUniqueId());
}
for (SentinelTrait sentinel : SentinelPlugin.instance.currentSentinelNPCs) {
if (sentinel.allTargets.isEventTarget(event)) {
sentinel.targetingHelper.addTarget(event.getPlayer().getUniqueId());
}
if (sentinel.allAvoids.isEventTarget(event)) {
sentinel.targetingHelper.addAvoid(event.getPlayer().getUniqueId());
}
}
}
});
}

/**
* Tries to get a Sentinel from an entity. Returns null if it is not a Sentinel.
*/
public SentinelTrait tryGetSentinel(Entity entity) {
if (CitizensAPI.getNPCRegistry().isNPC(entity)) {
NPC npc = CitizensAPI.getNPCRegistry().getNPC(entity);
if (npc.hasTrait(SentinelTrait.class)) {
return npc.getTrait(SentinelTrait.class);
}
}
return null;
}

/**
* Called when combat occurs in the world (and has not yet been processed by other plugins),
* to handle things like cancelling invalid damage to/from a Sentinel NPC,
* changing damage values given to or received from an NPC,
* and if relevant handling config options that require overriding damage events.
*/
@EventHandler(priority = EventPriority.LOW)
public void whenAttacksAreHappening(EntityDamageByEntityEvent event) {
if (event.isCancelled()) {
return;
}
UUID victimUuid = event.getEntity().getUniqueId();
for (SentinelTrait sentinel : SentinelPlugin.instance.currentSentinelNPCs) {
sentinel.whenSomethingMightDie(victimUuid);
}
SentinelTrait victim = tryGetSentinel(event.getEntity());
if (victim != null) {
victim.whenAttacksAreHappeningToMe(event);
}
SentinelTrait attacker = tryGetSentinel(event.getDamager());
if (attacker != null) {
attacker.whenAttacksAreHappeningFromMe(event);
}
if (event.getDamager() instanceof Projectile) {
ProjectileSource source = ((Projectile) event.getDamager()).getShooter();
if (source instanceof Entity) {
SentinelTrait shooter = tryGetSentinel((Entity) source);
if (shooter != null) {
shooter.whenAttacksAreHappeningFromMyArrow(event);
}
}
}
}

/**
* Called when combat has occurred in the world (and has been processed by all other plugins), to handle things like cancelling invalid damage to/from a Sentinel NPC,
* adding targets (if combat occurs near an NPC), and if relevant handling config options that require overriding damage events.
*/
@EventHandler(priority = EventPriority.LOWEST)
public void whenAttacksHappened(EntityDamageByEntityEvent event) {
if (event.isCancelled()) {
return;
}
SentinelTrait victim = tryGetSentinel(event.getEntity());
if (victim != null) {
victim.whenAttacksHappened(event);
}
SentinelTrait attacker = tryGetSentinel(event.getDamager());
if (attacker != null) {
attacker.whenAttacksHappened(event);
}
if (event.getDamager() instanceof Projectile) {
ProjectileSource source = ((Projectile) event.getDamager()).getShooter();
if (source instanceof Entity) {
SentinelTrait shooter = tryGetSentinel((Entity) source);
if (shooter != null) {
shooter.whenAttacksHappened(event);
}
}
}
for (SentinelTrait sentinel : SentinelPlugin.instance.currentSentinelNPCs) {
UUID guarding = sentinel.getGuarding();
if (guarding != null) {
if (event.getEntity().getUniqueId().equals(guarding)) {
sentinel.whenAttacksHappened(event);
}
}
}
}

/**
* Called when any entity dies, to process drops handling and targeting updates.
*/
@EventHandler
public void whenAnEnemyDies(EntityDeathEvent event) {
UUID dead = event.getEntity().getUniqueId();
for (SentinelTrait sentinel : SentinelPlugin.instance.currentSentinelNPCs) {
sentinel.whenAnEnemyDies(dead);
sentinel.whenSomethingDies(event);
if (sentinel.getLivingEntity().getUniqueId().equals(dead)) {
sentinel.whenWeDie(event);
}
}
}

/**
* Called when a player teleports, to handle NPC guard updates.
*/
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerTeleports(final PlayerTeleportEvent event) {
if (event.isCancelled()) {
return;
}
UUID uuid = event.getPlayer().getUniqueId();
for (SentinelTrait sentinel : SentinelPlugin.instance.currentSentinelNPCs) {
if (sentinel.getGuarding() != null && sentinel.getGuarding().equals(uuid)) {
sentinel.onPlayerTeleports(event);
}
}
}

/**
* Called every time a player moves at all, for use with monitoring if players move into range of an NPC.
*/
@EventHandler
public void onPlayerMovesInRange(PlayerMoveEvent event) {
if (event.isCancelled()) {
return;
}
if (event.getTo().toVector().equals(event.getFrom().toVector())) {
return;
}
for (SentinelTrait sentinel : SentinelPlugin.instance.currentSentinelNPCs) {
sentinel.onPlayerMovesInRange(event);
}
}
}
@@ -59,6 +59,11 @@
*/
public static SentinelPlugin instance;

/**
* A list of all currently spawned Sentinel NPCs.
*/
public ArrayList<SentinelTrait> currentSentinelNPCs = new ArrayList<>();

/**
* Permissions handler.
*/

0 comments on commit 0f4828b

Please sign in to comment.
You can’t perform that action at this time.