Skip to content

Commit

Permalink
Revert "Apply new conventions to lang package (SkriptLang#6918)"
Browse files Browse the repository at this point in the history
This reverts commit 2f4232d.
  • Loading branch information
APickledWalrus committed Jul 27, 2024
1 parent 9a77531 commit 8679518
Show file tree
Hide file tree
Showing 45 changed files with 557 additions and 347 deletions.
36 changes: 19 additions & 17 deletions src/main/java/ch/njol/skript/classes/Changer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
* 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> {

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

Expand All @@ -44,44 +45,45 @@ enum ChangeMode {
* <p>
* Unlike {@link Expression#acceptChange(ChangeMode)} this method must not print errors.
*
* @param mode The {@link ChangeMode} to test.
* @param mode
* @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.
*/
Class<?> @Nullable [] acceptChange(ChangeMode mode);
@Nullable
public abstract Class<?>[] 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 The {@link ChangeMode} to test.
* @param mode
* @throws UnsupportedOperationException (optional) if this method was called on an unsupported ChangeMode.
*/
void change(T[] what, Object @Nullable [] delta, ChangeMode mode);
public abstract void change(T[] what, @Nullable Object[] delta, ChangeMode mode);

abstract class ChangerUtils {

public static <T> void change(Changer<T> changer, Object[] what, Object @Nullable [] delta, ChangeMode mode) {
//noinspection unchecked
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) {
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 expression The expression to test
* @param e The expression to test
* @param mode The ChangeMode to use in the test
* @param types The types to test for
* @return Whether <tt>expression.{@link Expression#change(Event, Object[], ChangeMode) change}(event, type[], mode)</tt> can be used or not.
* @return Whether <tt>e.{@link Expression#change(Event, Object[], ChangeMode) change}(event, type[], mode)</tt> can be used or not.
*/
public static boolean acceptsChange(final Expression<?> expression, final ChangeMode mode, final Class<?>... types) {
final Class<?>[] validTypes = expression.acceptChange(mode);
if (validTypes == null)
public static boolean acceptsChange(final Expression<?> e, final ChangeMode mode, final Class<?>... types) {
final Class<?>[] cs = e.acceptChange(mode);
if (cs == null)
return false;
for (final Class<?> type : types) {
for (final Class<?> validType : validTypes) {
if (validType.isArray() ? validType.getComponentType().isAssignableFrom(type) : validType.isAssignableFrom(type))
for (final Class<?> c : cs) {
if (c.isArray() ? c.getComponentType().isAssignableFrom(type) : c.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<?> event, Object[][] params) {
public Location[] execute(FunctionEvent<?> e, 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
7 changes: 4 additions & 3 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.jetbrains.annotations.Nullable;
import org.eclipse.jdt.annotation.Nullable;

import java.util.Iterator;

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

public static @Nullable Condition parse(String input, @Nullable String defaultError) {
@Nullable
@SuppressWarnings({"rawtypes", "unchecked"})
public static 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.jetbrains.annotations.Nullable;
import org.eclipse.jdt.annotation.Nullable;

/**
* Represents an element that can print details involving an event.
Expand Down
12 changes: 8 additions & 4 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.jetbrains.annotations.Nullable;
import org.eclipse.jdt.annotation.Nullable;

import java.util.Iterator;

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

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

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

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

Expand Down
7 changes: 4 additions & 3 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.jetbrains.annotations.Nullable;
import org.eclipse.jdt.annotation.Nullable;

import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -69,10 +69,11 @@ 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.
*/
public static @Nullable EffectSection parse(String input, @Nullable String defaultError, @Nullable SectionNode sectionNode, @Nullable List<TriggerItem> triggerItems) {
@Nullable
@SuppressWarnings({"unchecked", "rawtypes"})
public static 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.jetbrains.annotations.Nullable;
import org.eclipse.jdt.annotation.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: 14 additions & 8 deletions src/main/java/ch/njol/skript/lang/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
import ch.njol.util.Checker;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.skriptlang.skript.lang.converter.Converter;

import java.util.HashMap;
Expand Down Expand Up @@ -63,7 +64,8 @@ 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 @@ -105,7 +107,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<? extends T> stream(Event event) {
default Stream<@NonNull ? extends T> stream(Event event) {
Iterator<? extends T> iterator = iterator(event);
if (iterator == null) {
return Stream.empty();
Expand Down Expand Up @@ -176,8 +178,9 @@ default boolean canBeSingle() {
* @see Converter
* @see ConvertedExpression
*/
@Nullable
@SuppressWarnings("unchecked")
<R> @Nullable Expression<? extends R> getConvertedExpression(Class<R>... to);
<R> Expression<? extends R> getConvertedExpression(Class<R>... to);

/**
* Gets the return type of this expression.
Expand Down Expand Up @@ -262,7 +265,8 @@ 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 @@ -312,7 +316,8 @@ 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.
*/
Class<?> @Nullable [] acceptChange(ChangeMode mode);
@Nullable
Class<?>[] acceptChange(ChangeMode mode);

/**
* Tests all accepted change modes, and if so what type it expects the <code>delta</code> to be.
Expand All @@ -338,7 +343,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, Object @Nullable [] delta, ChangeMode mode);
void change(Event event, @Nullable Object[] delta, ChangeMode mode);

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

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

import org.jetbrains.annotations.Nullable;
import org.eclipse.jdt.annotation.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> {

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

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

Expand Down
25 changes: 15 additions & 10 deletions src/main/java/ch/njol/skript/lang/ExpressionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.eclipse.jdt.annotation.Nullable;

import java.lang.reflect.Array;
import java.util.ArrayList;
Expand All @@ -47,7 +47,8 @@ public class ExpressionList<T> implements Expression<T> {
protected boolean and;
private final boolean single;

private final @Nullable ExpressionList<?> source;
@Nullable
private final ExpressionList<?> source;

public ExpressionList(Expression<? extends T>[] expressions, Class<T> returnType, boolean and) {
this(expressions, returnType, and, null);
Expand Down Expand Up @@ -88,38 +89,40 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is
}

@Override
public @Nullable T getSingle(Event event) {
@Nullable
public T getSingle(Event event) {
if (!single)
throw new UnsupportedOperationException();
Expression<? extends T> expression = CollectionUtils.getRandom(expressions);
return expression != null ? expression.getSingle(event) : null;
}

@Override
@SuppressWarnings("unchecked")
public T[] getArray(Event event) {
if (and)
return getAll(event);
Expression<? extends T> expression = CollectionUtils.getRandom(expressions);
//noinspection unchecked
return expression != null ? expression.getArray(event) : (T[]) Array.newInstance(returnType, 0);
}

@Override
@SuppressWarnings("unchecked")
public T[] getAll(Event event) {
List<T> values = new ArrayList<>();
for (Expression<? extends T> expr : expressions)
values.addAll(Arrays.asList(expr.getAll(event)));
//noinspection unchecked
return values.toArray((T[]) Array.newInstance(returnType, values.size()));
}

@Override
public @Nullable Iterator<? extends T> iterator(Event event) {
@Nullable
public Iterator<? extends T> iterator(Event event) {
if (!and) {
Expression<? extends T> expression = CollectionUtils.getRandom(expressions);
return expression != null ? expression.iterator(event) : null;
}
return new Iterator<>() {
return new Iterator<T>() {
private int i = 0;
@Nullable
private Iterator<? extends T> current = null;
Expand Down Expand Up @@ -175,8 +178,9 @@ public boolean check(Event event, Checker<? super T> checker) {
}

@Override
@Nullable
@SuppressWarnings("unchecked")
public <R> @Nullable Expression<? extends R> getConvertedExpression(Class<R>... to) {
public <R> Expression<? extends R> getConvertedExpression(Class<R>... to) {
Expression<? extends R>[] exprs = new Expression[expressions.length];
Class<?>[] returnTypes = new Class[expressions.length];
for (int i = 0; i < exprs.length; i++) {
Expand Down Expand Up @@ -211,7 +215,8 @@ public void invertAnd() {
}

@Override
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
Class<?>[] exprClasses = expressions[0].acceptChange(mode);
if (exprClasses == null)
return null;
Expand All @@ -229,7 +234,7 @@ public void invertAnd() {
}

@Override
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) throws UnsupportedOperationException {
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) throws UnsupportedOperationException {
for (Expression<?> expr : expressions) {
expr.change(event, delta, mode);
}
Expand Down
Loading

0 comments on commit 8679518

Please sign in to comment.