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

🚀 Add number format expression #4663

Closed
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f1a8f7f
🚀 Add number format expression
AyhamAl-Ali Mar 15, 2022
c7b37d8
Little changes
AyhamAl-Ali Mar 15, 2022
cde535c
Prevent variables usage
AyhamAl-Ali Mar 15, 2022
ee12498
Support variables as custom format
AyhamAl-Ali Mar 15, 2022
f2348ed
Make it return null if format is invalid
AyhamAl-Ali Mar 16, 2022
df572d8
Add test script
AyhamAl-Ali Mar 16, 2022
05307fd
Update test script
AyhamAl-Ali Mar 16, 2022
66f12a4
Update test script
AyhamAl-Ali Mar 16, 2022
7184c95
Changes
AyhamAl-Ali Mar 16, 2022
dc61b39
Fix class to use same technique as #4664
AyhamAl-Ali Apr 21, 2023
cc78229
Merge branch 'master' into ench/formatted-number
AyhamAl-Ali Apr 21, 2023
8654a94
Apply suggestions from code review
AyhamAl-Ali Apr 22, 2023
f30aded
Add helpful comments and use lambda
AyhamAl-Ali Apr 22, 2023
b4077e8
Update src/main/java/ch/njol/skript/expressions/ExprFormatNumber.java
AyhamAl-Ali Mar 15, 2024
25d66c1
Change expression to function
AyhamAl-Ali Apr 5, 2024
ecd4a4a
Remove unused imports
AyhamAl-Ali Apr 5, 2024
0d5dc9a
Merge branch 'dev/feature' into ench/formatted-number
AyhamAl-Ali Apr 5, 2024
4488904
Refactoring and merge base, updating examples
AyhamAl-Ali Apr 5, 2024
664ebf1
return null instead of empty string
AyhamAl-Ali Apr 5, 2024
0ce55ab
Update examples
AyhamAl-Ali Apr 5, 2024
a04f476
Warn instead of erroring, but probably better to remove
AyhamAl-Ali Apr 5, 2024
f238bc0
Address reviews
AyhamAl-Ali Apr 6, 2024
80be1e0
better variable naming
AyhamAl-Ali Apr 6, 2024
ac9a7b4
Merge branch 'dev/feature' into ench/formatted-number
Moderocky Apr 12, 2024
9cdec48
Merge branch 'dev/feature' into ench/formatted-number
Moderocky Apr 13, 2024
926ee62
Merge branch 'dev/feature' into ench/formatted-number
Moderocky May 30, 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
132 changes: 132 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprFormatNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* 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.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.expressions.base.PropertyExpression;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.VariableString;
import ch.njol.skript.util.Getter;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import java.text.DecimalFormat;
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved

@Name("Formatted Number")
@Description(
"Converts numbers to human-readable format. By default, '###,###' (e.g. '123,456,789') will be used. For reference, see this "
+ "<a href=\"https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html\" target=\"_blank\">article</a>."
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
)
@Examples({
"command /formatnumber <number>:",
"\taliases: fn",
"\ttrigger:",
"\t\tsend \"Formatted: %formatted arg-1%\" to sender"
})
@Since("INSERT VERSION")
public class ExprFormatNumber extends PropertyExpression<Number, String> {

private static final DecimalFormat DEFAULT_FORMAT = new DecimalFormat("###,###");

static {
Skript.registerExpression(ExprFormatNumber.class, String.class, ExpressionType.PROPERTY,
"%numbers% formatted [human-readable] [(with|as) %-string%]",
"[human-readable] formatted %numbers% [(with|as) %-string%]");
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
}

@SuppressWarnings("NotNullFieldNotInitialized")
private DecimalFormat format;

@Nullable
private Expression<? extends String> customFormat;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
setExpr((Expression<? extends Number>) exprs[0]);
customFormat = (Expression<? extends String>) exprs[1];

boolean isSimpleString = customFormat instanceof VariableString && ((VariableString) customFormat).isSimple();
// if literal or is simple variable string then we can get the value at init and check
if (customFormat instanceof Literal || isSimpleString) {
String customFormatValue;
if (isSimpleString) {
customFormatValue = ((VariableString) customFormat).toString(null);
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
} else {
customFormatValue = ((Literal<String>) customFormat).getSingle();
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
}

// try to use user provided format
if (customFormatValue != null) {
try {
format = new DecimalFormat(customFormatValue);
} catch (IllegalArgumentException e) {
Skript.error("Invalid number format: " + customFormatValue);
return false;
}
}
// if not set then assign default format
} else if (customFormat == null) {
format = DEFAULT_FORMAT;
}

return true;
}

@Override
protected String[] get(Event event, Number[] source) {
DecimalFormat format;
String formatString;

if (customFormat != null && this.format == null) { // customFormat is not Literal or VariableString
formatString = customFormat.getSingle(event);
if (formatString == null)
return new String[0];

try {
format = new DecimalFormat(formatString);
} catch (IllegalArgumentException e) {
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
return new String[0];
}
} else {
format = this.format;
}

return get(source, format::format);
}

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

@Override
public String toString(@Nullable Event event, boolean debug) {
return getExpr().toString(event, debug) + " formatted as " + (customFormat != null ? customFormat.toString(event, debug) : DEFAULT_FORMAT.toPattern());
}

}
17 changes: 17 additions & 0 deletions src/test/skript/tests/regressions/4663-formatted numbers.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test "formatted numbers":
set {_num1} to formatted 123456789
assert {_num1} = "123,456,789" with "default number format failed ##1"

set {_num2} to 1234567 formatted human-readable
assert {_num2} = "1,234,567" with "default number format failed ##2"

set {_num3} to formatted 12345678 as "##,##.00"
assert {_num3} = "1,2,3,4,5,6,7,8.00" with "custom number format failed ##1"

set {_cFormat} to "####,####"
set {_num4} to formatted 12345678 as {_cFormat}
assert {_num4} = "12,34,56,78" with "custom number format failed ##2"

set {_cFormat2} to "##,.##"
set {_num5} to formatted 12345678 as {_cFormat2}
assert {_num5} is not set with "custom number format failed ##3"