Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix return type of some list expressions #3952

Merged
Show file tree
Hide file tree
Changes from 2 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
45 changes: 28 additions & 17 deletions src/main/java/ch/njol/skript/expressions/ExprReversedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@
*/
package ch.njol.skript.expressions;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
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;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.util.LiteralUtils;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Name("Reversed List")
@Description("Reverses given list.")
Expand All @@ -46,22 +47,32 @@ public class ExprReversedList extends SimpleExpression<Object> {
Skript.registerExpression(ExprReversedList.class, Object.class, ExpressionType.COMBINED, "reversed %objects%");
}

@SuppressWarnings("null")
@SuppressWarnings("NotNullFieldNotInitialized")
private Expression<?> list;

@Override
@SuppressWarnings({"null", "unchecked"})
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
list = exprs[0].getConvertedExpression(Object.class);
return list != null;
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
list = LiteralUtils.defendExpression(exprs[0]);
return LiteralUtils.canInitSafely(list);
}

@Override
@Nullable
protected Object[] get(Event e) {
List<Object> reversed = Arrays.asList(list.getAll(e).clone());
Collections.reverse(reversed);
return reversed.toArray();
Object[] inputArray = list.getArray(e).clone();
Object[] array = (Object[]) Array.newInstance(getReturnType(), inputArray.length);
System.arraycopy(inputArray, 0, array, 0, inputArray.length);
reverse(array);
return array;
}

private void reverse(Object[] array) {
for (int i = 0; i < array.length / 2; i++) {
Object temp = array[i];
int reverse = array.length - i - 1;
array[i] = array[reverse];
array[reverse] = temp;
}
}

@Override
Expand All @@ -71,7 +82,7 @@ public boolean isSingle() {

@Override
public Class<?> getReturnType() {
return Object.class;
return list.getReturnType();
}

@Override
Expand Down
26 changes: 12 additions & 14 deletions src/main/java/ch/njol/skript/expressions/ExprShuffledList.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
*/
package ch.njol.skript.expressions;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import ch.njol.skript.util.LiteralUtils;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -46,33 +48,29 @@ public class ExprShuffledList extends SimpleExpression<Object> {
Skript.registerExpression(ExprShuffledList.class, Object.class, ExpressionType.COMBINED, "shuffled %objects%");
}

@SuppressWarnings("null")
@SuppressWarnings("NotNullFieldNotInitialized")
private Expression<?> list;

@Override
@SuppressWarnings({"null", "unchecked"})
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
list = exprs[0].getConvertedExpression(Object.class);
return list != null;
list = LiteralUtils.defendExpression(exprs[0]);
return LiteralUtils.canInitSafely(list);
}

@Override
@Nullable
protected Object[] get(Event e) {
Object[] origin = list.getAll(e);
List<Object> shuffled = Arrays.asList(origin.clone()); // Not yet shuffled...

try {
Collections.shuffle(shuffled);
} catch (IllegalArgumentException ex) { // In case elements are not comparable
Skript.error("Tried to sort a list, but some objects are not comparable!");
}
return shuffled.toArray();
Object[] origin = list.getArray(e).clone();
List<Object> shuffled = Arrays.asList(origin); // Not yet shuffled...
Collections.shuffle(shuffled);

Object[] array = (Object[]) Array.newInstance(getReturnType(), origin.length);
return shuffled.toArray(array);
}

@Override
public Class<? extends Object> getReturnType() {
return Object.class;
return list.getReturnType();
}

@Override
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/ch/njol/skript/expressions/ExprSortedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
*/
package ch.njol.skript.expressions;

import java.lang.reflect.Array;
import java.util.Arrays;

import ch.njol.skript.util.LiteralUtils;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -46,21 +48,20 @@ public class ExprSortedList extends SimpleExpression<Object> {
Skript.registerExpression(ExprSortedList.class, Object.class, ExpressionType.COMBINED, "sorted %objects%");
}

@SuppressWarnings("null")
@SuppressWarnings("NotNullFieldNotInitialized")
private Expression<?> list;

@Override
@SuppressWarnings({"null", "unchecked"})
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
list = exprs[0].getConvertedExpression(Object.class);
return list != null;
list = LiteralUtils.defendExpression(exprs[0]);
return LiteralUtils.canInitSafely(list);
}

@Override
@Nullable
protected Object[] get(Event e) {
Object[] unsorted = list.getAll(e);
Object[] sorted = new Object[unsorted.length]; // Not yet sorted...
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];
Expand All @@ -87,8 +88,8 @@ public boolean isSingle() {
}

@Override
public Class<? extends Object> getReturnType() {
return Object.class;
public Class<?> getReturnType() {
return list.getReturnType();
}

@Override
Expand Down