From 6a0ae1f8020b6b369cd99257cbf66aea5711bef2 Mon Sep 17 00:00:00 2001 From: Ankoki-Dev Date: Tue, 14 Mar 2023 17:50:21 +0000 Subject: [PATCH 01/11] Multiple Random Numbers Support Took 26 minutes --- .../skript/expressions/ExprRandomNumber.java | 70 +++++++++++-------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java index d5a15a5b464..69eafff1efc 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java +++ b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java @@ -39,62 +39,74 @@ * @author Peter Güttinger */ @Name("Random Number") -@Description({"A random number or integer between two given numbers. Use 'number' if you want any number with decimal parts, or use use 'integer' if you only want whole numbers.", - "Please note that the order of the numbers doesn't matter, i.e. random number between 2 and 1 will work as well as random number between 1 and 2."}) +@Description({"A given amount of random numbers or integers between two given numbers. Use 'number' if you want any number with decimal parts, or use use 'integer' if you only want whole numbers.", + "Please note that the order of the numbers doesn't matter, i.e. random number between 2 and 1 will work as well as random number between 1 and 2."}) @Examples({"set the player's health to a random number between 5 and 10", - "send \"You rolled a %random integer from 1 to 6%!\" to the player"}) -@Since("1.4") + "send \"You rolled a %random integer from 1 to 6%!\" to the player", + "set {_chances::*} to 5 random integers between 5 and 96"}) +@Since("1.4, INSERT VERSION (Multiple random numbers.)") public class ExprRandomNumber extends SimpleExpression { static { Skript.registerExpression(ExprRandomNumber.class, Number.class, ExpressionType.COMBINED, - "[a] random (1¦integer|2¦number) (from|between) %number% (to|and) %number%"); + "[a|%-number%] random (1¦integer[s]|2¦number[s]) (from|between) %number% (to|and) %number%"); } - + @SuppressWarnings("null") - private Expression lower, upper; - + private Expression amount, lower, upper; + private final Random rand = new Random(); - + private boolean integer; - + @SuppressWarnings({"unchecked", "null"}) @Override public boolean init(final Expression[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parser) { - lower = (Expression) exprs[0]; - upper = (Expression) exprs[1]; + amount = (Expression) exprs[0]; + lower = (Expression) exprs[1]; + upper = (Expression) exprs[2]; integer = parser.mark == 1; return true; } - + @Override @Nullable - protected Number[] get(final Event e) { - final Number l = lower.getSingle(e); - final Number u = upper.getSingle(e); - if (u == null || l == null) - return null; - final double ll = Math.min(l.doubleValue(), u.doubleValue()); - final double uu = Math.max(l.doubleValue(), u.doubleValue()); + protected Number[] get(Event event) { + Number amountNumber = null; + if (amount != null) + amountNumber = amount.getSingle(event); + Number lowerNumber = lower.getSingle(event); + Number upperNumber = upper.getSingle(event); + if (upperNumber == null || lowerNumber == null) + return new Number[0]; + int amount = amountNumber == null ? 1 : amountNumber.intValue(); + double lower = Math.min(lowerNumber.doubleValue(), upperNumber.doubleValue()); + double upper = Math.max(lowerNumber.doubleValue(), upperNumber.doubleValue()); if (integer) { - return new Long[] {Math2.ceil(ll) + Math2.mod(rand.nextLong(), Math2.floor(uu) - Math2.ceil(ll) + 1)}; + Long[] longs = new Long[amount]; + for (int i = 0; i < amount; i++) + longs[i] = Math2.ceil(lower) + Math2.mod(rand.nextLong(), Math2.floor(upper) - Math2.ceil(lower) + 1); + return new Long[] {Math2.ceil(lower) + Math2.mod(rand.nextLong(), Math2.floor(upper) - Math2.ceil(lower) + 1)}; } else { - return new Double[] {ll + rand.nextDouble() * (uu - ll)}; + Double[] doubles = new Double[amount]; + for (int i = 0; i < amount; i++) + doubles[i] = lower + rand.nextDouble() * (upper - lower); + return doubles; } } - + @Override public Class getReturnType() { return integer ? Long.class : Double.class; } - + @Override - public String toString(final @Nullable Event e, final boolean debug) { - return "a random " + (integer ? "integer" : "number") + " between " + lower.toString(e, debug) + " and " + upper.toString(e, debug); + public String toString(@Nullable Event event, boolean debug) { + return (amount == null ? "a" : amount.toString(event, debug)) + " random " + (integer ? "integer" : "number") + " between " + lower.toString(event, debug) + " and " + upper.toString(event, debug); } - + @Override public boolean isSingle() { - return true; + return amount == null; } - + } From e1123430792ddbd6b12e891ac4feb33d29f41a2d Mon Sep 17 00:00:00 2001 From: Ankoki-Dev Date: Tue, 14 Mar 2023 17:52:32 +0000 Subject: [PATCH 02/11] Change pattern a wee bit. Took 2 minutes --- src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java index 69eafff1efc..3dd1f84718e 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java +++ b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java @@ -48,7 +48,7 @@ public class ExprRandomNumber extends SimpleExpression { static { Skript.registerExpression(ExprRandomNumber.class, Number.class, ExpressionType.COMBINED, - "[a|%-number%] random (1¦integer[s]|2¦number[s]) (from|between) %number% (to|and) %number%"); + "[a|%-number%] random (1¦integer|2¦number)[s] (from|between) %number% (to|and) %number%"); } @SuppressWarnings("null") From 53583506a910c5b954d1e9b2ece64343d7ec3622 Mon Sep 17 00:00:00 2001 From: Ankoki-Dev Date: Tue, 14 Mar 2023 17:57:25 +0000 Subject: [PATCH 03/11] Use parse tags instead of parse marks. Took 3 minutes --- .../java/ch/njol/skript/expressions/ExprRandomNumber.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java index 3dd1f84718e..d4e42663ca5 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java +++ b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java @@ -48,7 +48,7 @@ public class ExprRandomNumber extends SimpleExpression { static { Skript.registerExpression(ExprRandomNumber.class, Number.class, ExpressionType.COMBINED, - "[a|%-number%] random (1¦integer|2¦number)[s] (from|between) %number% (to|and) %number%"); + "[a|%-number%] random (:integer|number)[s] (from|between) %number% (to|and) %number%"); } @SuppressWarnings("null") @@ -64,7 +64,7 @@ public boolean init(final Expression[] exprs, final int matchedPattern, final amount = (Expression) exprs[0]; lower = (Expression) exprs[1]; upper = (Expression) exprs[2]; - integer = parser.mark == 1; + integer = parser.hasTag("integer"); return true; } From b4b8c008db24b8af02fd6160a3e118abcf90c990 Mon Sep 17 00:00:00 2001 From: Ankoki-Dev Date: Tue, 14 Mar 2023 18:31:00 +0000 Subject: [PATCH 04/11] Requested Changes Took 15 minutes --- .../skript/expressions/ExprRandomNumber.java | 35 ++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java index d4e42663ca5..0a22c3eb85a 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java +++ b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java @@ -35,36 +35,31 @@ import ch.njol.util.Kleenean; import ch.njol.util.Math2; -/** - * @author Peter Güttinger - */ -@Name("Random Number") +@Name("Random Numbers") @Description({"A given amount of random numbers or integers between two given numbers. Use 'number' if you want any number with decimal parts, or use use 'integer' if you only want whole numbers.", - "Please note that the order of the numbers doesn't matter, i.e. random number between 2 and 1 will work as well as random number between 1 and 2."}) + "Please note that the order of the numbers doesn't matter, i.e. random number between 2 and 1 will work as well as random number between 1 and 2."}) @Examples({"set the player's health to a random number between 5 and 10", - "send \"You rolled a %random integer from 1 to 6%!\" to the player", - "set {_chances::*} to 5 random integers between 5 and 96"}) -@Since("1.4, INSERT VERSION (Multiple random numbers.)") + "send \"You rolled a %random integer from 1 to 6%!\" to the player", + "set {_chances::*} to 5 random integers between 5 and 96"}) +@Since("1.4, INSERT VERSION (Multiple random numbers)") public class ExprRandomNumber extends SimpleExpression { + static { Skript.registerExpression(ExprRandomNumber.class, Number.class, ExpressionType.COMBINED, - "[a|%-number%] random (:integer|number)[s] (from|between) %number% (to|and) %number%"); + "[a|%-number%] random (:integer|number)[s] (from|between) %number% (to|and) %number%"); } - @SuppressWarnings("null") - private Expression amount, lower, upper; - + private Expression amount, lower, upper; private final Random rand = new Random(); - private boolean integer; - @SuppressWarnings({"unchecked", "null"}) @Override - public boolean init(final Expression[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parser) { - amount = (Expression) exprs[0]; + @SuppressWarnings("unchecked") + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + amount = (Expression) exprs[0]; lower = (Expression) exprs[1]; upper = (Expression) exprs[2]; - integer = parser.hasTag("integer"); + integer = parseResult.hasTag("integer"); return true; } @@ -72,8 +67,8 @@ public boolean init(final Expression[] exprs, final int matchedPattern, final @Nullable protected Number[] get(Event event) { Number amountNumber = null; - if (amount != null) - amountNumber = amount.getSingle(event); + if (this.amount == null) + amountNumber = this.amount.getSingle(event); Number lowerNumber = lower.getSingle(event); Number upperNumber = upper.getSingle(event); if (upperNumber == null || lowerNumber == null) @@ -85,7 +80,7 @@ protected Number[] get(Event event) { Long[] longs = new Long[amount]; for (int i = 0; i < amount; i++) longs[i] = Math2.ceil(lower) + Math2.mod(rand.nextLong(), Math2.floor(upper) - Math2.ceil(lower) + 1); - return new Long[] {Math2.ceil(lower) + Math2.mod(rand.nextLong(), Math2.floor(upper) - Math2.ceil(lower) + 1)}; + return longs; } else { Double[] doubles = new Double[amount]; for (int i = 0; i < amount; i++) From 70d13be3af0e56c35ea0a1d1df4d7014bb517b66 Mon Sep 17 00:00:00 2001 From: Ankoki-Dev Date: Tue, 14 Mar 2023 18:35:29 +0000 Subject: [PATCH 05/11] Requested Changes Took 3 minutes --- .../java/ch/njol/skript/expressions/ExprRandomNumber.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java index 0a22c3eb85a..6e6c24c8952 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java +++ b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java @@ -66,14 +66,11 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye @Override @Nullable protected Number[] get(Event event) { - Number amountNumber = null; - if (this.amount == null) - amountNumber = this.amount.getSingle(event); Number lowerNumber = lower.getSingle(event); Number upperNumber = upper.getSingle(event); if (upperNumber == null || lowerNumber == null) return new Number[0]; - int amount = amountNumber == null ? 1 : amountNumber.intValue(); + int amount = this.amount == null ? 1 : this.amount.getOptionalSingle(event).orElse(1).intValue(); double lower = Math.min(lowerNumber.doubleValue(), upperNumber.doubleValue()); double upper = Math.max(lowerNumber.doubleValue(), upperNumber.doubleValue()); if (integer) { From c6b810dea377f07fdecabff2fcec887e16ba9d4f Mon Sep 17 00:00:00 2001 From: sovdee <10354869+sovdeeth@users.noreply.github.com> Date: Tue, 19 Mar 2024 18:16:39 +0100 Subject: [PATCH 06/11] Apply suggestions from code review Co-authored-by: Ayham Al Ali <20037329+AyhamAl-Ali@users.noreply.github.com> --- .../java/ch/njol/skript/expressions/ExprRandomNumber.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java index 6e6c24c8952..6148468919c 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java +++ b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java @@ -36,8 +36,10 @@ import ch.njol.util.Math2; @Name("Random Numbers") -@Description({"A given amount of random numbers or integers between two given numbers. Use 'number' if you want any number with decimal parts, or use use 'integer' if you only want whole numbers.", - "Please note that the order of the numbers doesn't matter, i.e. random number between 2 and 1 will work as well as random number between 1 and 2."}) +@Description({ + "A given amount of random numbers or integers between two given numbers. Use 'number' if you want any number with decimal parts, or use use 'integer' if you only want whole numbers.", + "Please note that the order of the numbers doesn't matter, i.e. random number between 2 and 1 will work as well as random number between 1 and 2." +}) @Examples({"set the player's health to a random number between 5 and 10", "send \"You rolled a %random integer from 1 to 6%!\" to the player", "set {_chances::*} to 5 random integers between 5 and 96"}) From 24252e5ea26d67045493625cbc21d8d0ce08fa08 Mon Sep 17 00:00:00 2001 From: sovde <10354869+sovdeeth@users.noreply.github.com> Date: Tue, 19 Mar 2024 19:16:23 +0100 Subject: [PATCH 07/11] Requested changes, added tests, bug fixes --- .../skript/expressions/ExprRandomNumber.java | 52 +++++++++---- .../syntaxes/expressions/ExprRandomNumber.sk | 78 +++++++++++++++++++ 2 files changed, 117 insertions(+), 13 deletions(-) create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprRandomNumber.sk diff --git a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java index ad9134c2ee9..c50ce78128b 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java +++ b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java @@ -18,6 +18,7 @@ */ package ch.njol.skript.expressions; +import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; @@ -44,7 +45,8 @@ @Examples({ "set the player's health to a random number between 5 and 10", "send \"You rolled a %random integer from 1 to 6%!\" to the player", - "set {_chances::*} to 5 random integers between 5 and 96" + "set {_chances::*} to 5 random integers between 5 and 96", + "set {_decimals::*} to 3 random numbers between 2.7 and -1.5" }) @Since("1.4, INSERT VERSION (Multiple random numbers)") public class ExprRandomNumber extends SimpleExpression { @@ -54,7 +56,9 @@ public class ExprRandomNumber extends SimpleExpression { "[a|%-number%] random (:integer|number)[s] (from|between) %number% (to|and) %number%"); } - private Expression amount, lower, upper; + @Nullable + private Expression amount; + private Expression lower, upper; private boolean isInteger; @Override @@ -72,39 +76,61 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye protected Number[] get(Event event) { Number lowerNumber = lower.getSingle(event); Number upperNumber = upper.getSingle(event); - if (upperNumber == null || lowerNumber == null) + if (upperNumber == null || lowerNumber == null || !Double.isFinite(lowerNumber.doubleValue()) || !Double.isFinite(upperNumber.doubleValue())) + return new Number[0]; + + Number amountNumber = this.amount == null ? 1 : this.amount.getSingle(event); + if (amountNumber == null || amountNumber.intValue() <= 0 || !Double.isFinite(amountNumber.doubleValue())) return new Number[0]; - int amount = this.amount == null ? 1 : this.amount.getOptionalSingle(event).orElse(1).intValue(); + + int amount = amountNumber.intValue(); double lower = Math.min(lowerNumber.doubleValue(), upperNumber.doubleValue()); double upper = Math.max(lowerNumber.doubleValue(), upperNumber.doubleValue()); Random random = ThreadLocalRandom.current(); if (isInteger) { Long[] longs = new Long[amount]; + long floored_upper = Math2.floor(upper); + long ceiled_lower = Math2.ceil(lower); + + // catch issues like `integer between 0.5 and 0.6` + if (upper - lower < 1 && ceiled_lower - floored_upper <= 1) { + if (floored_upper == ceiled_lower || lower == ceiled_lower) { + Arrays.fill(longs, ceiled_lower); + return longs; + } + if (upper == floored_upper) { + Arrays.fill(longs, floored_upper); + return longs; + } + return new Long[0]; + } + for (int i = 0; i < amount; i++) - longs[i] = Math2.ceil(lower) + Math2.mod(random.nextLong(), Math2.floor(upper) - Math2.ceil(lower) + 1); + longs[i] = Math2.ceil(lower) + Math2.mod(random.nextLong(), floored_upper - ceiled_lower + 1); return longs; + // non-integers } else { Double[] doubles = new Double[amount]; for (int i = 0; i < amount; i++) - doubles[i] = lower + random.nextDouble() * (upper - lower); + doubles[i] = Math.min(lower + random.nextDouble() * (upper - lower), upper); return doubles; } } @Override - public Class getReturnType() { - return isInteger ? Long.class : Double.class; + public boolean isSingle() { + return amount == null; } @Override - public String toString(@Nullable Event event, boolean debug) { - return (amount == null ? "a" : amount.toString(event, debug)) + " random " + (isInteger ? "integer" : "number") + " between " + lower.toString(event, debug) + " and " + upper.toString(event, debug); - + public Class getReturnType() { + return isInteger ? Long.class : Double.class; } @Override - public boolean isSingle() { - return amount == null; + public String toString(@Nullable Event event, boolean debug) { + return (amount == null ? "a" : amount.toString(event, debug)) + " random " + (isInteger ? "integer" : "number") + + (amount == null ? "" : "s") + " between " + lower.toString(event, debug) + " and " + upper.toString(event, debug); } } diff --git a/src/test/skript/tests/syntaxes/expressions/ExprRandomNumber.sk b/src/test/skript/tests/syntaxes/expressions/ExprRandomNumber.sk new file mode 100644 index 00000000000..571b33f3fa9 --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprRandomNumber.sk @@ -0,0 +1,78 @@ +test "random numbers": + loop 1000 times: + set {_num} to random number between 1 and 2.5 + assert {_num} is set with "(1) failed to generate random number: %{_num}%" + assert {_num} is between 1 and 2.5 with "(1) random number outside bounds: %{_num}%" + + set {_int} to random integer between 1 and 2.5 + assert {_int} is set with "(2) failed to generate random int: %{_int}%" + assert {_int} is between 1 and 2.5 with "(2) random int outside bounds: %{_int}%" + assert {_int} is 1 or 2 with "(2) random int generated an invalid result: %{_int}%" + + # negatives + set {_num} to random number between 1 and -2.5 + assert {_num} is set with "(3) failed to generate random number: %{_num}%" + assert {_num} is between 1 and -2.5 with "(3) random number outside bounds: %{_num}%" + + set {_int} to random integer between 1 and -2.5 + assert {_int} is set with "(4) failed to generate random int: %{_int}%" + assert {_int} is between 1 and -2.5 with "(4) random int outside bounds: %{_int}%" + assert {_int} is 1 or 0 or -1 or -2 with "(4) random int generated an invalid result: %{_int}%" + + # multiple values + set {_nums::*} to 3 random numbers between 1 and -2.5 + assert {_nums::*} is set with "(5) failed to generate random number: %{_nums::*}%" + assert size of {_nums::*} is 3 with "(6) random numbers generated the wrong amount of results: %size of {_nums::*}% != 3" + assert {_nums::*} is between 1 and -2.5 with "(5) random number outside bounds: %{_nums::*}%" + + set {_ints::*} to 3 random integers between 1 and -2.5 + assert {_ints::*} is set with "(6) failed to generate random int: %{_ints::*}%" + assert size of {_ints::*} is 3 with "(6) random ints generated the wrong amount of results: %size of {_ints::*}% != 3" + assert {_ints::*} are between 1 and -2.5 with "(6) random int outside bounds: %{_ints::*}%" + assert {_ints::*} are 1 or 0 or -1 or -2 with "(6) random int generated an invalid result: %{_ints::*}%" + + # null checks + set {_num} to random number between {_null} and 1 + assert {_num} is not set with "(7) random number with null bound returned non-null result: %{_num}%" + set {_num} to random number between 0 and {_null} + assert {_num} is not set with "(8) random number with null bound returned non-null result: %{_num}%" + set {_num::*} to {_null} random numbers between 0 and 10 + assert {_num::*} is not set with "(9) random number with null amount returned non-null result: %{_num::*}%" + + set {_int} to random integer between {_null} and 1 + assert {_int} is not set with "(10) random integer with null bound returned non-null result: %{_int}%" + set {_int} to random integer between 0 and {_null} + assert {_int} is not set with "(11) random integer with null bound returned non-null result: %{_int}%" + set {_int::*} to {_null} random integers between 0 and 10 + assert {_int::*} is not set with "(12) random integer with null bound returned non-null result: %{_int::*}%" + + # edge cases + set {_num} to random number between 0 and 0 + assert {_num} is 0 with "(13) random number between 0 and 0 returned non-zero result: %{_num}%" + set {_int} to random integer between 0 and 0 + assert {_int} is 0 with "(14) random integer between 0 and 0 returned non-zero result: %{_int}%" + + set {_int} to random integer between 0.5 and 0.6 + assert {_int} is not set with "(15) random integer between 0.5 and 0.6 returned non-null result: %{_int}%" + + set {_int::*} to -10 random integers between 1 and 10 + assert {_int::*} is not set with "(16) -10 random integers returned non-null result: %{_int::*}%" + + # NaN/infinity + set {_int} to random integer between 10 and NaN value + assert {_int} is not set with "(17) random integer between 10 and NaN value returned non-null result: %{_int}%" + + set {_int} to random integer between 10 and infinity value + assert {_int} is not set with "(18) random integer between 10 and infinity value returned non-null result: %{_int}%" + + set {_int} to random integer between 10 and -infinity value + assert {_int} is not set with "(19) random integer between 10 and -infinity value returned non-null result: %{_int}%" + + set {_int::*} to NaN value random integers between 1 and 10 + assert {_int::*} is not set with "(20) NaN value random integers returned non-null result: %{_int}%" + + set {_int::*} to infinity value random integers between 1 and 10 + assert {_int::*} is not set with "(21) NaN value random integers returned non-null result: %{_int::*}%" + + set {_int::*} to -infinity value random integers between 1 and 10 + assert {_int::*} is not set with "(22) NaN value random integers returned non-null result: %{_int::*}%" \ No newline at end of file From c9a69de7eb5b135b28533a3c6428e238e174ba1d Mon Sep 17 00:00:00 2001 From: sovde <10354869+sovdeeth@users.noreply.github.com> Date: Tue, 19 Mar 2024 19:38:13 +0100 Subject: [PATCH 08/11] Add check for literal 1 in isSingle --- src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java index c50ce78128b..e06e971f6eb 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java +++ b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java @@ -22,6 +22,7 @@ import java.util.Random; import java.util.concurrent.ThreadLocalRandom; +import ch.njol.skript.lang.Literal; import ch.njol.util.Math2; import org.bukkit.event.Event; import org.eclipse.jdt.annotation.Nullable; @@ -119,6 +120,8 @@ protected Number[] get(Event event) { @Override public boolean isSingle() { + if (amount instanceof Literal) + return ((Literal) amount).getSingle().intValue() == 1; return amount == null; } From f60dec4472ae33a9ba9d4f0889a60793363eb3d0 Mon Sep 17 00:00:00 2001 From: sovde <10354869+sovdeeth@users.noreply.github.com> Date: Tue, 19 Mar 2024 19:48:05 +0100 Subject: [PATCH 09/11] Use Integer instead of Number --- .../njol/skript/expressions/ExprRandomNumber.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java index e06e971f6eb..9adada0a7cf 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java +++ b/src/main/java/ch/njol/skript/expressions/ExprRandomNumber.java @@ -54,18 +54,18 @@ public class ExprRandomNumber extends SimpleExpression { static { Skript.registerExpression(ExprRandomNumber.class, Number.class, ExpressionType.COMBINED, - "[a|%-number%] random (:integer|number)[s] (from|between) %number% (to|and) %number%"); + "[a|%-integer%] random (:integer|number)[s] (from|between) %number% (to|and) %number%"); } @Nullable - private Expression amount; + private Expression amount; private Expression lower, upper; private boolean isInteger; @Override @SuppressWarnings("unchecked") public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { - amount = (Expression) exprs[0]; + amount = (Expression) exprs[0]; lower = (Expression) exprs[1]; upper = (Expression) exprs[2]; isInteger = parseResult.hasTag("integer"); @@ -80,11 +80,10 @@ protected Number[] get(Event event) { if (upperNumber == null || lowerNumber == null || !Double.isFinite(lowerNumber.doubleValue()) || !Double.isFinite(upperNumber.doubleValue())) return new Number[0]; - Number amountNumber = this.amount == null ? 1 : this.amount.getSingle(event); - if (amountNumber == null || amountNumber.intValue() <= 0 || !Double.isFinite(amountNumber.doubleValue())) + Integer amount = this.amount == null ? Integer.valueOf(1) : this.amount.getSingle(event); + if (amount == null || amount <= 0) return new Number[0]; - int amount = amountNumber.intValue(); double lower = Math.min(lowerNumber.doubleValue(), upperNumber.doubleValue()); double upper = Math.max(lowerNumber.doubleValue(), upperNumber.doubleValue()); Random random = ThreadLocalRandom.current(); @@ -121,7 +120,7 @@ protected Number[] get(Event event) { @Override public boolean isSingle() { if (amount instanceof Literal) - return ((Literal) amount).getSingle().intValue() == 1; + return ((Literal) amount).getSingle() == 1; return amount == null; } From ba355872427b33b460632558e1275e75f93825a6 Mon Sep 17 00:00:00 2001 From: sovde <10354869+sovdeeth@users.noreply.github.com> Date: Tue, 19 Mar 2024 19:53:17 +0100 Subject: [PATCH 10/11] forgot to delete the infinity tests --- .../skript/tests/syntaxes/expressions/ExprRandomNumber.sk | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/test/skript/tests/syntaxes/expressions/ExprRandomNumber.sk b/src/test/skript/tests/syntaxes/expressions/ExprRandomNumber.sk index 571b33f3fa9..45ad642b288 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprRandomNumber.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprRandomNumber.sk @@ -70,9 +70,3 @@ test "random numbers": set {_int::*} to NaN value random integers between 1 and 10 assert {_int::*} is not set with "(20) NaN value random integers returned non-null result: %{_int}%" - - set {_int::*} to infinity value random integers between 1 and 10 - assert {_int::*} is not set with "(21) NaN value random integers returned non-null result: %{_int::*}%" - - set {_int::*} to -infinity value random integers between 1 and 10 - assert {_int::*} is not set with "(22) NaN value random integers returned non-null result: %{_int::*}%" \ No newline at end of file From 6642c9837b0ce0087d3f0e2e5e6da7717cf0d344 Mon Sep 17 00:00:00 2001 From: sovde <10354869+sovdeeth@users.noreply.github.com> Date: Tue, 19 Mar 2024 19:54:11 +0100 Subject: [PATCH 11/11] Update skript-aliases --- skript-aliases | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skript-aliases b/skript-aliases index e99be898254..490bbeadf6e 160000 --- a/skript-aliases +++ b/skript-aliases @@ -1 +1 @@ -Subproject commit e99be898254965b0207992f66a3289ab6744106e +Subproject commit 490bbeadf6e44e26dd436acfd191dae5b740ebe6