-
-
Notifications
You must be signed in to change notification settings - Fork 451
Fix ExprTime IllegalArgumentException #8795
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
Open
erenkarakal
wants to merge
3
commits into
SkriptLang:dev/patch
Choose a base branch
from
erenkarakal:patch/fix-expr-time
base: dev/patch
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |||||
| import ch.njol.skript.util.Time; | ||||||
| import ch.njol.skript.util.Timeperiod; | ||||||
| import ch.njol.skript.util.Timespan; | ||||||
| import ch.njol.skript.util.Timespan.TimePeriod; | ||||||
| import ch.njol.util.Kleenean; | ||||||
| import ch.njol.util.coll.CollectionUtils; | ||||||
| import org.bukkit.World; | ||||||
|
|
@@ -35,12 +36,14 @@ | |||||
| public class ExprTime extends PropertyExpression<World, Time> { | ||||||
|
|
||||||
| // 18000 is the offset to allow for using "add 2:00" without going to a new day | ||||||
| // and causing unexpected behaviour | ||||||
| // and causing unexpected behavior | ||||||
| private static final int TIME_TO_TIMESPAN_OFFSET = 18000; | ||||||
|
|
||||||
| static { | ||||||
| Skript.registerExpression(ExprTime.class, Time.class, ExpressionType.PROPERTY, | ||||||
| "[the] time[s] [([with]in|of) %worlds%]", "%worlds%'[s] time[s]"); | ||||||
| "[the] time[s] [([with]in|of) %worlds%]", | ||||||
| "%worlds%'[s] time[s]" | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| @SuppressWarnings("unchecked") | ||||||
|
|
@@ -57,54 +60,49 @@ protected Time[] get(Event event, World[] worlds) { | |||||
|
|
||||||
| @Override | ||||||
| @Nullable | ||||||
| public Class<?>[] acceptChange(final ChangeMode mode) { | ||||||
| switch (mode) { | ||||||
| case ADD: | ||||||
| case REMOVE: | ||||||
| public Class<?>[] acceptChange(ChangeMode mode) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| return switch (mode) { | ||||||
| case ADD, REMOVE -> | ||||||
| // allow time to avoid conversion to timespan, which causes all sorts of headaches | ||||||
| return CollectionUtils.array(Time.class, Timespan.class); | ||||||
| case SET: | ||||||
| return CollectionUtils.array(Time.class, Timeperiod.class); | ||||||
| default: | ||||||
| return null; | ||||||
| } | ||||||
| CollectionUtils.array(Time.class, Timespan.class); | ||||||
| case SET -> CollectionUtils.array(Time.class, Timeperiod.class); | ||||||
| default -> null; | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { | ||||||
| if (delta == null) | ||||||
| return; | ||||||
|
|
||||||
| Object time = delta[0]; | ||||||
| if (time == null) | ||||||
| Object t = delta[0]; | ||||||
| if (t == null) | ||||||
| return; | ||||||
|
|
||||||
| World[] worlds = getExpr().getArray(event); | ||||||
|
|
||||||
| long ticks = 0; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would just assign this using a switch expression |
||||||
| if (time instanceof Time) { | ||||||
| if (mode != ChangeMode.SET) { | ||||||
| ticks = ((Time) time).getTicks() - TIME_TO_TIMESPAN_OFFSET; | ||||||
| } else { | ||||||
| ticks = ((Time) time).getTicks(); | ||||||
| switch (t) { | ||||||
| case Time time -> { | ||||||
| if (mode != ChangeMode.SET) { | ||||||
| ticks = time.getTicks() - TIME_TO_TIMESPAN_OFFSET; | ||||||
| } else { | ||||||
| ticks = time.getTicks(); | ||||||
| } | ||||||
| } | ||||||
| } else if (time instanceof Timespan) { | ||||||
| ticks = ((Timespan) time).getAs(Timespan.TimePeriod.TICK); | ||||||
| } else if (time instanceof Timeperiod) { | ||||||
| ticks = ((Timeperiod) time).start; | ||||||
| case Timespan timespan -> ticks = timespan.getAs(TimePeriod.TICK); | ||||||
| case Timeperiod timeperiod -> ticks = timeperiod.start; | ||||||
| default -> {} | ||||||
| } | ||||||
|
|
||||||
| for (World world : worlds) { | ||||||
| if (world.getFullTime() == 0) | ||||||
| continue; // the world doesn't have a world clock, can't modify time | ||||||
|
|
||||||
| switch (mode) { | ||||||
| case ADD: | ||||||
| world.setTime(world.getTime() + ticks); | ||||||
| break; | ||||||
| case REMOVE: | ||||||
| world.setTime(world.getTime() - ticks); | ||||||
| break; | ||||||
| case SET: | ||||||
| world.setTime(ticks); | ||||||
| break; | ||||||
| case ADD -> world.setTime(world.getTime() + ticks); | ||||||
| case REMOVE -> world.setTime(world.getTime() - ticks); | ||||||
| case SET -> world.setTime(ticks); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.