From 302f8f9ac7cac1d1a1a68ce85cb75fad4fafd4a1 Mon Sep 17 00:00:00 2001 From: sovdee Date: Sun, 25 Jun 2023 00:01:30 -0700 Subject: [PATCH 1/2] Adds Clamp Function (#5573) --- .../skript/classes/data/DefaultFunctions.java | 53 ++++++++++++++----- .../skript/tests/syntaxes/functions/clamp.sk | 43 +++++++++++++++ 2 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 src/test/skript/tests/syntaxes/functions/clamp.sk diff --git a/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java b/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java index aeb0fe4735b..eee9206b0a0 100644 --- a/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java +++ b/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java @@ -18,19 +18,10 @@ */ package ch.njol.skript.classes.data; -import java.math.BigDecimal; -import java.math.RoundingMode; -import java.util.Calendar; - -import ch.njol.skript.lang.function.FunctionEvent; -import ch.njol.skript.lang.function.JavaFunction; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.util.Vector; - import ch.njol.skript.expressions.base.EventValueExpression; +import ch.njol.skript.lang.function.FunctionEvent; import ch.njol.skript.lang.function.Functions; +import ch.njol.skript.lang.function.JavaFunction; import ch.njol.skript.lang.function.Parameter; import ch.njol.skript.lang.function.SimpleJavaFunction; import ch.njol.skript.lang.util.SimpleLiteral; @@ -41,8 +32,16 @@ import ch.njol.util.Math2; import ch.njol.util.StringUtils; import ch.njol.util.coll.CollectionUtils; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.util.Vector; import org.eclipse.jdt.annotation.Nullable; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.Calendar; + public class DefaultFunctions { private static String str(double n) { @@ -305,7 +304,37 @@ public Number[] executeSimple(Object[][] params) { }.description("Returns the minimum number from a list of numbers.") .examples("min(1) = 1", "min(1, 2, 3, 4) = 1", "min({some list variable::*})") .since("2.2")); - + + Functions.registerFunction(new SimpleJavaFunction("clamp", new Parameter[]{ + new Parameter<>("values", DefaultClasses.NUMBER, false, null), + new Parameter<>("min", DefaultClasses.NUMBER, true, null), + new Parameter<>("max", DefaultClasses.NUMBER, true, null) + }, DefaultClasses.NUMBER, false) { + @Override + public @Nullable Number[] executeSimple(Object[][] params) { + Number[] values = (Number[]) params[0]; + Double[] clampedValues = new Double[values.length]; + double min = ((Number) params[1][0]).doubleValue(); + double max = ((Number) params[2][0]).doubleValue(); + // we'll be nice and swap them if they're in the wrong order + double trueMin = Math.min(min, max); + double trueMax = Math.max(min, max); + for (int i = 0; i < values.length; i++) { + double value = values[i].doubleValue(); + clampedValues[i] = Math.max(Math.min(value, trueMax), trueMin); + } + return clampedValues; + } + }).description("Clamps one or more values between two numbers.") + .examples( + "clamp(5, 0, 10) = 5", + "clamp(5.5, 0, 5) = 5", + "clamp(0.25, 0, 0.5) = 0.25", + "clamp(5, 7, 10) = 7", + "clamp((5, 0, 10, 9, 13), 7, 10) = (7, 7, 10, 9, 10)", + "set {_clamped::*} to clamp({_values::*}, 0, 10)") + .since("INSERT VERSION"); + // misc Functions.registerFunction(new SimpleJavaFunction("world", new Parameter[] { diff --git a/src/test/skript/tests/syntaxes/functions/clamp.sk b/src/test/skript/tests/syntaxes/functions/clamp.sk new file mode 100644 index 00000000000..98b9ad85dc5 --- /dev/null +++ b/src/test/skript/tests/syntaxes/functions/clamp.sk @@ -0,0 +1,43 @@ +test "clamp numbers": + # Normal Cases + assert clamp(1, 0, 2) is 1 with "(single ints) min < value < max" + assert clamp(1, 1, 2) is 1 with "(single ints) min = value < max" + assert clamp(2, 1, 2) is 2 with "(single ints) min < value = max" + assert clamp(0, 1, 2) is 1 with "(single ints) value < min < max" + assert clamp(3, 1, 2) is 2 with "(single ints) min < max < value" + assert clamp(3, 2, 1) is 2 with "(single ints) max < min < value" + + assert clamp(1.999, 0.0, 2.0) is 1.999 with "(single floats) min < value < max" + assert clamp(1.999, 1.999, 2.0) is 1.999 with "(single floats) min = value < max" + assert clamp(2.0, 1.999, 2.0) is 2.0 with "(single floats) min < value = max" + assert clamp(0.0, 1.999, 2.0) is 1.999 with "(single floats) value < min < max" + assert clamp(3.0, 1.999, 2.0) is 2.0 with "(single floats) min < max < value" + assert clamp(2.999, 2.0, 1.999) is 2.0 with "(single floats) max < min < value" + + # Lists + set {_expected::*} to (0, 0, 1, 2, 2, and 2) + # this is dumb but comparing the lists directly didn't work + set {_got::*} to clamp((-1, 0, 1, 2, 3, and 4), 0, 2) + loop {_expected::*}: + assert {_got::%loop-index%} is loop-value with "(multiple ints)" + set {_got::*} to clamp((-1.999, 0.0, 1.0, 2.0, 3.0, and 4.0), 0.0, 2.0) + loop {_expected::*}: + assert {_got::%loop-index%} is loop-value with "(multiple floats)" + + # Edge Cases + assert clamp(1, {_null}, 2) is not set with "(single ints) min = null" + assert clamp(2, 1, {_null}) is not set with "(single ints) max = null" + assert clamp({_null}, 1, 2) is not set with "(single ints) value = null" + assert clamp(1, 0, NaN value) is not clamp(1, 0, NaN value) with "(single ints) min < value < NaN" + assert clamp(1, NaN value, 2) is not clamp(1, NaN value, 2) with "(single ints) NaN < value < max" + assert clamp(NaN value, 1, 2) is not clamp(NaN value, 1, 2) with "(single ints) min < NaN < max" + assert clamp(infinity value, 1, 2) is 2 with "(single ints) min < infinity < max" + assert clamp(-infinity value, 1, 2) is 1 with "(single ints) min < -infinity < max" + assert clamp(1, 0, infinity value) is 1 with "(single ints) min < value < infinity" + assert clamp(1, -infinity value, 2) is 1 with "(single ints) -infinity < value < max" + + set {_expected::*} to (NaN value, 0.0, and 2.0) + set {_got::*} to clamp(({_null}, NaN value, -infinity value, infinity value), 0.0, 2.0) + assert number within {_got::1} is not number within {_got::1} with "(edge cases list) NaN" # need within because the variables weren't cooperating + assert {_got::2} is {_expected::2} with "(edge cases list) -infinity" + assert {_got::3} is {_expected::3} with "(edge cases list) infinity" From 2e5dc2ea2361f346dc6b42863f12fc216ef68156 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 25 Jun 2023 02:58:32 -0600 Subject: [PATCH 2/2] Bump io.papermc.paper:paper-api from 1.20-R0.1-SNAPSHOT to 1.20.1-R0.1-SNAPSHOT (#5746) * Bump io.papermc.paper:paper-api Bumps io.papermc.paper:paper-api from 1.20-R0.1-SNAPSHOT to 1.20.1-R0.1-SNAPSHOT. --- updated-dependencies: - dependency-name: io.papermc.paper:paper-api dependency-type: direct:production ... Signed-off-by: dependabot[bot] * Update build.gradle * Update gradle.properties * Update and rename paper-1.20.json to paper-1.20.1.json --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> --- build.gradle | 4 ++-- gradle.properties | 2 +- .../java17/{paper-1.20.json => paper-1.20.1.json} | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) rename src/test/skript/environments/java17/{paper-1.20.json => paper-1.20.1.json} (85%) diff --git a/build.gradle b/build.gradle index f05c899d0b2..5920cb95ac8 100644 --- a/build.gradle +++ b/build.gradle @@ -29,7 +29,7 @@ dependencies { shadow group: 'org.bstats', name: 'bstats-bukkit', version: '3.0.2' shadow group: 'net.kyori', name: 'adventure-text-serializer-bungeecord', version: '4.3.0' - implementation group: 'io.papermc.paper', name: 'paper-api', version: '1.20-R0.1-SNAPSHOT' + implementation group: 'io.papermc.paper', name: 'paper-api', version: '1.20.1-R0.1-SNAPSHOT' implementation group: 'org.eclipse.jdt', name: 'org.eclipse.jdt.annotation', version: '2.2.700' implementation group: 'com.google.code.findbugs', name: 'findbugs', version: '3.0.1' implementation group: 'com.sk89q.worldguard', name: 'worldguard-legacy', version: '7.0.0-SNAPSHOT' @@ -254,7 +254,7 @@ void createTestTask(String name, String desc, String environments, int javaVersi } } -def latestEnv = 'java17/paper-1.20.json' +def latestEnv = 'java17/paper-1.20.1.json' def latestJava = 17 def oldestJava = 8 diff --git a/gradle.properties b/gradle.properties index 22cf891c6cb..815459f135e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,5 +2,5 @@ groupid=ch.njol name=skript version=2.7.0-beta3 jarName=Skript.jar -testEnv=java17/paper-1.20 +testEnv=java17/paper-1.20.1 testEnvJavaVersion=17 diff --git a/src/test/skript/environments/java17/paper-1.20.json b/src/test/skript/environments/java17/paper-1.20.1.json similarity index 85% rename from src/test/skript/environments/java17/paper-1.20.json rename to src/test/skript/environments/java17/paper-1.20.1.json index 73c81a4018c..3a117b97397 100644 --- a/src/test/skript/environments/java17/paper-1.20.json +++ b/src/test/skript/environments/java17/paper-1.20.1.json @@ -1,11 +1,11 @@ { - "name": "paper-1.20", + "name": "paper-1.20.1", "resources": [ {"source": "server.properties.generic", "target": "server.properties"} ], "paperDownloads": [ { - "version": "1.20", + "version": "1.20.1", "target": "paperclip.jar" } ],