Skip to content
Permalink
Browse files

attack hostiles to the side when chasing a primary target, for #153

  • Loading branch information...
mcmonkey4eva committed Mar 14, 2019
1 parent 7953ab5 commit de6a0f70b2009c3e8bdd3912038044e61e47f5e3
@@ -57,13 +57,41 @@ public void rechase() {

/**
* Causes the NPC to attempt an attack on a target.
* Returns whether any attack occurred.
*/
public void tryAttack(LivingEntity entity) {
public boolean tryAttack(LivingEntity target) {
if (tryAttackInternal(target)) {
return true;
}
LivingEntity quickTarget = targetingHelper.findQuickMeleeTarget();
if (quickTarget != null) {
if (itemHelper.isRanged()) {
if (!sentinel.autoswitch) {
return false;
}
itemHelper.swapToMelee();
if (itemHelper.isRanged()) {
return false;
}
}
if (tryAttackInternal(quickTarget)) {
chase(target);
return true;
}
chase(target);
}
return false;
}

/**
* Internal attack attempt logic.
*/
public boolean tryAttackInternal(LivingEntity entity) {
if (!entity.getWorld().equals(getLivingEntity().getWorld())) {
return;
return false;
}
if (!getLivingEntity().hasLineOfSight(entity)) {
return;
return false;
}
// TODO: Simplify this code!
sentinel.stats_attackAttempts++;
@@ -84,12 +112,12 @@ else if (sentinel.autoswitch && dist < sentinel.reach * sentinel.reach) {
if (SentinelPlugin.debugMe) {
debug("tryAttack refused, event cancellation");
}
return;
return false;
}
targetingHelper.addTarget(entity.getUniqueId());
for (SentinelIntegration si : SentinelPlugin.integrations) {
if (si.tryAttack(sentinel, entity)) {
return;
return true;
}
}
if (itemHelper.usesBow()) {
@@ -98,7 +126,7 @@ else if (sentinel.autoswitch && dist < sentinel.reach * sentinel.reach) {
if (sentinel.rangedChase) {
rechase();
}
return;
return false;
}
sentinel.timeSinceAttack = 0;
ItemStack item = itemHelper.getArrow();
@@ -109,10 +137,12 @@ else if (sentinel.autoswitch && dist < sentinel.reach * sentinel.reach) {
itemHelper.takeArrow();
itemHelper.grabNextItem();
}
return true;
}
}
else if (sentinel.rangedChase) {
chase(entity);
return false;
}
}
else if (itemHelper.usesSnowball()) {
@@ -121,7 +151,7 @@ else if (itemHelper.usesSnowball()) {
if (sentinel.rangedChase) {
rechase();
}
return;
return false;
}
sentinel.timeSinceAttack = 0;
ItemStack item = itemHelper.getArrow();
@@ -131,10 +161,12 @@ else if (itemHelper.usesSnowball()) {
itemHelper.takeSnowball();
itemHelper.grabNextItem();
}
return true;
}
}
else if (sentinel.rangedChase) {
chase(entity);
return false;
}
}
else if (itemHelper.usesPotion()) {
@@ -143,7 +175,7 @@ else if (itemHelper.usesPotion()) {
if (sentinel.rangedChase) {
rechase();
}
return;
return false;
}
sentinel.timeSinceAttack = 0;
if (SentinelTarget.v1_9) {
@@ -158,9 +190,11 @@ else if (itemHelper.usesPotion()) {
itemHelper.takeOne();
itemHelper.grabNextItem();
}
return true;
}
else if (sentinel.rangedChase) {
chase(entity);
return false;
}
}
else if (itemHelper.usesEgg()) {
@@ -169,17 +203,19 @@ else if (itemHelper.usesEgg()) {
if (sentinel.rangedChase) {
rechase();
}
return;
return false;
}
sentinel.timeSinceAttack = 0;
weaponHelper.fireEgg(entity.getEyeLocation());
if (sentinel.needsAmmo) {
itemHelper.takeOne();
itemHelper.grabNextItem();
}
return true;
}
else if (sentinel.rangedChase) {
chase(entity);
return false;
}
}
else if (itemHelper.usesPearl()) {
@@ -188,17 +224,19 @@ else if (itemHelper.usesPearl()) {
if (sentinel.rangedChase) {
rechase();
}
return;
return false;
}
sentinel.timeSinceAttack = 0;
weaponHelper.firePearl(entity);
if (sentinel.needsAmmo) {
itemHelper.takeOne();
itemHelper.grabNextItem();
}
return true;
}
else if (sentinel.rangedChase) {
chase(entity);
return false;
}
}
else if (itemHelper.usesWitherSkull()) {
@@ -207,17 +245,19 @@ else if (itemHelper.usesWitherSkull()) {
if (sentinel.rangedChase) {
rechase();
}
return;
return false;
}
sentinel.timeSinceAttack = 0;
weaponHelper.fireSkull(entity.getEyeLocation());
if (sentinel.needsAmmo) {
itemHelper.takeOne();
itemHelper.grabNextItem();
}
return true;
}
else if (sentinel.rangedChase) {
chase(entity);
return false;
}
}
else if (itemHelper.usesFireball()) {
@@ -226,17 +266,19 @@ else if (itemHelper.usesFireball()) {
if (sentinel.rangedChase) {
rechase();
}
return;
return false;
}
sentinel.timeSinceAttack = 0;
weaponHelper.fireFireball(entity.getEyeLocation());
if (sentinel.needsAmmo) {
itemHelper.takeOne();
itemHelper.grabNextItem();
}
return true;
}
else if (sentinel.rangedChase) {
chase(entity);
return false;
}
}
else if (itemHelper.usesLightning()) {
@@ -245,7 +287,7 @@ else if (itemHelper.usesLightning()) {
if (sentinel.rangedChase) {
rechase();
}
return;
return false;
}
sentinel.timeSinceAttack = 0;
sentinel.swingWeapon();
@@ -258,9 +300,11 @@ else if (itemHelper.usesLightning()) {
itemHelper.takeOne();
itemHelper.grabNextItem();
}
return true;
}
else if (sentinel.rangedChase) {
chase(entity);
return false;
}
}
else if (itemHelper.usesSpectral()) {
@@ -269,7 +313,7 @@ else if (itemHelper.usesSpectral()) {
if (sentinel.rangedChase) {
rechase();
}
return;
return false;
}
sentinel.timeSinceAttack = 0;
if (!entity.isGlowing()) {
@@ -288,10 +332,12 @@ else if (itemHelper.usesSpectral()) {
itemHelper.takeOne();
itemHelper.grabNextItem();
}
return true;
}
}
else if (sentinel.rangedChase) {
chase(entity);
return false;
}
}
else {
@@ -303,7 +349,7 @@ else if (sentinel.rangedChase) {
if (sentinel.closeChase) {
rechase();
}
return;
return false;
}
sentinel.timeSinceAttack = 0;
// TODO: Damage sword if needed!
@@ -315,13 +361,16 @@ else if (sentinel.rangedChase) {
itemHelper.reduceDurability();
itemHelper.grabNextItem();
}
return true;
}
else if (sentinel.closeChase) {
if (SentinelPlugin.debugMe) {
debug("tryAttack refused, range");
}
chase(entity);
return false;
}
}
return false;
}
}
@@ -415,6 +415,21 @@ public LivingEntity findBestTarget() {
return closest;
}

/**
* Finds a nearby target that can be hit with a melee attack.
*/
public LivingEntity findQuickMeleeTarget() {
double range = sentinel.reach * 0.75;
Location pos = getLivingEntity().getEyeLocation();
for (Entity loopEnt : getLivingEntity().getWorld().getNearbyEntities(pos, range, range, range)) {
if (loopEnt instanceof LivingEntity && shouldTarget((LivingEntity) loopEnt)
&& canSee((LivingEntity) loopEnt)) {
return (LivingEntity) loopEnt;
}
}
return null;
}

/**
* Updates the current avoids set for the NPC.
*/

0 comments on commit de6a0f7

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