Skip to content
Permalink
Browse files

Add SentinelNoMoreTargetsEvent for #266

  • Loading branch information...
mcmonkey4eva committed Mar 14, 2019
1 parent 9414fc2 commit 7953ab5683ddb307634f933ca8c18d089417f380
@@ -0,0 +1,37 @@
package org.mcmonkey.sentinel.events;

import net.citizensnpcs.api.event.NPCEvent;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.event.HandlerList;

/**
* Event fired when a Sentinel NPC has no targets remaining.
*/
public class SentinelNoMoreTargetsEvent extends NPCEvent {

/**
* Handler objects, for Bukkit internal usage.
*/
private static final HandlerList handlers = new HandlerList();

/**
* Constructs the attack event.
*/
public SentinelNoMoreTargetsEvent(NPC npc) {
super(npc);
}

/**
* Returns the handler list for use with Bukkit.
*/
public HandlerList getHandlers() {
return handlers;
}

/**
* Returns the handler list for use with Bukkit.
*/
public static HandlerList getHandlerList() {
return handlers;
}
}
@@ -4,13 +4,15 @@
import net.citizensnpcs.api.astar.AStarMachine;
import net.citizensnpcs.api.astar.pathfinder.*;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.mcmonkey.sentinel.*;
import org.mcmonkey.sentinel.events.SentinelNoMoreTargetsEvent;

import java.util.ArrayList;
import java.util.HashSet;
@@ -118,20 +120,37 @@ public void addTarget(UUID id) {
public boolean removeTarget(UUID id) {
SentinelCurrentTarget target = new SentinelCurrentTarget();
target.targetID = id;
boolean removed = currentTargets.remove(target);
boolean removed = removeTargetNoBounce(target);
if (removed && sentinel.squad != null) {
for (NPC npc : CitizensAPI.getNPCRegistry()) {
if (npc.hasTrait(SentinelTrait.class)) {
SentinelTrait squadMade = npc.getTrait(SentinelTrait.class);
if (squadMade.squad != null && squadMade.squad.equals(sentinel.squad)) {
sentinel.targetingHelper.currentTargets.remove(target);
sentinel.targetingHelper.removeTargetNoBounce(target);
}
}
}
}
return removed;
}

/**
* Removes a target directly from the NPC. Prefer {@code removeTarget} over this in most cases.
* Returns whether anything was removed.
*/
public boolean removeTargetNoBounce(SentinelCurrentTarget target) {
if (currentTargets.isEmpty()) {
return false;
}
if (currentTargets.remove(target)) {
if (currentTargets.isEmpty()) {
Bukkit.getPluginManager().callEvent(new SentinelNoMoreTargetsEvent(getNPC()));
}
return true;
}
return false;
}

/**
* Adds a target directly to the NPC. Prefer {@code addTarget} over this in most cases.
*/

0 comments on commit 7953ab5

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