From 1001e4ff9ab471b79101073434eedf95bdae0e76 Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Wed, 9 Oct 2024 16:09:49 +0200
Subject: [PATCH 01/26] init commit
---
.../ch/njol/skript/expressions/ExprAngle.java | 85 +++++++++++++++++++
.../tests/syntaxes/expressions/ExprAngle.sk | 10 +++
2 files changed, 95 insertions(+)
create mode 100644 src/main/java/ch/njol/skript/expressions/ExprAngle.java
create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprAngle.sk
diff --git a/src/main/java/ch/njol/skript/expressions/ExprAngle.java b/src/main/java/ch/njol/skript/expressions/ExprAngle.java
new file mode 100644
index 00000000000..1d5be896db3
--- /dev/null
+++ b/src/main/java/ch/njol/skript/expressions/ExprAngle.java
@@ -0,0 +1,85 @@
+package ch.njol.skript.expressions;
+
+import ch.njol.skript.Skript;
+import ch.njol.skript.doc.Description;
+import ch.njol.skript.doc.Examples;
+import ch.njol.skript.doc.Name;
+import ch.njol.skript.doc.Since;
+import ch.njol.skript.lang.Expression;
+import ch.njol.skript.lang.ExpressionType;
+import ch.njol.skript.lang.SkriptParser.ParseResult;
+import ch.njol.skript.lang.util.SimpleExpression;
+import ch.njol.util.Kleenean;
+import org.bukkit.event.Event;
+import org.jetbrains.annotations.Nullable;
+
+@Name("Angle")
+@Description({
+ "Represents the passed number value in degrees.",
+ "If radians is specified, converts the passed value to degrees. This conversion may not be entirely accurate, " +
+ "due to floating point precision.",
+})
+@Examples({
+ "set {_angle} to 90 degrees",
+ "{_angle} is 90 # true",
+ "180 degrees is pi # true",
+ "pi radians is 180 degrees # true"
+})
+@Since("INSERT VERSION")
+public class ExprAngle extends SimpleExpression {
+
+ static {
+ Skript.registerExpression(ExprAngle.class, Number.class, ExpressionType.SIMPLE,
+ "%number% deg[ree][s]",
+ "%number% rad[ian][s]");
+ }
+
+ private Expression angle;
+ private boolean isRadians;
+
+ @Override
+ public boolean init(Expression>[] expressions, int matchedPattern,
+ Kleenean isDelayed, ParseResult parseResult) {
+ //noinspection unchecked
+ angle = (Expression) expressions[0];
+ isRadians = matchedPattern == 0;
+ return true;
+ }
+
+ @Override
+ protected Number @Nullable [] get(Event event) {
+ Number number = angle.getSingle(event);
+
+ if (number == null)
+ return null;
+
+ if (isRadians) {
+ return new Double[]{Math.toDegrees(number.doubleValue())};
+ }
+
+ if (number instanceof Integer integer) {
+ return new Integer[]{integer};
+ } else if (number instanceof Long lng) {
+ return new Long[]{lng};
+ } else if (number instanceof Double dbl) {
+ return new Double[]{dbl};
+ }
+
+ return new Float[]{number.floatValue()};
+ }
+
+ @Override
+ public boolean isSingle() {
+ return true;
+ }
+
+ @Override
+ public Class extends Number> getReturnType() {
+ return Number.class;
+ }
+
+ @Override
+ public String toString(@Nullable Event event, boolean debug) {
+ return angle.toString(event, debug) + (isRadians ? " degrees" : " radians");
+ }
+}
diff --git a/src/test/skript/tests/syntaxes/expressions/ExprAngle.sk b/src/test/skript/tests/syntaxes/expressions/ExprAngle.sk
new file mode 100644
index 00000000000..9ade343fa5f
--- /dev/null
+++ b/src/test/skript/tests/syntaxes/expressions/ExprAngle.sk
@@ -0,0 +1,10 @@
+test "angle":
+ assert 90 degrees is 90 with "90 degrees is not 90"
+ assert -10.5 degrees is -10.5 with "-10.5 degrees is not -10.5"
+ assert 354698306983560 degrees is 354698306983560 with "354698306983560 degrees is not 354698306983560"
+
+ assert pi radians is 180.0 degrees with "pi radians is not 180 degrees"
+ assert 2 * pi radians is 360.0 degrees with "2pi radians is not 360 degrees"
+
+ assert 180 degrees is pi radians with "180 degrees is not pi radians"
+ assert 360 degrees is 2 * pi radians with "360 degrees is not 2pi radians"
From f461b241500113372382e53be2dcc1d7ed64e15e Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Wed, 9 Oct 2024 16:12:12 +0200
Subject: [PATCH 02/26] oops
---
src/main/java/ch/njol/skript/expressions/ExprAngle.java | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/main/java/ch/njol/skript/expressions/ExprAngle.java b/src/main/java/ch/njol/skript/expressions/ExprAngle.java
index 1d5be896db3..4498a0cf53a 100644
--- a/src/main/java/ch/njol/skript/expressions/ExprAngle.java
+++ b/src/main/java/ch/njol/skript/expressions/ExprAngle.java
@@ -42,7 +42,7 @@ public boolean init(Expression>[] expressions, int matchedPattern,
Kleenean isDelayed, ParseResult parseResult) {
//noinspection unchecked
angle = (Expression) expressions[0];
- isRadians = matchedPattern == 0;
+ isRadians = matchedPattern == 1;
return true;
}
@@ -82,4 +82,5 @@ public Class extends Number> getReturnType() {
public String toString(@Nullable Event event, boolean debug) {
return angle.toString(event, debug) + (isRadians ? " degrees" : " radians");
}
+
}
From 99986163897ed5cbe302964be28d9281e820e6fb Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Wed, 9 Oct 2024 16:13:12 +0200
Subject: [PATCH 03/26] removed unnecessary checks
---
.../java/ch/njol/skript/expressions/ExprAngle.java | 10 +---------
1 file changed, 1 insertion(+), 9 deletions(-)
diff --git a/src/main/java/ch/njol/skript/expressions/ExprAngle.java b/src/main/java/ch/njol/skript/expressions/ExprAngle.java
index 4498a0cf53a..fb7043fbdd9 100644
--- a/src/main/java/ch/njol/skript/expressions/ExprAngle.java
+++ b/src/main/java/ch/njol/skript/expressions/ExprAngle.java
@@ -57,15 +57,7 @@ public boolean init(Expression>[] expressions, int matchedPattern,
return new Double[]{Math.toDegrees(number.doubleValue())};
}
- if (number instanceof Integer integer) {
- return new Integer[]{integer};
- } else if (number instanceof Long lng) {
- return new Long[]{lng};
- } else if (number instanceof Double dbl) {
- return new Double[]{dbl};
- }
-
- return new Float[]{number.floatValue()};
+ return new Number[]{number};
}
@Override
From 52d028a27b264cd9048bfad3fe65b26a93bf3337 Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Wed, 9 Oct 2024 16:20:50 +0200
Subject: [PATCH 04/26] update other syntaxes
---
.../ch/njol/skript/effects/EffVectorRotateAroundAnother.java | 2 +-
src/main/java/ch/njol/skript/effects/EffVectorRotateXYZ.java | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/main/java/ch/njol/skript/effects/EffVectorRotateAroundAnother.java b/src/main/java/ch/njol/skript/effects/EffVectorRotateAroundAnother.java
index 2ffddda71d9..c6354ec3190 100644
--- a/src/main/java/ch/njol/skript/effects/EffVectorRotateAroundAnother.java
+++ b/src/main/java/ch/njol/skript/effects/EffVectorRotateAroundAnother.java
@@ -40,7 +40,7 @@
public class EffVectorRotateAroundAnother extends Effect {
static {
- Skript.registerEffect(EffVectorRotateAroundAnother.class, "rotate %vectors% around %vector% by %number% [degrees]");
+ Skript.registerEffect(EffVectorRotateAroundAnother.class, "rotate %vectors% around %vector% by %number%");
}
@SuppressWarnings("null")
diff --git a/src/main/java/ch/njol/skript/effects/EffVectorRotateXYZ.java b/src/main/java/ch/njol/skript/effects/EffVectorRotateXYZ.java
index a24ad0dcd51..127d0550913 100644
--- a/src/main/java/ch/njol/skript/effects/EffVectorRotateXYZ.java
+++ b/src/main/java/ch/njol/skript/effects/EffVectorRotateXYZ.java
@@ -44,7 +44,7 @@
public class EffVectorRotateXYZ extends Effect {
static {
- Skript.registerEffect(EffVectorRotateXYZ.class, "rotate %vectors% around (0¦x|1¦y|2¦z)(-| )axis by %number% [degrees]");
+ Skript.registerEffect(EffVectorRotateXYZ.class, "rotate %vectors% around (0¦x|1¦y|2¦z)(-| )axis by %number%");
}
private final static Character[] axes = new Character[] {'x', 'y', 'z'};
From b4a1f376a58bb24086d8bd9892e689c19221d102 Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Wed, 9 Oct 2024 19:01:11 +0200
Subject: [PATCH 05/26] move to parser
---
.../njol/skript/classes/data/JavaClasses.java | 319 ++++++++++--------
.../ch/njol/skript/expressions/ExprAngle.java | 78 -----
.../ExprAngle.sk => misc/angle.sk} | 4 +-
3 files changed, 181 insertions(+), 220 deletions(-)
delete mode 100644 src/main/java/ch/njol/skript/expressions/ExprAngle.java
rename src/test/skript/tests/{syntaxes/expressions/ExprAngle.sk => misc/angle.sk} (70%)
diff --git a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
index 660c1cc04ba..26b0eb630a3 100644
--- a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
+++ b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
@@ -1,21 +1,3 @@
-/**
- * This file is part of Skript.
- *
- * Skript is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Skript is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Skript. If not, see .
- *
- * Copyright Peter Güttinger, SkriptLang team and contributors
- */
package ch.njol.skript.classes.data;
import ch.njol.skript.SkriptConfig;
@@ -33,13 +15,17 @@
import ch.njol.yggdrasil.Fields;
import org.jetbrains.annotations.Nullable;
+import java.util.Map;
+import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaClasses {
public static final int VARIABLENAME_NUMBERACCURACY = 8;
- public static final Pattern INTEGER_PATTERN = Pattern.compile("-?[0-9]+");
- public static final Pattern NUMBER_PATTERN = Pattern.compile("-?[0-9]+(?>\\.[0-9]+)?%?");
+ public static final Pattern INTEGER_PATTERN =
+ Pattern.compile("(?-?[0-9]+) ((in )?(?rad(ian)?s?)|deg(ree)?s?)");
+ public static final Pattern NUMBER_PATTERN =
+ Pattern.compile("(?-?[0-9]+(?>\\.[0-9]+)?%?) ((in )?(?rad(ian)?s?)|deg(ree)?s?)");
static {
Classes.registerClass(new ClassInfo<>(Object.class, "object")
@@ -49,83 +35,27 @@ public class JavaClasses {
.usage("")
.examples("")
.since("1.0"));
-
+
Classes.registerClass(new ClassInfo<>(Number.class, "number")
.user("num(ber)?s?")
.name("Number")
- .description("A number, e.g. 2.5, 3, or -9812454.",
- "Please note that many expressions only need integers, i.e. will discard any fractional parts of any numbers without producing an error.")
+ .description(
+ "A number, e.g. 2.5, 3, -9812454, 30 degrees or 3.14 radians.",
+ "Please note that many expressions only need integers, i.e. " +
+ "will discard any fractional parts of any numbers without producing an error.",
+ "Radians will be converted to degrees.")
.usage("[-]###[.###] (any amount of digits; very large numbers will be truncated though)")
- .examples("set the player's health to 5.5",
- "set {_temp} to 2*{_temp} - 2.5")
+ .examples(
+ "set the player's health to 5.5",
+ "set {_temp} to 2*{_temp} - 2.5",
+ "set {_angle} to 3.14 in radians # will be converted to degrees"
+ )
.since("1.0")
// is registered after all other number classes
.defaultExpression(new SimpleLiteral<>(1, true))
- .parser(new Parser() {
- @Override
- @Nullable
- public Number parse(String s, ParseContext context) {
- if (!NUMBER_PATTERN.matcher(s).matches())
- return null;
- if (INTEGER_PATTERN.matcher(s).matches()) {
- try {
- return Long.valueOf(s);
- } catch (NumberFormatException ignored) { }
- }
- try {
- Double d = s.endsWith("%") ? Double.parseDouble(s.substring(0, s.length() - 1)) / 100 : Double.parseDouble(s);
- if (d.isNaN() || d.isInfinite())
- return null;
- return d;
- } catch (NumberFormatException e) {
- return null;
- }
- }
-
- @Override
- public String toString(Number n, int flags) {
- return StringUtils.toString(n.doubleValue(), SkriptConfig.numberAccuracy.value());
- }
-
- @Override
- public String toVariableNameString(Number n) {
- return StringUtils.toString(n.doubleValue(), VARIABLENAME_NUMBERACCURACY);
- }
- }).serializer(new Serializer() {
- @Override
- public Fields serialize(Number n) {
- throw new IllegalStateException(); // serialised natively by Yggdrasil
- }
-
- @Override
- public boolean canBeInstantiated() {
- return true;
- }
-
- @Override
- public void deserialize(Number o, Fields f) {
- assert false;
- }
+ .parser(new NumberParser())
+ .serializer(new NumberSerializer()));
- @Override
- @Nullable
- public Number deserialize(String s) {
- try {
- return Integer.valueOf(s);
- } catch (NumberFormatException ignored) {}
- try {
- return Double.valueOf(s);
- } catch (NumberFormatException e) {
- return null;
- }
- }
-
- @Override
- public boolean mustSyncDeserialization() {
- return false;
- }
- }));
-
Classes.registerClass(new ClassInfo<>(Long.class, "long")
.user("int(eger)?s?")
.name(ClassInfo.NO_DOC)
@@ -143,12 +73,12 @@ public Long parse(String s, ParseContext context) {
return null;
}
}
-
+
@Override
public String toString(Long l, int flags) {
return "" + l;
}
-
+
@Override
public String toVariableNameString(Long l) {
return "" + l;
@@ -158,12 +88,12 @@ public String toVariableNameString(Long l) {
public Fields serialize(Long n) {
throw new IllegalStateException(); // serialised natively by Yggdrasil
}
-
+
@Override
public boolean canBeInstantiated() {
return true;
}
-
+
@Override
public void deserialize(Long o, Fields f) {
assert false;
@@ -178,13 +108,13 @@ public Long deserialize(String s) {
return null;
}
}
-
+
@Override
public boolean mustSyncDeserialization() {
return false;
}
}));
-
+
Classes.registerClass(new ClassInfo<>(Integer.class, "integer")
.name(ClassInfo.NO_DOC)
.defaultExpression(new SimpleLiteral<>(1, true))
@@ -200,12 +130,12 @@ public Integer parse(String s, ParseContext context) {
return null;
}
}
-
+
@Override
public String toString(Integer i, int flags) {
return "" + i;
}
-
+
@Override
public String toVariableNameString(Integer i) {
return "" + i;
@@ -215,12 +145,12 @@ public String toVariableNameString(Integer i) {
public Fields serialize(Integer n) {
throw new IllegalStateException(); // serialised natively by Yggdrasil
}
-
+
@Override
public boolean canBeInstantiated() {
return true;
}
-
+
@Override
public void deserialize(Integer o, Fields f) {
assert false;
@@ -235,13 +165,13 @@ public Integer deserialize(String s) {
return null;
}
}
-
+
@Override
public boolean mustSyncDeserialization() {
return false;
}
}));
-
+
Classes.registerClass(new ClassInfo<>(Double.class, "double")
.name(ClassInfo.NO_DOC)
.defaultExpression(new SimpleLiteral<>(1., true))
@@ -262,12 +192,12 @@ public Double parse(String s, ParseContext context) {
return null;
}
}
-
+
@Override
public String toString(Double d, int flags) {
return StringUtils.toString(d, SkriptConfig.numberAccuracy.value());
}
-
+
@Override
public String toVariableNameString(Double d) {
return StringUtils.toString(d, VARIABLENAME_NUMBERACCURACY);
@@ -277,12 +207,12 @@ public String toVariableNameString(Double d) {
public Fields serialize(Double n) {
throw new IllegalStateException(); // serialised natively by Yggdrasil
}
-
+
@Override
public boolean canBeInstantiated() {
return true;
}
-
+
@Override
public void deserialize(Double o, Fields f) {
assert false;
@@ -297,13 +227,13 @@ public Double deserialize(String s) {
return null;
}
}
-
+
@Override
public boolean mustSyncDeserialization() {
return false;
}
}));
-
+
Classes.registerClass(new ClassInfo<>(Float.class, "float")
.name(ClassInfo.NO_DOC)
.defaultExpression(new SimpleLiteral<>(1f, true))
@@ -323,12 +253,12 @@ public Float parse(String s, ParseContext context) {
return null;
}
}
-
+
@Override
public String toString(Float f, int flags) {
return StringUtils.toString(f, SkriptConfig.numberAccuracy.value());
}
-
+
@Override
public String toVariableNameString(Float f) {
return StringUtils.toString(f.doubleValue(), VARIABLENAME_NUMBERACCURACY);
@@ -338,12 +268,12 @@ public String toVariableNameString(Float f) {
public Fields serialize(Float n) {
throw new IllegalStateException(); // serialised natively by Yggdrasil
}
-
+
@Override
public boolean canBeInstantiated() {
return true;
}
-
+
@Override
public void deserialize(Float o, Fields f) {
assert false;
@@ -358,13 +288,13 @@ public Float deserialize(String s) {
return null;
}
}
-
+
@Override
public boolean mustSyncDeserialization() {
return false;
}
}));
-
+
Classes.registerClass(new ClassInfo<>(Boolean.class, "boolean")
.user("booleans?")
.name("Boolean")
@@ -375,7 +305,7 @@ public boolean mustSyncDeserialization() {
.parser(new Parser() {
private final RegexMessage truePattern = new RegexMessage("boolean.true.pattern");
private final RegexMessage falsePattern = new RegexMessage("boolean.false.pattern");
-
+
@Override
@Nullable
public Boolean parse(String s, ParseContext context) {
@@ -385,15 +315,15 @@ public Boolean parse(String s, ParseContext context) {
return Boolean.FALSE;
return null;
}
-
+
private final Message trueName = new Message("boolean.true.name");
private final Message falseName = new Message("boolean.false.name");
-
+
@Override
public String toString(Boolean b, int flags) {
return b ? trueName.toString() : falseName.toString();
}
-
+
@Override
public String toVariableNameString(Boolean b) {
return "" + b;
@@ -403,12 +333,12 @@ public String toVariableNameString(Boolean b) {
public Fields serialize(Boolean n) {
throw new IllegalStateException(); // serialised natively by Yggdrasil
}
-
+
@Override
public boolean canBeInstantiated() {
return true;
}
-
+
@Override
public void deserialize(Boolean o, Fields f) {
assert false;
@@ -423,13 +353,13 @@ public Boolean deserialize(String s) {
return Boolean.FALSE;
return null;
}
-
+
@Override
public boolean mustSyncDeserialization() {
return false;
}
}));
-
+
Classes.registerClass(new ClassInfo<>(Short.class, "short")
.name(ClassInfo.NO_DOC)
.defaultExpression(new SimpleLiteral<>((short) 1, true))
@@ -445,12 +375,12 @@ public Short parse(String s, ParseContext context) {
return null;
}
}
-
+
@Override
public String toString(Short s, int flags) {
return "" + s;
}
-
+
@Override
public String toVariableNameString(Short s) {
return "" + s;
@@ -460,12 +390,12 @@ public String toVariableNameString(Short s) {
public Fields serialize(Short n) {
throw new IllegalStateException(); // serialised natively by Yggdrasil
}
-
+
@Override
public boolean canBeInstantiated() {
return true;
}
-
+
@Override
public void deserialize(Short o, Fields f) {
assert false;
@@ -480,13 +410,13 @@ public Short deserialize(String s) {
return null;
}
}
-
+
@Override
public boolean mustSyncDeserialization() {
return false;
}
}));
-
+
Classes.registerClass(new ClassInfo<>(Byte.class, "byte")
.name(ClassInfo.NO_DOC)
.defaultExpression(new SimpleLiteral<>((byte) 1, true))
@@ -502,12 +432,12 @@ public Byte parse(String s, ParseContext context) {
return null;
}
}
-
+
@Override
public String toString(Byte b, int flags) {
return "" + b;
}
-
+
@Override
public String toVariableNameString(Byte b) {
return "" + b;
@@ -517,12 +447,12 @@ public String toVariableNameString(Byte b) {
public Fields serialize(Byte n) {
throw new IllegalStateException(); // serialised natively by Yggdrasil
}
-
+
@Override
public boolean canBeInstantiated() {
return true;
}
-
+
@Override
public void deserialize(Byte o, Fields f) {
assert false;
@@ -537,13 +467,13 @@ public Byte deserialize(String s) {
return null;
}
}
-
+
@Override
public boolean mustSyncDeserialization() {
return false;
}
}));
-
+
Classes.registerClass(new ClassInfo<>(String.class, "string")
.user("(text|string)s?")
.name("Text")
@@ -581,22 +511,22 @@ public String parse(String s, ParseContext context) {
assert false;
return null;
}
-
+
@Override
public boolean canParse(ParseContext context) {
return context != ParseContext.DEFAULT;
}
-
+
@Override
public String toString(String s, int flags) {
return s;
}
-
+
@Override
public String getDebugMessage(String s) {
return '"' + s + '"';
}
-
+
@Override
public String toVariableNameString(String s) {
return s;
@@ -606,12 +536,12 @@ public String toVariableNameString(String s) {
public Fields serialize(String n) {
throw new IllegalStateException(); // serialised natively by Yggdrasil
}
-
+
@Override
public boolean canBeInstantiated() {
return true;
}
-
+
@Override
public void deserialize(String o, Fields f) {
assert false;
@@ -621,11 +551,120 @@ public void deserialize(String o, Fields f) {
public String deserialize(String s) {
return s;
}
-
+
@Override
public boolean mustSyncDeserialization() {
return false;
}
}));
}
+
+ private static class NumberParser extends Parser {
+
+ @Override
+ public @Nullable Number parse(String string, ParseContext context) {
+ Matcher numberMatcher = NUMBER_PATTERN.matcher(string);
+ if (!numberMatcher.matches())
+ return null;
+
+ Matcher intMatcher = INTEGER_PATTERN.matcher(string);
+ if (intMatcher.matches()) {
+ Number integer = convertInteger(intMatcher);
+ if (integer != null)
+ return integer;
+ }
+
+ return convertDouble(numberMatcher);
+ }
+
+ private @Nullable Number convertInteger(Matcher matcher) {
+ Map namedGroups = matcher.namedGroups();
+ String number = matcher.group("num");
+ if (namedGroups.containsKey("rad")) {
+ try {
+ return Math.toDegrees(Double.parseDouble(number));
+ } catch (NumberFormatException ignored) {
+ }
+ } else {
+ try {
+ return Long.valueOf(number);
+ } catch (NumberFormatException ignored) {
+ }
+ }
+
+ return null;
+ }
+
+ private @Nullable Double convertDouble(Matcher matcher) {
+ Map namedGroups = matcher.namedGroups();
+ String number = matcher.group("num");
+
+ try {
+ double d = number.endsWith("%") ?
+ Double.parseDouble(number.substring(0, number.length() - 1)) / 100 :
+ Double.parseDouble(number);
+
+ if (Double.isNaN(d))
+ return null;
+
+ if (namedGroups.containsKey("rad")) {
+ try {
+ d = Math.toDegrees(Double.parseDouble(number));
+ } catch (NumberFormatException ignored) {
+ }
+ }
+
+ return d;
+ } catch (NumberFormatException e) {
+ return null;
+ }
+ }
+
+ @Override
+ public String toString(Number number, int flags) {
+ return StringUtils.toString(number.doubleValue(), SkriptConfig.numberAccuracy.value());
+ }
+
+ @Override
+ public String toVariableNameString(Number number) {
+ return StringUtils.toString(number.doubleValue(), VARIABLENAME_NUMBERACCURACY);
+ }
+ }
+
+ private static class NumberSerializer extends Serializer {
+
+ @Override
+ public Fields serialize(Number number) {
+ throw new IllegalStateException(); // serialised natively by Yggdrasil
+ }
+
+ @Override
+ public boolean canBeInstantiated() {
+ return true;
+ }
+
+ @Override
+ public void deserialize(Number number, Fields fields) {
+ assert false;
+ }
+
+ @Override
+ public @Nullable Number deserialize(String string) {
+ try {
+ return Integer.valueOf(string);
+ } catch (NumberFormatException ignored) {
+ }
+ try {
+ return Double.valueOf(string);
+ } catch (NumberFormatException e) {
+ return null;
+ }
+ }
+
+ @Override
+ public boolean mustSyncDeserialization() {
+ return false;
+ }
+ }
+
}
diff --git a/src/main/java/ch/njol/skript/expressions/ExprAngle.java b/src/main/java/ch/njol/skript/expressions/ExprAngle.java
deleted file mode 100644
index fb7043fbdd9..00000000000
--- a/src/main/java/ch/njol/skript/expressions/ExprAngle.java
+++ /dev/null
@@ -1,78 +0,0 @@
-package ch.njol.skript.expressions;
-
-import ch.njol.skript.Skript;
-import ch.njol.skript.doc.Description;
-import ch.njol.skript.doc.Examples;
-import ch.njol.skript.doc.Name;
-import ch.njol.skript.doc.Since;
-import ch.njol.skript.lang.Expression;
-import ch.njol.skript.lang.ExpressionType;
-import ch.njol.skript.lang.SkriptParser.ParseResult;
-import ch.njol.skript.lang.util.SimpleExpression;
-import ch.njol.util.Kleenean;
-import org.bukkit.event.Event;
-import org.jetbrains.annotations.Nullable;
-
-@Name("Angle")
-@Description({
- "Represents the passed number value in degrees.",
- "If radians is specified, converts the passed value to degrees. This conversion may not be entirely accurate, " +
- "due to floating point precision.",
-})
-@Examples({
- "set {_angle} to 90 degrees",
- "{_angle} is 90 # true",
- "180 degrees is pi # true",
- "pi radians is 180 degrees # true"
-})
-@Since("INSERT VERSION")
-public class ExprAngle extends SimpleExpression {
-
- static {
- Skript.registerExpression(ExprAngle.class, Number.class, ExpressionType.SIMPLE,
- "%number% deg[ree][s]",
- "%number% rad[ian][s]");
- }
-
- private Expression angle;
- private boolean isRadians;
-
- @Override
- public boolean init(Expression>[] expressions, int matchedPattern,
- Kleenean isDelayed, ParseResult parseResult) {
- //noinspection unchecked
- angle = (Expression) expressions[0];
- isRadians = matchedPattern == 1;
- return true;
- }
-
- @Override
- protected Number @Nullable [] get(Event event) {
- Number number = angle.getSingle(event);
-
- if (number == null)
- return null;
-
- if (isRadians) {
- return new Double[]{Math.toDegrees(number.doubleValue())};
- }
-
- return new Number[]{number};
- }
-
- @Override
- public boolean isSingle() {
- return true;
- }
-
- @Override
- public Class extends Number> getReturnType() {
- return Number.class;
- }
-
- @Override
- public String toString(@Nullable Event event, boolean debug) {
- return angle.toString(event, debug) + (isRadians ? " degrees" : " radians");
- }
-
-}
diff --git a/src/test/skript/tests/syntaxes/expressions/ExprAngle.sk b/src/test/skript/tests/misc/angle.sk
similarity index 70%
rename from src/test/skript/tests/syntaxes/expressions/ExprAngle.sk
rename to src/test/skript/tests/misc/angle.sk
index 9ade343fa5f..1e5cc32aceb 100644
--- a/src/test/skript/tests/syntaxes/expressions/ExprAngle.sk
+++ b/src/test/skript/tests/misc/angle.sk
@@ -4,7 +4,7 @@ test "angle":
assert 354698306983560 degrees is 354698306983560 with "354698306983560 degrees is not 354698306983560"
assert pi radians is 180.0 degrees with "pi radians is not 180 degrees"
- assert 2 * pi radians is 360.0 degrees with "2pi radians is not 360 degrees"
+ assert 2 * pi in radians is 360.0 degrees with "2pi radians is not 360 degrees"
assert 180 degrees is pi radians with "180 degrees is not pi radians"
- assert 360 degrees is 2 * pi radians with "360 degrees is not 2pi radians"
+ assert 360 in degrees is 2 * pi radians with "360 degrees is not 2pi radians"
From cef8b87b9f2c8f8f4adc90c741ab005e8162f24c Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Wed, 9 Oct 2024 19:03:00 +0200
Subject: [PATCH 06/26] oops
---
src/main/java/ch/njol/skript/classes/data/JavaClasses.java | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
index 26b0eb630a3..dae65d83acf 100644
--- a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
+++ b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
@@ -23,9 +23,9 @@ public class JavaClasses {
public static final int VARIABLENAME_NUMBERACCURACY = 8;
public static final Pattern INTEGER_PATTERN =
- Pattern.compile("(?-?[0-9]+) ((in )?(?rad(ian)?s?)|deg(ree)?s?)");
+ Pattern.compile("(?-?[0-9]+)( (in )?(?rad(ian)?s?)|deg(ree)?s?)?");
public static final Pattern NUMBER_PATTERN =
- Pattern.compile("(?-?[0-9]+(?>\\.[0-9]+)?%?) ((in )?(?rad(ian)?s?)|deg(ree)?s?)");
+ Pattern.compile("(?-?[0-9]+(?>\\.[0-9]+)?%?)( (in )?(?rad(ian)?s?)|deg(ree)?s?)?");
static {
Classes.registerClass(new ClassInfo<>(Object.class, "object")
From c5d5c2547d8463bed06825f274566a8a791c5f6e Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Thu, 10 Oct 2024 18:55:24 +0200
Subject: [PATCH 07/26] big work
---
.../njol/skript/classes/data/JavaClasses.java | 783 ++++++++++--------
1 file changed, 415 insertions(+), 368 deletions(-)
diff --git a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
index dae65d83acf..e6014c81bc8 100644
--- a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
+++ b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
@@ -13,9 +13,11 @@
import ch.njol.skript.util.Utils;
import ch.njol.util.StringUtils;
import ch.njol.yggdrasil.Fields;
+import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
+import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -61,239 +63,28 @@ public class JavaClasses {
.name(ClassInfo.NO_DOC)
.before("integer", "short", "byte")
.defaultExpression(new SimpleLiteral<>((long) 1, true))
- .parser(new Parser() {
- @Override
- @Nullable
- public Long parse(String s, ParseContext context) {
- if (!INTEGER_PATTERN.matcher(s).matches())
- return null;
- try {
- return Long.valueOf(s);
- } catch (NumberFormatException e) {
- return null;
- }
- }
-
- @Override
- public String toString(Long l, int flags) {
- return "" + l;
- }
-
- @Override
- public String toVariableNameString(Long l) {
- return "" + l;
- }
- }).serializer(new Serializer() {
- @Override
- public Fields serialize(Long n) {
- throw new IllegalStateException(); // serialised natively by Yggdrasil
- }
-
- @Override
- public boolean canBeInstantiated() {
- return true;
- }
-
- @Override
- public void deserialize(Long o, Fields f) {
- assert false;
- }
-
- @Override
- @Nullable
- public Long deserialize(String s) {
- try {
- return Long.parseLong(s);
- } catch (NumberFormatException e) {
- return null;
- }
- }
-
- @Override
- public boolean mustSyncDeserialization() {
- return false;
- }
- }));
+ .parser(new LongParser())
+ .serializer(new LongSerializer()));
Classes.registerClass(new ClassInfo<>(Integer.class, "integer")
.name(ClassInfo.NO_DOC)
.defaultExpression(new SimpleLiteral<>(1, true))
- .parser(new Parser() {
- @Override
- @Nullable
- public Integer parse(String s, ParseContext context) {
- if (!INTEGER_PATTERN.matcher(s).matches())
- return null;
- try {
- return Integer.valueOf(s);
- } catch (NumberFormatException e) {
- return null;
- }
- }
-
- @Override
- public String toString(Integer i, int flags) {
- return "" + i;
- }
-
- @Override
- public String toVariableNameString(Integer i) {
- return "" + i;
- }
- }).serializer(new Serializer() {
- @Override
- public Fields serialize(Integer n) {
- throw new IllegalStateException(); // serialised natively by Yggdrasil
- }
-
- @Override
- public boolean canBeInstantiated() {
- return true;
- }
-
- @Override
- public void deserialize(Integer o, Fields f) {
- assert false;
- }
-
- @Override
- @Nullable
- public Integer deserialize(String s) {
- try {
- return Integer.parseInt(s);
- } catch (NumberFormatException e) {
- return null;
- }
- }
-
- @Override
- public boolean mustSyncDeserialization() {
- return false;
- }
- }));
+ .parser(new IntegerParser())
+ .serializer(new IntegerSerializer()));
Classes.registerClass(new ClassInfo<>(Double.class, "double")
.name(ClassInfo.NO_DOC)
.defaultExpression(new SimpleLiteral<>(1., true))
.after("long")
.before("float", "integer", "short", "byte")
- .parser(new Parser() {
- @Override
- @Nullable
- public Double parse(String s, ParseContext context) {
- if (!NUMBER_PATTERN.matcher(s).matches())
- return null;
- try {
- Double d = s.endsWith("%") ? Double.parseDouble(s.substring(0, s.length() - 1)) / 100 : Double.parseDouble(s);
- if (d.isNaN() || d.isInfinite())
- return null;
- return d;
- } catch (NumberFormatException e) {
- return null;
- }
- }
-
- @Override
- public String toString(Double d, int flags) {
- return StringUtils.toString(d, SkriptConfig.numberAccuracy.value());
- }
-
- @Override
- public String toVariableNameString(Double d) {
- return StringUtils.toString(d, VARIABLENAME_NUMBERACCURACY);
- }
- }).serializer(new Serializer() {
- @Override
- public Fields serialize(Double n) {
- throw new IllegalStateException(); // serialised natively by Yggdrasil
- }
-
- @Override
- public boolean canBeInstantiated() {
- return true;
- }
-
- @Override
- public void deserialize(Double o, Fields f) {
- assert false;
- }
-
- @Override
- @Nullable
- public Double deserialize(String s) {
- try {
- return Double.parseDouble(s);
- } catch (NumberFormatException e) {
- return null;
- }
- }
-
- @Override
- public boolean mustSyncDeserialization() {
- return false;
- }
- }));
+ .parser(new DoubleParser())
+ .serializer(new DoubleSerializer()));
Classes.registerClass(new ClassInfo<>(Float.class, "float")
.name(ClassInfo.NO_DOC)
.defaultExpression(new SimpleLiteral<>(1f, true))
- .parser(new Parser() {
- @Override
- @Nullable
- public Float parse(String s, ParseContext context) {
- if (!NUMBER_PATTERN.matcher(s).matches())
- return null;
- try {
- Float f = s.endsWith("%") ? Float.parseFloat(s.substring(0, s.length() - 1)) / 100 : Float.parseFloat(s);
- if (f.isNaN() || f.isInfinite()) {
- return null;
- }
- return f;
- } catch (NumberFormatException e) {
- return null;
- }
- }
-
- @Override
- public String toString(Float f, int flags) {
- return StringUtils.toString(f, SkriptConfig.numberAccuracy.value());
- }
-
- @Override
- public String toVariableNameString(Float f) {
- return StringUtils.toString(f.doubleValue(), VARIABLENAME_NUMBERACCURACY);
- }
- }).serializer(new Serializer() {
- @Override
- public Fields serialize(Float n) {
- throw new IllegalStateException(); // serialised natively by Yggdrasil
- }
-
- @Override
- public boolean canBeInstantiated() {
- return true;
- }
-
- @Override
- public void deserialize(Float o, Fields f) {
- assert false;
- }
-
- @Override
- @Nullable
- public Float deserialize(String s) {
- try {
- return Float.parseFloat(s);
- } catch (NumberFormatException e) {
- return null;
- }
- }
-
- @Override
- public boolean mustSyncDeserialization() {
- return false;
- }
- }));
+ .parser(new FloatParser())
+ .serializer(new FloatSerializer()));
Classes.registerClass(new ClassInfo<>(Boolean.class, "boolean")
.user("booleans?")
@@ -363,116 +154,14 @@ public boolean mustSyncDeserialization() {
Classes.registerClass(new ClassInfo<>(Short.class, "short")
.name(ClassInfo.NO_DOC)
.defaultExpression(new SimpleLiteral<>((short) 1, true))
- .parser(new Parser() {
- @Override
- @Nullable
- public Short parse(String s, ParseContext context) {
- if (!INTEGER_PATTERN.matcher(s).matches())
- return null;
- try {
- return Short.valueOf(s);
- } catch (NumberFormatException e) {
- return null;
- }
- }
-
- @Override
- public String toString(Short s, int flags) {
- return "" + s;
- }
-
- @Override
- public String toVariableNameString(Short s) {
- return "" + s;
- }
- }).serializer(new Serializer() {
- @Override
- public Fields serialize(Short n) {
- throw new IllegalStateException(); // serialised natively by Yggdrasil
- }
-
- @Override
- public boolean canBeInstantiated() {
- return true;
- }
-
- @Override
- public void deserialize(Short o, Fields f) {
- assert false;
- }
-
- @Override
- @Nullable
- public Short deserialize(String s) {
- try {
- return Short.parseShort(s);
- } catch (NumberFormatException e) {
- return null;
- }
- }
-
- @Override
- public boolean mustSyncDeserialization() {
- return false;
- }
- }));
+ .parser(new ShortParser())
+ .serializer(new ShortSerializer()));
Classes.registerClass(new ClassInfo<>(Byte.class, "byte")
.name(ClassInfo.NO_DOC)
.defaultExpression(new SimpleLiteral<>((byte) 1, true))
- .parser(new Parser() {
- @Override
- @Nullable
- public Byte parse(String s, ParseContext context) {
- if (!INTEGER_PATTERN.matcher(s).matches())
- return null;
- try {
- return Byte.valueOf(s);
- } catch (NumberFormatException e) {
- return null;
- }
- }
-
- @Override
- public String toString(Byte b, int flags) {
- return "" + b;
- }
-
- @Override
- public String toVariableNameString(Byte b) {
- return "" + b;
- }
- }).serializer(new Serializer() {
- @Override
- public Fields serialize(Byte n) {
- throw new IllegalStateException(); // serialised natively by Yggdrasil
- }
-
- @Override
- public boolean canBeInstantiated() {
- return true;
- }
-
- @Override
- public void deserialize(Byte o, Fields f) {
- assert false;
- }
-
- @Override
- @Nullable
- public Byte deserialize(String s) {
- try {
- return Byte.parseByte(s);
- } catch (NumberFormatException e) {
- return null;
- }
- }
-
- @Override
- public boolean mustSyncDeserialization() {
- return false;
- }
- }));
+ .parser(new ByteParser())
+ .serializer(new ByteSerializer()));
Classes.registerClass(new ClassInfo<>(String.class, "string")
.user("(text|string)s?")
@@ -559,65 +248,117 @@ public boolean mustSyncDeserialization() {
}));
}
- private static class NumberParser extends Parser {
-
- @Override
- public @Nullable Number parse(String string, ParseContext context) {
- Matcher numberMatcher = NUMBER_PATTERN.matcher(string);
- if (!numberMatcher.matches())
- return null;
+ /**
+ * Converts a string to a number formatted as an integer.
+ *
+ * Applies {@code stringToNumber} for parsing the number, then tries to
+ * convert radians to degrees if the string contains a radian group.
+ * If the string could be parsed, applies {@code converter} to convert
+ * the number to the desired type.
+ *
+ *
+ * @param string The string with the possible number.
+ * @param stringToNumber The function to parse the number, e.g. {@link Integer#parseInt(String)}.
+ * @param converter The function to convert the number to the desired type, e.g. {@link Number#intValue()}.
+ * @return The parsed string, or null if the string could not be parsed.
+ */
+ @Contract(pure = true)
+ private static @Nullable T convertIntegerFormatted(
+ String string,
+ Function stringToNumber,
+ Function converter
+ ) {
+ Matcher matcher = INTEGER_PATTERN.matcher(string);
+
+ if (!matcher.matches())
+ return null;
- Matcher intMatcher = INTEGER_PATTERN.matcher(string);
- if (intMatcher.matches()) {
- Number integer = convertInteger(intMatcher);
- if (integer != null)
- return integer;
+ Map namedGroups = matcher.namedGroups();
+ String number = matcher.group("num");
+ if (namedGroups.containsKey("rad")) {
+ try {
+ return converter.apply(Math.toDegrees(stringToNumber.apply(number).doubleValue()));
+ } catch (NumberFormatException ignored) {
+ }
+ } else {
+ try {
+ return converter.apply(stringToNumber.apply(number));
+ } catch (NumberFormatException ignored) {
}
-
- return convertDouble(numberMatcher);
}
- private @Nullable Number convertInteger(Matcher matcher) {
- Map namedGroups = matcher.namedGroups();
- String number = matcher.group("num");
+ return null;
+ }
+
+ /**
+ * Converts a string to a number formatted as an decimal.
+ *
+ * Applies {@code stringToNumber} for parsing the number.
+ * If the number is a percentage, it gets parsed as a double between 0-1.
+ * Then tries to convert radians to degrees if the string contains a radian group.
+ * If the string could be parsed, applies {@code converter} to convert the number to the desired type.
+ *
+ *
+ * @param string The string with the possible number.
+ * @param stringToNumber The function to parse the number, e.g. {@link Integer#parseInt(String)}.
+ * @param converter The function to convert the number to the desired type, e.g. {@link Number#intValue()}.
+ * @param isValid The function to check if the number is valid, e.g. {@link Double#isNaN()}.
+ * @return The parsed string, or null if the string could not be parsed.
+ */
+ @Contract(pure = true)
+ private static @Nullable T convertDecimalFormatted(
+ String string,
+ Function stringToNumber,
+ Function converter,
+ Function isValid
+ ) {
+ Matcher matcher = NUMBER_PATTERN.matcher(string);
+
+ if (!matcher.matches())
+ return null;
+
+ Map namedGroups = matcher.namedGroups();
+ String number = matcher.group("num");
+
+ try {
+ double d = number.endsWith("%") ?
+ stringToNumber.apply(number.substring(0, number.length() - 1)).doubleValue() / 100.0 :
+ stringToNumber.apply(number).doubleValue();
+
+ T result = converter.apply(d);
+
+ if (isValid.apply(result))
+ return null;
+
if (namedGroups.containsKey("rad")) {
try {
- return Math.toDegrees(Double.parseDouble(number));
- } catch (NumberFormatException ignored) {
- }
- } else {
- try {
- return Long.valueOf(number);
+ return converter.apply(Math.toDegrees(d));
} catch (NumberFormatException ignored) {
}
}
+ return result;
+ } catch (NumberFormatException ex) {
return null;
}
+ }
- private @Nullable Double convertDouble(Matcher matcher) {
- Map namedGroups = matcher.namedGroups();
- String number = matcher.group("num");
-
- try {
- double d = number.endsWith("%") ?
- Double.parseDouble(number.substring(0, number.length() - 1)) / 100 :
- Double.parseDouble(number);
+ private static class NumberParser extends Parser {
- if (Double.isNaN(d))
- return null;
+ @Override
+ public @Nullable Number parse(String string, ParseContext context) {
+ Matcher numberMatcher = NUMBER_PATTERN.matcher(string);
+ if (!numberMatcher.matches())
+ return null;
- if (namedGroups.containsKey("rad")) {
- try {
- d = Math.toDegrees(Double.parseDouble(number));
- } catch (NumberFormatException ignored) {
- }
- }
+ Integer integerAttempt = convertIntegerFormatted(string, Integer::parseInt, Number::intValue);
+ if (integerAttempt != null)
+ return integerAttempt;
- return d;
- } catch (NumberFormatException e) {
- return null;
- }
+ return convertDecimalFormatted(string,
+ Double::parseDouble,
+ Function.identity(),
+ d -> !d.isNaN() && !d.isInfinite());
}
@Override
@@ -667,4 +408,310 @@ public boolean mustSyncDeserialization() {
}
}
+ private static class LongParser extends Parser {
+
+ @Override
+ public @Nullable Long parse(String string, ParseContext context) {
+ return convertIntegerFormatted(string, Long::parseLong, Number::longValue);
+ }
+
+ @Override
+ public String toString(Long l, int flags) {
+ return l.toString();
+ }
+
+ @Override
+ public String toVariableNameString(Long l) {
+ return l.toString();
+ }
+ }
+
+ private static class LongSerializer extends Serializer {
+
+ @Override
+ public Fields serialize(Long l) {
+ throw new IllegalStateException(); // serialised natively by Yggdrasil
+ }
+
+ @Override
+ public boolean canBeInstantiated() {
+ return true;
+ }
+
+ @Override
+ public void deserialize(Long l, Fields fields) {
+ assert false;
+ }
+
+ @Override
+ public @Nullable Long deserialize(String string) {
+ try {
+ return Long.parseLong(string);
+ } catch (NumberFormatException ex) {
+ return null;
+ }
+ }
+
+ @Override
+ public boolean mustSyncDeserialization() {
+ return false;
+ }
+ }
+
+ private static class IntegerParser extends Parser {
+
+ @Override
+ public @Nullable Integer parse(String string, ParseContext context) {
+ return convertIntegerFormatted(string, Integer::parseInt, Number::intValue);
+ }
+
+ @Override
+ public String toString(Integer i, int flags) {
+ return i.toString();
+ }
+
+ @Override
+ public String toVariableNameString(Integer i) {
+ return i.toString();
+ }
+ }
+
+ private static class IntegerSerializer extends Serializer {
+
+ @Override
+ public Fields serialize(Integer i) {
+ throw new IllegalStateException(); // serialised natively by Yggdrasil
+ }
+
+ @Override
+ public boolean canBeInstantiated() {
+ return true;
+ }
+
+ @Override
+ public void deserialize(Integer i, Fields fields) {
+ assert false;
+ }
+
+ @Override
+ public @Nullable Integer deserialize(String string) {
+ try {
+ return Integer.parseInt(string);
+ } catch (NumberFormatException ex) {
+ return null;
+ }
+ }
+
+ @Override
+ public boolean mustSyncDeserialization() {
+ return false;
+ }
+ }
+
+ private static class DoubleParser extends Parser {
+
+ @Override
+ public @Nullable Double parse(String string, ParseContext context) {
+ return convertDecimalFormatted(string,
+ Double::parseDouble,
+ Function.identity(),
+ d -> !d.isNaN() && !d.isInfinite());
+ }
+
+ @Override
+ public String toString(Double d, int flags) {
+ return StringUtils.toString(d, SkriptConfig.numberAccuracy.value());
+ }
+
+ @Override
+ public String toVariableNameString(Double d) {
+ return StringUtils.toString(d, VARIABLENAME_NUMBERACCURACY);
+ }
+ }
+
+ private static class DoubleSerializer extends Serializer {
+
+ @Override
+ public Fields serialize(Double d) {
+ throw new IllegalStateException(); // serialised natively by Yggdrasil
+ }
+
+ @Override
+ public boolean canBeInstantiated() {
+ return true;
+ }
+
+ @Override
+ public void deserialize(Double d, Fields fields) {
+ assert false;
+ }
+
+ @Override
+ public @Nullable Double deserialize(String string) {
+ try {
+ return Double.parseDouble(string);
+ } catch (NumberFormatException ex) {
+ return null;
+ }
+ }
+
+ @Override
+ public boolean mustSyncDeserialization() {
+ return false;
+ }
+ }
+
+ private static class FloatParser extends Parser {
+
+ @Override
+ public @Nullable Float parse(String string, ParseContext context) {
+ return convertDecimalFormatted(string,
+ Float::parseFloat,
+ Number::floatValue,
+ f -> !f.isNaN() && !f.isInfinite());
+ }
+
+ @Override
+ public String toString(Float f, int flags) {
+ return StringUtils.toString(f, SkriptConfig.numberAccuracy.value());
+ }
+
+ @Override
+ public String toVariableNameString(Float f) {
+ return StringUtils.toString(f.doubleValue(), VARIABLENAME_NUMBERACCURACY);
+ }
+ }
+
+ private static class FloatSerializer extends Serializer {
+
+ @Override
+ public Fields serialize(Float f) {
+ throw new IllegalStateException(); // serialised natively by Yggdrasil
+ }
+
+ @Override
+ public boolean canBeInstantiated() {
+ return true;
+ }
+
+ @Override
+ public void deserialize(Float f, Fields fields) {
+ assert false;
+ }
+
+ @Override
+ public @Nullable Float deserialize(String string) {
+ try {
+ return Float.parseFloat(string);
+ } catch (NumberFormatException ex) {
+ return null;
+ }
+ }
+
+ @Override
+ public boolean mustSyncDeserialization() {
+ return false;
+ }
+ }
+
+ private static class ShortParser extends Parser {
+
+ @Override
+ public @Nullable Short parse(String string, ParseContext context) {
+ return convertIntegerFormatted(string, Short::parseShort, Number::shortValue);
+ }
+
+ @Override
+ public String toString(Short s, int flags) {
+ return s.toString();
+ }
+
+ @Override
+ public String toVariableNameString(Short s) {
+ return s.toString();
+ }
+ }
+
+ private static class ShortSerializer extends Serializer {
+
+ @Override
+ public Fields serialize(Short s) {
+ throw new IllegalStateException(); // serialised natively by Yggdrasil
+ }
+
+ @Override
+ public boolean canBeInstantiated() {
+ return true;
+ }
+
+ @Override
+ public void deserialize(Short s, Fields fields) {
+ assert false;
+ }
+
+ @Override
+ public @Nullable Short deserialize(String string) {
+ try {
+ return Short.parseShort(string);
+ } catch (NumberFormatException ex) {
+ return null;
+ }
+ }
+
+ @Override
+ public boolean mustSyncDeserialization() {
+ return false;
+ }
+ }
+
+ private static class ByteParser extends Parser {
+
+ @Override
+ public @Nullable Byte parse(String string, ParseContext context) {
+ return convertIntegerFormatted(string, Byte::parseByte, Number::byteValue);
+ }
+
+ @Override
+ public String toString(Byte b, int flags) {
+ return b.toString();
+ }
+
+ @Override
+ public String toVariableNameString(Byte b) {
+ return b.toString();
+ }
+ }
+
+ private static class ByteSerializer extends Serializer {
+
+ @Override
+ public Fields serialize(Byte b) {
+ throw new IllegalStateException(); // serialised natively by Yggdrasil
+ }
+
+ @Override
+ public boolean canBeInstantiated() {
+ return true;
+ }
+
+ @Override
+ public void deserialize(Byte b, Fields fields) {
+ assert false;
+ }
+
+ @Override
+ public @Nullable Byte deserialize(String string) {
+ try {
+ return Byte.parseByte(string);
+ } catch (NumberFormatException ex) {
+ return null;
+ }
+ }
+
+ @Override
+ public boolean mustSyncDeserialization() {
+ return false;
+ }
+ }
+
}
From b378a3046a7db5ca7f62522b766bd160726129a5 Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Thu, 10 Oct 2024 19:35:11 +0200
Subject: [PATCH 08/26] forgot !
---
.../njol/skript/classes/data/JavaClasses.java | 29 +++++++++----------
.../skript/hooks/economy/classes/Money.java | 2 +-
2 files changed, 15 insertions(+), 16 deletions(-)
diff --git a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
index e6014c81bc8..098fb05a4d5 100644
--- a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
+++ b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
@@ -16,7 +16,6 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
-import java.util.Map;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -26,7 +25,7 @@ public class JavaClasses {
public static final int VARIABLENAME_NUMBERACCURACY = 8;
public static final Pattern INTEGER_PATTERN =
Pattern.compile("(?-?[0-9]+)( (in )?(?rad(ian)?s?)|deg(ree)?s?)?");
- public static final Pattern NUMBER_PATTERN =
+ public static final Pattern DECIMAL_PATTERN =
Pattern.compile("(?-?[0-9]+(?>\\.[0-9]+)?%?)( (in )?(?rad(ian)?s?)|deg(ree)?s?)?");
static {
@@ -273,9 +272,8 @@ public boolean mustSyncDeserialization() {
if (!matcher.matches())
return null;
- Map namedGroups = matcher.namedGroups();
String number = matcher.group("num");
- if (namedGroups.containsKey("rad")) {
+ if (matcher.group("rad") != null) {
try {
return converter.apply(Math.toDegrees(stringToNumber.apply(number).doubleValue()));
} catch (NumberFormatException ignored) {
@@ -312,27 +310,28 @@ public boolean mustSyncDeserialization() {
Function converter,
Function isValid
) {
- Matcher matcher = NUMBER_PATTERN.matcher(string);
+ Matcher matcher = DECIMAL_PATTERN.matcher(string);
if (!matcher.matches())
return null;
- Map namedGroups = matcher.namedGroups();
String number = matcher.group("num");
try {
- double d = number.endsWith("%") ?
- stringToNumber.apply(number.substring(0, number.length() - 1)).doubleValue() / 100.0 :
- stringToNumber.apply(number).doubleValue();
-
- T result = converter.apply(d);
+ T result;
+ if (number.endsWith("%")) {
+ double d = stringToNumber.apply(number.substring(0, number.length() - 1)).doubleValue() / 100.0;
+ result = converter.apply(d);
+ } else {
+ result = stringToNumber.apply(number);
+ }
- if (isValid.apply(result))
+ if (!isValid.apply(result))
return null;
- if (namedGroups.containsKey("rad")) {
+ if (matcher.group("rad") != null) {
try {
- return converter.apply(Math.toDegrees(d));
+ return converter.apply(Math.toDegrees(result.doubleValue()));
} catch (NumberFormatException ignored) {
}
}
@@ -347,7 +346,7 @@ private static class NumberParser extends Parser {
@Override
public @Nullable Number parse(String string, ParseContext context) {
- Matcher numberMatcher = NUMBER_PATTERN.matcher(string);
+ Matcher numberMatcher = DECIMAL_PATTERN.matcher(string);
if (!numberMatcher.matches())
return null;
diff --git a/src/main/java/ch/njol/skript/hooks/economy/classes/Money.java b/src/main/java/ch/njol/skript/hooks/economy/classes/Money.java
index bc4d550dabf..dafe28d61e6 100644
--- a/src/main/java/ch/njol/skript/hooks/economy/classes/Money.java
+++ b/src/main/java/ch/njol/skript/hooks/economy/classes/Money.java
@@ -158,7 +158,7 @@ private static Money parseMoney(String s, String addition) {
@Nullable
private static Double parseDouble(String s) {
- if (!JavaClasses.NUMBER_PATTERN.matcher(s).matches())
+ if (!JavaClasses.DECIMAL_PATTERN.matcher(s).matches())
return null;
try {
return Double.parseDouble(s);
From 036adf35823de4e5e73b48c01873e580a5602db9 Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Thu, 10 Oct 2024 19:43:59 +0200
Subject: [PATCH 09/26] fix doubleValue call
---
.../java/ch/njol/skript/classes/data/JavaClasses.java | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
index 098fb05a4d5..e037e1710f2 100644
--- a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
+++ b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
@@ -45,7 +45,7 @@ public class JavaClasses {
"Please note that many expressions only need integers, i.e. " +
"will discard any fractional parts of any numbers without producing an error.",
"Radians will be converted to degrees.")
- .usage("[-]###[.###] (any amount of digits; very large numbers will be truncated though)")
+ .usage("[-]###[.###] [[in ](rad[ian][s]|deg[ree][s])] (any amount of digits; very large numbers will be truncated though)")
.examples(
"set the player's health to 5.5",
"set {_temp} to 2*{_temp} - 2.5",
@@ -295,10 +295,12 @@ public boolean mustSyncDeserialization() {
* If the number is a percentage, it gets parsed as a double between 0-1.
* Then tries to convert radians to degrees if the string contains a radian group.
* If the string could be parsed, applies {@code converter} to convert the number to the desired type.
+ * If the number is not valid by {@code isValid}, returns null.
*
*
* @param string The string with the possible number.
* @param stringToNumber The function to parse the number, e.g. {@link Integer#parseInt(String)}.
+ * @param fromPercentage A function that divides the value by 100.
* @param converter The function to convert the number to the desired type, e.g. {@link Number#intValue()}.
* @param isValid The function to check if the number is valid, e.g. {@link Double#isNaN()}.
* @return The parsed string, or null if the string could not be parsed.
@@ -307,6 +309,7 @@ public boolean mustSyncDeserialization() {
private static @Nullable T convertDecimalFormatted(
String string,
Function stringToNumber,
+ Function fromPercentage,
Function converter,
Function isValid
) {
@@ -320,8 +323,7 @@ public boolean mustSyncDeserialization() {
try {
T result;
if (number.endsWith("%")) {
- double d = stringToNumber.apply(number.substring(0, number.length() - 1)).doubleValue() / 100.0;
- result = converter.apply(d);
+ result = fromPercentage.apply(stringToNumber.apply(number.substring(0, number.length() - 1)));
} else {
result = stringToNumber.apply(number);
}
@@ -356,6 +358,7 @@ private static class NumberParser extends Parser {
return convertDecimalFormatted(string,
Double::parseDouble,
+ d -> d / 100.0,
Function.identity(),
d -> !d.isNaN() && !d.isInfinite());
}
@@ -513,6 +516,7 @@ private static class DoubleParser extends Parser {
public @Nullable Double parse(String string, ParseContext context) {
return convertDecimalFormatted(string,
Double::parseDouble,
+ d -> d / 100.0,
Function.identity(),
d -> !d.isNaN() && !d.isInfinite());
}
@@ -566,6 +570,7 @@ private static class FloatParser extends Parser {
public @Nullable Float parse(String string, ParseContext context) {
return convertDecimalFormatted(string,
Float::parseFloat,
+ d -> d / 100f,
Number::floatValue,
f -> !f.isNaN() && !f.isInfinite());
}
From 491c7cfb111cf5bc5d3cd92c1ae43eb00fec6eaf Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Fri, 11 Oct 2024 18:19:31 +0200
Subject: [PATCH 10/26] fix parenthesis
---
src/main/java/ch/njol/skript/classes/data/JavaClasses.java | 4 ++--
src/test/skript/tests/misc/angle.sk | 4 ++--
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
index e037e1710f2..07d621e6826 100644
--- a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
+++ b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
@@ -24,9 +24,9 @@ public class JavaClasses {
public static final int VARIABLENAME_NUMBERACCURACY = 8;
public static final Pattern INTEGER_PATTERN =
- Pattern.compile("(?-?[0-9]+)( (in )?(?rad(ian)?s?)|deg(ree)?s?)?");
+ Pattern.compile("(?-?[0-9]+)( (in )?((?rad(ian)?s?)|deg(ree)?s?))?");
public static final Pattern DECIMAL_PATTERN =
- Pattern.compile("(?-?[0-9]+(?>\\.[0-9]+)?%?)( (in )?(?rad(ian)?s?)|deg(ree)?s?)?");
+ Pattern.compile("(?-?[0-9]+(?>\\.[0-9]+)?%?)( (in )?((?rad(ian)?s?)|deg(ree)?s?))?");
static {
Classes.registerClass(new ClassInfo<>(Object.class, "object")
diff --git a/src/test/skript/tests/misc/angle.sk b/src/test/skript/tests/misc/angle.sk
index 1e5cc32aceb..7664a6c2fef 100644
--- a/src/test/skript/tests/misc/angle.sk
+++ b/src/test/skript/tests/misc/angle.sk
@@ -6,5 +6,5 @@ test "angle":
assert pi radians is 180.0 degrees with "pi radians is not 180 degrees"
assert 2 * pi in radians is 360.0 degrees with "2pi radians is not 360 degrees"
- assert 180 degrees is pi radians with "180 degrees is not pi radians"
- assert 360 in degrees is 2 * pi radians with "360 degrees is not 2pi radians"
+ assert 180.0 degrees is pi radians with "180 degrees is not pi radians"
+ assert 360.0 in degrees is 2 * pi radians with "360 degrees is not 2pi radians"
From fcd2c60663b4b83fbce9541a15bcb42342a0c0b5 Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Fri, 11 Oct 2024 18:27:55 +0200
Subject: [PATCH 11/26] add expression
---
.../ch/njol/skript/expressions/ExprAngle.java | 78 +++++++++++++++++++
1 file changed, 78 insertions(+)
create mode 100644 src/main/java/ch/njol/skript/expressions/ExprAngle.java
diff --git a/src/main/java/ch/njol/skript/expressions/ExprAngle.java b/src/main/java/ch/njol/skript/expressions/ExprAngle.java
new file mode 100644
index 00000000000..bdcad704daf
--- /dev/null
+++ b/src/main/java/ch/njol/skript/expressions/ExprAngle.java
@@ -0,0 +1,78 @@
+package ch.njol.skript.expressions;
+
+import ch.njol.skript.Skript;
+import ch.njol.skript.doc.Description;
+import ch.njol.skript.doc.Examples;
+import ch.njol.skript.doc.Name;
+import ch.njol.skript.doc.Since;
+import ch.njol.skript.lang.Expression;
+import ch.njol.skript.lang.ExpressionType;
+import ch.njol.skript.lang.SkriptParser.ParseResult;
+import ch.njol.skript.lang.util.SimpleExpression;
+import ch.njol.util.Kleenean;
+import org.bukkit.event.Event;
+import org.jetbrains.annotations.Nullable;
+
+@Name("Angle")
+@Description({
+ "Represents the passed number value in degrees.",
+ "If radians is specified, converts the passed value to degrees. This conversion may not be entirely accurate, " +
+ "due to floating point precision.",
+})
+@Examples({
+ "set {_angle} to 90 degrees",
+ "{_angle} is 90 # true",
+ "180 degrees is pi # true",
+ "pi radians is 180 degrees # true"
+})
+@Since("INSERT VERSION")
+public class ExprAngle extends SimpleExpression {
+
+ static {
+ Skript.registerExpression(ExprAngle.class, Number.class, ExpressionType.SIMPLE,
+ "%number% [in ]deg[ree][s]",
+ "%number% [in ]rad[ian][s]");
+ }
+
+ private Expression angle;
+ private boolean isRadians;
+
+ @Override
+ public boolean init(Expression>[] expressions, int matchedPattern,
+ Kleenean isDelayed, ParseResult parseResult) {
+ //noinspection unchecked
+ angle = (Expression) expressions[0];
+ isRadians = matchedPattern == 1;
+ return true;
+ }
+
+ @Override
+ protected Number @Nullable [] get(Event event) {
+ Number number = angle.getSingle(event);
+
+ if (number == null)
+ return null;
+
+ if (isRadians) {
+ return new Double[]{Math.toDegrees(number.doubleValue())};
+ }
+
+ return new Number[]{number};
+ }
+
+ @Override
+ public boolean isSingle() {
+ return true;
+ }
+
+ @Override
+ public Class extends Number> getReturnType() {
+ return Number.class;
+ }
+
+ @Override
+ public String toString(@Nullable Event event, boolean debug) {
+ return angle.toString(event, debug) + (isRadians ? " degrees" : " radians");
+ }
+
+}
\ No newline at end of file
From 46b507e715785e9d1e745b454b75d7b066f77239 Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Sat, 12 Oct 2024 14:39:47 +0200
Subject: [PATCH 12/26] update tests
---
src/test/skript/tests/misc/angle.sk | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/src/test/skript/tests/misc/angle.sk b/src/test/skript/tests/misc/angle.sk
index 7664a6c2fef..666a7b762a8 100644
--- a/src/test/skript/tests/misc/angle.sk
+++ b/src/test/skript/tests/misc/angle.sk
@@ -8,3 +8,26 @@ test "angle":
assert 180.0 degrees is pi radians with "180 degrees is not pi radians"
assert 360.0 in degrees is 2 * pi radians with "360 degrees is not 2pi radians"
+
+ assert infinity value degrees is infinity value with "infinity degrees is not infinity"
+ assert -infinity value degrees is -infinity value with "-infinity degrees is not -infinity"
+ assert infinity value radians is infinity value with "infinity radians is not infinity"
+ assert -infinity value radians is -infinity value with "-infinity radians is not -infinity"
+
+ assert ("90 degrees" parsed as number) is 90 with "90 degrees parsed as number is not 90"
+ assert ("3.141592653589793 radians" parsed as number) is 180.0 with "pi radians parsed as number is not 180 degrees"
+ assert ("90 degrees" parsed as number) is 90 degrees with "90 degrees parsed as number is not 90 degrees"
+ assert ("3.141592653589793 radians" parsed as number) is 180.0 degrees with "pi radians parsed as number is not 180 degrees"
+ assert ("90 degrees" parsed as number) is 0.5 * pi radians with "90 degrees parsed as number is not 0.5pi radians"
+ assert ("3.141592653589793 radians" parsed as number) is pi radians with "pi radians parsed as number is not pi radians"
+
+ assert 90, 180 and 270 in degrees is 90, 180 and 270 with "90, 180 and 270 degrees is not 90, 180 and 270"
+ assert 0.5 * pi, pi and 1.5 * pi in radians is 90.0, 180.0 and 270.0 with "0.5pi, pi and 1.5pi radians is not 90, 180 and 270"
+
+ assert ("n" parsed as number) degrees is not set with "'n' parsed as number degrees is set"
+ assert ("n" parsed as number) radians is not set with "'n' parsed as number radians is set"
+ assert ("n degrees" parsed as number) is not set with "'n' parsed as number degrees is set"
+ assert ("n radians" parsed as number) is not set with "'n' parsed as number radians is set"
+
+ assert NaN value degrees is NaN value with "NaN degrees is not NaN"
+ assert NaN value radians is NaN value with "NaN radians is not NaN"
From 8d552b98c71a4c24581cbc2438c7f0722586fdab Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Sat, 12 Oct 2024 14:39:59 +0200
Subject: [PATCH 13/26] requested changes
---
.../njol/skript/classes/data/JavaClasses.java | 64 +++++++++++--------
.../ch/njol/skript/expressions/ExprAngle.java | 25 +++++---
2 files changed, 52 insertions(+), 37 deletions(-)
diff --git a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
index 07d621e6826..11b28791bc6 100644
--- a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
+++ b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
@@ -23,10 +23,34 @@
public class JavaClasses {
public static final int VARIABLENAME_NUMBERACCURACY = 8;
+
+ /**
+ * Matches an integer with an optional unit of radians or degrees.
+ *
+ * First, the actual number format {@code num} is specified. Then, an optional angle unit is specified.
+ * For this, the {@code rad} group is used. This specifies that the number is in radians.
+ * This is used to determine if the number should be converted to degrees.
+ * Degrees is not a named group because it just returns the value in the {@code num} group, which
+ * is the default behaviour.
+ * Optionally, the user can use {@code x in degrees} instead of {@code x degrees}.
+ *
+ */
public static final Pattern INTEGER_PATTERN =
- Pattern.compile("(?-?[0-9]+)( (in )?((?rad(ian)?s?)|deg(ree)?s?))?");
+ Pattern.compile("(?-?[0-9]+)(?: (?:in )?(?:(?rad(?:ian)?s?)|deg(?:ree)?s?))?");
+
+ /**
+ * Matches a decimal number with an optional unit of radians or degrees.
+ *
+ * First, the actual number format {@code num} is specified. Then, an optional angle unit is specified.
+ * For this, the {@code rad} group is used. This specifies that the number is in radians.
+ * This is used to determine if the number should be converted to degrees.
+ * Degrees is not a named group because it just returns the value in the {@code num} group, which
+ * is the default behaviour.
+ * Optionally, the user can use {@code x in degrees} instead of {@code x degrees}.
+ *
+ */
public static final Pattern DECIMAL_PATTERN =
- Pattern.compile("(?-?[0-9]+(?>\\.[0-9]+)?%?)( (in )?((?rad(ian)?s?)|deg(ree)?s?))?");
+ Pattern.compile("(?-?[0-9]+(?>\\.[0-9]+)?%?)(?: (?:in )?(?:(?rad(?:ian)?s?)|deg(?:ree)?s?))?");
static {
Classes.registerClass(new ClassInfo<>(Object.class, "object")
@@ -295,23 +319,18 @@ public boolean mustSyncDeserialization() {
* If the number is a percentage, it gets parsed as a double between 0-1.
* Then tries to convert radians to degrees if the string contains a radian group.
* If the string could be parsed, applies {@code converter} to convert the number to the desired type.
- * If the number is not valid by {@code isValid}, returns null.
*
*
* @param string The string with the possible number.
* @param stringToNumber The function to parse the number, e.g. {@link Integer#parseInt(String)}.
- * @param fromPercentage A function that divides the value by 100.
* @param converter The function to convert the number to the desired type, e.g. {@link Number#intValue()}.
- * @param isValid The function to check if the number is valid, e.g. {@link Double#isNaN()}.
* @return The parsed string, or null if the string could not be parsed.
*/
@Contract(pure = true)
private static @Nullable T convertDecimalFormatted(
String string,
Function stringToNumber,
- Function fromPercentage,
- Function converter,
- Function isValid
+ Function converter
) {
Matcher matcher = DECIMAL_PATTERN.matcher(string);
@@ -323,14 +342,12 @@ public boolean mustSyncDeserialization() {
try {
T result;
if (number.endsWith("%")) {
- result = fromPercentage.apply(stringToNumber.apply(number.substring(0, number.length() - 1)));
+ T extracted = stringToNumber.apply(number.substring(0, number.length() - 1));
+ result = converter.apply(extracted.doubleValue() / 100.0);
} else {
result = stringToNumber.apply(number);
}
- if (!isValid.apply(result))
- return null;
-
if (matcher.group("rad") != null) {
try {
return converter.apply(Math.toDegrees(result.doubleValue()));
@@ -356,11 +373,8 @@ private static class NumberParser extends Parser {
if (integerAttempt != null)
return integerAttempt;
- return convertDecimalFormatted(string,
- Double::parseDouble,
- d -> d / 100.0,
- Function.identity(),
- d -> !d.isNaN() && !d.isInfinite());
+ Double parsed = convertDecimalFormatted(string, Double::parseDouble, Function.identity());
+ return parsed == null || parsed.isInfinite() || parsed.isNaN() ? null : parsed;
}
@Override
@@ -514,11 +528,9 @@ private static class DoubleParser extends Parser {
@Override
public @Nullable Double parse(String string, ParseContext context) {
- return convertDecimalFormatted(string,
- Double::parseDouble,
- d -> d / 100.0,
- Function.identity(),
- d -> !d.isNaN() && !d.isInfinite());
+ Double parsed = convertDecimalFormatted(string, Double::parseDouble, Function.identity());
+
+ return parsed == null || parsed.isInfinite() || parsed.isNaN() ? null : parsed;
}
@Override
@@ -568,11 +580,9 @@ private static class FloatParser extends Parser {
@Override
public @Nullable Float parse(String string, ParseContext context) {
- return convertDecimalFormatted(string,
- Float::parseFloat,
- d -> d / 100f,
- Number::floatValue,
- f -> !f.isNaN() && !f.isInfinite());
+ Float parsed = convertDecimalFormatted(string, Float::parseFloat, Number::floatValue);
+
+ return parsed == null || parsed.isInfinite() || parsed.isNaN() ? null : parsed;
}
@Override
diff --git a/src/main/java/ch/njol/skript/expressions/ExprAngle.java b/src/main/java/ch/njol/skript/expressions/ExprAngle.java
index bdcad704daf..c85b264a1a2 100644
--- a/src/main/java/ch/njol/skript/expressions/ExprAngle.java
+++ b/src/main/java/ch/njol/skript/expressions/ExprAngle.java
@@ -30,8 +30,10 @@ public class ExprAngle extends SimpleExpression {
static {
Skript.registerExpression(ExprAngle.class, Number.class, ExpressionType.SIMPLE,
- "%number% [in ]deg[ree][s]",
- "%number% [in ]rad[ian][s]");
+ "%number% [in] deg[ree][s]",
+ "%number% [in] rad[ian][s]",
+ "%numbers% in deg[ree][s]",
+ "%numbers% in rad[ian][s]");
}
private Expression angle;
@@ -42,27 +44,30 @@ public boolean init(Expression>[] expressions, int matchedPattern,
Kleenean isDelayed, ParseResult parseResult) {
//noinspection unchecked
angle = (Expression) expressions[0];
- isRadians = matchedPattern == 1;
+ isRadians = matchedPattern % 2 != 0;
return true;
}
@Override
protected Number @Nullable [] get(Event event) {
- Number number = angle.getSingle(event);
+ Number[] numbers = angle.getAll(event);
- if (number == null)
+ if (numbers == null)
return null;
if (isRadians) {
- return new Double[]{Math.toDegrees(number.doubleValue())};
+ for (int i = 0; i < numbers.length; i++)
+ numbers[i] = Math.toDegrees(numbers[i].doubleValue());
+
+ return numbers;
}
- return new Number[]{number};
+ return numbers;
}
@Override
public boolean isSingle() {
- return true;
+ return angle.isSingle();
}
@Override
@@ -72,7 +77,7 @@ public Class extends Number> getReturnType() {
@Override
public String toString(@Nullable Event event, boolean debug) {
- return angle.toString(event, debug) + (isRadians ? " degrees" : " radians");
+ return angle.toString(event, debug) + " in " + (isRadians ? "degrees" : "radians");
}
-}
\ No newline at end of file
+}
From 27d0c78b0343db24a63067512757fc0668c9a1b6 Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Sat, 12 Oct 2024 15:15:46 +0200
Subject: [PATCH 14/26] oops
---
src/test/skript/tests/misc/angle.sk | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/test/skript/tests/misc/angle.sk b/src/test/skript/tests/misc/angle.sk
index 666a7b762a8..f6deadfb12a 100644
--- a/src/test/skript/tests/misc/angle.sk
+++ b/src/test/skript/tests/misc/angle.sk
@@ -29,5 +29,5 @@ test "angle":
assert ("n degrees" parsed as number) is not set with "'n' parsed as number degrees is set"
assert ("n radians" parsed as number) is not set with "'n' parsed as number radians is set"
- assert NaN value degrees is NaN value with "NaN degrees is not NaN"
- assert NaN value radians is NaN value with "NaN radians is not NaN"
+ assert isNaN(NaN value degrees) is true with "NaN degrees is not NaN"
+ assert isNaN(NaN value radians) is true with "NaN radians is not NaN"
From 6ee87f16637db089711b155cc576e5033b17a1f3 Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Sat, 12 Oct 2024 18:30:10 +0200
Subject: [PATCH 15/26] remove unnecessary converter application
---
.../njol/skript/classes/data/JavaClasses.java | 53 ++++++++++++-------
1 file changed, 34 insertions(+), 19 deletions(-)
diff --git a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
index 11b28791bc6..1afb4cc3f11 100644
--- a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
+++ b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
@@ -276,20 +276,20 @@ public boolean mustSyncDeserialization() {
*
* Applies {@code stringToNumber} for parsing the number, then tries to
* convert radians to degrees if the string contains a radian group.
- * If the string could be parsed, applies {@code converter} to convert
- * the number to the desired type.
+ * If the string could be parsed and converted to radians,
+ * applies {@code fromDouble} to convert the returned {@link Double} to the desired type.
*
*
* @param string The string with the possible number.
* @param stringToNumber The function to parse the number, e.g. {@link Integer#parseInt(String)}.
- * @param converter The function to convert the number to the desired type, e.g. {@link Number#intValue()}.
+ * @param fromDouble The function to convert a {@link Double} to the desired type, e.g. {@link Double#intValue()}.
* @return The parsed string, or null if the string could not be parsed.
*/
@Contract(pure = true)
private static @Nullable T convertIntegerFormatted(
String string,
- Function stringToNumber,
- Function converter
+ Function stringToNumber,
+ Function fromDouble
) {
Matcher matcher = INTEGER_PATTERN.matcher(string);
@@ -299,12 +299,12 @@ public boolean mustSyncDeserialization() {
String number = matcher.group("num");
if (matcher.group("rad") != null) {
try {
- return converter.apply(Math.toDegrees(stringToNumber.apply(number).doubleValue()));
+ return fromDouble.apply(Math.toDegrees(stringToNumber.apply(number).doubleValue()));
} catch (NumberFormatException ignored) {
}
} else {
try {
- return converter.apply(stringToNumber.apply(number));
+ return stringToNumber.apply(number);
} catch (NumberFormatException ignored) {
}
}
@@ -313,24 +313,25 @@ public boolean mustSyncDeserialization() {
}
/**
- * Converts a string to a number formatted as an decimal.
+ * Converts a string to a number formatted as a decimal.
*
* Applies {@code stringToNumber} for parsing the number.
- * If the number is a percentage, it gets parsed as a double between 0-1.
+ * If the number is a percentage, it gets parsed using the double value divided by 100, and {@code fromDouble}.
* Then tries to convert radians to degrees if the string contains a radian group.
- * If the string could be parsed, applies {@code converter} to convert the number to the desired type.
+ * If the string could be parsed and converted to radians,
+ * applies {@code fromDouble} to convert the returned {@link Double} to the desired type.
*
*
* @param string The string with the possible number.
* @param stringToNumber The function to parse the number, e.g. {@link Integer#parseInt(String)}.
- * @param converter The function to convert the number to the desired type, e.g. {@link Number#intValue()}.
+ * @param fromDouble The function to convert a {@link Double} to the desired type, e.g. {@link Double#intValue()}.
* @return The parsed string, or null if the string could not be parsed.
*/
@Contract(pure = true)
private static @Nullable T convertDecimalFormatted(
String string,
Function stringToNumber,
- Function converter
+ Function fromDouble
) {
Matcher matcher = DECIMAL_PATTERN.matcher(string);
@@ -343,14 +344,14 @@ public boolean mustSyncDeserialization() {
T result;
if (number.endsWith("%")) {
T extracted = stringToNumber.apply(number.substring(0, number.length() - 1));
- result = converter.apply(extracted.doubleValue() / 100.0);
+ result = fromDouble.apply(extracted.doubleValue() / 100.0);
} else {
result = stringToNumber.apply(number);
}
if (matcher.group("rad") != null) {
try {
- return converter.apply(Math.toDegrees(result.doubleValue()));
+ return fromDouble.apply(Math.toDegrees(result.doubleValue()));
} catch (NumberFormatException ignored) {
}
}
@@ -369,7 +370,7 @@ private static class NumberParser extends Parser {
if (!numberMatcher.matches())
return null;
- Integer integerAttempt = convertIntegerFormatted(string, Integer::parseInt, Number::intValue);
+ Integer integerAttempt = convertIntegerFormatted(string, Integer::parseInt, Double::intValue);
if (integerAttempt != null)
return integerAttempt;
@@ -386,6 +387,7 @@ public String toString(Number number, int flags) {
public String toVariableNameString(Number number) {
return StringUtils.toString(number.doubleValue(), VARIABLENAME_NUMBERACCURACY);
}
+
}
private static class NumberSerializer extends Serializer {
@@ -422,13 +424,14 @@ public void deserialize(Number number, Fields fields) {
public boolean mustSyncDeserialization() {
return false;
}
+
}
private static class LongParser extends Parser {
@Override
public @Nullable Long parse(String string, ParseContext context) {
- return convertIntegerFormatted(string, Long::parseLong, Number::longValue);
+ return convertIntegerFormatted(string, Long::parseLong, Double::longValue);
}
@Override
@@ -440,6 +443,7 @@ public String toString(Long l, int flags) {
public String toVariableNameString(Long l) {
return l.toString();
}
+
}
private static class LongSerializer extends Serializer {
@@ -472,13 +476,14 @@ public void deserialize(Long l, Fields fields) {
public boolean mustSyncDeserialization() {
return false;
}
+
}
private static class IntegerParser extends Parser {
@Override
public @Nullable Integer parse(String string, ParseContext context) {
- return convertIntegerFormatted(string, Integer::parseInt, Number::intValue);
+ return convertIntegerFormatted(string, Integer::parseInt, Double::intValue);
}
@Override
@@ -490,6 +495,7 @@ public String toString(Integer i, int flags) {
public String toVariableNameString(Integer i) {
return i.toString();
}
+
}
private static class IntegerSerializer extends Serializer {
@@ -522,6 +528,7 @@ public void deserialize(Integer i, Fields fields) {
public boolean mustSyncDeserialization() {
return false;
}
+
}
private static class DoubleParser extends Parser {
@@ -542,6 +549,7 @@ public String toString(Double d, int flags) {
public String toVariableNameString(Double d) {
return StringUtils.toString(d, VARIABLENAME_NUMBERACCURACY);
}
+
}
private static class DoubleSerializer extends Serializer {
@@ -574,6 +582,7 @@ public void deserialize(Double d, Fields fields) {
public boolean mustSyncDeserialization() {
return false;
}
+
}
private static class FloatParser extends Parser {
@@ -594,6 +603,7 @@ public String toString(Float f, int flags) {
public String toVariableNameString(Float f) {
return StringUtils.toString(f.doubleValue(), VARIABLENAME_NUMBERACCURACY);
}
+
}
private static class FloatSerializer extends Serializer {
@@ -626,13 +636,14 @@ public void deserialize(Float f, Fields fields) {
public boolean mustSyncDeserialization() {
return false;
}
+
}
private static class ShortParser extends Parser {
@Override
public @Nullable Short parse(String string, ParseContext context) {
- return convertIntegerFormatted(string, Short::parseShort, Number::shortValue);
+ return convertIntegerFormatted(string, Short::parseShort, Double::shortValue);
}
@Override
@@ -644,6 +655,7 @@ public String toString(Short s, int flags) {
public String toVariableNameString(Short s) {
return s.toString();
}
+
}
private static class ShortSerializer extends Serializer {
@@ -676,13 +688,14 @@ public void deserialize(Short s, Fields fields) {
public boolean mustSyncDeserialization() {
return false;
}
+
}
private static class ByteParser extends Parser {
@Override
public @Nullable Byte parse(String string, ParseContext context) {
- return convertIntegerFormatted(string, Byte::parseByte, Number::byteValue);
+ return convertIntegerFormatted(string, Byte::parseByte, Double::byteValue);
}
@Override
@@ -694,6 +707,7 @@ public String toString(Byte b, int flags) {
public String toVariableNameString(Byte b) {
return b.toString();
}
+
}
private static class ByteSerializer extends Serializer {
@@ -726,6 +740,7 @@ public void deserialize(Byte b, Fields fields) {
public boolean mustSyncDeserialization() {
return false;
}
+
}
}
From 79ce8231838676a2b5c88334494a3a07ace63618 Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Sat, 12 Oct 2024 19:44:48 +0200
Subject: [PATCH 16/26] remove converter
---
.../njol/skript/classes/data/JavaClasses.java | 37 ++++++++-----------
1 file changed, 16 insertions(+), 21 deletions(-)
diff --git a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
index 1afb4cc3f11..690caf7c0ad 100644
--- a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
+++ b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
@@ -276,20 +276,16 @@ public boolean mustSyncDeserialization() {
*
* Applies {@code stringToNumber} for parsing the number, then tries to
* convert radians to degrees if the string contains a radian group.
- * If the string could be parsed and converted to radians,
- * applies {@code fromDouble} to convert the returned {@link Double} to the desired type.
*
*
* @param string The string with the possible number.
* @param stringToNumber The function to parse the number, e.g. {@link Integer#parseInt(String)}.
- * @param fromDouble The function to convert a {@link Double} to the desired type, e.g. {@link Double#intValue()}.
* @return The parsed string, or null if the string could not be parsed.
*/
@Contract(pure = true)
private static @Nullable T convertIntegerFormatted(
String string,
- Function stringToNumber,
- Function fromDouble
+ Function stringToNumber
) {
Matcher matcher = INTEGER_PATTERN.matcher(string);
@@ -299,7 +295,8 @@ public boolean mustSyncDeserialization() {
String number = matcher.group("num");
if (matcher.group("rad") != null) {
try {
- return fromDouble.apply(Math.toDegrees(stringToNumber.apply(number).doubleValue()));
+ //noinspection unchecked
+ return (T) (Double) Math.toDegrees(stringToNumber.apply(number).doubleValue());
} catch (NumberFormatException ignored) {
}
} else {
@@ -318,20 +315,16 @@ public boolean mustSyncDeserialization() {
* Applies {@code stringToNumber} for parsing the number.
* If the number is a percentage, it gets parsed using the double value divided by 100, and {@code fromDouble}.
* Then tries to convert radians to degrees if the string contains a radian group.
- * If the string could be parsed and converted to radians,
- * applies {@code fromDouble} to convert the returned {@link Double} to the desired type.
*
*
* @param string The string with the possible number.
* @param stringToNumber The function to parse the number, e.g. {@link Integer#parseInt(String)}.
- * @param fromDouble The function to convert a {@link Double} to the desired type, e.g. {@link Double#intValue()}.
* @return The parsed string, or null if the string could not be parsed.
*/
@Contract(pure = true)
private static @Nullable T convertDecimalFormatted(
String string,
- Function stringToNumber,
- Function fromDouble
+ Function stringToNumber
) {
Matcher matcher = DECIMAL_PATTERN.matcher(string);
@@ -344,14 +337,16 @@ public boolean mustSyncDeserialization() {
T result;
if (number.endsWith("%")) {
T extracted = stringToNumber.apply(number.substring(0, number.length() - 1));
- result = fromDouble.apply(extracted.doubleValue() / 100.0);
+ //noinspection unchecked
+ result = (T) (Double) (extracted.doubleValue() / 100.0);
} else {
result = stringToNumber.apply(number);
}
if (matcher.group("rad") != null) {
try {
- return fromDouble.apply(Math.toDegrees(result.doubleValue()));
+ //noinspection unchecked
+ return (T) (Double) Math.toDegrees(result.doubleValue());
} catch (NumberFormatException ignored) {
}
}
@@ -370,11 +365,11 @@ private static class NumberParser extends Parser {
if (!numberMatcher.matches())
return null;
- Integer integerAttempt = convertIntegerFormatted(string, Integer::parseInt, Double::intValue);
+ Integer integerAttempt = convertIntegerFormatted(string, Integer::parseInt);
if (integerAttempt != null)
return integerAttempt;
- Double parsed = convertDecimalFormatted(string, Double::parseDouble, Function.identity());
+ Double parsed = convertDecimalFormatted(string, Double::parseDouble);
return parsed == null || parsed.isInfinite() || parsed.isNaN() ? null : parsed;
}
@@ -431,7 +426,7 @@ private static class LongParser extends Parser {
@Override
public @Nullable Long parse(String string, ParseContext context) {
- return convertIntegerFormatted(string, Long::parseLong, Double::longValue);
+ return convertIntegerFormatted(string, Long::parseLong);
}
@Override
@@ -483,7 +478,7 @@ private static class IntegerParser extends Parser {
@Override
public @Nullable Integer parse(String string, ParseContext context) {
- return convertIntegerFormatted(string, Integer::parseInt, Double::intValue);
+ return convertIntegerFormatted(string, Integer::parseInt);
}
@Override
@@ -535,7 +530,7 @@ private static class DoubleParser extends Parser {
@Override
public @Nullable Double parse(String string, ParseContext context) {
- Double parsed = convertDecimalFormatted(string, Double::parseDouble, Function.identity());
+ Double parsed = convertDecimalFormatted(string, Double::parseDouble);
return parsed == null || parsed.isInfinite() || parsed.isNaN() ? null : parsed;
}
@@ -589,7 +584,7 @@ private static class FloatParser extends Parser {
@Override
public @Nullable Float parse(String string, ParseContext context) {
- Float parsed = convertDecimalFormatted(string, Float::parseFloat, Number::floatValue);
+ Float parsed = convertDecimalFormatted(string, Float::parseFloat);
return parsed == null || parsed.isInfinite() || parsed.isNaN() ? null : parsed;
}
@@ -643,7 +638,7 @@ private static class ShortParser extends Parser {
@Override
public @Nullable Short parse(String string, ParseContext context) {
- return convertIntegerFormatted(string, Short::parseShort, Double::shortValue);
+ return convertIntegerFormatted(string, Short::parseShort);
}
@Override
@@ -695,7 +690,7 @@ private static class ByteParser extends Parser {
@Override
public @Nullable Byte parse(String string, ParseContext context) {
- return convertIntegerFormatted(string, Byte::parseByte, Double::byteValue);
+ return convertIntegerFormatted(string, Byte::parseByte);
}
@Override
From 87b9e840d02b25076b3d00d9487fcd0bc5e93bf0 Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Sun, 13 Oct 2024 16:12:04 +0200
Subject: [PATCH 17/26] le fix
---
src/main/java/ch/njol/skript/expressions/ExprAngle.java | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/src/main/java/ch/njol/skript/expressions/ExprAngle.java b/src/main/java/ch/njol/skript/expressions/ExprAngle.java
index c85b264a1a2..d271db5fa10 100644
--- a/src/main/java/ch/njol/skript/expressions/ExprAngle.java
+++ b/src/main/java/ch/njol/skript/expressions/ExprAngle.java
@@ -17,7 +17,7 @@
@Description({
"Represents the passed number value in degrees.",
"If radians is specified, converts the passed value to degrees. This conversion may not be entirely accurate, " +
- "due to floating point precision.",
+ "due to floating point precision.",
})
@Examples({
"set {_angle} to 90 degrees",
@@ -52,9 +52,6 @@ public boolean init(Expression>[] expressions, int matchedPattern,
protected Number @Nullable [] get(Event event) {
Number[] numbers = angle.getAll(event);
- if (numbers == null)
- return null;
-
if (isRadians) {
for (int i = 0; i < numbers.length; i++)
numbers[i] = Math.toDegrees(numbers[i].doubleValue());
From 18bf9c705d47a3f37ec66091c924a6fd44298169 Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Sun, 13 Oct 2024 16:22:44 +0200
Subject: [PATCH 18/26] fix merge w/ display entities
---
.../njol/skript/classes/data/JavaClasses.java | 36 +++++++++++++++++++
.../skript/bukkit/misc/effects/EffRotate.java | 6 ++--
2 files changed, 39 insertions(+), 3 deletions(-)
diff --git a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
index 690caf7c0ad..16ea808a5a3 100644
--- a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
+++ b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
@@ -1,9 +1,11 @@
package ch.njol.skript.classes.data;
+import ch.njol.skript.Skript;
import ch.njol.skript.SkriptConfig;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.classes.Serializer;
+import ch.njol.skript.expressions.base.EventValueExpression;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.lang.VariableString;
import ch.njol.skript.lang.util.SimpleLiteral;
@@ -15,6 +17,7 @@
import ch.njol.yggdrasil.Fields;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
+import org.joml.Quaternionf;
import java.util.function.Function;
import java.util.regex.Matcher;
@@ -269,6 +272,39 @@ public boolean mustSyncDeserialization() {
return false;
}
}));
+
+ // joml type - for display entities
+ if (Skript.classExists("org.joml.Quaternionf"))
+ Classes.registerClass(new ClassInfo<>(Quaternionf.class, "quaternion")
+ .user("quaternionf?s?")
+ .name("Quaternion")
+ .description("Quaternions are four dimensional vectors, often used for representing rotations.")
+ .since("INSERT VERSION")
+ .parser(new Parser<>() {
+ public boolean canParse(ParseContext context) {
+ return false;
+ }
+
+ @Override
+ public String toString(Quaternionf quaternion, int flags) {
+ return "w:" + Skript.toString(quaternion.w()) + ", x:" + Skript.toString(quaternion.x()) + ", y:" + Skript.toString(quaternion.y()) + ", z:" + Skript.toString(quaternion.z());
+ }
+
+ @Override
+ public String toVariableNameString(Quaternionf quaternion) {
+ return quaternion.w() + "," + quaternion.x() + "," + quaternion.y() + "," + quaternion.z();
+ }
+ })
+ .defaultExpression(new EventValueExpression<>(Quaternionf.class))
+ .cloner(quaternion -> {
+ try {
+ // Implements cloneable, but doesn't return a Quaternionf.
+ // org.joml improper override. Returns Object.
+ return (Quaternionf) quaternion.clone();
+ } catch (CloneNotSupportedException e) {
+ return null;
+ }
+ }));
}
/**
diff --git a/src/main/java/org/skriptlang/skript/bukkit/misc/effects/EffRotate.java b/src/main/java/org/skriptlang/skript/bukkit/misc/effects/EffRotate.java
index 14ed2fc1737..7a560456640 100644
--- a/src/main/java/org/skriptlang/skript/bukkit/misc/effects/EffRotate.java
+++ b/src/main/java/org/skriptlang/skript/bukkit/misc/effects/EffRotate.java
@@ -50,9 +50,9 @@ public class EffRotate extends Effect {
static {
Skript.registerEffect(EffRotate.class,
- "rotate %vectors/quaternions/displays% around [the] [global] (:x|:y|:z)(-| )axis by %number% [degrees]",
- "rotate %quaternions/displays% around [the|its|their] local (:x|:y|:z)(-| )ax(i|e)s by %number% [degrees]",
- "rotate %vectors/quaternions/displays% around [the] %vector% by %number% [degrees]",
+ "rotate %vectors/quaternions/displays% around [the] [global] (:x|:y|:z)(-| )axis by %number%",
+ "rotate %quaternions/displays% around [the|its|their] local (:x|:y|:z)(-| )ax(i|e)s by %number%",
+ "rotate %vectors/quaternions/displays% around [the] %vector% by %number%",
"rotate %quaternions/displays% by x %number%, y %number%(, [and]| and) z %number%"
);
}
From ad27be881fd1f957ecb6ad7d4080fcc7f459b9df Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Sun, 13 Oct 2024 16:44:47 +0200
Subject: [PATCH 19/26] most "regular" regex
---
src/main/java/ch/njol/skript/classes/data/JavaClasses.java | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
index 16ea808a5a3..1c7277810fa 100644
--- a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
+++ b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
@@ -39,7 +39,8 @@ public class JavaClasses {
*
*/
public static final Pattern INTEGER_PATTERN =
- Pattern.compile("(?-?[0-9]+)(?: (?:in )?(?:(?rad(?:ian)?s?)|deg(?:ree)?s?))?");
+ Pattern.compile("(?-?\\d+(_\\d+)*)" +
+ "(?: (?:in )?(?:(?rad(?:ian)?s?)|deg(?:ree)?s?))?");
/**
* Matches a decimal number with an optional unit of radians or degrees.
@@ -53,7 +54,8 @@ public class JavaClasses {
*
*/
public static final Pattern DECIMAL_PATTERN =
- Pattern.compile("(?-?[0-9]+(?>\\.[0-9]+)?%?)(?: (?:in )?(?:(?rad(?:ian)?s?)|deg(?:ree)?s?))?");
+ Pattern.compile("(?-?\\d+(_\\d+)*(?>\\.\\d+(_\\d+)*)?%?)" +
+ "(?: (?:in )?(?:(?rad(?:ian)?s?)|deg(?:ree)?s?))?");
static {
Classes.registerClass(new ClassInfo<>(Object.class, "object")
From 38543caf2876dedb9f92345d1ada747c87abed1b Mon Sep 17 00:00:00 2001
From: Efnilite <35348263+Efnilite@users.noreply.github.com>
Date: Thu, 17 Oct 2024 17:10:31 +0200
Subject: [PATCH 20/26] fix tests
---
.../njol/skript/classes/data/JavaClasses.java | 22 +++++++++++++++++--
.../ch/njol/skript/effects/EffContinue.java | 2 +-
.../java/ch/njol/skript/effects/EffExit.java | 3 +--
3 files changed, 22 insertions(+), 5 deletions(-)
diff --git a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
index 1c7277810fa..0c9f2a142a3 100644
--- a/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
+++ b/src/main/java/ch/njol/skript/classes/data/JavaClasses.java
@@ -27,6 +27,14 @@ public class JavaClasses {
public static final int VARIABLENAME_NUMBERACCURACY = 8;
+ /**
+ * The format of an integer.
+ *
+ * Has an optional negative sign and may contain one underscores followed by any number of digits.
+ *
+ */
+ public static final String INTEGER_NUMBER_PATTERN = "-?\\d+(_\\d+)*";
+
/**
* Matches an integer with an optional unit of radians or degrees.
*
@@ -39,9 +47,19 @@ public class JavaClasses {
*
*/
public static final Pattern INTEGER_PATTERN =
- Pattern.compile("(?-?\\d+(_\\d+)*)" +
+ Pattern.compile("(?" + INTEGER_NUMBER_PATTERN + ")" +
"(?: (?:in )?(?:(?rad(?:ian)?s?)|deg(?:ree)?s?))?");
+ /**
+ * The format of a decimal number.
+ *
+ * Has an optional negative sign and may contain one underscores followed by any number of digits,
+ * in the whole part or the fractional part. The fractional part is optional. May be followed by a percentage sign,
+ * to indicate that the number is a percentage.
+ *
+ */
+ public static final String DECIMAL_NUMBER_PATTERN = "-?\\d+(_\\d+)*(?>\\.\\d+(_\\d+)*)?%?";
+
/**
* Matches a decimal number with an optional unit of radians or degrees.
*