Skip to content
11 changes: 8 additions & 3 deletions src/main/java/ch/njol/skript/lang/DefaultExpressionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.lang.SkriptParser.ExprInfo;
import ch.njol.util.StringUtils;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
* Utility class for {@link DefaultExpression}.
*/
final class DefaultExpressionUtils {
@ApiStatus.Internal
public final class DefaultExpressionUtils {

/**
* Check if {@code expr} is valid with the settings from {@code exprInfo}.
Expand All @@ -20,7 +22,8 @@ final class DefaultExpressionUtils {
* @param index The index of the {@link ClassInfo} in {@code exprInfo} used to grab {@code expr}.
* @return {@link DefaultExpressionError} if it's not valid, otherwise {@code null}.
*/
static @Nullable DefaultExpressionError isValid(DefaultExpression<?> expr, ExprInfo exprInfo, int index) {
@ApiStatus.Internal
public static @Nullable DefaultExpressionError isValid(DefaultExpression<?> expr, ExprInfo exprInfo, int index) {
if (expr == null) {
return DefaultExpressionError.NOT_FOUND;
} else if (!(expr instanceof Literal<?>) && (exprInfo.flagMask & SkriptParser.PARSE_EXPRESSIONS) == 0) {
Expand All @@ -35,7 +38,8 @@ final class DefaultExpressionUtils {
return null;
}

enum DefaultExpressionError {
@ApiStatus.Internal
public enum DefaultExpressionError {
/**
* Error type for when a {@link DefaultExpression} can not be found for a {@link Class}.
*/
Expand Down Expand Up @@ -132,6 +136,7 @@ public String getError(List<String> codeNames, String pattern) {
* @param pattern The pattern to include in the error message.
* @return error message.
*/
@ApiStatus.Internal
public abstract String getError(List<String> codeNames, String pattern);

/**
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/ch/njol/skript/lang/EventRestrictedSyntax.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package ch.njol.skript.lang;

import ch.njol.skript.Skript;
import ch.njol.util.Kleenean;
import ch.njol.util.StringUtils;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
* A syntax element that restricts the events it can be used in.
Expand All @@ -23,4 +29,25 @@ public interface EventRestrictedSyntax {
*/
Class<? extends Event>[] supportedEvents();

/**
* Creates a readable list of the user-facing names of the given event classes.
* @param supportedEvents The classes of the events to list.
* @return A string containing the names of the events as a list: {@code "the on death event, the on explosion event, or the on player join event"}.
*/
static @NotNull String supportedEventsNames(Class<? extends Event>[] supportedEvents) {
List<String> names = new ArrayList<>();

for (SkriptEventInfo<?> eventInfo : Skript.getEvents()) {
for (Class<? extends Event> eventClass : supportedEvents) {
for (Class<? extends Event> event : eventInfo.events) {
if (event.isAssignableFrom(eventClass)) {
names.add("the %s event".formatted(eventInfo.getName().toLowerCase()));
}
}
}
}

return StringUtils.join(names, ", ", " or ");
}

}
Loading