Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions src/main/java/ch/njol/skript/effects/EffSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,19 @@

import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Keywords;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.doc.*;
import ch.njol.skript.expressions.ExprInput;
import ch.njol.skript.expressions.ExprSortedList;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.InputSource;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.*;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.Variable;
import ch.njol.skript.lang.parser.ParserInstance;
import ch.njol.util.Kleenean;
import ch.njol.util.Pair;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.*;

@Name("Sort")
@Description("""
Expand All @@ -47,6 +35,8 @@
@Keywords("input")
public class EffSort extends Effect implements InputSource {

private record MappedValue(Object original, Object mapped) { }

static {
Skript.registerEffect(EffSort.class, "sort %~objects% [in (:descending|ascending) order] [(by|based on) <.+>]");
if (!ParserInstance.isRegistered(InputData.class))
Expand Down Expand Up @@ -100,20 +90,20 @@ protected void execute(Event event) {
return;
}
} else {
Map<Object, Object> valueToMappedValue = new LinkedHashMap<>();
List<MappedValue> mappedValues = new ArrayList<>();
for (Iterator<Pair<String, Object>> it = unsortedObjects.variablesIterator(event); it.hasNext(); ) {
Pair<String, Object> pair = it.next();
currentIndex = pair.getKey();
currentValue = pair.getValue();
Object mappedValue = mappingExpr.getSingle(event);
if (mappedValue == null)
return;
valueToMappedValue.put(currentValue, mappedValue);
mappedValues.add(new MappedValue(currentValue, mappedValue));
}
try {
sorted = valueToMappedValue.entrySet().stream()
.sorted(Map.Entry.comparingByValue((o1, o2) -> ExprSortedList.compare(o1, o2) * sortingMultiplier))
.map(Map.Entry::getKey)
sorted = mappedValues.stream()
.sorted((o1, o2) -> ExprSortedList.compare(o1.mapped(), o2.mapped()) * sortingMultiplier)
.map(MappedValue::original)
.toArray();
} catch (IllegalArgumentException | ClassCastException e) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test "eff sort collapsing duplicates":
set {_levels::a} to 1
set {_levels::b} to 2

add (shuffled "a", "a", and "b") to {_items::*}

assert {_items::*} contains "a", "a", and "b" with "Items were not added correctly"

sort {_items::*} in descending order by {_levels::%input%}

assert {_items::*} is "b", "a", and "a" with "Items were not sorted correctly or duplicates were collapsed"