Skip to content

Commit e2e2e76

Browse files
Add SyntaxInfo builder methods (#7993)
1 parent c9b2222 commit e2e2e76

File tree

3 files changed

+101
-45
lines changed

3 files changed

+101
-45
lines changed

src/main/java/ch/njol/skript/conditions/base/PropertyCondition.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,11 @@ public enum PropertyType {
8585
* @param type Must be plural, for example <i>players</i> in <i>players can fly</i>
8686
* @param <E> The Condition type.
8787
* @return The registered {@link SyntaxInfo}.
88+
* @deprecated Use {@link #infoBuilder(Class, PropertyType, String, String)} to build a {@link SyntaxInfo}
89+
* and then register it using {@code registry} ({@link SyntaxRegistry#register(SyntaxRegistry.Key, SyntaxInfo)}).
8890
*/
8991
@ApiStatus.Experimental
92+
@Deprecated(since = "INSERT VERSION", forRemoval = true)
9093
public static <E extends Condition> SyntaxInfo<E> register(SyntaxRegistry registry, Class<E> condition, String property, String type) {
9194
return register(registry, condition, PropertyType.BE, property, type);
9295
}
@@ -99,27 +102,41 @@ public static <E extends Condition> SyntaxInfo<E> register(SyntaxRegistry regist
99102
* @param type Must be plural, for example <i>players</i> in <i>players can fly</i>
100103
* @param <E> The Condition type.
101104
* @return The registered {@link SyntaxInfo}.
105+
* @deprecated Use {@link #infoBuilder(Class, PropertyType, String, String)} to build a {@link SyntaxInfo}
106+
* and then register it using {@code registry} ({@link SyntaxRegistry#register(SyntaxRegistry.Key, SyntaxInfo)}).
102107
*/
103108
@ApiStatus.Experimental
109+
@Deprecated(since = "INSERT VERSION", forRemoval = true)
104110
public static <E extends Condition> SyntaxInfo<E> register(SyntaxRegistry registry, Class<E> condition, PropertyType propertyType, String property, String type) {
105111
if (type.contains("%"))
106112
throw new SkriptAPIException("The type argument must not contain any '%'s");
107-
SyntaxInfo.Builder<?, E> builder = SyntaxInfo.builder(condition).priority(DEFAULT_PRIORITY);
108-
switch (propertyType) {
109-
case BE -> builder.addPatterns("%" + type + "% (is|are) " + property,
110-
"%" + type + "% (isn't|is not|aren't|are not) " + property);
111-
case CAN -> builder.addPatterns("%" + type + "% can " + property,
112-
"%" + type + "% (can't|cannot|can not) " + property);
113-
case HAVE -> builder.addPatterns("%" + type + "% (has|have) " + property,
114-
"%" + type + "% (doesn't|does not|do not|don't) have " + property);
115-
case WILL -> builder.addPatterns("%" + type + "% will " + property,
116-
"%" + type + "% (will (not|neither)|won't) " + property);
117-
}
118-
SyntaxInfo<E> info = builder.build();
113+
SyntaxInfo<E> info = infoBuilder(condition, propertyType, property, type).build();
119114
registry.register(SyntaxRegistry.CONDITION, info);
120115
return info;
121116
}
122117

118+
/**
119+
* Creates a builder for a {@link SyntaxInfo} representing a {@link PropertyCondition}.
120+
* Patterns will be appended based on the {@code propertyType} (see {@link #getPatterns(PropertyType, String, String)}).
121+
* The info will use {@link #DEFAULT_PRIORITY} as its {@link SyntaxInfo#priority()}.
122+
* @param condition The condition class to be represented by the info.
123+
* @param propertyType The property type, see {@link PropertyType}
124+
* @param property The property name. For example, {@code empty} in {@code %strings% are empty}.
125+
* @param type The type(s) on which the property is present. Must be plural.
126+
* For example {@code strings} in {@code %strings% are empty}.
127+
* @param <E> The Condition type.
128+
* @return A {@link SyntaxInfo} representing the property conditon.
129+
*/
130+
@ApiStatus.Experimental
131+
public static <E extends Condition> SyntaxInfo.Builder<? extends SyntaxInfo.Builder<?, E>, E> infoBuilder(
132+
Class<E> condition, PropertyType propertyType, String property, String type) {
133+
if (type.contains("%"))
134+
throw new SkriptAPIException("The type argument must not contain any '%'s");
135+
return SyntaxInfo.builder(condition)
136+
.priority(DEFAULT_PRIORITY)
137+
.addPatterns(getPatterns(propertyType, property, type));
138+
}
139+
123140
/**
124141
* Registers a new property condition. The property type is set to {@link PropertyType#BE}.
125142
*

src/main/java/ch/njol/skript/expressions/base/EventValueExpression.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,13 @@ public class EventValueExpression<T> extends SimpleExpression<T> implements Defa
7676
* @param <T> The return type.
7777
* @param <E> The Expression type.
7878
* @return The registered {@link SyntaxInfo}.
79+
* @deprecated Use {@link #infoBuilder(Class, Class, String...)} to build a {@link SyntaxInfo}
80+
* and then register it using {@code registry} ({@link SyntaxRegistry#register(SyntaxRegistry.Key, SyntaxInfo)}).
7981
*/
8082
@ApiStatus.Experimental
83+
@Deprecated(since = "INSERT VERSION", forRemoval = true)
8184
public static <E extends EventValueExpression<T>, T> SyntaxInfo.Expression<E, T> register(SyntaxRegistry registry, Class<E> expressionClass, Class<T> returnType, String pattern) {
82-
SyntaxInfo.Expression<E, T> info = SyntaxInfo.Expression.builder(expressionClass, returnType)
83-
.priority(DEFAULT_PRIORITY)
84-
.addPattern("[the] " + pattern)
85-
.build();
86-
registry.register(SyntaxRegistry.EXPRESSION, info);
87-
return info;
85+
return register(registry, expressionClass, returnType, new String[]{pattern});
8886
}
8987

9088
/**
@@ -99,23 +97,42 @@ public static <E extends EventValueExpression<T>, T> SyntaxInfo.Expression<E, T>
9997
* @param <T> The return type.
10098
* @param <E> The Expression type.
10199
* @return The registered {@link SyntaxInfo}.
100+
* @deprecated Use {@link #infoBuilder(Class, Class, String...)} to build a {@link SyntaxInfo}
101+
* and then register it using {@code registry} ({@link SyntaxRegistry#register(SyntaxRegistry.Key, SyntaxInfo)}).
102102
*/
103+
@ApiStatus.Experimental
104+
@Deprecated(since = "INSERT VERSION", forRemoval = true)
103105
public static <E extends EventValueExpression<T>, T> DefaultSyntaxInfos.Expression<E, T> register(
104106
SyntaxRegistry registry,
105107
Class<E> expressionClass,
106108
Class<T> returnType,
107109
String ... patterns
108110
) {
111+
SyntaxInfo.Expression<E, T> info = infoBuilder(expressionClass, returnType, patterns).build();
112+
registry.register(SyntaxRegistry.EXPRESSION, info);
113+
return info;
114+
}
115+
116+
/**
117+
* Creates a builder for a {@link SyntaxInfo} representing a {@link EventValueExpression} with the provided patterns.
118+
* The info will use {@link #DEFAULT_PRIORITY} as its {@link SyntaxInfo#priority()}.
119+
* This method will append '[the]' to the beginning of each patterns
120+
* @param expressionClass The expression class to be represented by the info.
121+
* @param returnType The class representing the expression's return type.
122+
* @param patterns The patterns to match for creating this expression.
123+
* @param <T> The return type.
124+
* @param <E> The Expression type.
125+
* @return The registered {@link SyntaxInfo}.
126+
*/
127+
@ApiStatus.Experimental
128+
public static <E extends EventValueExpression<T>, T> SyntaxInfo.Expression.Builder<? extends SyntaxInfo.Expression.Builder<?, E, T>, E, T> infoBuilder(
129+
Class<E> expressionClass, Class<T> returnType, String... patterns) {
109130
for (int i = 0; i < patterns.length; i++) {
110-
if (!StringUtils.startsWithIgnoreCase(patterns[i], "[the] "))
111-
patterns[i] = "[the] " + patterns[i];
131+
patterns[i] = "[the] " + patterns[i];
112132
}
113-
SyntaxInfo.Expression<E, T> info = SyntaxInfo.Expression.builder(expressionClass, returnType)
133+
return SyntaxInfo.Expression.builder(expressionClass, returnType)
114134
.priority(DEFAULT_PRIORITY)
115-
.addPatterns(patterns)
116-
.build();
117-
registry.register(SyntaxRegistry.EXPRESSION, info);
118-
return info;
135+
.addPatterns(patterns);
119136
}
120137

121138
/**

src/main/java/ch/njol/skript/expressions/base/PropertyExpression.java

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,29 +91,17 @@ public static String[] getDefaultPatterns(String property, String fromType) {
9191
* @param <T> The return type.
9292
* @param <E> The Expression type.
9393
* @return The registered {@link SyntaxInfo}.
94+
* @deprecated Use {@link #infoBuilder(Class, Class, String, String, boolean)} to build a {@link SyntaxInfo}
95+
* and then register it using {@code registry} ({@link SyntaxRegistry#register(SyntaxRegistry.Key, SyntaxInfo)}).
9496
*/
9597
@ApiStatus.Experimental
98+
@Deprecated(since = "INSERT VERSION", forRemoval = true)
9699
public static <E extends Expression<T>, T> SyntaxInfo.Expression<E, T> register(SyntaxRegistry registry, Class<E> expressionClass, Class<T> returnType, String property, String fromType) {
97-
SyntaxInfo.Expression<E, T> info = SyntaxInfo.Expression.builder(expressionClass, returnType)
98-
.priority(DEFAULT_PRIORITY)
99-
.addPatterns(getPatterns(property, fromType))
100-
.build();
100+
SyntaxInfo.Expression<E, T> info = infoBuilder(expressionClass, returnType, property, fromType, false).build();
101101
registry.register(SyntaxRegistry.EXPRESSION, info);
102102
return info;
103103
}
104104

105-
/**
106-
* Registers an expression as {@link ExpressionType#PROPERTY} with the two default property patterns "property of %types%" and "%types%'[s] property"
107-
*
108-
* @param expressionClass the PropertyExpression class being registered.
109-
* @param type the main expression type the property is based off of.
110-
* @param property the name of the property.
111-
* @param fromType should be plural to support multiple objects but doesn't have to be.
112-
*/
113-
public static <T> void register(Class<? extends Expression<T>> expressionClass, Class<T> type, String property, String fromType) {
114-
Skript.registerExpression(expressionClass, type, ExpressionType.PROPERTY, getPatterns(property, fromType));
115-
}
116-
117105
/**
118106
* Registers an expression with the two default property patterns "property [of %types%]" and "%types%'[s] property"
119107
* This method also makes the expression type optional to force a default expression on the property expression.
@@ -126,17 +114,51 @@ public static <T> void register(Class<? extends Expression<T>> expressionClass,
126114
* @param <T> The return type.
127115
* @param <E> The Expression type.
128116
* @return The registered {@link SyntaxInfo}.
117+
* @deprecated Use {@link #infoBuilder(Class, Class, String, String, boolean)} to build a {@link SyntaxInfo}
118+
* and then register it using {@code registry} ({@link SyntaxRegistry#register(SyntaxRegistry.Key, SyntaxInfo)}).
129119
*/
130120
@ApiStatus.Experimental
121+
@Deprecated(since = "INSERT VERSION", forRemoval = true)
131122
public static <E extends Expression<T>, T> SyntaxInfo.Expression<E, T> registerDefault(SyntaxRegistry registry, Class<E> expressionClass, Class<T> returnType, String property, String fromType) {
132-
SyntaxInfo.Expression<E, T> info = SyntaxInfo.Expression.builder(expressionClass, returnType)
133-
.priority(DEFAULT_PRIORITY)
134-
.addPatterns(getDefaultPatterns(property, fromType))
135-
.build();
123+
SyntaxInfo.Expression<E, T> info = infoBuilder(expressionClass, returnType, property, fromType, true).build();
136124
registry.register(SyntaxRegistry.EXPRESSION, info);
137125
return info;
138126
}
139127

128+
/**
129+
* Registers an expression with the two default property patterns "property [of %types%]" and "%types%'[s] property"
130+
* This method also makes the expression type optional to force a default expression on the property expression.
131+
*
132+
* @param expressionClass The expression class to be represented by the info.
133+
* @param returnType The class representing the expression's return type.
134+
* @param property The property name. For example, {@code length} in {@code length of %strings%}.
135+
* @param type The type(s) on which the property is present. Should typically be plural.
136+
* For example, {@code strings} in {@code length of %strings%}.
137+
* @param isDefault Whether {@code type} can be optional in the patterns (e.g., filled by a {@link ch.njol.skript.lang.DefaultExpression}).
138+
* @param <T> The return type.
139+
* @param <E> The Expression type.
140+
* @return The registered {@link SyntaxInfo}.
141+
*/
142+
@ApiStatus.Experimental
143+
public static <E extends Expression<T>, T> SyntaxInfo.Expression.Builder<? extends SyntaxInfo.Expression.Builder<?, E, T>, E, T> infoBuilder(
144+
Class<E> expressionClass, Class<T> returnType, String property, String type, boolean isDefault) {
145+
return SyntaxInfo.Expression.builder(expressionClass, returnType)
146+
.priority(DEFAULT_PRIORITY)
147+
.addPatterns(patternsOf(property, type, isDefault));
148+
}
149+
150+
/**
151+
* Registers an expression as {@link ExpressionType#PROPERTY} with the two default property patterns "property of %types%" and "%types%'[s] property"
152+
*
153+
* @param expressionClass the PropertyExpression class being registered.
154+
* @param type the main expression type the property is based off of.
155+
* @param property the name of the property.
156+
* @param fromType should be plural to support multiple objects but doesn't have to be.
157+
*/
158+
public static <T> void register(Class<? extends Expression<T>> expressionClass, Class<T> type, String property, String fromType) {
159+
Skript.registerExpression(expressionClass, type, ExpressionType.PROPERTY, getPatterns(property, fromType));
160+
}
161+
140162
/**
141163
* Registers an expression as {@link ExpressionType#PROPERTY} with the two default property patterns "property [of %types%]" and "%types%'[s] property"
142164
* This method also makes the expression type optional to force a default expression on the property expression.

0 commit comments

Comments
 (0)