Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework number parser and angle support #7139

Open
wants to merge 39 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
1001e4f
init commit
Efnilite Oct 9, 2024
f461b24
oops
Efnilite Oct 9, 2024
b3849ce
Merge branch 'dev/feature' into angle
Efnilite Oct 9, 2024
9998616
removed unnecessary checks
Efnilite Oct 9, 2024
efbb1af
Merge remote-tracking branch 'origin/angle' into angle
Efnilite Oct 9, 2024
52d028a
update other syntaxes
Efnilite Oct 9, 2024
b4a1f37
move to parser
Efnilite Oct 9, 2024
cef8b87
oops
Efnilite Oct 9, 2024
c5d5c25
big work
Efnilite Oct 10, 2024
b378a30
forgot !
Efnilite Oct 10, 2024
036adf3
fix doubleValue call
Efnilite Oct 10, 2024
491c7cf
fix parenthesis
Efnilite Oct 11, 2024
fcd2c60
add expression
Efnilite Oct 11, 2024
46b507e
update tests
Efnilite Oct 12, 2024
8d552b9
requested changes
Efnilite Oct 12, 2024
27d0c78
oops
Efnilite Oct 12, 2024
6ee87f1
remove unnecessary converter application
Efnilite Oct 12, 2024
79ce823
remove converter
Efnilite Oct 12, 2024
87b9e84
le fix
Efnilite Oct 13, 2024
a0212de
Merge branch 'dev/feature' into angle
Efnilite Oct 13, 2024
18bf9c7
fix merge w/ display entities
Efnilite Oct 13, 2024
ad27be8
most "regular" regex
Efnilite Oct 13, 2024
cfd09ce
Merge branch 'dev/feature' into angle
sovdeeth Oct 15, 2024
38543ca
fix tests
Efnilite Oct 17, 2024
983e376
fix _ support
Efnilite Oct 17, 2024
a6b27bc
fix rotate
Efnilite Oct 17, 2024
e392b57
Merge branch 'dev/feature' into angle
Efnilite Oct 26, 2024
8d0e8c5
Merge branch 'dev/feature' into angle
Efnilite Oct 28, 2024
686ddc3
minor changes
Efnilite Oct 28, 2024
b5168cc
remove double return
Efnilite Oct 28, 2024
6707d0d
fix ExprRotate
Efnilite Oct 28, 2024
f9afddb
Merge branch 'dev/feature' into angle
Efnilite Nov 1, 2024
e4cd70b
Merge branch 'dev/feature' into angle
Efnilite Nov 5, 2024
d8daf93
Merge branch 'dev/feature' into angle
sovdeeth Nov 8, 2024
656e4ad
Merge branch 'dev/feature' into angle
Efnilite Nov 10, 2024
a9402b5
Merge branch 'dev/feature' into angle
Efnilite Nov 14, 2024
dacf629
oopsie
Efnilite Nov 20, 2024
ae2765a
Merge branch 'dev/feature' into angle
Moderocky Nov 30, 2024
28742ef
Merge branch 'dev/feature' into angle
Efnilite Dec 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,055 changes: 588 additions & 467 deletions src/main/java/ch/njol/skript/classes/data/JavaClasses.java

Large diffs are not rendered by default.

20 changes: 1 addition & 19 deletions src/main/java/ch/njol/skript/effects/EffContinue.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.effects;

