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

Handle invalid amounts in ExprRandomCharacter #6502

Merged
merged 4 commits into from
Mar 27, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ public class ExprRandomCharacter extends SimpleExpression<String> {

static {
Skript.registerExpression(ExprRandomCharacter.class, String.class, ExpressionType.COMBINED,
"[a|%-number%] random [:alphanumeric] character[s] (from|between) %string% (to|and) %string%");
"[a|%-integer%] random [:alphanumeric] character[s] (from|between) %string% (to|and) %string%");
}

@Nullable
private Expression<Number> amount;
private Expression<Integer> amount;
private Expression<String> from, to;
private boolean isAlphanumeric;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
amount = (Expression<Number>) exprs[0];
amount = (Expression<Integer>) exprs[0];
from = (Expression<String>) exprs[1];
to = (Expression<String>) exprs[2];
isAlphanumeric = parseResult.hasTag("alphanumeric");
Expand All @@ -70,7 +70,10 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
@Override
@Nullable
protected String[] get(Event event) {
int amount = this.amount == null ? 1 : this.amount.getOptionalSingle(event).orElse(1).intValue();
Integer amount = this.amount == null ? Integer.valueOf(1) : this.amount.getSingle(event);
if (amount == null || amount <= 0)
return new String[0];

String from = this.from.getSingle(event);
String to = this.to.getSingle(event);
if (from == null || to == null)
Expand Down Expand Up @@ -117,7 +120,7 @@ protected String[] get(Event event) {
@Override
public boolean isSingle() {
if (amount instanceof Literal)
return ((Literal<Number>) amount).getSingle().intValue() == 1;
return ((Literal<Integer>) amount).getSingle() == 1;
return amount == null;
}

Expand Down
8 changes: 8 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprCharacters.sk
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
test "characters between":
set {_a} to 1 random character between "a" and "z"
assert {_a} is set with "failed to generate random character"

set {_a::*} to -10 random character between "a" and "z"
assert {_a::*} is not set with "-10 random characters returned non-null value: %{_a::*}%"

set {_a::*} to {_null} random character between "a" and "z"
assert {_a::*} is not set with "null random characters returned non-null value: %{_a::*}%"

assert join characters between "a" and "d" is "abcd" with "Invalid characters between a and d"
assert join characters between "d" and "a" is "dcba" with "Invalid characters between d and a"
assert join characters between "Z" and "a" is "Z[\]^_`a" with "Invalid characters between Z and a"
Expand Down
Loading