Skip to content

Commit

Permalink
Handle invalid amounts in ExprRandomCharacter (#6502)
Browse files Browse the repository at this point in the history
* protect against bad amounts

* Update ExprCharacters.sk

.%

* switch to Integer instead of Number and let chaos reign

---------

Co-authored-by: Moderocky <admin@moderocky.com>
  • Loading branch information
sovdeeth and Moderocky committed Mar 27, 2024
1 parent 7a79d87 commit 86658cf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
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

0 comments on commit 86658cf

Please sign in to comment.