From 2975762dc897216c96dbc4f3a8c4574d4fd776a1 Mon Sep 17 00:00:00 2001 From: _tud Date: Thu, 5 Oct 2023 17:44:40 +0300 Subject: [PATCH 1/3] Fix ExprSortedList --- .../skript/expressions/ExprSortedList.java | 46 +++++++++---------- .../syntaxes/expressions/ExprSortedList.sk | 18 ++++++++ 2 files changed, 39 insertions(+), 25 deletions(-) create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprSortedList.sk diff --git a/src/main/java/ch/njol/skript/expressions/ExprSortedList.java b/src/main/java/ch/njol/skript/expressions/ExprSortedList.java index a19779c0774..270d5b3dc08 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprSortedList.java +++ b/src/main/java/ch/njol/skript/expressions/ExprSortedList.java @@ -32,19 +32,18 @@ import ch.njol.util.coll.CollectionUtils; import org.bukkit.event.Event; import org.eclipse.jdt.annotation.Nullable; +import org.skriptlang.skript.lang.comparator.Comparator; +import org.skriptlang.skript.lang.comparator.Comparators; import java.lang.reflect.Array; -import java.util.Arrays; @Name("Sorted List") -@Description({"Sorts given list in natural order. All objects in list must be comparable;", - "if they're not, this expression will return nothing." -}) -@Examples({"set {_sorted::*} to sorted {_players::*}"}) +@Description("Sorts given list in natural order. All objects in list must be comparable; if they're not, this expression will return nothing.") +@Examples("set {_sorted::*} to sorted {_players::*}") @Since("2.2-dev19") public class ExprSortedList extends SimpleExpression { - static{ + static { Skript.registerExpression(ExprSortedList.class, Object.class, ExpressionType.COMBINED, "sorted %objects%"); } @@ -67,29 +66,26 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye @Override @Nullable - protected Object[] get(Event e) { - Object[] unsorted = list.getArray(e); - Object[] sorted = (Object[]) Array.newInstance(getReturnType(), unsorted.length); // Not yet sorted... - - for (int i = 0; i < sorted.length; i++) { - Object value = unsorted[i]; - if (value instanceof Long) { - // Hope it fits to the double... - sorted[i] = (double) (Long) value; - } else { - // No conversion needed - sorted[i] = value; - } - } - + protected Object[] get(Event event) { try { - Arrays.sort(sorted); // Now sorted - } catch (IllegalArgumentException | ClassCastException ex) { // In case elements are not comparable - return new Object[]{}; // We don't have a sorted array available + return list.stream(event) + .sorted(ExprSortedList::compare) + .toArray(); + } catch (IllegalArgumentException | ClassCastException e) { + return (Object[]) Array.newInstance(getReturnType(), 0); } - return sorted; } + @SuppressWarnings("unchecked") + private static int compare(A a, B b) throws IllegalArgumentException, ClassCastException { + Comparator comparator = Comparators.getComparator((Class) a.getClass(), (Class) b.getClass()); + if (comparator != null && comparator.supportsOrdering()) + return comparator.compare(a, b).getRelation(); + if (!(a instanceof Comparable)) + throw new IllegalArgumentException(); + return ((Comparable) a).compareTo(b); + } + @Override @Nullable @SuppressWarnings("unchecked") diff --git a/src/test/skript/tests/syntaxes/expressions/ExprSortedList.sk b/src/test/skript/tests/syntaxes/expressions/ExprSortedList.sk new file mode 100644 index 00000000000..6fd9d0e55f8 --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprSortedList.sk @@ -0,0 +1,18 @@ +test "sort": + # Populate list + set {_i} to 0 + loop 5 times: + set {_i} to {_i} + 1 + set {_list::%{_i}%} to a random integer from 1 to 100 + loop 5 times: + set {_i} to {_i} + 1 + set {_list::%{_i}%} to a random number from 1 to 100 + + # Test sorting + loop sorted {_list::*}: + if {_prev} is not set: + set {_prev} to loop-value + continue + assert loop-value >= {_prev} with "Couldn't sort correctly" + + assert (sorted 1 and "test") is not set with "Sorting incomparable values returned a value" From 35da6b7164dea4666e9831bb28c28b343eb89745 Mon Sep 17 00:00:00 2001 From: _tud Date: Fri, 6 Oct 2023 12:19:36 +0300 Subject: [PATCH 2/3] Update src/main/java/ch/njol/skript/expressions/ExprSortedList.java Co-authored-by: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> --- src/main/java/ch/njol/skript/expressions/ExprSortedList.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprSortedList.java b/src/main/java/ch/njol/skript/expressions/ExprSortedList.java index 270d5b3dc08..3cfed13b0a6 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprSortedList.java +++ b/src/main/java/ch/njol/skript/expressions/ExprSortedList.java @@ -69,8 +69,8 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye protected Object[] get(Event event) { try { return list.stream(event) - .sorted(ExprSortedList::compare) - .toArray(); + .sorted(ExprSortedList::compare) + .toArray(); } catch (IllegalArgumentException | ClassCastException e) { return (Object[]) Array.newInstance(getReturnType(), 0); } From 287e1ebd864f696c44de0e0d8f6b75974e3f817c Mon Sep 17 00:00:00 2001 From: _tud Date: Mon, 9 Oct 2023 13:41:41 +0300 Subject: [PATCH 3/3] Fix test Co-authored-by: Moderocky --- .../skript/tests/syntaxes/expressions/ExprSortedList.sk | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/test/skript/tests/syntaxes/expressions/ExprSortedList.sk b/src/test/skript/tests/syntaxes/expressions/ExprSortedList.sk index 6fd9d0e55f8..819486dbaaa 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprSortedList.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprSortedList.sk @@ -10,9 +10,8 @@ test "sort": # Test sorting loop sorted {_list::*}: - if {_prev} is not set: - set {_prev} to loop-value - continue - assert loop-value >= {_prev} with "Couldn't sort correctly" + if {_prev} is set: + assert loop-value >= {_prev} with "Couldn't sort correctly" + set {_prev} to loop-value assert (sorted 1 and "test") is not set with "Sorting incomparable values returned a value"