Skip to content

Commit

Permalink
Implement Buildable interface
Browse files Browse the repository at this point in the history
Allows converting SyntaxInfos back into Builders
  • Loading branch information
APickledWalrus committed Sep 29, 2024
1 parent 73a61cd commit f4d1df6
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.bukkit.registration.BukkitSyntaxInfosImpl.EventImpl;
import org.skriptlang.skript.registration.SyntaxInfo;
import org.skriptlang.skript.registration.SyntaxInfo.Builder;

import java.util.Collection;

Expand Down Expand Up @@ -36,6 +37,13 @@ static <E extends SkriptEvent> Builder<? extends Builder<?, E>, E> builder(
return new EventImpl.BuilderImpl<>(eventClass, name);
}

/**
* {@inheritDoc}
*/
@Override
@Contract("-> new")
Builder<? extends Builder<?, E>, E> builder();

/**
* @return The listening behavior for the SkriptEvent. Determines when the event should trigger.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ static final class EventImpl<E extends SkriptEvent> implements Event<E> {
this.events = ImmutableList.copyOf(events);
}

@Override
public Builder<? extends Builder<?, E>, E> builder() {
var builder = new BuilderImpl<>(type(), name);
defaultInfo.builder().applyTo(builder);
builder.listeningBehavior(listeningBehavior);
builder.documentationId(id);
if (since != null) {
builder.since(since);
}
if (documentationId != null) {
builder.documentationId(documentationId);
}
builder.addDescription(description);
builder.addExamples(examples);
builder.addKeywords(keywords);
builder.addRequiredPlugins(requiredPlugins);
builder.addEvents(events);
return builder;
}

@Override
public ListeningBehavior listeningBehavior() {
return listeningBehavior;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ static <E extends ch.njol.skript.lang.Expression<R>, R> Builder<? extends Builde
return new ExpressionImpl.BuilderImpl<>(expressionClass);
}

/**
* {@inheritDoc}
*/
@Override
@Contract("-> new")
Builder<? extends Builder<?, E, R>, E, R> builder();

/**
* @return The class representing the supertype of all values the Expression may return.
*/
Expand Down Expand Up @@ -125,6 +132,13 @@ static <E extends org.skriptlang.skript.lang.structure.Structure> Builder<? exte
return new StructureImpl.BuilderImpl<>(structureClass);
}

/**
* {@inheritDoc}
*/
@Override
@Contract("-> new")
Builder<? extends Builder<?, E>, E> builder();

/**
* @return The entry validator to use for handling the Structure's entries.
* If null, the Structure is expected to manually handle any entries.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.google.common.base.Preconditions;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.entry.EntryValidator;
import org.skriptlang.skript.registration.SyntaxInfo.Builder;
import org.skriptlang.skript.registration.SyntaxInfoImpl.BuilderImpl;
import org.skriptlang.skript.util.Priority;

import java.util.Collection;
Expand All @@ -29,6 +31,14 @@ static class ExpressionImpl<E extends ch.njol.skript.lang.Expression<R>, R>
this.returnType = returnType;
}

@Override
public Expression.Builder<? extends Expression.Builder<?, E, R>, E, R> builder() {
var builder = new BuilderImpl<>(type());
super.builder().applyTo(builder);
builder.returnType(returnType);
return builder;
}

@Override
public Class<R> returnType() {
return returnType;
Expand Down Expand Up @@ -116,6 +126,17 @@ static class StructureImpl<E extends org.skriptlang.skript.lang.structure.Struct
this.nodeType = nodeType;
}

@Override
public Structure.Builder<? extends Structure.Builder<?, E>, E> builder() {
var builder = new BuilderImpl<>(type());
super.builder().applyTo(builder);
if (entryValidator != null) {
builder.entryValidator(entryValidator);
}
builder.nodeType(nodeType);
return builder;
}

@Override
public @Nullable EntryValidator entryValidator() {
return entryValidator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Unmodifiable;
import org.skriptlang.skript.registration.SyntaxInfoImpl.BuilderImpl;
import org.skriptlang.skript.util.Builder.Buildable;
import org.skriptlang.skript.util.Priority;

import java.util.Collection;
Expand All @@ -15,7 +16,7 @@
* @param <E> The class providing the implementation of the syntax this info represents.
*/
@ApiStatus.Experimental
public interface SyntaxInfo<E extends SyntaxElement> extends DefaultSyntaxInfos {
public interface SyntaxInfo<E extends SyntaxElement> extends Buildable<SyntaxInfo.Builder<?, ?>, SyntaxInfo<?>>, DefaultSyntaxInfos {

/**
* A priority for infos with patterns that only match simple text (they do not have any {@link Expression}s).
Expand Down Expand Up @@ -47,6 +48,13 @@ static <E extends SyntaxElement> Builder<? extends Builder<?, E>, E> builder(Cla
return new BuilderImpl<>(type);
}

/**
* {@inheritDoc}
*/
@Override
@Contract("-> new")
Builder<? extends Builder<?, E>, E> builder();

/**
* @return The origin of this syntax.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ protected SyntaxInfoImpl(
this.priority = priority;
}

@Override
public Builder<? extends Builder<?, T>, T> builder() {
var builder = new BuilderImpl<>(type);
builder.origin(origin);
if (supplier != null) {
builder.supplier(supplier);
}
builder.addPatterns(patterns);
builder.priority(priority);
return builder;
}

@Override
public SyntaxOrigin origin() {
return origin;
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/skriptlang/skript/util/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,27 @@
/**
* An interface providing common methods to be implemented for any builder.
*
* @param <B> The type of builder being used.
* @param <T> The type of object being built.
*/
@ApiStatus.Experimental
public interface Builder<B extends Builder<B, T>, T> {

/**
* Represents an object that can be converted back into a builder.
* @param <B> The type of builder being used.
* @param <T> The type of object being built.
*/
interface Buildable<B extends Builder<B, T>, T> {

/**
* @return A builder representing this object.
*/
@Contract("-> new")
B builder();

}

/**
* @return An object of <code>T</code> built from the values specified by this builder.
*/
Expand Down

0 comments on commit f4d1df6

Please sign in to comment.