import ch.njol.skript.Skript;
Expand Down Expand Up @@ -60,7 +42,7 @@ public class EffContinue extends Effect {
static {
Skript.registerEffect(EffContinue.class,
"continue [this loop|[the] [current] loop]",
"continue [the] <" + JavaClasses.INTEGER_PATTERN + ">(st|nd|rd|th) loop"
"continue [the] <" + JavaClasses.INTEGER_NUMBER_PATTERN + ">(st|nd|rd|th) loop"
);
}

Expand Down
29 changes: 5 additions & 24 deletions src/main/java/ch/njol/skript/effects/EffExit.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.effects;

import ch.njol.skript.Skript;
Expand All @@ -33,19 +15,18 @@
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability;

import java.util.ArrayList;
import java.util.List;

@Name("Exit")
@Description("Exits a given amount of loops and conditionals, or the entire trigger.")
@Examples({
"if player has any ore:",
"\tstop",
"\tstop",
"message \"%player% has no ores!\"",
"loop blocks above the player:",
"\tloop-block is not air:",
"\t\texit 2 sections",
"\tset loop-block to water"
"\tloop-block is not air:",
"\t\texit 2 sections",
"\tset loop-block to water"
})
@Since("<i>unknown</i> (before 2.1)")
public class EffExit extends Effect {
Expand All @@ -54,7 +35,7 @@ public class EffExit extends Effect {
Skript.registerEffect(EffExit.class,
"(exit|stop) [trigger]",
"(exit|stop) [1|a|the|this] (section|1:loop|2:conditional)",
"(exit|stop) <" + JavaClasses.INTEGER_PATTERN + "> (section|1:loop|2:conditional)s",
"(exit|stop) <" + JavaClasses.INTEGER_NUMBER_PATTERN + "> (section|1:loop|2:conditional)s",
"(exit|stop) all (section|1:loop|2:conditional)s");
}

Expand Down
77 changes: 77 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprAngle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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<Number> {

static {
Skript.registerExpression(ExprAngle.class, Number.class, ExpressionType.SIMPLE,
"%number% [in] deg[ree][s]",
"%number% [in] rad[ian][s]",
"%numbers% in deg[ree][s]",
"%numbers% in rad[ian][s]");
Comment on lines +32 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to support gradians (grad[s]/gon) while you're here?
They're pretty obscure nowadays so I don't mind, but you do occasionally see them used in map stuff and cartography.

}

private Expression<Number> angle;
private boolean isRadians;

@Override
public boolean init(Expression<?>[] expressions, int matchedPattern,
Kleenean isDelayed, ParseResult parseResult) {
//noinspection unchecked
angle = (Expression<Number>) expressions[0];
isRadians = matchedPattern % 2 != 0;
return true;
}

@Override
protected Number @Nullable [] get(Event event) {
Number[] numbers = angle.getArray(event);

if (isRadians)
for (int i = 0; i < numbers.length; i++)
numbers[i] = Math.toDegrees(numbers[i].doubleValue());

return numbers;
}

@Override
public boolean isSingle() {
return angle.isSingle();
}

@Override
public Class<? extends Number> getReturnType() {
return Number.class;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return angle.toString(event, debug) + " in " + (isRadians ? "degrees" : "radians");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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%"
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public class ExprRotate extends SimpleExpression<Object> {

static {
Skript.registerExpression(ExprRotate.class, Object.class, ExpressionType.SIMPLE,
"%quaternions/vectors% rotated around [the] [global] (:x|:y|:z)(-| )axis by %number% [degrees]",
"%quaternions% rotated around [the|its|their] local (:x|:y|:z)(-| )ax(i|e)s by %number% [degrees]",
"%quaternions/vectors% rotated around [the] %vector% by %number% [degrees]",
"%quaternions/vectors% rotated around [the] [global] (:x|:y|:z)(-| )axis by %number%",
"%quaternions% rotated around [the|its|their] local (:x|:y|:z)(-| )ax(i|e)s by %number%",
"%quaternions/vectors% rotated around [the] %vector% by %number%",
"%quaternions% rotated by x %number%, y %number%(, [and]| and) z %number%");
}

Expand Down Expand Up @@ -157,6 +157,11 @@ public Class<?> getReturnType() {
return (matchedPattern == 1 || matchedPattern == 3) ? Quaternionf.class : toRotate.getReturnType();
}

@Override
public Class<?>[] possibleReturnTypes() {
return new Class<?>[]{Quaternionf.class, Vector.class};
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return switch (matchedPattern) {
Expand Down
33 changes: 33 additions & 0 deletions src/test/skript/tests/misc/angle.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
test "angle":
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
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 in radians is 360.0 degrees with "2pi radians is not 360 degrees"

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 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"
Loading