From 4a96ab0c68060368c347f0658386653513797ab5 Mon Sep 17 00:00:00 2001 From: SirSmurfy2 Date: Thu, 22 May 2025 12:54:10 -0400 Subject: [PATCH 01/11] Initial Commit --- .../expressions/ExprCenterLocations.java | 103 ++++++++++++++++++ .../expressions/ExprCenterLocations.sk | 23 ++++ 2 files changed, 126 insertions(+) create mode 100644 src/main/java/ch/njol/skript/expressions/ExprCenterLocations.java create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprCenterLocations.sk diff --git a/src/main/java/ch/njol/skript/expressions/ExprCenterLocations.java b/src/main/java/ch/njol/skript/expressions/ExprCenterLocations.java new file mode 100644 index 00000000000..8e05ac0294d --- /dev/null +++ b/src/main/java/ch/njol/skript/expressions/ExprCenterLocations.java @@ -0,0 +1,103 @@ +package ch.njol.skript.expressions; + +import ch.njol.skript.Skript; +import ch.njol.skript.config.Node; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Example; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.SyntaxStringBuilder; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.event.Event; +import org.jetbrains.annotations.Nullable; +import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer; + +@Name("Center of Locations") +@Description("Get the center location from two locations in the same world.") +@Example(""" + set {_center} to the center location from location(0, 0, 0) to location(10, 10, 10) + set {_centerBlock} to the block at {_center} + """) +@Since("INSERT VERSION") +public class ExprCenterLocations extends SimpleExpression implements SyntaxRuntimeErrorProducer { + + static { + Skript.registerExpression(ExprCenterLocations.class, Location.class, ExpressionType.COMBINED, + "[the] (center|middle) (point|location) from %location% to %location%"); + } + + private Expression location1; + private Expression location2; + private Node node; + + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + //noinspection unchecked + location1 = (Expression) exprs[0]; + //noinspection unchecked + location2 = (Expression) exprs[1]; + node = getParser().getNode(); + return true; + } + + @Override + protected Location @Nullable [] get(Event event) { + Location loc1 = location1.getSingle(event); + Location loc2 = location2.getSingle(event); + if (loc1 == null || loc2 == null) { + return null; + } else if (loc1.getWorld() != loc2.getWorld()) { + error("Cannot get the center location of two locations in different worlds."); + return null; + } + World world = loc1.getWorld(); + double xLoc = getCenter(loc1.getX(), loc2.getX()); + double yLoc = getCenter(loc1.getY(), loc2.getY()); + double zLoc = getCenter(loc1.getZ(), loc2.getZ()); + return new Location[] {new Location(world, xLoc, yLoc, zLoc)}; + } + + private double getCenter(double pos1, double pos2) { + double highestPos = pos1; + double lowestPos = pos2; + if (pos2 > pos1) { + highestPos = pos2; + lowestPos = pos1; + } + double difference = Math.abs(highestPos - lowestPos); + double halved = difference / 2; + return highestPos - halved; + } + + @Override + public boolean isSingle() { + return true; + } + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public Node getNode() { + return node; + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return new SyntaxStringBuilder(event, debug) + .append("the center location from") + .append(location1) + .append("to") + .append(location2) + .toString(); + } + +} diff --git a/src/test/skript/tests/syntaxes/expressions/ExprCenterLocations.sk b/src/test/skript/tests/syntaxes/expressions/ExprCenterLocations.sk new file mode 100644 index 00000000000..6f9e4480915 --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprCenterLocations.sk @@ -0,0 +1,23 @@ + +test "center locations - simple": + set {_loc1} to location(0, 0, 0) + set {_loc2} to {_loc1} ~ vector(10, 10, 10) + set {_center} to the center location from {_loc1} to {_loc2} + assert {_center} is location(5, 5, 5) with "Simple center loc1 -> loc2 is incorrect" + set {_center2} to the center location from {_loc2} to {_loc1} + assert {_center2} is location(5, 5, 5) with "Simple center loc2 -> loc1 is incorrect" + +test "center locations - complex": + set {_loc1} to location(10.3, 38.6, 72.9) + set {_loc2} to location(-24.2, 63.8, 598.1) + set {_center} to the center location from {_loc1} to {_loc2} + assert {_center} is location(-6.95, 51.2, 335.5) with "Complex center loc1 -> loc2 is incorrect" + set {_center2} to the center location from {_loc2} to {_loc1} + assert {_center} is location(-6.95, 51.2, 335.5) with "Complex center loc2 -> loc1 is incorrect" + +test "center location - world error": + set {_loc1} to location(0, 0, 0) + set {_loc2} to location(0, 0, 0, "world_the_end") + # TODO: Change to catch runtime errors + set {_center} to the center location from {_loc1} to {_loc2} + assert {_center} is not set with "Should not be able to get a center location from locations in different worlds" From a989358e6c752955dd9ec9c21bf09690795f340a Mon Sep 17 00:00:00 2001 From: SirSmurfy2 Date: Thu, 22 May 2025 13:01:56 -0400 Subject: [PATCH 02/11] Update ExprCenterLocations.sk --- .../tests/syntaxes/expressions/ExprCenterLocations.sk | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/test/skript/tests/syntaxes/expressions/ExprCenterLocations.sk b/src/test/skript/tests/syntaxes/expressions/ExprCenterLocations.sk index 6f9e4480915..9bc174db08f 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprCenterLocations.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprCenterLocations.sk @@ -13,7 +13,15 @@ test "center locations - complex": set {_center} to the center location from {_loc1} to {_loc2} assert {_center} is location(-6.95, 51.2, 335.5) with "Complex center loc1 -> loc2 is incorrect" set {_center2} to the center location from {_loc2} to {_loc1} - assert {_center} is location(-6.95, 51.2, 335.5) with "Complex center loc2 -> loc1 is incorrect" + assert {_center2} is location(-6.95, 51.2, 335.5) with "Complex center loc2 -> loc1 is incorrect" + +test "center locations - negative": + set {_loc1} to location(-48.2, -33.6, -97.8) + set {_loc2} to location(-257.3, -59.3, -327.4) + set {_center} to the center location from {_loc1} to {_loc2} + assert {_center} is location(-152.75, -46.45, -212.6) with "Negative center loc1 -> loc2 is incorrect" + set {_center2} to the center location from {_loc2} to {_loc1} + assert {_center2} is location(-152.75, -46.45, -212.6) with "Negative center loc2 -> loc1 is incorrect" test "center location - world error": set {_loc1} to location(0, 0, 0) From 7138126c29c799d544fd6001306f84dd264cd526 Mon Sep 17 00:00:00 2001 From: SirSmurfy2 Date: Thu, 22 May 2025 14:34:58 -0400 Subject: [PATCH 03/11] Update ExprCenterLocations.java --- .../java/ch/njol/skript/expressions/ExprCenterLocations.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprCenterLocations.java b/src/main/java/ch/njol/skript/expressions/ExprCenterLocations.java index 8e05ac0294d..49895c8ec53 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprCenterLocations.java +++ b/src/main/java/ch/njol/skript/expressions/ExprCenterLocations.java @@ -29,7 +29,8 @@ public class ExprCenterLocations extends SimpleExpression implements S static { Skript.registerExpression(ExprCenterLocations.class, Location.class, ExpressionType.COMBINED, - "[the] (center|middle) (point|location) from %location% to %location%"); + "[the] (center|middle) (point|location) from %location% to %location%", + "[the] (center|middle) (point|location) (of|between) %location% and %location%"); } private Expression location1; From 7043d158639ccc2c7fe46011002705805207a757 Mon Sep 17 00:00:00 2001 From: SirSmurfy2 Date: Thu, 22 May 2025 15:51:39 -0400 Subject: [PATCH 04/11] Requested Changes --- ...ations.java => ExprMidpointLocations.java} | 19 ++++++------ .../expressions/ExprCenterLocations.sk | 31 ------------------- .../expressions/ExprMidpointLocations.sk | 31 +++++++++++++++++++ 3 files changed, 40 insertions(+), 41 deletions(-) rename src/main/java/ch/njol/skript/expressions/{ExprCenterLocations.java => ExprMidpointLocations.java} (78%) delete mode 100644 src/test/skript/tests/syntaxes/expressions/ExprCenterLocations.sk create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprMidpointLocations.sk diff --git a/src/main/java/ch/njol/skript/expressions/ExprCenterLocations.java b/src/main/java/ch/njol/skript/expressions/ExprMidpointLocations.java similarity index 78% rename from src/main/java/ch/njol/skript/expressions/ExprCenterLocations.java rename to src/main/java/ch/njol/skript/expressions/ExprMidpointLocations.java index 49895c8ec53..f83d15e22ca 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprCenterLocations.java +++ b/src/main/java/ch/njol/skript/expressions/ExprMidpointLocations.java @@ -18,19 +18,18 @@ import org.jetbrains.annotations.Nullable; import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer; -@Name("Center of Locations") -@Description("Get the center location from two locations in the same world.") +@Name("Midpoint of Locations") +@Description("Get the midpoint from two locations in the same world.") @Example(""" - set {_center} to the center location from location(0, 0, 0) to location(10, 10, 10) + set {_center} to the midpoint between location(0, 0, 0) and location(10, 10, 10) set {_centerBlock} to the block at {_center} """) @Since("INSERT VERSION") -public class ExprCenterLocations extends SimpleExpression implements SyntaxRuntimeErrorProducer { +public class ExprMidpointLocations extends SimpleExpression implements SyntaxRuntimeErrorProducer { static { - Skript.registerExpression(ExprCenterLocations.class, Location.class, ExpressionType.COMBINED, - "[the] (center|middle) (point|location) from %location% to %location%", - "[the] (center|middle) (point|location) (of|between) %location% and %location%"); + Skript.registerExpression(ExprMidpointLocations.class, Location.class, ExpressionType.COMBINED, + "[the] mid[-]point (of|between) %location% and %location%"); } private Expression location1; @@ -54,7 +53,7 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye if (loc1 == null || loc2 == null) { return null; } else if (loc1.getWorld() != loc2.getWorld()) { - error("Cannot get the center location of two locations in different worlds."); + error("Cannot get the midpoint of two locations in different worlds."); return null; } World world = loc1.getWorld(); @@ -94,9 +93,9 @@ public Node getNode() { @Override public String toString(@Nullable Event event, boolean debug) { return new SyntaxStringBuilder(event, debug) - .append("the center location from") + .append("the midpoint between") .append(location1) - .append("to") + .append("and") .append(location2) .toString(); } diff --git a/src/test/skript/tests/syntaxes/expressions/ExprCenterLocations.sk b/src/test/skript/tests/syntaxes/expressions/ExprCenterLocations.sk deleted file mode 100644 index 9bc174db08f..00000000000 --- a/src/test/skript/tests/syntaxes/expressions/ExprCenterLocations.sk +++ /dev/null @@ -1,31 +0,0 @@ - -test "center locations - simple": - set {_loc1} to location(0, 0, 0) - set {_loc2} to {_loc1} ~ vector(10, 10, 10) - set {_center} to the center location from {_loc1} to {_loc2} - assert {_center} is location(5, 5, 5) with "Simple center loc1 -> loc2 is incorrect" - set {_center2} to the center location from {_loc2} to {_loc1} - assert {_center2} is location(5, 5, 5) with "Simple center loc2 -> loc1 is incorrect" - -test "center locations - complex": - set {_loc1} to location(10.3, 38.6, 72.9) - set {_loc2} to location(-24.2, 63.8, 598.1) - set {_center} to the center location from {_loc1} to {_loc2} - assert {_center} is location(-6.95, 51.2, 335.5) with "Complex center loc1 -> loc2 is incorrect" - set {_center2} to the center location from {_loc2} to {_loc1} - assert {_center2} is location(-6.95, 51.2, 335.5) with "Complex center loc2 -> loc1 is incorrect" - -test "center locations - negative": - set {_loc1} to location(-48.2, -33.6, -97.8) - set {_loc2} to location(-257.3, -59.3, -327.4) - set {_center} to the center location from {_loc1} to {_loc2} - assert {_center} is location(-152.75, -46.45, -212.6) with "Negative center loc1 -> loc2 is incorrect" - set {_center2} to the center location from {_loc2} to {_loc1} - assert {_center2} is location(-152.75, -46.45, -212.6) with "Negative center loc2 -> loc1 is incorrect" - -test "center location - world error": - set {_loc1} to location(0, 0, 0) - set {_loc2} to location(0, 0, 0, "world_the_end") - # TODO: Change to catch runtime errors - set {_center} to the center location from {_loc1} to {_loc2} - assert {_center} is not set with "Should not be able to get a center location from locations in different worlds" diff --git a/src/test/skript/tests/syntaxes/expressions/ExprMidpointLocations.sk b/src/test/skript/tests/syntaxes/expressions/ExprMidpointLocations.sk new file mode 100644 index 00000000000..67cd8b55b98 --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprMidpointLocations.sk @@ -0,0 +1,31 @@ + +test "midpoint locations - simple": + set {_loc1} to location(0, 0, 0) + set {_loc2} to {_loc1} ~ vector(10, 10, 10) + set {_center} to the midpoint between {_loc1} and {_loc2} + assert {_center} is location(5, 5, 5) with "Simple midpoint loc1 -> loc2 is incorrect" + set {_center2} to the midpoint between {_loc2} and {_loc1} + assert {_center2} is location(5, 5, 5) with "Simple midpoint loc2 -> loc1 is incorrect" + +test "midpoint locations - complex": + set {_loc1} to location(10.3, 38.6, 72.9) + set {_loc2} to location(-24.2, 63.8, 598.1) + set {_center} to the midpoint between {_loc1} and {_loc2} + assert {_center} is location(-6.95, 51.2, 335.5) with "Complex midpoint loc1 -> loc2 is incorrect" + set {_center2} to the midpoint between {_loc2} and {_loc1} + assert {_center2} is location(-6.95, 51.2, 335.5) with "Complex midpoint loc2 -> loc1 is incorrect" + +test "midpoint locations - negative": + set {_loc1} to location(-48.2, -33.6, -97.8) + set {_loc2} to location(-257.3, -59.3, -327.4) + set {_center} to the midpoint between {_loc1} and {_loc2} + assert {_center} is location(-152.75, -46.45, -212.6) with "Negative midpoint loc1 -> loc2 is incorrect" + set {_center2} to the midpoint between {_loc2} and {_loc1} + assert {_center2} is location(-152.75, -46.45, -212.6) with "Negative midpoint loc2 -> loc1 is incorrect" + +test "midpoint location - world error": + set {_loc1} to location(0, 0, 0) + set {_loc2} to location(0, 0, 0, "world_the_end") + # TODO: Change to catch runtime errors + set {_center} to the midpoint between {_loc1} and {_loc2} + assert {_center} is not set with "Should not be able to get a center location from locations in different worlds" From de435269b5822e5161245ec79a6921ed8609cf07 Mon Sep 17 00:00:00 2001 From: SirSmurfy2 Date: Thu, 22 May 2025 16:59:42 -0400 Subject: [PATCH 05/11] Update ExprMidpointLocations.java --- .../expressions/ExprMidpointLocations.java | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprMidpointLocations.java b/src/main/java/ch/njol/skript/expressions/ExprMidpointLocations.java index f83d15e22ca..033757e8631 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprMidpointLocations.java +++ b/src/main/java/ch/njol/skript/expressions/ExprMidpointLocations.java @@ -15,6 +15,7 @@ import org.bukkit.Location; import org.bukkit.World; import org.bukkit.event.Event; +import org.bukkit.util.Vector; import org.jetbrains.annotations.Nullable; import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer; @@ -57,22 +58,8 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye return null; } World world = loc1.getWorld(); - double xLoc = getCenter(loc1.getX(), loc2.getX()); - double yLoc = getCenter(loc1.getY(), loc2.getY()); - double zLoc = getCenter(loc1.getZ(), loc2.getZ()); - return new Location[] {new Location(world, xLoc, yLoc, zLoc)}; - } - - private double getCenter(double pos1, double pos2) { - double highestPos = pos1; - double lowestPos = pos2; - if (pos2 > pos1) { - highestPos = pos2; - lowestPos = pos1; - } - double difference = Math.abs(highestPos - lowestPos); - double halved = difference / 2; - return highestPos - halved; + Vector vector = loc1.toVector().getMidpoint(loc2.toVector()); + return new Location[] {vector.toLocation(world)}; } @Override From 50efb635e5d4f56cf7dc44328d1a8d6908a60fd7 Mon Sep 17 00:00:00 2001 From: SirSmurfy2 Date: Sun, 25 May 2025 10:03:32 -0400 Subject: [PATCH 06/11] Allow Vectors --- .../njol/skript/expressions/ExprMidpoint.java | 129 ++++++++++++++++++ .../expressions/ExprMidpointLocations.java | 90 ------------ .../syntaxes/expressions/ExprMidpoint.sk | 67 +++++++++ .../expressions/ExprMidpointLocations.sk | 31 ----- 4 files changed, 196 insertions(+), 121 deletions(-) create mode 100644 src/main/java/ch/njol/skript/expressions/ExprMidpoint.java delete mode 100644 src/main/java/ch/njol/skript/expressions/ExprMidpointLocations.java create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprMidpoint.sk delete mode 100644 src/test/skript/tests/syntaxes/expressions/ExprMidpointLocations.sk diff --git a/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java b/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java new file mode 100644 index 00000000000..0567095293f --- /dev/null +++ b/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java @@ -0,0 +1,129 @@ +package ch.njol.skript.expressions; + +import ch.njol.skript.Skript; +import ch.njol.skript.config.Node; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Example; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.SyntaxStringBuilder; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import ch.njol.util.coll.CollectionUtils; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import org.jetbrains.annotations.Nullable; +import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer; + +@Name("Midpoint") +@Description("Get the midpoint between two vectors or two locations in the same world.") +@Example(""" + set {_center} to the midpoint between location(0, 0, 0) and location(10, 10, 10) + set {_centerBlock} to the block at {_center} + """) +@Example("set {_midpoint} to the mid-point of vector(20, 10, 5) and vector(3, 6, 9)") +@Since("INSERT VERSION") +public class ExprMidpoint extends SimpleExpression implements SyntaxRuntimeErrorProducer { + + static { + Skript.registerExpression(ExprMidpoint.class, Object.class, ExpressionType.COMBINED, + "[the] mid[-]point (of|between) %location/vector% and %location/vector%"); + } + + private Expression object1; + private Expression object2; + private boolean parseCheck = false; + private Class classType = null; + private Node node; + + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + object1 = exprs[0]; + object2 = exprs[1]; + Class type1 = object1.canReturn(Location.class) + ? Location.class : (object1.canReturn(Vector.class) ? Vector.class : null); + Class type2 = object2.canReturn(Location.class) + ? Location.class : (object2.canReturn(Vector.class) ? Vector.class : null); + if (type1 != null && type2 != null) { + if (type1 != type2) { + Skript.error("Cannot get the midpoint between a location and a vector."); + return false; + } + parseCheck = true; + classType = type1; + } + node = getParser().getNode(); + return true; + } + + @Override + protected Object @Nullable [] get(Event event) { + Object object1 = this.object1.getSingle(event); + Object object2 = this.object2.getSingle(event); + if (object1 == null || object2 == null) { + return null; + } else if (!parseCheck) { + if (object1 instanceof Location && object2 instanceof Location) { + classType = Location.class; + } else if (object1 instanceof Vector && object2 instanceof Vector) { + classType = Vector.class; + } else { + error("Cannot get the midpoint between a location and a vector."); + return null; + } + } + if (classType.equals(Location.class)) { + Location loc1 = (Location) object1; + Location loc2 = (Location) object2; + if (loc1.getWorld() != loc2.getWorld()) { + error("Cannot get the midpoint of two locations in different worlds."); + return null; + } + World world = loc1.getWorld(); + Vector vector = loc1.toVector().getMidpoint(loc2.toVector()); + return new Location[] {vector.toLocation(world)}; + } else { + Vector vector1 = (Vector) object1; + Vector vector2 = (Vector) object2; + return new Vector[] {vector1.getMidpoint(vector2)}; + } + } + + @Override + public boolean isSingle() { + return true; + } + + @Override + public Class getReturnType() { + return classType != null ? classType : Object.class; + } + + @Override + public Class[] possibleReturnTypes() { + if (classType != null) + return CollectionUtils.array(classType); + return CollectionUtils.array(Location.class, Vector.class); + } + + @Override + public Node getNode() { + return node; + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return new SyntaxStringBuilder(event, debug) + .append("the midpoint between") + .append(object1) + .append("and") + .append(object2) + .toString(); + } + +} diff --git a/src/main/java/ch/njol/skript/expressions/ExprMidpointLocations.java b/src/main/java/ch/njol/skript/expressions/ExprMidpointLocations.java deleted file mode 100644 index 033757e8631..00000000000 --- a/src/main/java/ch/njol/skript/expressions/ExprMidpointLocations.java +++ /dev/null @@ -1,90 +0,0 @@ -package ch.njol.skript.expressions; - -import ch.njol.skript.Skript; -import ch.njol.skript.config.Node; -import ch.njol.skript.doc.Description; -import ch.njol.skript.doc.Example; -import ch.njol.skript.doc.Name; -import ch.njol.skript.doc.Since; -import ch.njol.skript.lang.Expression; -import ch.njol.skript.lang.ExpressionType; -import ch.njol.skript.lang.SkriptParser.ParseResult; -import ch.njol.skript.lang.SyntaxStringBuilder; -import ch.njol.skript.lang.util.SimpleExpression; -import ch.njol.util.Kleenean; -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.event.Event; -import org.bukkit.util.Vector; -import org.jetbrains.annotations.Nullable; -import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer; - -@Name("Midpoint of Locations") -@Description("Get the midpoint from two locations in the same world.") -@Example(""" - set {_center} to the midpoint between location(0, 0, 0) and location(10, 10, 10) - set {_centerBlock} to the block at {_center} - """) -@Since("INSERT VERSION") -public class ExprMidpointLocations extends SimpleExpression implements SyntaxRuntimeErrorProducer { - - static { - Skript.registerExpression(ExprMidpointLocations.class, Location.class, ExpressionType.COMBINED, - "[the] mid[-]point (of|between) %location% and %location%"); - } - - private Expression location1; - private Expression location2; - private Node node; - - @Override - public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { - //noinspection unchecked - location1 = (Expression) exprs[0]; - //noinspection unchecked - location2 = (Expression) exprs[1]; - node = getParser().getNode(); - return true; - } - - @Override - protected Location @Nullable [] get(Event event) { - Location loc1 = location1.getSingle(event); - Location loc2 = location2.getSingle(event); - if (loc1 == null || loc2 == null) { - return null; - } else if (loc1.getWorld() != loc2.getWorld()) { - error("Cannot get the midpoint of two locations in different worlds."); - return null; - } - World world = loc1.getWorld(); - Vector vector = loc1.toVector().getMidpoint(loc2.toVector()); - return new Location[] {vector.toLocation(world)}; - } - - @Override - public boolean isSingle() { - return true; - } - - @Override - public Class getReturnType() { - return Location.class; - } - - @Override - public Node getNode() { - return node; - } - - @Override - public String toString(@Nullable Event event, boolean debug) { - return new SyntaxStringBuilder(event, debug) - .append("the midpoint between") - .append(location1) - .append("and") - .append(location2) - .toString(); - } - -} diff --git a/src/test/skript/tests/syntaxes/expressions/ExprMidpoint.sk b/src/test/skript/tests/syntaxes/expressions/ExprMidpoint.sk new file mode 100644 index 00000000000..01e21240c45 --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprMidpoint.sk @@ -0,0 +1,67 @@ + +test "midpoint locations - simple": + set {_loc1} to location(0, 0, 0) + set {_loc2} to {_loc1} ~ vector(10, 10, 10) + set {_midpoint} to the midpoint between {_loc1} and {_loc2} + assert {_midpoint} is location(5, 5, 5) with "Simple midpoint loc1 -> loc2 is incorrect" + set {_midpoint2} to the midpoint between {_loc2} and {_loc1} + assert {_midpoint2} is location(5, 5, 5) with "Simple midpoint loc2 -> loc1 is incorrect" + +test "midpoint locations - complex": + set {_loc1} to location(10.3, 38.6, 72.9) + set {_loc2} to location(-24.2, 63.8, 598.1) + set {_midpoint} to the midpoint between {_loc1} and {_loc2} + assert {_midpoint} is location(-6.95, 51.2, 335.5) with "Complex midpoint loc1 -> loc2 is incorrect" + set {_midpoint2} to the midpoint between {_loc2} and {_loc1} + assert {_midpoint2} is location(-6.95, 51.2, 335.5) with "Complex midpoint loc2 -> loc1 is incorrect" + +test "midpoint locations - negative": + set {_loc1} to location(-48.2, -33.6, -97.8) + set {_loc2} to location(-257.3, -59.3, -327.4) + set {_midpoint} to the midpoint between {_loc1} and {_loc2} + assert {_midpoint} is location(-152.75, -46.45, -212.6) with "Negative midpoint loc1 -> loc2 is incorrect" + set {_midpoint2} to the midpoint between {_loc2} and {_loc1} + assert {_midpoint2} is location(-152.75, -46.45, -212.6) with "Negative midpoint loc2 -> loc1 is incorrect" + +test "midpoint location - world error": + set {_loc1} to location(0, 0, 0) + set {_loc2} to location(0, 0, 0, "world_the_end") + # TODO: Change to catch runtime error + set {_midpoint} to the midpoint between {_loc1} and {_loc2} + assert {_midpoint} is not set with "Should not be able to get a midpoint from locations in different worlds" + +test "midpoint vectors - simple": + set {_vector1} to vector(0, 0, 0) + set {_vector2} to vector(10, 10, 10) + set {_midpoint} to the midpoint between {_vector1} and {_vector2} + assert {_midpoint} is vector(5, 5, 5) with "Simple midpoint vector1 -> vector2 is incorrect" + set {_midpoint2} to the midpoint between {_vector2} and {_vector1} + assert {_midpoint2} is vector(5, 5, 5) with "Simple midpoint vector2 -> vector1 is incorrect" + +test "midpoint vectors - complex": + set {_vector1} to vector(10.3, 38.6, 72.9) + set {_vector2} to vector(-24.2, 63.8, 598.1) + set {_midpoint} to the midpoint between {_vector1} and {_vector2} + assert {_midpoint} is vector(-6.95, 51.2, 335.5) with "Complex midpoint vector1 -> vector2 is incorrect" + set {_midpoint2} to the midpoint between {_vector2} and {_vector1} + assert {_midpoint2} is vector(-6.95, 51.2, 335.5) with "Complex midpoint vector2 -> vector1 is incorrect" + +test "midpoint vectors - negative": + set {_vector1} to vector(-48.2, -33.6, -97.8) + set {_vector2} to vector(-257.3, -59.3, -327.4) + set {_midpoint} to the midpoint between {_vector1} and {_vector2} + assert {_midpoint} is vector(-152.75, -46.45, -212.6) with "Negative midpoint vector1 -> vector2 is incorrect" + set {_midpoint2} to the midpoint between {_vector2} and {_vector1} + assert {_midpoint2} is vector(-152.75, -46.45, -212.6) with "Negative midpoint vector2 -> vector1 is incorrect" + +test "midpoint type error": + parse: + set {_midpoint} to the midpoint between location(0, 0, 0) and vector(0, 0, 0) + assert last parse logs is set with "Midpoint between location and vector should error" + assert last parse logs is "Cannot get the midpoint between a location and a vector." with "Incorrect error of midpoint between location and vector" + + set {_loc} to location(0, 0, 0) + set {_vector} to vector(0, 0, 0) + # TODO: Change to catch runtime error + set {_midpoint} to the midpoint between {_loc} and {_vector} + assert {_midpoint} is not set with "Should not be able to get a midpoint between a location and a vector" diff --git a/src/test/skript/tests/syntaxes/expressions/ExprMidpointLocations.sk b/src/test/skript/tests/syntaxes/expressions/ExprMidpointLocations.sk deleted file mode 100644 index 67cd8b55b98..00000000000 --- a/src/test/skript/tests/syntaxes/expressions/ExprMidpointLocations.sk +++ /dev/null @@ -1,31 +0,0 @@ - -test "midpoint locations - simple": - set {_loc1} to location(0, 0, 0) - set {_loc2} to {_loc1} ~ vector(10, 10, 10) - set {_center} to the midpoint between {_loc1} and {_loc2} - assert {_center} is location(5, 5, 5) with "Simple midpoint loc1 -> loc2 is incorrect" - set {_center2} to the midpoint between {_loc2} and {_loc1} - assert {_center2} is location(5, 5, 5) with "Simple midpoint loc2 -> loc1 is incorrect" - -test "midpoint locations - complex": - set {_loc1} to location(10.3, 38.6, 72.9) - set {_loc2} to location(-24.2, 63.8, 598.1) - set {_center} to the midpoint between {_loc1} and {_loc2} - assert {_center} is location(-6.95, 51.2, 335.5) with "Complex midpoint loc1 -> loc2 is incorrect" - set {_center2} to the midpoint between {_loc2} and {_loc1} - assert {_center2} is location(-6.95, 51.2, 335.5) with "Complex midpoint loc2 -> loc1 is incorrect" - -test "midpoint locations - negative": - set {_loc1} to location(-48.2, -33.6, -97.8) - set {_loc2} to location(-257.3, -59.3, -327.4) - set {_center} to the midpoint between {_loc1} and {_loc2} - assert {_center} is location(-152.75, -46.45, -212.6) with "Negative midpoint loc1 -> loc2 is incorrect" - set {_center2} to the midpoint between {_loc2} and {_loc1} - assert {_center2} is location(-152.75, -46.45, -212.6) with "Negative midpoint loc2 -> loc1 is incorrect" - -test "midpoint location - world error": - set {_loc1} to location(0, 0, 0) - set {_loc2} to location(0, 0, 0, "world_the_end") - # TODO: Change to catch runtime errors - set {_center} to the midpoint between {_loc1} and {_loc2} - assert {_center} is not set with "Should not be able to get a center location from locations in different worlds" From 27ee18307961baee094acfa493573aac97ca3fad Mon Sep 17 00:00:00 2001 From: SirSmurfy2 Date: Wed, 28 May 2025 17:05:27 -0400 Subject: [PATCH 07/11] Test + Error Update --- .../njol/skript/expressions/ExprMidpoint.java | 2 +- .../syntaxes/expressions/ExprMidpoint.sk | 34 +++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java b/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java index 0567095293f..42ab907cdf9 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java +++ b/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java @@ -73,7 +73,7 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye } else if (object1 instanceof Vector && object2 instanceof Vector) { classType = Vector.class; } else { - error("Cannot get the midpoint between a location and a vector."); + error("You can only get the midpoint between two locations or two vectors."); return null; } } diff --git a/src/test/skript/tests/syntaxes/expressions/ExprMidpoint.sk b/src/test/skript/tests/syntaxes/expressions/ExprMidpoint.sk index 01e21240c45..a1de901fc9e 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprMidpoint.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprMidpoint.sk @@ -23,6 +23,13 @@ test "midpoint locations - negative": set {_midpoint2} to the midpoint between {_loc2} and {_loc1} assert {_midpoint2} is location(-152.75, -46.45, -212.6) with "Negative midpoint loc2 -> loc1 is incorrect" +test "midpoint locations - function/variable": + set {_loc} to location(20, 30, 40) + set {_midpoint} to the midpoint between location(50, 60, 70) and {_loc} + assert {_midpoint} is location(35, 45, 55) with "Midpoint locations function -> variable is incorrect" + set {_midpoint2} to the midpoint between {_loc} and location(50, 60, 70) + assert {_midpoint2} is location(35, 45, 55) with "Midpoint locations variable -> function is incorrect" + test "midpoint location - world error": set {_loc1} to location(0, 0, 0) set {_loc2} to location(0, 0, 0, "world_the_end") @@ -54,14 +61,37 @@ test "midpoint vectors - negative": set {_midpoint2} to the midpoint between {_vector2} and {_vector1} assert {_midpoint2} is vector(-152.75, -46.45, -212.6) with "Negative midpoint vector2 -> vector1 is incorrect" +test "midpoint vectors - function/variable": + set {_vec} to vector(20, 30, 40) + set {_midpoint} to the midpoint between vector(50, 60, 70) and {_vec} + assert {_midpoint} is vector(35, 45, 55) with "Midpoint vectors function -> variable is incorrect" + set {_midpoint2} to the midpoint between {_vec} and vector(50, 60, 70) + assert {_midpoint2} is vector(35, 45, 55) with "Midpoint vectors variable -> function is incorrect" + test "midpoint type error": parse: set {_midpoint} to the midpoint between location(0, 0, 0) and vector(0, 0, 0) assert last parse logs is set with "Midpoint between location and vector should error" - assert last parse logs is "Cannot get the midpoint between a location and a vector." with "Incorrect error of midpoint between location and vector" + assert last parse logs is "You can only get the midpoint between two locations or two vectors." with "Incorrect error of midpoint between location and vector" set {_loc} to location(0, 0, 0) set {_vector} to vector(0, 0, 0) + # TODO: Change to catch runtime error set {_midpoint} to the midpoint between {_loc} and {_vector} - assert {_midpoint} is not set with "Should not be able to get a midpoint between a location and a vector" + assert {_midpoint} is not set with "Midpoint of location variable -> vector variable should error" + + set {_midpoint2} to the midpoint between {_vector} and {_loc} + assert {_midpoint2} is not set with "Midpoint of vector variable -> location variable should error" + + set {_midpoint3} to the midpoint between location(0, 0, 0) and {_vec} + assert {_midpoint3} is not set with "Midpoint of location function -> vector variable should error" + + set {_midpoint4} to the midpoint between {_vec} and location(0, 0, 0) + assert {_midpoint4} is not set with "Midpoint of vector variable -> location function should error" + + set {_midpoint5} to the midpoint between vector(0, 0, 0) and {_loc} + assert {_midpoint5} is not set with "Midpoint of vector function -> location variable should error" + + set {_midpoint6} to the midpoint between {_loc} and vector(0, 0, 0) + assert {_midpoint6} is not set with "Midpoint of location variable -> vector function should error" From abe7dd74004fce0812a3e9106079b60a7961c017 Mon Sep 17 00:00:00 2001 From: SirSmurfy2 Date: Wed, 28 May 2025 17:16:26 -0400 Subject: [PATCH 08/11] Update ExprMidpoint.java --- src/main/java/ch/njol/skript/expressions/ExprMidpoint.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java b/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java index 42ab907cdf9..bd330b55a8e 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java +++ b/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java @@ -51,7 +51,7 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye ? Location.class : (object2.canReturn(Vector.class) ? Vector.class : null); if (type1 != null && type2 != null) { if (type1 != type2) { - Skript.error("Cannot get the midpoint between a location and a vector."); + Skript.error("You can only get the midpoint between two locations or two vectors."); return false; } parseCheck = true; From dd9b5024c494085d28c30b1dcaf1b4899f3b23ad Mon Sep 17 00:00:00 2001 From: SirSmurfy2 Date: Thu, 29 May 2025 23:16:13 -0400 Subject: [PATCH 09/11] Update ExprMidpoint.java --- .../njol/skript/expressions/ExprMidpoint.java | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java b/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java index bd330b55a8e..6bc97379357 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java +++ b/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java @@ -30,6 +30,8 @@ @Since("INSERT VERSION") public class ExprMidpoint extends SimpleExpression implements SyntaxRuntimeErrorProducer { + private static final int NONE = 0, LOCATION = 1, VECTOR = 2, BOTH = 3; + static { Skript.registerExpression(ExprMidpoint.class, Object.class, ExpressionType.COMBINED, "[the] mid[-]point (of|between) %location/vector% and %location/vector%"); @@ -37,7 +39,6 @@ public class ExprMidpoint extends SimpleExpression implements SyntaxRunt private Expression object1; private Expression object2; - private boolean parseCheck = false; private Class classType = null; private Node node; @@ -45,18 +46,23 @@ public class ExprMidpoint extends SimpleExpression implements SyntaxRunt public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { object1 = exprs[0]; object2 = exprs[1]; - Class type1 = object1.canReturn(Location.class) - ? Location.class : (object1.canReturn(Vector.class) ? Vector.class : null); - Class type2 = object2.canReturn(Location.class) - ? Location.class : (object2.canReturn(Vector.class) ? Vector.class : null); - if (type1 != null && type2 != null) { - if (type1 != type2) { + int type1 = checkExpressionType(object1); + int type2 = checkExpressionType(object2); + if (type1 != type2) { + // If both 'type1' and 'type2' are either a Location or a Vector, then error, since they don't match + if ((type1 == LOCATION || type1 == VECTOR) && (type2 == LOCATION || type2 == VECTOR)) { Skript.error("You can only get the midpoint between two locations or two vectors."); return false; } - parseCheck = true; - classType = type1; + // Otherwise, checks will be done in 'get' + } else if (type1 == LOCATION) { + // Both 'type1' and 'type2' are a Location + classType = Location.class; + } else if (type1 == VECTOR) { + // Both 'type1' and 'type2' are a Vector + classType = Vector.class; } + // Otherwise, checks will be done in 'get' node = getParser().getNode(); return true; } @@ -67,7 +73,7 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye Object object2 = this.object2.getSingle(event); if (object1 == null || object2 == null) { return null; - } else if (!parseCheck) { + } else if (classType == null) { if (object1 instanceof Location && object2 instanceof Location) { classType = Location.class; } else if (object1 instanceof Vector && object2 instanceof Vector) { @@ -94,6 +100,17 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye } } + private int checkExpressionType(Expression expr) { + if (expr.canReturn(Location.class)) { + if (expr.canReturn(Vector.class)) + return BOTH; + return LOCATION; + } else if (expr.canReturn(Vector.class)) { + return VECTOR; + } + return NONE; + } + @Override public boolean isSingle() { return true; From 936b4c85d2b44a79164b78fcb1713ac56fbce256 Mon Sep 17 00:00:00 2001 From: SirSmurfy2 Date: Fri, 30 May 2025 13:30:58 -0400 Subject: [PATCH 10/11] Update ExprMidpoint.java --- .../njol/skript/expressions/ExprMidpoint.java | 65 +++++++------------ 1 file changed, 24 insertions(+), 41 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java b/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java index 6bc97379357..19a1efb0794 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java +++ b/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java @@ -11,8 +11,8 @@ import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.skript.lang.SyntaxStringBuilder; import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.skript.registrations.Classes; import ch.njol.util.Kleenean; -import ch.njol.util.coll.CollectionUtils; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.event.Event; @@ -39,30 +39,27 @@ public class ExprMidpoint extends SimpleExpression implements SyntaxRunt private Expression object1; private Expression object2; - private Class classType = null; + private Class[] classTypes = null; + private Class superType; private Node node; @Override public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { object1 = exprs[0]; object2 = exprs[1]; - int type1 = checkExpressionType(object1); - int type2 = checkExpressionType(object2); - if (type1 != type2) { - // If both 'type1' and 'type2' are either a Location or a Vector, then error, since they don't match - if ((type1 == LOCATION || type1 == VECTOR) && (type2 == LOCATION || type2 == VECTOR)) { + Class[] type1 = checkExpressionType(object1); + Class[] type2 = checkExpressionType(object2); + if (type1.length == 1 && type2.length == 1) { + if (type1[0] != type2[0]) { Skript.error("You can only get the midpoint between two locations or two vectors."); return false; } - // Otherwise, checks will be done in 'get' - } else if (type1 == LOCATION) { - // Both 'type1' and 'type2' are a Location - classType = Location.class; - } else if (type1 == VECTOR) { - // Both 'type1' and 'type2' are a Vector - classType = Vector.class; + classTypes = type1; + superType = type1[0]; + } else { + classTypes = type1.length > type2.length ? type1 : type2; + superType = Classes.getSuperClassInfo(classTypes).getC(); } - // Otherwise, checks will be done in 'get' node = getParser().getNode(); return true; } @@ -73,19 +70,7 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye Object object2 = this.object2.getSingle(event); if (object1 == null || object2 == null) { return null; - } else if (classType == null) { - if (object1 instanceof Location && object2 instanceof Location) { - classType = Location.class; - } else if (object1 instanceof Vector && object2 instanceof Vector) { - classType = Vector.class; - } else { - error("You can only get the midpoint between two locations or two vectors."); - return null; - } - } - if (classType.equals(Location.class)) { - Location loc1 = (Location) object1; - Location loc2 = (Location) object2; + } else if (object1 instanceof Location loc1 && object2 instanceof Location loc2) { if (loc1.getWorld() != loc2.getWorld()) { error("Cannot get the midpoint of two locations in different worlds."); return null; @@ -93,22 +78,22 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye World world = loc1.getWorld(); Vector vector = loc1.toVector().getMidpoint(loc2.toVector()); return new Location[] {vector.toLocation(world)}; - } else { - Vector vector1 = (Vector) object1; - Vector vector2 = (Vector) object2; + } else if (object1 instanceof Vector vector1 && object2 instanceof Vector vector2) { return new Vector[] {vector1.getMidpoint(vector2)}; + } else { + error("You can only get the midpoint between two locations or two vectors."); + return null; } } - private int checkExpressionType(Expression expr) { + private Class[] checkExpressionType(Expression expr) { if (expr.canReturn(Location.class)) { - if (expr.canReturn(Vector.class)) - return BOTH; - return LOCATION; + if (!expr.canReturn(Vector.class)) + return new Class[] {Location.class}; } else if (expr.canReturn(Vector.class)) { - return VECTOR; + return new Class[] {Vector.class}; } - return NONE; + return new Class[] {Location.class, Vector.class}; } @Override @@ -118,14 +103,12 @@ public boolean isSingle() { @Override public Class getReturnType() { - return classType != null ? classType : Object.class; + return superType; } @Override public Class[] possibleReturnTypes() { - if (classType != null) - return CollectionUtils.array(classType); - return CollectionUtils.array(Location.class, Vector.class); + return classTypes; } @Override From 905253c10f8136813245bf00c6c45e30a507c99c Mon Sep 17 00:00:00 2001 From: SirSmurfy2 Date: Thu, 5 Jun 2025 15:33:19 -0400 Subject: [PATCH 11/11] Runtime Errors --- .../njol/skript/expressions/ExprMidpoint.java | 4 +- .../syntaxes/expressions/ExprMidpoint.sk | 40 +++++++++++-------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java b/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java index 19a1efb0794..fadaf50b083 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java +++ b/src/main/java/ch/njol/skript/expressions/ExprMidpoint.java @@ -30,11 +30,9 @@ @Since("INSERT VERSION") public class ExprMidpoint extends SimpleExpression implements SyntaxRuntimeErrorProducer { - private static final int NONE = 0, LOCATION = 1, VECTOR = 2, BOTH = 3; - static { Skript.registerExpression(ExprMidpoint.class, Object.class, ExpressionType.COMBINED, - "[the] mid[-]point (of|between) %location/vector% and %location/vector%"); + "[the] mid[-]point (of|between) %object% and %object%"); } private Expression object1; diff --git a/src/test/skript/tests/syntaxes/expressions/ExprMidpoint.sk b/src/test/skript/tests/syntaxes/expressions/ExprMidpoint.sk index a1de901fc9e..cd700bf2d5b 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprMidpoint.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprMidpoint.sk @@ -1,4 +1,6 @@ +using error catching + test "midpoint locations - simple": set {_loc1} to location(0, 0, 0) set {_loc2} to {_loc1} ~ vector(10, 10, 10) @@ -33,9 +35,9 @@ test "midpoint locations - function/variable": test "midpoint location - world error": set {_loc1} to location(0, 0, 0) set {_loc2} to location(0, 0, 0, "world_the_end") - # TODO: Change to catch runtime error - set {_midpoint} to the midpoint between {_loc1} and {_loc2} - assert {_midpoint} is not set with "Should not be able to get a midpoint from locations in different worlds" + catch runtime errors: + set {_midpoint} to the midpoint between {_loc1} and {_loc2} + assert last caught errors is "Cannot get the midpoint of two locations in different worlds." with "ExprMidpoint should error when getting midpoint of locations in different worlds" test "midpoint vectors - simple": set {_vector1} to vector(0, 0, 0) @@ -77,21 +79,27 @@ test "midpoint type error": set {_loc} to location(0, 0, 0) set {_vector} to vector(0, 0, 0) - # TODO: Change to catch runtime error - set {_midpoint} to the midpoint between {_loc} and {_vector} - assert {_midpoint} is not set with "Midpoint of location variable -> vector variable should error" + set {_error} to "You can only get the midpoint between two locations or two vectors." + catch runtime errors: + set {_midpoint} to the midpoint between {_loc} and {_vector} + assert last caught errors is {_error} with "Midpoint of location variable -> vector variable should error" - set {_midpoint2} to the midpoint between {_vector} and {_loc} - assert {_midpoint2} is not set with "Midpoint of vector variable -> location variable should error" + catch runtime errors: + set {_midpoint2} to the midpoint between {_vector} and {_loc} + assert last caught errors is {_error} with "Midpoint of vector variable -> location variable should error" - set {_midpoint3} to the midpoint between location(0, 0, 0) and {_vec} - assert {_midpoint3} is not set with "Midpoint of location function -> vector variable should error" + catch runtime errors: + set {_midpoint3} to the midpoint between location(0, 0, 0) and {_vector} + assert last caught errors is {_error} with "Midpoint of location function -> vector variable should error" - set {_midpoint4} to the midpoint between {_vec} and location(0, 0, 0) - assert {_midpoint4} is not set with "Midpoint of vector variable -> location function should error" + catch runtime errors: + set {_midpoint4} to the midpoint between {_vector} and location(0, 0, 0) + assert last caught errors is {_error} with "Midpoint of vector variable -> location function should error" - set {_midpoint5} to the midpoint between vector(0, 0, 0) and {_loc} - assert {_midpoint5} is not set with "Midpoint of vector function -> location variable should error" + catch runtime errors: + set {_midpoint5} to the midpoint between vector(0, 0, 0) and {_loc} + assert last caught errors is {_error} with "Midpoint of vector function -> location variable should error" - set {_midpoint6} to the midpoint between {_loc} and vector(0, 0, 0) - assert {_midpoint6} is not set with "Midpoint of location variable -> vector function should error" + catch runtime errors: + set {_midpoint6} to the midpoint between {_loc} and vector(0, 0, 0) + assert last caught errors is {_error} with "Midpoint of location variable -> vector function should error"