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 sorting indices of a list with children #6897

Merged
merged 9 commits into from
Jul 27, 2024
36 changes: 17 additions & 19 deletions src/main/java/ch/njol/skript/classes/Changer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@
* isn't overridden.
* <p>
* Some useful Changers can be found in {@link DefaultChangers}
*
* @author Peter Güttinger
*
* @see DefaultChangers
* @see Expression
*/
public interface Changer<T> {

public static enum ChangeMode {
enum ChangeMode {
ADD, SET, REMOVE, REMOVE_ALL, DELETE, RESET;
}

Expand All @@ -45,45 +44,44 @@ public static enum ChangeMode {
* <p>
* Unlike {@link Expression#acceptChange(ChangeMode)} this method must not print errors.
*
* @param mode
* @param mode The {@link ChangeMode} to test.
* @return An array of types that {@link #change(Object[], Object[], ChangeMode)} accepts as its <code>delta</code> parameter (which can be arrays to denote that multiple of
* that type are accepted), or null if the given mode is not supported. For {@link ChangeMode#DELETE} and {@link ChangeMode#RESET} this can return any non-null array to
* mark them as supported.
*/
@Nullable
public abstract Class<?>[] acceptChange(ChangeMode mode);
Class<?> @Nullable [] acceptChange(ChangeMode mode);

/**
* @param what The objects to change
* @param delta An array with one or more instances of one or more of the the classes returned by {@link #acceptChange(ChangeMode)} for the given change mode (null for
* {@link ChangeMode#DELETE} and {@link ChangeMode#RESET}). <b>This can be a Object[], thus casting is not allowed.</b>
* @param mode
* @param mode The {@link ChangeMode} to test.
* @throws UnsupportedOperationException (optional) if this method was called on an unsupported ChangeMode.
*/
public abstract void change(T[] what, @Nullable Object[] delta, ChangeMode mode);
void change(T[] what, Object @Nullable [] delta, ChangeMode mode);

public static abstract class ChangerUtils {
@SuppressWarnings("unchecked")
public static <T, V> void change(final Changer<T> changer, final Object[] what, final @Nullable Object[] delta, final ChangeMode mode) {
abstract class ChangerUtils {

public static <T> void change(Changer<T> changer, Object[] what, Object @Nullable [] delta, ChangeMode mode) {
//noinspection unchecked
changer.change((T[]) what, delta, mode);
}

/**
* Tests whether an expression accepts changes of a certain type. If multiple types are given it test for whether any of the types is accepted.
*
* @param e The expression to test
* @param expression The expression to test
* @param mode The ChangeMode to use in the test
* @param types The types to test for
* @return Whether <tt>e.{@link Expression#change(Event, Object[], ChangeMode) change}(event, type[], mode)</tt> can be used or not.
* @return Whether <tt>expression.{@link Expression#change(Event, Object[], ChangeMode) change}(event, type[], mode)</tt> can be used or not.
*/
public static boolean acceptsChange(final Expression<?> e, final ChangeMode mode, final Class<?>... types) {
final Class<?>[] cs = e.acceptChange(mode);
if (cs == null)
public static boolean acceptsChange(final Expression<?> expression, final ChangeMode mode, final Class<?>... types) {
final Class<?>[] validTypes = expression.acceptChange(mode);
if (validTypes == null)
return false;
for (final Class<?> type : types) {
for (final Class<?> c : cs) {
if (c.isArray() ? c.getComponentType().isAssignableFrom(type) : c.isAssignableFrom(type))
for (final Class<?> validType : validTypes) {
if (validType.isArray() ? validType.getComponentType().isAssignableFrom(type) : validType.isAssignableFrom(type))
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public World[] executeSimple(Object[][] params) {
}, DefaultClasses.LOCATION, true) {
@Override
@Nullable
public Location[] execute(FunctionEvent<?> e, Object[][] params) {
public Location[] execute(FunctionEvent<?> event, Object[][] params) {
for (int i : new int[] {0, 1, 2, 4, 5}) {
if (params[i] == null || params[i].length == 0 || params[i][0] == null)
return null;
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/ch/njol/skript/expressions/ExprIndices.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.util.LiteralUtils;
import ch.njol.util.Kleenean;
import ch.njol.util.Pair;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

Expand Down Expand Up @@ -101,8 +102,14 @@ protected String[] get(Event e) {
if (sort) {
int direction = descending ? -1 : 1;
return variable.entrySet().stream()
.map((entry) -> new Pair<>(
entry.getKey(),
entry.getValue() instanceof Map<?,?>
? ((Map<?,?>) entry.getValue()).get(null)
: entry.getValue()
))
.sorted((a, b) -> ExprSortedList.compare(a.getValue(), b.getValue()) * direction)
.map(Entry::getKey)
.map(Pair::getKey)
.toArray(String[]::new);
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/ch/njol/skript/lang/Condition.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Checker;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;

Expand Down Expand Up @@ -67,12 +67,11 @@ public final boolean isNegated() {
return negated;
}

@Nullable
@SuppressWarnings({"rawtypes", "unchecked"})
public static Condition parse(String input, @Nullable String defaultError) {
public static @Nullable Condition parse(String input, @Nullable String defaultError) {
input = input.trim();
while (input.startsWith("(") && SkriptParser.next(input, 0, ParseContext.DEFAULT) == input.length())
input = input.substring(1, input.length() - 1);
//noinspection unchecked,rawtypes
return (Condition) SkriptParser.parse(input, (Iterator) Skript.getConditions().iterator(), defaultError);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/lang/Debuggable.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package ch.njol.skript.lang;

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

/**
* Represents an element that can print details involving an event.
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/ch/njol/skript/lang/Effect.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import ch.njol.skript.log.ParseLogHandler;
import ch.njol.skript.log.SkriptLogger;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;

Expand All @@ -50,11 +50,8 @@ public final boolean run(Event event) {
return true;
}

@Nullable
@SuppressWarnings({"rawtypes", "unchecked"})
public static Effect parse(String input, @Nullable String defaultError) {
ParseLogHandler log = SkriptLogger.startParseLogHandler();
try {
public static @Nullable Effect parse(String input, @Nullable String defaultError) {
try (ParseLogHandler log = SkriptLogger.startParseLogHandler()) {
EffFunctionCall functionCall = EffFunctionCall.parse(input);
if (functionCall != null) {
log.printLog();
Expand All @@ -72,6 +69,7 @@ public static Effect parse(String input, @Nullable String defaultError) {
}
log.clear();

//noinspection unchecked,rawtypes
Effect effect = (Effect) SkriptParser.parse(input, (Iterator) Skript.getEffects().iterator(), defaultError);
if (effect != null) {
log.printLog();
Expand All @@ -80,8 +78,6 @@ public static Effect parse(String input, @Nullable String defaultError) {

log.printError();
return null;
} finally {
log.stop();
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/ch/njol/skript/lang/EffectSection.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.parser.ParserInstance;
import ch.njol.util.Kleenean;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -69,11 +69,10 @@ public abstract boolean init(Expression<?>[] expressions,
/**
* Similar to {@link Section#parse(String, String, SectionNode, List)}, but will only attempt to parse from other {@link EffectSection}s.
*/
@Nullable
@SuppressWarnings({"unchecked", "rawtypes"})
public static EffectSection parse(String input, @Nullable String defaultError, @Nullable SectionNode sectionNode, @Nullable List<TriggerItem> triggerItems) {
public static @Nullable EffectSection parse(String input, @Nullable String defaultError, @Nullable SectionNode sectionNode, @Nullable List<TriggerItem> triggerItems) {
SectionContext sectionContext = ParserInstance.get().getData(SectionContext.class);

//noinspection unchecked,rawtypes
return sectionContext.modify(sectionNode, triggerItems, () ->
(EffectSection) SkriptParser.parse(
input,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/lang/EffectSectionEffect.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

/**
* Represents the Effect aspect of an EffectSection. This allows for the use of EffectSections as effects, rather than just sections.
Expand Down
22 changes: 8 additions & 14 deletions src/main/java/ch/njol/skript/lang/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
import ch.njol.util.Checker;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.converter.Converter;

import java.util.HashMap;
Expand Down Expand Up @@ -64,8 +63,7 @@ public interface Expression<T> extends SyntaxElement, Debuggable {
* @return The value or null if this expression doesn't have any value for the event
* @throws UnsupportedOperationException (optional) if this was called on a non-single expression
*/
@Nullable
T getSingle(Event event);
@Nullable T getSingle(Event event);

/**
* Get an optional of the single value of this expression.
Expand Down Expand Up @@ -107,7 +105,7 @@ default Optional<T> getOptionalSingle(Event event) {
* @param event The event
* @return A non-null stream of this expression's non-null values
*/
default Stream<@NonNull ? extends T> stream(Event event) {
default Stream<? extends T> stream(Event event) {
Iterator<? extends T> iterator = iterator(event);
if (iterator == null) {
return Stream.empty();
Expand Down Expand Up @@ -178,9 +176,8 @@ default boolean canBeSingle() {
* @see Converter
* @see ConvertedExpression
*/
@Nullable
@SuppressWarnings("unchecked")
<R> Expression<? extends R> getConvertedExpression(Class<R>... to);
<R> @Nullable Expression<? extends R> getConvertedExpression(Class<R>... to);

/**
* Gets the return type of this expression.
Expand Down Expand Up @@ -265,8 +262,7 @@ default boolean canReturn(Class<?> returnType) {
* @param event The event to be used for evaluation
* @return An iterator to iterate over all values of this expression which may be empty and/or null, but must not return null elements.
*/
@Nullable
Iterator<? extends T> iterator(Event event);
@Nullable Iterator<? extends T> iterator(Event event);

/**
* Checks whether the given 'loop-...' expression should match this loop, e.g. loop-block matches any loops that loop through blocks and loop-argument matches an
Expand Down Expand Up @@ -316,8 +312,7 @@ default boolean canReturn(Class<?> returnType) {
* that type are accepted), or null if the given mode is not supported. For {@link ChangeMode#DELETE} and {@link ChangeMode#RESET} this can return any non-null array to
* mark them as supported.
*/
@Nullable
Class<?>[] acceptChange(ChangeMode mode);
Class<?> @Nullable [] acceptChange(ChangeMode mode);

/**
* Tests all accepted change modes, and if so what type it expects the <code>delta</code> to be.
Expand All @@ -343,7 +338,7 @@ default Map<ChangeMode, Class<?>[]> getAcceptedChangeModes() {
* @param mode The {@link ChangeMode} of the attempted change
* @throws UnsupportedOperationException (optional) - If this method was called on an unsupported ChangeMode.
*/
void change(Event event, @Nullable Object[] delta, ChangeMode mode);
void change(Event event, Object @Nullable [] delta, ChangeMode mode);

/**
* This method is called before this expression is set to another one.
Expand All @@ -356,8 +351,7 @@ default Map<ChangeMode, Class<?>[]> getAcceptedChangeModes() {
* @param delta Initial delta array.
* @return Delta array to use for change.
*/
@Nullable
default Object[] beforeChange(Expression<?> changed, @Nullable Object[] delta) {
default Object @Nullable [] beforeChange(Expression<?> changed, Object @Nullable [] delta) {
if (delta == null || delta.length == 0) // Nothing to nothing
return null;

Expand Down
8 changes: 3 additions & 5 deletions src/main/java/ch/njol/skript/lang/ExpressionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
*/
package ch.njol.skript.lang;

import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

/**
* Represents an expression's information, for use when creating new instances of expressions.
*/
public class ExpressionInfo<E extends Expression<T>, T> extends SyntaxElementInfo<E> {

@Nullable
public ExpressionType expressionType;
public @Nullable ExpressionType expressionType;
public Class<T> returnType;

public ExpressionInfo(String[] patterns, Class<T> returnType, Class<E> expressionClass, String originClassPath) throws IllegalArgumentException {
Expand All @@ -51,8 +50,7 @@ public Class<T> getReturnType() {
* Get the type of this expression.
* @return The type of this Expression
*/
@Nullable
public ExpressionType getExpressionType() {
public @Nullable ExpressionType getExpressionType() {
return expressionType;
}

Expand Down
Loading