Skip to content
Draft
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
56 changes: 23 additions & 33 deletions src/main/java/ch/njol/skript/expressions/ExprVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.lang.util.SimpleLiteral;
import ch.njol.util.Kleenean;

/**
Expand All @@ -23,13 +23,13 @@
@Example("message \"This server is running Minecraft %minecraft version% on Bukkit %bukkit version%\"")
@Example("message \"This server is powered by Skript %skript version%\"")
@Since("2.0")
public class ExprVersion extends SimpleExpression<String> {
private static enum VersionType {
public class ExprVersion extends SimpleLiteral<String> {

private enum VersionType {
BUKKIT("Bukkit") {
@Override
public String get() {
return "" + Bukkit.getBukkitVersion();
return Bukkit.getBukkitVersion();
}
},
MINECRAFT("Minecraft") {
Expand All @@ -44,53 +44,43 @@ public String get() {
return Skript.getVersion().toString();
}
};

private final String name;
private VersionType(final String name) {

VersionType(final String name) {
this.name = name;
}

@Override
public String toString() {
return name;
}

public abstract String get();
}

static {
Skript.registerExpression(ExprVersion.class, String.class, ExpressionType.SIMPLE, "(0¦[craft]bukkit|1¦minecraft|2¦skript)( |-)version");
}

@SuppressWarnings("null")
private VersionType type;

private VersionType versionType;

public ExprVersion() {
super(new String[0], String.class, false);
}

@SuppressWarnings("null")
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
type = VersionType.values()[parseResult.mark];
versionType = VersionType.values()[parseResult.mark];
data = new String[] {versionType.get()};
return true;
}

@Override
protected String[] get(final Event e) {
return new String[] {type.get()};
}


@Override
public String toString(final @Nullable Event e, final boolean debug) {
return type + " version";
return versionType + " version";
}

@Override
public boolean isSingle() {
return true;
}

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


}
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