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

Update ExprRound Allowing Multiple Numbers + Patterns #7552

Open
wants to merge 6 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
81 changes: 52 additions & 29 deletions src/main/java/ch/njol/skript/expressions/ExprRound.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package ch.njol.skript.expressions;

import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

import ch.njol.skript.Skript;
import org.skriptlang.skript.lang.converter.Converter;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
Expand All @@ -13,44 +9,64 @@
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.SyntaxStringBuilder;
import ch.njol.skript.util.Patterns;
import ch.njol.util.Kleenean;
import ch.njol.util.Math2;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

/**
* @author Peter Güttinger
*/
@Name("Rounding")
@Description("Rounds numbers normally, up (ceiling) or down (floor) respectively.")
@Examples({"set {var} to rounded health of player",
"set line 1 of the block to rounded \"%(1.5 * player's level)%\"",
"add rounded down argument to the player's health"})
@Examples({
"set {var} to rounded health of player",
"set line 1 of the block to rounded \"%(1.5 * player's level)%\"",
"add rounded down argument to the player's health"
})
@Since("2.0")
public class ExprRound extends PropertyExpression<Number, Long> {

private enum RoundType {
FLOOR, ROUND, CEIL
}

private final static Patterns<RoundType> patterns = new Patterns<>(new Object[][]{
{"[a|the] (round[ed] down|floored) %numbers%", RoundType.FLOOR},
{"%numbers% (round[ed] down|floored)", RoundType.FLOOR},
{"[a|the] round[ed] %numbers%", RoundType.ROUND},
{"%numbers% round[ed]", RoundType.ROUND},
{"[a|the] (round[ed] up|ceil[ing]ed) %numbers%", RoundType.CEIL},
{"%numbers% (round[ed] up|ceil[ing]ed)", RoundType.CEIL}
});

static {
Skript.registerExpression(ExprRound.class, Long.class, ExpressionType.PROPERTY,
"(a|the|) round[ed] down %number%",
"(a|the|) round[ed] %number%",
"(a|the|) round[ed] up %number%");
Skript.registerExpression(ExprRound.class, Long.class, ExpressionType.PROPERTY, patterns.getPatterns());
}

int action;

@SuppressWarnings({"unchecked", "null"})

private RoundType roundType;

@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
//noinspection unchecked
setExpr((Expression<? extends Number>) exprs[0]);
action = matchedPattern - 1;
roundType = patterns.getInfo(matchedPattern);
return true;
}

@Override
protected Long[] get(final Event e, final Number[] source) {
return get(source, n -> {
if (n instanceof Integer)
return n.longValue();
else if (n instanceof Long)
return (Long) n;
return action == -1 ? Math2.floor(n.doubleValue()) : action == 0 ? Math2.round(n.doubleValue()) : Math2.ceil(n.doubleValue());
protected Long @Nullable [] get(Event event, Number[] source) {
return get(source, number -> {
if (number instanceof Integer integer) {
return integer.longValue();
} else if (number instanceof Long long1) {
return long1;
}

return switch (roundType) {
case FLOOR -> Math2.floor(number.doubleValue());
case ROUND -> Math2.round(number.doubleValue());
case CEIL -> Math2.ceil(number.doubleValue());
};
});
}

Expand All @@ -60,8 +76,15 @@ public Class<? extends Long> getReturnType() {
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
return (action == -1 ? "floor" : action == 0 ? "round" : "ceil") + "(" + getExpr().toString(e, debug) + ")";
public String toString(@Nullable Event event, boolean debug) {
SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug);
builder.append(switch (roundType) {
case FLOOR -> "floored";
case ROUND -> "rounded";
case CEIL -> "ceiled";
});
builder.append(getExpr());
return builder.toString();
}

}
15 changes: 15 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprRound.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test "rounding":
assert floored 4.6 is 4 with "Floored 4.6 failed"
assert rounded 4.6 is 5 with "Rounded 4.6 failed"
assert rounded 4.4 is 4 with "Rounded 4.4 failed"
assert ceiled 4.4 is 5 with "Ceiled 4.4 failed"

assert 10.6 floored is 10 with "Floored 10.6 failed"
assert 10.6 rounded is 11 with "Rounded 10.6 failed"
assert 10.4 rounded is 10 with "Rounded 10.4 failed"
assert 10.4 ceiled is 11 with "Ceiled 10.4 failed"

set {_nums::*} to 67.3, 98.5, 105.7, 300.1 and 500
assert floored {_nums::*} is (67, 98, 105, 300 and 500) with "Failed to floor multiple numbers"
assert rounded {_nums::*} is (67, 99, 106, 300 and 500) with "Failed to round multiple numbers"
assert ceiled {_nums::*} is (68, 99, 106, 301 and 500) with "Failed to ceil multiple numbers"