Skip to content

Commit a4da04d

Browse files
committed
Add more supported time-spans and 'otherwise' default expressions.
1 parent 9e118f4 commit a4da04d

File tree

13 files changed

+483
-2
lines changed

13 files changed

+483
-2
lines changed

src/main/java/mx/kenzie/skript/compiler/SkriptLangSpec.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ private SkriptLangSpec() {
154154
new NotEqual(),
155155
new IsEqual(),
156156
new Contains(),
157+
new TernaryOtherwiseExpression(),
158+
new BinaryOtherwiseExpression(),
157159
new StringLiteral(),
158160
new SupplierSection(),
159161
new RunnableSection(),
@@ -176,6 +178,12 @@ private SkriptLangSpec() {
176178
new DynamicFunctionExpression(),
177179
new MillisecondsExpression(),
178180
new SecondsExpression(),
181+
new MinutesExpression(),
182+
new HoursExpression(),
183+
new DaysExpression(),
184+
new WeeksExpression(),
185+
new MonthsExpression(),
186+
new YearsExpression(),
179187
new IntegerLiteral(),
180188
new LongLiteral(),
181189
new FloatLiteral(),
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package mx.kenzie.skript.lang.syntax.generic;
2+
3+
import mx.kenzie.foundation.MethodBuilder;
4+
import mx.kenzie.foundation.Type;
5+
import mx.kenzie.skript.api.Referent;
6+
import mx.kenzie.skript.api.note.ForceExtract;
7+
import mx.kenzie.skript.api.syntax.SimpleExpression;
8+
import mx.kenzie.skript.compiler.CommonTypes;
9+
import mx.kenzie.skript.compiler.Context;
10+
import mx.kenzie.skript.compiler.Pattern;
11+
import mx.kenzie.skript.compiler.SkriptLangSpec;
12+
import mx.kenzie.skript.lang.element.StandardElements;
13+
import mx.kenzie.skript.lang.handler.StandardHandlers;
14+
15+
import java.lang.reflect.Method;
16+
17+
public class BinaryOtherwiseExpression extends SimpleExpression implements Referent {
18+
19+
public BinaryOtherwiseExpression() {
20+
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "%Object% (otherwise|\\\\?) %Object%");
21+
try {
22+
handlers.put(StandardHandlers.GET, BinaryOtherwiseExpression.class.getMethod("getResult", Object.class, Object.class));
23+
} catch (NoSuchMethodException e) {
24+
e.printStackTrace();
25+
}
26+
}
27+
28+
@Override
29+
public Type getReturnType() {
30+
return CommonTypes.OBJECT;
31+
}
32+
33+
@Override
34+
public boolean allowAsInputFor(Type type) {
35+
return super.allowAsInputFor(type) || type.equals(CommonTypes.OBJECT);
36+
}
37+
38+
@Override
39+
public Type getHolderType() {
40+
return CommonTypes.OBJECT;
41+
}
42+
43+
@Override
44+
public void compile(Context context, Pattern.Match match) throws Throwable {
45+
final MethodBuilder method = context.getMethod();
46+
assert method != null;
47+
final Method target = handlers.get(context.getHandlerMode());
48+
assert target != null;
49+
this.writeCall(method, target, context);
50+
}
51+
52+
@ForceExtract
53+
public static Object getResult(Object first, Object def) {
54+
if (first == null) return def;
55+
return first;
56+
}
57+
58+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package mx.kenzie.skript.lang.syntax.generic;
2+
3+
import mx.kenzie.foundation.MethodBuilder;
4+
import mx.kenzie.foundation.Type;
5+
import mx.kenzie.skript.api.Referent;
6+
import mx.kenzie.skript.api.note.ForceExtract;
7+
import mx.kenzie.skript.api.syntax.SimpleExpression;
8+
import mx.kenzie.skript.compiler.CommonTypes;
9+
import mx.kenzie.skript.compiler.Context;
10+
import mx.kenzie.skript.compiler.Pattern;
11+
import mx.kenzie.skript.compiler.SkriptLangSpec;
12+
import mx.kenzie.skript.lang.element.StandardElements;
13+
import mx.kenzie.skript.lang.handler.StandardHandlers;
14+
15+
import java.lang.reflect.Method;
16+
17+
public class TernaryOtherwiseExpression extends SimpleExpression implements Referent {
18+
19+
public TernaryOtherwiseExpression() {
20+
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "if %Boolean% then %Object% otherwise %Object%",
21+
"%Boolean% \\\\? %Object% : %Object%");
22+
try {
23+
handlers.put(StandardHandlers.GET, TernaryOtherwiseExpression.class.getMethod("getResult", Boolean.class, Object.class, Object.class));
24+
} catch (NoSuchMethodException e) {
25+
e.printStackTrace();
26+
}
27+
}
28+
29+
@Override
30+
public Type getReturnType() {
31+
return CommonTypes.OBJECT;
32+
}
33+
34+
@Override
35+
public boolean allowAsInputFor(Type type) {
36+
return super.allowAsInputFor(type) || type.equals(CommonTypes.OBJECT);
37+
}
38+
39+
@Override
40+
public Type getHolderType() {
41+
return CommonTypes.OBJECT;
42+
}
43+
44+
@Override
45+
public void compile(Context context, Pattern.Match match) throws Throwable {
46+
final MethodBuilder method = context.getMethod();
47+
assert method != null;
48+
final Method target = handlers.get(context.getHandlerMode());
49+
assert target != null;
50+
this.writeCall(method, target, context);
51+
}
52+
53+
@ForceExtract
54+
public static Object getResult(Boolean check, Object first, Object def) {
55+
if (check == null) return def;
56+
if (check) return first;
57+
return def;
58+
}
59+
60+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package mx.kenzie.skript.lang.syntax.timing;
2+
3+
import mx.kenzie.foundation.MethodBuilder;
4+
import mx.kenzie.foundation.Type;
5+
import mx.kenzie.skript.api.note.ForceExtract;
6+
import mx.kenzie.skript.api.syntax.SimpleExpression;
7+
import mx.kenzie.skript.compiler.*;
8+
import mx.kenzie.skript.error.ScriptRuntimeError;
9+
import mx.kenzie.skript.lang.element.StandardElements;
10+
import mx.kenzie.skript.lang.handler.StandardHandlers;
11+
12+
import java.lang.reflect.Method;
13+
import java.time.Duration;
14+
15+
public class DaysExpression extends SimpleExpression {
16+
17+
public DaysExpression() {
18+
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "%Number% day[s]");
19+
try {
20+
handlers.put(StandardHandlers.FIND, this.getClass().getMethod("find", Object.class));
21+
} catch (NoSuchMethodException e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
26+
@Override
27+
public boolean allowAsInputFor(Type type) {
28+
return CommonTypes.DURATION.equals(type) || CommonTypes.OBJECT.equals(type) || super.allowAsInputFor(type);
29+
}
30+
31+
@Override
32+
public Pattern.Match match(String thing, Context context) {
33+
return super.match(thing, context);
34+
}
35+
36+
@Override
37+
public void compile(Context context, Pattern.Match match) {
38+
final MethodBuilder method = context.getMethod();
39+
assert method != null;
40+
final Method target = handlers.get(StandardHandlers.FIND);
41+
assert target != null;
42+
this.writeCall(method, target, context);
43+
context.setState(CompileState.STATEMENT);
44+
}
45+
46+
@ForceExtract
47+
public static Object find(Object object) {
48+
if (!(object instanceof Number number))
49+
throw new ScriptRuntimeError("Timespan expression requires number.");
50+
return Duration.ofDays(number.longValue());
51+
}
52+
53+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package mx.kenzie.skript.lang.syntax.timing;
2+
3+
import mx.kenzie.foundation.MethodBuilder;
4+
import mx.kenzie.foundation.Type;
5+
import mx.kenzie.skript.api.note.ForceExtract;
6+
import mx.kenzie.skript.api.syntax.SimpleExpression;
7+
import mx.kenzie.skript.compiler.*;
8+
import mx.kenzie.skript.error.ScriptRuntimeError;
9+
import mx.kenzie.skript.lang.element.StandardElements;
10+
import mx.kenzie.skript.lang.handler.StandardHandlers;
11+
12+
import java.lang.reflect.Method;
13+
import java.time.Duration;
14+
15+
public class HoursExpression extends SimpleExpression {
16+
17+
public HoursExpression() {
18+
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "%Number% hour[s]");
19+
try {
20+
handlers.put(StandardHandlers.FIND, this.getClass().getMethod("find", Object.class));
21+
} catch (NoSuchMethodException e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
26+
@Override
27+
public boolean allowAsInputFor(Type type) {
28+
return CommonTypes.DURATION.equals(type) || CommonTypes.OBJECT.equals(type) || super.allowAsInputFor(type);
29+
}
30+
31+
@Override
32+
public Pattern.Match match(String thing, Context context) {
33+
return super.match(thing, context);
34+
}
35+
36+
@Override
37+
public void compile(Context context, Pattern.Match match) {
38+
final MethodBuilder method = context.getMethod();
39+
assert method != null;
40+
final Method target = handlers.get(StandardHandlers.FIND);
41+
assert target != null;
42+
this.writeCall(method, target, context);
43+
context.setState(CompileState.STATEMENT);
44+
}
45+
46+
@ForceExtract
47+
public static Object find(Object object) {
48+
if (!(object instanceof Number)) {
49+
throw new ScriptRuntimeError("Timespan expression requires number.");
50+
} else {
51+
final Number number = (Number) object;
52+
return Duration.ofHours(number.longValue());
53+
}
54+
}
55+
56+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package mx.kenzie.skript.lang.syntax.timing;
2+
3+
import mx.kenzie.foundation.MethodBuilder;
4+
import mx.kenzie.foundation.Type;
5+
import mx.kenzie.skript.api.note.ForceExtract;
6+
import mx.kenzie.skript.api.syntax.SimpleExpression;
7+
import mx.kenzie.skript.compiler.*;
8+
import mx.kenzie.skript.error.ScriptRuntimeError;
9+
import mx.kenzie.skript.lang.element.StandardElements;
10+
import mx.kenzie.skript.lang.handler.StandardHandlers;
11+
12+
import java.lang.reflect.Method;
13+
import java.time.Duration;
14+
15+
public class MinutesExpression extends SimpleExpression {
16+
17+
public MinutesExpression() {
18+
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "%Number% min[ute][s]");
19+
try {
20+
handlers.put(StandardHandlers.FIND, this.getClass().getMethod("find", Object.class));
21+
} catch (NoSuchMethodException e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
26+
@Override
27+
public boolean allowAsInputFor(Type type) {
28+
return CommonTypes.DURATION.equals(type) || CommonTypes.OBJECT.equals(type) || super.allowAsInputFor(type);
29+
}
30+
31+
@Override
32+
public Pattern.Match match(String thing, Context context) {
33+
return super.match(thing, context);
34+
}
35+
36+
@Override
37+
public void compile(Context context, Pattern.Match match) {
38+
final MethodBuilder method = context.getMethod();
39+
assert method != null;
40+
final Method target = handlers.get(StandardHandlers.FIND);
41+
assert target != null;
42+
this.writeCall(method, target, context);
43+
context.setState(CompileState.STATEMENT);
44+
}
45+
46+
@ForceExtract
47+
public static Object find(Object object) {
48+
if (!(object instanceof Number)) {
49+
throw new ScriptRuntimeError("Timespan expression requires number.");
50+
} else {
51+
final Number number = (Number) object;
52+
return Duration.ofMinutes(number.longValue());
53+
}
54+
}
55+
56+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package mx.kenzie.skript.lang.syntax.timing;
2+
3+
import mx.kenzie.foundation.MethodBuilder;
4+
import mx.kenzie.foundation.Type;
5+
import mx.kenzie.skript.api.note.ForceExtract;
6+
import mx.kenzie.skript.api.syntax.SimpleExpression;
7+
import mx.kenzie.skript.compiler.*;
8+
import mx.kenzie.skript.error.ScriptRuntimeError;
9+
import mx.kenzie.skript.lang.element.StandardElements;
10+
import mx.kenzie.skript.lang.handler.StandardHandlers;
11+
12+
import java.lang.reflect.Method;
13+
import java.time.Duration;
14+
15+
public class MonthsExpression extends SimpleExpression {
16+
17+
public MonthsExpression() {
18+
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "%Number% month[s]");
19+
try {
20+
handlers.put(StandardHandlers.FIND, this.getClass().getMethod("find", Object.class));
21+
} catch (NoSuchMethodException e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
26+
@Override
27+
public boolean allowAsInputFor(Type type) {
28+
return CommonTypes.DURATION.equals(type) || CommonTypes.OBJECT.equals(type) || super.allowAsInputFor(type);
29+
}
30+
31+
@Override
32+
public Pattern.Match match(String thing, Context context) {
33+
return super.match(thing, context);
34+
}
35+
36+
@Override
37+
public void compile(Context context, Pattern.Match match) {
38+
final MethodBuilder method = context.getMethod();
39+
assert method != null;
40+
final Method target = handlers.get(StandardHandlers.FIND);
41+
assert target != null;
42+
this.writeCall(method, target, context);
43+
context.setState(CompileState.STATEMENT);
44+
}
45+
46+
@ForceExtract
47+
public static Object find(Object object) {
48+
if (!(object instanceof Number number))
49+
throw new ScriptRuntimeError("Timespan expression requires number.");
50+
return Duration.ofDays(number.longValue() * 30);
51+
}
52+
53+
}

src/main/java/mx/kenzie/skript/lang/syntax/timing/SecondsExpression.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ public SecondsExpression() {
2525

2626
@Override
2727
public boolean allowAsInputFor(Type type) {
28-
return CommonTypes.DURATION.equals(type) || CommonTypes.OBJECT.equals(type);
28+
return CommonTypes.DURATION.equals(type) || CommonTypes.OBJECT.equals(type) || super.allowAsInputFor(type);
2929
}
3030

3131
@Override
3232
public Pattern.Match match(String thing, Context context) {
33-
if (!thing.endsWith(" seconds") && !thing.endsWith(" second")) return null;
3433
return super.match(thing, context);
3534
}
3635

0 commit comments

Comments
 (0)