Skip to content

Commit

Permalink
Return the constructed info for static registration methods
Browse files Browse the repository at this point in the history
  • Loading branch information
APickledWalrus committed Sep 2, 2024
1 parent f1f4915 commit 96874c5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 78 deletions.
68 changes: 28 additions & 40 deletions src/main/java/ch/njol/skript/conditions/base/PropertyCondition.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,57 +96,45 @@ public enum PropertyType {
}

/**
* @param registry the SyntaxRegistry to register this PropertyCondition with.
* @param condition the class to register
* @param property the property name, for example <i>fly</i> in <i>players can fly</i>
* @param type must be plural, for example <i>players</i> in <i>players can fly</i>
* @param registry The SyntaxRegistry to register with.
* @param condition The class to register
* @param property The property name, for example <i>fly</i> in <i>players can fly</i>
* @param type Must be plural, for example <i>players</i> in <i>players can fly</i>
* @param <E> The Condition type.
* @return The registered {@link SyntaxInfo}.
*/
@ApiStatus.Experimental
public static void register(SyntaxRegistry registry, Class<? extends Condition> condition, String property, String type) {
register(registry, condition, PropertyType.BE, property, type);
public static <E extends Condition> SyntaxInfo<E> register(SyntaxRegistry registry, Class<E> condition, String property, String type) {
return register(registry, condition, PropertyType.BE, property, type);
}

/**
* @param registry the SyntaxRegistry to register this PropertyCondition with.
* @param condition the class to register
* @param propertyType the property type, see {@link PropertyType}
* @param property the property name, for example <i>fly</i> in <i>players can fly</i>
* @param type must be plural, for example <i>players</i> in <i>players can fly</i>
* @param registry The SyntaxRegistry to register with.
* @param condition The class to register
* @param propertyType The property type, see {@link PropertyType}
* @param property The property name, for example <i>fly</i> in <i>players can fly</i>
* @param type Must be plural, for example <i>players</i> in <i>players can fly</i>
* @param <E> The Condition type.
* @return The registered {@link SyntaxInfo}.
*/
@ApiStatus.Experimental
public static void register(SyntaxRegistry registry, Class<? extends Condition> condition, PropertyType propertyType, String property, String type) {
public static <E extends Condition> SyntaxInfo<E> register(SyntaxRegistry registry, Class<E> condition, PropertyType propertyType, String property, String type) {
if (type.contains("%"))
throw new SkriptAPIException("The type argument must not contain any '%'s");
SyntaxInfo.Builder<?, ? extends Condition> builder = SyntaxInfo.builder(condition).priority(DEFAULT_PRIORITY);
SyntaxInfo.Builder<?, E> builder = SyntaxInfo.builder(condition).priority(DEFAULT_PRIORITY);
switch (propertyType) {
case BE:
builder.addPatterns(
"%" + type + "% (is|are) " + property,
"%" + type + "% (isn't|is not|aren't|are not) " + property
);
break;
case CAN:
builder.addPatterns(
"%" + type + "% can " + property,
"%" + type + "% (can't|cannot|can not) " + property
);
break;
case HAVE:
builder.addPatterns(
"%" + type + "% (has|have) " + property,
"%" + type + "% (doesn't|does not|do not|don't) have " + property
);
break;
case WILL:
builder.addPatterns(
"%" + type + "% will " + property,
"%" + type + "% (will (not|neither)|won't) " + property
);
break;
default:
assert false;
case BE -> builder.addPatterns("%" + type + "% (is|are) " + property,
"%" + type + "% (isn't|is not|aren't|are not) " + property);
case CAN -> builder.addPatterns("%" + type + "% can " + property,
"%" + type + "% (can't|cannot|can not) " + property);
case HAVE -> builder.addPatterns("%" + type + "% (has|have) " + property,
"%" + type + "% (doesn't|does not|do not|don't) have " + property);
case WILL -> builder.addPatterns("%" + type + "% will " + property,
"%" + type + "% (will (not|neither)|won't) " + property);
}
registry.register(SyntaxRegistry.CONDITION, builder.build());
SyntaxInfo<E> info = builder.build();
registry.register(SyntaxRegistry.CONDITION, info);
return info;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,26 @@ public class EventValueExpression<T> extends SimpleExpression<T> implements Defa

/**
* Registers an event value expression with the provided pattern.
* The syntax info will be forced to use the {@link #DEFAULT_PRIORITY} priority.
* This also adds '[the]' to the start of the pattern.
*
* @param registry the SyntaxRegistry to register this PropertyExpression with.
* @param expressionClass The class that represents this EventValueExpression.
* @param type The return type of the expression.
* @param pattern The pattern for this syntax.
* @param registry The SyntaxRegistry to register with.
* @param expressionClass The EventValueExpression class being registered.
* @param returnType The class representing the expression's return type.
* @param pattern The pattern to match for creating this expression.
* @param <T> The return type.
* @param <E> The Expression type.
* @return The registered {@link SyntaxInfo}.
*/
@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)
.returnType(type)
public static <E extends EventValueExpression<T>, T> SyntaxInfo.Expression<E, T> register(SyntaxRegistry registry, Class<E> expressionClass, Class<T> returnType, String pattern) {
SyntaxInfo.Expression<E, T> info = SyntaxInfo.Expression.builder(expressionClass)
.returnType(returnType)
.priority(DEFAULT_PRIORITY)
.addPattern("[the] " + pattern)
.build()
);
.build();
registry.register(SyntaxRegistry.EXPRESSION, info);
return info;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,25 @@ public abstract class PropertyExpression<F, T> extends SimpleExpression<T> {
/**
* Registers an expression with the two default property patterns "property of %types%" and "%types%'[s] property"
*
* @param registry the SyntaxRegistry to register this PropertyExpression with.
* @param expressionClass the PropertyExpression class being registered.
* @param type the main expression type the property is based off of.
* @param property the name of the property.
* @param fromType should be plural to support multiple objects but doesn't have to be.
* @param registry The SyntaxRegistry to register with.
* @param expressionClass The PropertyExpression class being registered.
* @param returnType The class representing the expression's return type.
* @param property The name of the property.
* @param fromType Should be plural to support multiple objects but doesn't have to be.
* @param <T> The return type.
* @param <E> The Expression type.
* @return The registered {@link SyntaxInfo}.
*/
@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)
.returnType(type)
public static <E extends Expression<T>, T> SyntaxInfo.Expression<E, T> register(SyntaxRegistry registry, Class<E> expressionClass, Class<T> returnType, String property, String fromType) {
SyntaxInfo.Expression<E, T> info = SyntaxInfo.Expression.builder(expressionClass)
.returnType(returnType)
.priority(DEFAULT_PRIORITY)
.addPatterns(
"[the] " + property + " of %" + fromType + "%",
"%" + fromType + "%'[s] " + property
)
.build()
);
.addPatterns("[the] " + property + " of %" + fromType + "%",
"%" + fromType + "%'[s] " + property)
.build();
registry.register(SyntaxRegistry.EXPRESSION, info);
return info;
}

/**
Expand All @@ -92,23 +94,25 @@ public static <T> void register(Class<? extends Expression<T>> expressionClass,
* Registers an expression with the two default property patterns "property [of %types%]" and "%types%'[s] property"
* This method also makes the expression type optional to force a default expression on the property expression.
*
* @param registry the SyntaxRegistry to register this PropertyExpression with.
* @param expressionClass the PropertyExpression class being registered.
* @param type the main expression type the property is based off of.
* @param property the name of the property.
* @param fromType should be plural to support multiple objects but doesn't have to be.
* @param registry The SyntaxRegistry to register with.
* @param expressionClass The PropertyExpression class being registered.
* @param returnType The class representing the expression's return type.
* @param property The name of the property.
* @param fromType Should be plural to support multiple objects but doesn't have to be.
* @param <T> The return type.
* @param <E> The Expression type.
* @return The registered {@link SyntaxInfo}.
*/
@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)
.returnType(type)
public static <E extends Expression<T>, T> SyntaxInfo.Expression<E, T> registerDefault(SyntaxRegistry registry, Class<E> expressionClass, Class<T> returnType, String property, String fromType) {
SyntaxInfo.Expression<E, T> info = SyntaxInfo.Expression.builder(expressionClass)
.returnType(returnType)
.priority(DEFAULT_PRIORITY)
.addPatterns(
"[the] " + property + " [of %" + fromType + "%]",
"%" + fromType + "%'[s] " + property
)
.build()
);
.addPatterns("[the] " + property + " [of %" + fromType + "%]",
"%" + fromType + "%'[s] " + property)
.build();
registry.register(SyntaxRegistry.EXPRESSION, info);
return info;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package org.skriptlang.skript.registration;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.entry.EntryValidator;
import org.skriptlang.skript.registration.DefaultSyntaxInfosImpl.ExpressionImpl;
import org.skriptlang.skript.registration.DefaultSyntaxInfosImpl.StructureImpl;

interface DefaultSyntaxInfos {
/**
* This class is not safe to be directly referenced.
* Use {@link SyntaxInfo} instead.
*/
@ApiStatus.Internal
@ApiStatus.Experimental
public interface DefaultSyntaxInfos {

/**
* A syntax info to be used for {@link ch.njol.skript.lang.Expression}s.
* It differs from a typical info in that it also has a return type.
* @param <E> The class providing the implementation of the Expression this info represents.
* @param <R> The type of the return type of the Expression.
*/
@ApiStatus.Experimental
interface Expression<E extends ch.njol.skript.lang.Expression<R>, R> extends SyntaxInfo<E> {

/**
Expand Down Expand Up @@ -66,6 +74,7 @@ interface Builder<B extends Builder<B, E, R>, E extends ch.njol.skript.lang.Expr
* It contains additional details including the {@link EntryValidator} to use, if any.
* @param <E> The class providing the implementation of the Structure this info represents.
*/
@ApiStatus.Experimental
interface Structure<E extends org.skriptlang.skript.lang.structure.Structure> extends SyntaxInfo<E> {

/**
Expand Down

0 comments on commit 96874c5

Please sign in to comment.