From 0f61b5f5615f98f74f62231089d7b3283cae9061 Mon Sep 17 00:00:00 2001 From: Ethan Sovde Date: Mon, 27 Mar 2023 21:52:40 -0400 Subject: [PATCH 1/6] Create CondHasLineOfSight.java --- .../skript/conditions/CondHasLineOfSight.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java diff --git a/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java b/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java new file mode 100644 index 00000000000..d7ef72752b4 --- /dev/null +++ b/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java @@ -0,0 +1,52 @@ +package ch.njol.skript.conditions; + +import ch.njol.skript.Skript; +import ch.njol.skript.lang.Condition; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.util.Kleenean; +import org.bukkit.Location; +import org.bukkit.entity.Entity; +import org.bukkit.entity.LivingEntity; +import org.bukkit.event.Event; +import org.eclipse.jdt.annotation.Nullable; + +public class CondHasLineOfSight extends Condition { + + static { + Skript.registerCondition(CondHasLineOfSight.class, + "%livingentities% (has|have) [a] [direct] line of sight to %entities/locations%", + "%livingentities% does(n't| not) have [a] [direct] line of sight to %entities/locations%", + "%livingentities% has no [direct] line of sight to %entities/locations%"); + } + + private Expression viewers; + private Expression targets; + + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + viewers = (Expression) exprs[0]; + targets = exprs[1]; + if (matchedPattern > 0) setNegated(true); + return true; + } + + @Override + public boolean check(Event event) { + return targets.check(event, (target) -> { + if (target instanceof Entity) { + return viewers.check(event, (viewer) -> viewer.hasLineOfSight((Entity) target), isNegated()); + } else if (target instanceof Location) { + return viewers.check(event, (viewer) -> viewer.hasLineOfSight((Location) target), isNegated()); + } else { + return isNegated(); + } + }); + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return viewers.toString(event, debug) + " has " + (isNegated() ? "no" : "")+ " line of sight to " + targets.toString(event,debug); + } + +} From 5654eca54b377a9cf6b91443dfdbcb6df733564d Mon Sep 17 00:00:00 2001 From: Sovde Date: Tue, 28 Mar 2023 20:24:08 -0400 Subject: [PATCH 2/6] docs + test --- .../skript/conditions/CondHasLineOfSight.java | 40 ++++++++++++++++--- .../syntaxes/conditions/CondHasLineOfSight.sk | 16 ++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 src/test/skript/tests/syntaxes/conditions/CondHasLineOfSight.sk diff --git a/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java b/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java index d7ef72752b4..d6c31cac7be 100644 --- a/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java +++ b/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java @@ -1,6 +1,28 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * Copyright Peter Güttinger, SkriptLang team and contributors + */ package ch.njol.skript.conditions; import ch.njol.skript.Skript; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; import ch.njol.skript.lang.Condition; import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.SkriptParser.ParseResult; @@ -11,6 +33,14 @@ import org.bukkit.event.Event; import org.eclipse.jdt.annotation.Nullable; +@Name("Has Line of Sight") +@Description("Checks whether living entities have an unobstructed line of sight to other entities or locations.") +@Examples({ + "player has direct line of sight to location 5 blocks to the right of player", + "victim has line of sight to attacker", + "player has no line of sight to location 100 blocks in front of player" +}) +@Since("INSERT VERSION") public class CondHasLineOfSight extends Condition { static { @@ -35,18 +65,18 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye public boolean check(Event event) { return targets.check(event, (target) -> { if (target instanceof Entity) { - return viewers.check(event, (viewer) -> viewer.hasLineOfSight((Entity) target), isNegated()); + return viewers.check(event, (viewer) -> viewer.hasLineOfSight((Entity) target)); } else if (target instanceof Location) { - return viewers.check(event, (viewer) -> viewer.hasLineOfSight((Location) target), isNegated()); + return viewers.check(event, (viewer) -> viewer.hasLineOfSight((Location) target)); } else { - return isNegated(); + return false; } - }); + }, isNegated()); } @Override public String toString(@Nullable Event event, boolean debug) { - return viewers.toString(event, debug) + " has " + (isNegated() ? "no" : "")+ " line of sight to " + targets.toString(event,debug); + return viewers.toString(event, debug) + " has " + (isNegated() ? "no" : "") + " line of sight to " + targets.toString(event,debug); } } diff --git a/src/test/skript/tests/syntaxes/conditions/CondHasLineOfSight.sk b/src/test/skript/tests/syntaxes/conditions/CondHasLineOfSight.sk new file mode 100644 index 00000000000..7a746f83a00 --- /dev/null +++ b/src/test/skript/tests/syntaxes/conditions/CondHasLineOfSight.sk @@ -0,0 +1,16 @@ +test "line of sight": + loop blocks between (spawn of world "world") and (spawn of world "world" ~ vector(0, 10, 0)): + set {_block::%loop-block%} to type of loop-block + set loop-block to air + spawn pig at spawn of world "world" + set {_pig1} to last spawned entity + spawn pig at ((spawn of world "world") ~ vector(0, 10, 0)): + assert pig has line of sight to {_pig1} with "Pigs should have line of sight to each other 1" + assert {_pig1} has line of sight to pig with "Pigs should have line of sight to each other 2" + set block at ((spawn of world "world") ~ vector(0, 5, 0)) to stone + assert pig has no line of sight to {_pig1} with "Pigs should not have line of sight to each other 1" + assert {_pig1} has no line of sight to pig with "Pigs should not have line of sight to each other 2" + delete pig + delete random entity out of {_pig1} + loop blocks between (spawn of world "world") and (spawn of world "world" ~ vector(0, 10, 0)): + set loop-block to {_block::%loop-block%} From ca7aa9cd0b19ea587fdee1ae142e116aeffa45aa Mon Sep 17 00:00:00 2001 From: Sovde Date: Thu, 30 Mar 2023 01:22:10 -0400 Subject: [PATCH 3/6] Requested Changes --- .../java/ch/njol/skript/conditions/CondHasLineOfSight.java | 5 +++-- .../skript/tests/syntaxes/conditions/CondHasLineOfSight.sk | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java b/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java index d6c31cac7be..19cd1130869 100644 --- a/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java +++ b/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java @@ -57,7 +57,8 @@ public class CondHasLineOfSight extends Condition { public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { viewers = (Expression) exprs[0]; targets = exprs[1]; - if (matchedPattern > 0) setNegated(true); + if (matchedPattern > 0) + setNegated(true); return true; } @@ -76,7 +77,7 @@ public boolean check(Event event) { @Override public String toString(@Nullable Event event, boolean debug) { - return viewers.toString(event, debug) + " has " + (isNegated() ? "no" : "") + " line of sight to " + targets.toString(event,debug); + return viewers.toString(event, debug) + " has" + (isNegated() ? " no" : "") + " line of sight to " + targets.toString(event,debug); } } diff --git a/src/test/skript/tests/syntaxes/conditions/CondHasLineOfSight.sk b/src/test/skript/tests/syntaxes/conditions/CondHasLineOfSight.sk index 7a746f83a00..8e23a75f8af 100644 --- a/src/test/skript/tests/syntaxes/conditions/CondHasLineOfSight.sk +++ b/src/test/skript/tests/syntaxes/conditions/CondHasLineOfSight.sk @@ -11,6 +11,6 @@ test "line of sight": assert pig has no line of sight to {_pig1} with "Pigs should not have line of sight to each other 1" assert {_pig1} has no line of sight to pig with "Pigs should not have line of sight to each other 2" delete pig - delete random entity out of {_pig1} + delete entity within {_pig1} loop blocks between (spawn of world "world") and (spawn of world "world" ~ vector(0, 10, 0)): set loop-block to {_block::%loop-block%} From f38115a040b110cbeb84c19a4cf9f3356eb546b3 Mon Sep 17 00:00:00 2001 From: Sovde Date: Thu, 30 Mar 2023 01:24:27 -0400 Subject: [PATCH 4/6] Non-embarrassing test code --- .../tests/syntaxes/conditions/CondHasLineOfSight.sk | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/test/skript/tests/syntaxes/conditions/CondHasLineOfSight.sk b/src/test/skript/tests/syntaxes/conditions/CondHasLineOfSight.sk index 8e23a75f8af..63f8faff16e 100644 --- a/src/test/skript/tests/syntaxes/conditions/CondHasLineOfSight.sk +++ b/src/test/skript/tests/syntaxes/conditions/CondHasLineOfSight.sk @@ -1,16 +1,18 @@ test "line of sight": - loop blocks between (spawn of world "world") and (spawn of world "world" ~ vector(0, 10, 0)): + set {_a} to (spawn of world "world") + set {_b} to {_a} ~ vector(0, 10, 0) + loop blocks between {_a} and {_b}: set {_block::%loop-block%} to type of loop-block set loop-block to air spawn pig at spawn of world "world" set {_pig1} to last spawned entity - spawn pig at ((spawn of world "world") ~ vector(0, 10, 0)): + spawn pig at {_b}: assert pig has line of sight to {_pig1} with "Pigs should have line of sight to each other 1" assert {_pig1} has line of sight to pig with "Pigs should have line of sight to each other 2" - set block at ((spawn of world "world") ~ vector(0, 5, 0)) to stone + set block at ({_a} ~ vector(0, 5, 0)) to stone assert pig has no line of sight to {_pig1} with "Pigs should not have line of sight to each other 1" assert {_pig1} has no line of sight to pig with "Pigs should not have line of sight to each other 2" delete pig delete entity within {_pig1} - loop blocks between (spawn of world "world") and (spawn of world "world" ~ vector(0, 10, 0)): + loop blocks between {_a} and {_b}: set loop-block to {_block::%loop-block%} From 0678759831398ff20df7931d56b13a118b309efa Mon Sep 17 00:00:00 2001 From: sovdee Date: Fri, 31 Mar 2023 11:47:18 -0400 Subject: [PATCH 5/6] Update src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java Co-authored-by: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> --- src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java b/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java index 19cd1130869..b64a69d0c4f 100644 --- a/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java +++ b/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java @@ -47,7 +47,7 @@ public class CondHasLineOfSight extends Condition { Skript.registerCondition(CondHasLineOfSight.class, "%livingentities% (has|have) [a] [direct] line of sight to %entities/locations%", "%livingentities% does(n't| not) have [a] [direct] line of sight to %entities/locations%", - "%livingentities% has no [direct] line of sight to %entities/locations%"); + "%livingentities% (has|have) no [direct] line of sight to %entities/locations%"); } private Expression viewers; From 0abf83f2203910b475cbc0bd2478c08e8fc5934b Mon Sep 17 00:00:00 2001 From: sovdee Date: Wed, 17 May 2023 09:29:34 -0700 Subject: [PATCH 6/6] Update src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java Co-authored-by: Fusezion --- .../java/ch/njol/skript/conditions/CondHasLineOfSight.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java b/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java index b64a69d0c4f..dd4a18f9ab0 100644 --- a/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java +++ b/src/main/java/ch/njol/skript/conditions/CondHasLineOfSight.java @@ -57,8 +57,7 @@ public class CondHasLineOfSight extends Condition { public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { viewers = (Expression) exprs[0]; targets = exprs[1]; - if (matchedPattern > 0) - setNegated(true); + setNegated(matchedPattern > 0); return true; }