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

⚒️ Fix ExprFormatTime CCE + Support non-literals #4664

Merged
merged 23 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b73cf7c
⚒️ Fix ExprFormatTime CCE
AyhamAl-Ali Mar 15, 2022
83f380b
:)
AyhamAl-Ali Mar 15, 2022
afa6453
Support variables in custom format + fix CCE
AyhamAl-Ali Mar 15, 2022
14788cc
Revert protected get
AyhamAl-Ali Mar 15, 2022
50f8cf7
Make it return null of pattern in invalid
AyhamAl-Ali Mar 16, 2022
eda6628
Indentation
AyhamAl-Ali Mar 16, 2022
51bdb85
Add test script
AyhamAl-Ali Mar 16, 2022
01c60c6
Change test script name
AyhamAl-Ali Mar 16, 2022
df07e9d
Update test script
AyhamAl-Ali Mar 16, 2022
57cc0f5
Changes
AyhamAl-Ali Mar 16, 2022
9894e2d
Apply suggestions from code review
AyhamAl-Ali Apr 1, 2022
258c67a
Merge branch 'master' into fix/formatted-date-cce
AyhamAl-Ali May 2, 2022
930e808
Requested Changes
AyhamAl-Ali Jun 24, 2022
8beef74
Merge remote-tracking branch 'AyhamAl-Ali/fix/formatted-date-cce' int…
AyhamAl-Ali Jun 24, 2022
73ddfb8
Merge branch 'master' into fix/formatted-date-cce
AyhamAl-Ali Jun 24, 2022
ad3d678
Update src/main/java/ch/njol/skript/expressions/ExprFormatDate.java
AyhamAl-Ali Jul 15, 2022
37761fe
Improve code & test script
AyhamAl-Ali Jul 16, 2022
de4f9e2
Improvements
AyhamAl-Ali Jul 16, 2022
3903ae4
Merge branch 'master' into fix/formatted-date-cce
AyhamAl-Ali Jul 16, 2022
63aaf5f
Update src/main/java/ch/njol/skript/expressions/ExprFormatDate.java
AyhamAl-Ali Jul 17, 2022
8fa9a4b
Fix build
AyhamAl-Ali Jul 17, 2022
1cf3dc0
Merge branch 'master' into fix/formatted-date-cce
TPGamesNL Jul 18, 2022
9adda7b
Merge branch 'master' into fix/formatted-date-cce
TPGamesNL Jul 18, 2022
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
56 changes: 36 additions & 20 deletions src/main/java/ch/njol/skript/expressions/ExprFormatTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.text.SimpleDateFormat;

import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.VariableString;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -31,18 +33,23 @@
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.Date;
import ch.njol.skript.util.Getter;
import ch.njol.util.Kleenean;

@Name("Formatted Time")
@Description("Converts date to human-readable text format. By default, 'yyyy-MM-dd HH:mm:ss z' (e.g. '2018-03-30 16:03:12 +01') will be used. For reference, see this "
+ "<a href=\"https://en.wikipedia.org/wiki/ISO_8601\">Wikipedia article</a>.")
@Examples("now formatted human-readable")
@Since("2.2-dev31")
@Description({
"Converts date to human-readable text format. By default, 'yyyy-MM-dd HH:mm:ss z' (e.g. '2018-03-30 16:03:12 +01') will be used. For reference, see this "
+ "<a href=\"https://en.wikipedia.org/wiki/ISO_8601\">Wikipedia article</a>."
})
@Examples({
"command /date:",
"\ttrigger:",
"\t\tsend \"Full date: %now formatted human-readable%\" to sender",
"\t\tsend \"Short date: %now formatted as \"\"yyyy-MM-dd\"\"%\" to sender"
})
@Since("2.2-dev31, INSERT VERSION (support variables in format)")
public class ExprFormatTime extends PropertyExpression<Date, String> {

private static final String defaultFormat = "yyyy-MM-dd HH:mm:ss z";
Expand All @@ -51,35 +58,44 @@ public class ExprFormatTime extends PropertyExpression<Date, String> {
Skript.registerExpression(ExprFormatTime.class, String.class, ExpressionType.PROPERTY, "%dates% formatted [human-readable] [(with|as) %-string%]");
}

@SuppressWarnings("null")
@SuppressWarnings("NotNullFieldNotInitialized")
private SimpleDateFormat format;

@SuppressWarnings("NotNullFieldNotInitialized")
private Expression<? extends String> customFormat;

@Override
@SuppressWarnings("null")
@SuppressWarnings({"null", "unchecked"})
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
setExpr((Expression<? extends Date>) exprs[0]);
if (exprs[1] != null) {
if (!(exprs[1] instanceof Literal)) {
VariableString str = (VariableString) exprs[1];
if (!str.isSimple()) {
Skript.error("Date format must not contain variables!");
return false;
}
customFormat = (Expression<? extends String>) exprs[1];

if (customFormat instanceof Literal || (customFormat instanceof VariableString && ((VariableString) customFormat).isSimple())) {
try {
format = new SimpleDateFormat(customFormat.getSingle(null));
} catch (Exception e) {
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
Skript.error("Incorrect date format used: " + e.getMessage());
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
format = new SimpleDateFormat((String) exprs[1].getSingle(null));
} else {
} else if (customFormat == null) {
format = new SimpleDateFormat(defaultFormat);
}

return true;
}


@Override
protected String[] get(Event e, Date[] source) {
return get(source, new Getter<String, Date>() {
@Override
public String get(Date date) {
if (format == null) {
try {
format = new SimpleDateFormat(customFormat.getSingle(e));
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception ex) {
return null;
}
}
return format.format(new java.util.Date(date.getTimestamp()));
}
});
Expand All @@ -92,7 +108,7 @@ public Class<? extends String> getReturnType() {

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

}
20 changes: 20 additions & 0 deletions src/test/skript/tests/regressions/4664-formatted time.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
test "formatted time":
set {_default} to "yyyy-MM-dd HH:mm:ss z"
set {_now} to now
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved

set {_date1} to {_now} formatted
assert {_date1} = {_now} formatted as {_default} with "default date format failed ##1"

set {_date2} to {_now} formatted human-readable
assert {_date2} = {_now} formatted as {_default} with "default date format failed ##2"

set {_date3} to now formatted as "HH:mm"
assert length of {_date3} = 5 with "custom date format failed ##1"

set {_cFormat} to "hh:mm"
set {_date4} to now formatted as {_cFormat}
assert length of {_date4} = 5 with "custom date format failed ##2"

set {_cFormat2} to "i"
set {_date5} to now formatted as {_cFormat2}
assert {_date5} is not set with "custom date format failed ##3"