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

Create clamp expression #6567

Open
wants to merge 13 commits into
base: dev/feature
Choose a base branch
from
25 changes: 12 additions & 13 deletions src/main/java/ch/njol/skript/expressions/ExprClamp.java
Expand Up @@ -25,7 +25,7 @@
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
Expand All @@ -34,28 +34,28 @@
@Name("Clamp")
@Description("Clamps one or more values between two numbers.")
@Examples({
"clamp 5 between 0 and 10 # result = 5",
"clamp 5.5 between 0 and 5 # result = 5",
"clamp 0.25 between 0 and 0.5 # result = 0.25",
"clamp 5 between 7 and 10 # result = 7",
"clamp (5, 0, 10, 9, 13) between 7 and 10 # result = (7, 7, 10, 9, 10)",
"5 clamped between 0 and 10 # result = 5",
"5.5 clamped between 0 and 5 # result = 5",
"0.25 clamped between 0 and 0.5 # result = 0.25",
"5 clamped between 7 and 10 # result = 7",
"(5, 0, 10, 9, 13) clamped between 7 and 10 # result = (7, 7, 10, 9, 10)",
"",
"set {_clamped::*} to clamp {_values::*} between 0 and 10"
"set {_clamped::*} to {_values::*} clamped between 0 and 10"
})
@Since("INSERT VERSION")
public class ExprClamp extends SimpleExpression<Number> {

static {
Skript.registerExpression(ExprClamp.class, Number.class, ExpressionType.COMBINED,
"clamp %numbers% between %number% and %number%");
"%numbers% clamped between %number% and %number%");
}

private Expression<Number> values, min, max;

@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
values = (Expression<Number>) expressions[0];
min = ((Expression<Number>) expressions[1]);
min = (Expression<Number>) expressions[1];
max = (Expression<Number>) expressions[2];
return true;
}
Expand All @@ -65,8 +65,8 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is
protected Number[] get(Event event) {
Number[] numbers = values.getArray(event);
Double[] clampedValues = new Double[numbers.length];
double min = this.min.getSingle(event).doubleValue();
double max = this.max.getSingle(event).doubleValue();
double min = this.min.getSingle(event) != null ? this.min.getSingle(event).doubleValue() : 0;
double max = this.max.getSingle(event) != null ? this.max.getSingle(event).doubleValue() : 0;
Phill310 marked this conversation as resolved.
Show resolved Hide resolved
// Make sure the min and max are in the correct order
double trueMin = Math.min(min, max);
double trueMax = Math.max(min, max);
Expand All @@ -93,5 +93,4 @@ public String toString(@Nullable Event event, boolean debug) {
+ max.toString(event, debug);
}

Phill310 marked this conversation as resolved.
Show resolved Hide resolved

}