Skip to content

Commit

Permalink
Use builder method for Expression return type
Browse files Browse the repository at this point in the history
  • Loading branch information
APickledWalrus committed Jun 28, 2024
1 parent 5f483a5 commit a11a9a9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -1574,7 +1574,8 @@ public static <E extends Expression<T>, T> void registerExpression(
Class<E> expressionType, Class<T> returnType, ExpressionType type, String... patterns
) throws IllegalArgumentException {
checkAcceptRegistrations();
skriptRegistry.register(SyntaxRegistry.EXPRESSION, SyntaxInfo.Expression.builder(expressionType, returnType)
skriptRegistry.register(SyntaxRegistry.EXPRESSION, SyntaxInfo.Expression.builder(expressionType)
.returnType(returnType)
.priority(type.priority())
.origin(getSyntaxOrigin(JavaPlugin.getProvidingPlugin(expressionType)))
.addPatterns(patterns)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ public class EventValueExpression<T> extends SimpleExpression<T> implements Defa
*/
@ApiStatus.Experimental
public static <T> void register(SyntaxRegistry registry, Class<? extends EventValueExpression<T>> expressionClass, Class<T> type, String pattern) {
registry.register(SyntaxRegistry.EXPRESSION, SyntaxInfo.Expression.builder(expressionClass, type)
.priority(DEFAULT_PRIORITY)
.addPattern("[the] " + pattern)
.build()
registry.register(SyntaxRegistry.EXPRESSION, SyntaxInfo.Expression.builder(expressionClass)
.returnType(type)
.priority(DEFAULT_PRIORITY)
.addPattern("[the] " + pattern)
.build()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public abstract class PropertyExpression<F, T> extends SimpleExpression<T> {
*/
@ApiStatus.Experimental
public static <T> void register(SyntaxRegistry registry, Class<? extends Expression<T>> expressionClass, Class<T> type, String property, String fromType) {
registry.register(SyntaxRegistry.EXPRESSION, SyntaxInfo.Expression.builder(expressionClass, type)
registry.register(SyntaxRegistry.EXPRESSION, SyntaxInfo.Expression.builder(expressionClass)
.returnType(type)
.priority(DEFAULT_PRIORITY)
.addPatterns(
"[the] " + property + " of %" + fromType + "%",
Expand Down Expand Up @@ -101,7 +102,8 @@ public static <T> void register(Class<? extends Expression<T>> expressionClass,
*/
@ApiStatus.Experimental
public static <T> void registerDefault(SyntaxRegistry registry, Class<? extends Expression<T>> expressionClass, Class<T> type, String property, String fromType) {
registry.register(SyntaxRegistry.EXPRESSION, SyntaxInfo.Expression.builder(expressionClass, type)
registry.register(SyntaxRegistry.EXPRESSION, SyntaxInfo.Expression.builder(expressionClass)
.returnType(type)
.priority(DEFAULT_PRIORITY)
.addPatterns(
"[the] " + property + " [of %" + fromType + "%]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ interface Expression<E extends ch.njol.skript.lang.Expression<R>, R> extends Syn
/**
* Constructs a builder for an expression syntax info.
* @param expressionClass The Expression class the info will represent.
* @return An Expression-specific builder for creating a syntax info representing <code>type</code>.
* @return An Expression-specific builder for creating a syntax info representing <code>expressionClass</code>.
* @param <E> The class providing the implementation of the Expression this info represents.
* @param <R> The type of the return type of the Expression.
*/
@Contract("_, _ -> new")
static <E extends ch.njol.skript.lang.Expression<R>, R> Builder<? extends Builder<?, E, R>, E, R> builder(Class<E> expressionClass, Class<R> returnType) {
return new ExpressionImpl.BuilderImpl<>(expressionClass, returnType);
@Contract("_ -> new")
static <E extends ch.njol.skript.lang.Expression<R>, R> Builder<? extends Builder<?, E, R>, E, R> builder(Class<E> expressionClass) {
return new ExpressionImpl.BuilderImpl<>(expressionClass);
}

/**
Expand All @@ -53,13 +52,22 @@ static <E extends ch.njol.skript.lang.Expression<R>, R> Builder<? extends Builde

/**
* An Expression-specific builder is used for constructing a new Expression syntax info.
* @see #builder(Class, Class)
* @see #builder(Class)
* @param <B> The type of builder being used.
* @param <E> The Expression class providing the implementation of the syntax info being built.
* @param <R> The type of the return type of the Expression.
*/
interface Builder<B extends Builder<B, E, R>, E extends ch.njol.skript.lang.Expression<R>, R> extends SyntaxInfo.Builder<B, E> {

/**
* Sets the class representing the supertype of all values the Expression may return.
* @param returnType The class to use as the return type.
* @return This builder.
* @see Expression#returnType()
*/
@Contract("_ -> this")
B returnType(Class<R> returnType);

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -118,7 +126,7 @@ public boolean canBeSection() {
/**
* Constructs a builder for a structure syntax info.
* @param structureClass The Structure class the info will represent.
* @return A Structure-specific builder for creating a syntax info representing <code>type</code>.
* @return A Structure-specific builder for creating a syntax info representing <code>structureClass</code>.
* @param <E> The class providing the implementation of the Structure this info represents.
*/
@Contract("_ -> new")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.skriptlang.skript.registration;

import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.entry.EntryValidator;
import org.skriptlang.skript.util.Priority;
Expand All @@ -39,9 +40,10 @@ public static class ExpressionImpl<E extends ch.njol.skript.lang.Expression<R>,

ExpressionImpl(
SyntaxOrigin origin, Class<E> type, @Nullable Supplier<E> supplier,
Collection<String> patterns, Priority priority, Class<R> returnType
Collection<String> patterns, Priority priority, @Nullable Class<R> returnType
) {
super(origin, type, supplier, patterns, priority);
Preconditions.checkArgument(returnType != null, "An expression syntax info must have a return type.");
this.returnType = returnType;
}

Expand Down Expand Up @@ -77,15 +79,21 @@ public String toString() {
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
static final class BuilderImpl<B extends Expression.Builder<B, E, R>, E extends ch.njol.skript.lang.Expression<R>, R>
extends SyntaxInfoImpl.BuilderImpl<B, E>
implements Expression.Builder<B, E, R> {

private final Class<R> returnType;
private @Nullable Class<R> returnType;

BuilderImpl(Class<E> expressionClass, Class<R> returnType) {
BuilderImpl(Class<E> expressionClass) {
super(expressionClass);
}

@Override
public B returnType(Class<R> returnType) {
this.returnType = returnType;
return (B) this;
}

public Expression<E, R> build() {
Expand Down

0 comments on commit a11a9a9

Please sign in to comment.