Skip to content

Commit 7f92566

Browse files
committed
Sentinel ProtectFromRange
1 parent ac3079d commit 7f92566

6 files changed

Lines changed: 45 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ Sentinel is integrated into by external plugins as well, including:
221221
- /sentinel squad SQUAD - Sets the NPC's squad name (null for none). NPCs with the same squad name share aggro (if a player angers one NPC, the rest get angry too).
222222
- /sentinel reach REACH - Sets the NPC's reach (how far it can punch).
223223
- /sentinel projectilerange RANGE - Sets the NPC's projectile range (how far it is willing to shoot projectiles).
224+
- /sentinel protectfromrange RANGE - Sets the maximum range after which damage should start being ignored. If zero, this does nothing. Set eg 100 to ignore all damage from over 100 blocks away.
224225
- /sentinel avoidreturnpoint - Changes the location the NPC runs to when avoid mode is activated, or removes it if the NPC is already there.
225226
- **Toggleable NPC configuration commands:**
226227
- /sentinel invincible \['true'/'false'\] - Toggles whether the NPC is invincible.

src/main/java/org/mcmonkey/sentinel/SentinelTrait.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,15 @@ public void removeAvoid(String target) {
372372
/**
373373
* Whether the NPC should be protected from damage by ignore targets.
374374
*/
375-
@Persist ("protected")
375+
@Persist("protected")
376376
public boolean protectFromIgnores = false;
377377

378+
/**
379+
* Maximum distance after which damage starts being ignored.
380+
*/
381+
@Persist("protectFromRange")
382+
public double protectFromRange = 0;
383+
378384
/**
379385
* Whether the NPC "fights back" against attacks (targets anyone that damages it).
380386
*/
@@ -977,6 +983,14 @@ else if (projectileSource != null && targetingHelper.isIgnored(projectileSource)
977983
return;
978984
}
979985
}
986+
if (isMe && protectFromRange > 0) {
987+
Location loc1 = damager.getLocation();
988+
Location loc2 = getLivingEntity().getLocation();
989+
if (loc1.getWorld() != loc2.getWorld() || loc1.distanceSquared(loc2) > protectFromRange * protectFromRange) {
990+
event.setCancelled(true);
991+
return;
992+
}
993+
}
980994
boolean isKilling = event.getEntity() instanceof LivingEntity && event.getFinalDamage() >= ((LivingEntity) event.getEntity()).getHealth();
981995
boolean isFriend = getGuarding() != null && SentinelUtilities.uuidEquals(event.getEntity().getUniqueId(), getGuarding());
982996
boolean attackerIsMe = damager.getUniqueId().equals(getLivingEntity().getUniqueId());

src/main/java/org/mcmonkey/sentinel/commands/SentinelInfoCommands.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ else if (sentinel.getGuarding() != null) {
6464
addLineIfNeeded(paginator, "Guard Selection Range", sentinel.guardSelectionRange);
6565
addLineIfNeeded(paginator, "Invincibility Enabled", sentinel.invincible);
6666
addLineIfNeeded(paginator, "Protected Enabled", sentinel.protectFromIgnores);
67+
addLineIfNeeded(paginator, "Protect From Range", sentinel.protectFromRange);
6768
addLineIfNeeded(paginator, "Fightback Enabled", sentinel.fightback);
6869
addLineIfNeeded(paginator, "Ranged Chasing Enabled", sentinel.rangedChase);
6970
addLineIfNeeded(paginator, "Close-Quarters Chasing Enabled", sentinel.closeChase);

src/main/java/org/mcmonkey/sentinel/commands/SentinelTargetCommands.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,30 @@ public void protectedFromIgnores(CommandContext args, CommandSender sender, Sent
289289
}
290290
}
291291

292+
@Command(aliases = {"sentinel"}, usage = "protectfromrange RANGE",
293+
desc = "Sets the maximum range after which damage should start being ignored. If zero, this does nothing. Set eg 100 to ignore all damage from over 100 blocks away.",
294+
modifiers = { "protectfromrange" }, permission = "sentinel.protectfromrange", min = 1, max = 2)
295+
@Requirements(livingEntity = true, ownership = true, traits = {SentinelTrait.class})
296+
public void protectFromRange(CommandContext args, CommandSender sender, SentinelTrait sentinel) {
297+
if (args.argsLength() <= 1) {
298+
sender.sendMessage(SentinelCommand.prefixGood + "Current protect-from-range: " + SentinelCommand.colorEmphasis + sentinel.protectFromRange);
299+
return;
300+
}
301+
try {
302+
double d = Double.parseDouble(args.getString(1));
303+
if (d >= 0 && d < 500) {
304+
sentinel.protectFromRange = d;
305+
sender.sendMessage(SentinelCommand.prefixGood + "Protect-from-range set!");
306+
}
307+
else {
308+
throw new NumberFormatException("Number out of range (must be >= 0 and < 500).");
309+
}
310+
}
311+
catch (NumberFormatException ex) {
312+
sender.sendMessage(SentinelCommand.prefixBad + "Invalid range number: " + ex.getMessage());
313+
}
314+
}
315+
292316
@Command(aliases = {"sentinel"}, usage = "fightback ['true'/'false']",
293317
desc = "Toggles whether the NPC will fight back.",
294318
modifiers = {"fightback"}, permission = "sentinel.fightback", min = 1, max = 2)

src/main/resources/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ sentinel defaults:
2121
heal rate: 100
2222
# Whether the NPC should be protected from damage by ignore targets.
2323
protected: true
24+
# Maximum range after which damage should start being ignored. If zero, this does nothing. Set eg 100 to ignore all damage from over 100 blocks away.
25+
protect from range: 0
2426
# Whether this NPC fights back.
2527
fightback: true
2628
# Whether this NPC needs ammo to fight with.

src/main/resources/plugin.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ permissions:
5858
sentinel.invincible: true
5959
# /sentinel protected
6060
sentinel.protected: true
61+
# /sentinel protectfromrange
62+
sentinel.protectfromrange: true
6163
# /sentinel fightback
6264
sentinel.fightback: true
6365
# /sentinel runaway

0 commit comments

Comments
 (0)