-
-
Notifications
You must be signed in to change notification settings - Fork 400
EvtRealTime #7332
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
Merged
sovdeeth
merged 24 commits into
SkriptLang:dev/feature
from
Absolutionism:dev/EvtServerTime
Mar 16, 2025
Merged
EvtRealTime #7332
Changes from 10 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
a1d003c
Initial Commit
Absolutionism d898019
Merge remote-tracking branch 'upstream/dev/feature' into dev/EvtServe…
Absolutionism f38b233
Testing
Absolutionism c6d9616
More Testing
Absolutionism 5ea7554
Working State
Absolutionism 3494f0f
Notes
Absolutionism 450cd50
Merge remote-tracking branch 'upstream/dev/feature' into dev/EvtServe…
Absolutionism c96eae4
Timer Usage
Absolutionism bce37b2
Multiple Times
Absolutionism e19a23d
Fix Time Parser
Absolutionism fc3434d
Partial Changes
Absolutionism 85ead18
Changed Name+Syntax
Absolutionism c0fb0f0
Whoops
Absolutionism 2a52bb9
Merge branch 'dev/feature' into dev/EvtServerTime
Absolutionism ebd4580
Left over outdated syntax
Absolutionism 9f43ff9
Requested Changes
Absolutionism 1878737
var name
Absolutionism af1bde1
Merge branch 'dev/feature' into dev/EvtServerTime
Absolutionism 6d084c9
Requested Changes
Absolutionism 20d8a96
Merge branch 'dev/feature' into dev/EvtServerTime
Absolutionism 013590e
Requested Changes
Absolutionism 78771f2
scheduleAtFixedRate
Absolutionism 2465968
Combine
Absolutionism b28e595
Merge branch 'dev/feature' into dev/EvtServerTime
sovdeeth 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 |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package ch.njol.skript.events; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.SkriptEventHandler; | ||
import ch.njol.skript.lang.Literal; | ||
import ch.njol.skript.lang.SkriptEvent; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import ch.njol.skript.util.Time; | ||
import org.bukkit.event.Event; | ||
import org.bukkit.event.HandlerList; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.*; | ||
|
||
public class EvtServerTime extends SkriptEvent { | ||
|
||
public static class ServerTimeEvent extends Event { | ||
@Override | ||
public @NotNull HandlerList getHandlers() { | ||
throw new IllegalStateException(); | ||
} | ||
} | ||
|
||
private static final long HOUR_12_MILLISECONDS = 43200000; | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private static final long HOUR_24_MILLISECONDS = HOUR_12_MILLISECONDS * 2; | ||
private static final Timer timer; | ||
|
||
static { | ||
Skript.registerEvent("Server Time", EvtServerTime.class, ServerTimeEvent.class, | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"(server|real) time (of|at) %times%") | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.description("Called when the local time of the server reaches the provided time.") | ||
.examples( | ||
"on server time of 14:20:", | ||
"on real time at 2:30am:", | ||
"on server time at 6:10 pm:", | ||
"on real time of 5:00 am and 5:00 pm:", | ||
"on server time of 5:00 and 17:00:" | ||
) | ||
.since("INSERT VERSION"); | ||
|
||
timer = new Timer("EvtServerTime-Tasks"); | ||
} | ||
|
||
private Literal<Time> times; | ||
private boolean unloaded = false; | ||
private List<ServerTimeInfo> infoList = new ArrayList<>(); | ||
|
||
@Override | ||
public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResult) { | ||
//noinspection unchecked | ||
times = (Literal<Time>) args[0]; | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean postLoad() { | ||
Calendar currentCalendar = Calendar.getInstance(); | ||
currentCalendar.setTimeZone(TimeZone.getDefault()); | ||
for (Time time : times.getArray()) { | ||
Calendar expectedCalendar = Calendar.getInstance(); | ||
expectedCalendar.setTimeZone(TimeZone.getDefault()); | ||
expectedCalendar.set(Calendar.MINUTE, time.getMinute()); | ||
expectedCalendar.set(Calendar.SECOND, 0); | ||
expectedCalendar.set(Calendar.MILLISECOND, 0); | ||
expectedCalendar.set(Calendar.HOUR_OF_DAY, time.getHour()); | ||
// Ensure the execution time is in the future and not the past | ||
while (expectedCalendar.before(currentCalendar)) { | ||
expectedCalendar.add(Calendar.HOUR_OF_DAY, 24); | ||
} | ||
ServerTimeInfo info = new ServerTimeInfo(time, expectedCalendar.getTimeInMillis()); | ||
infoList.add(info); | ||
createNewTask(info); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public void unload() { | ||
unloaded = true; | ||
for (ServerTimeInfo info : infoList) { | ||
if (info.task != null) | ||
info.task.cancel(); | ||
} | ||
timer.purge(); | ||
} | ||
|
||
@Override | ||
public boolean check(Event event) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean isEventPrioritySupported() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return "server time of " + times.toString(event, debug); | ||
} | ||
|
||
private void execute() { | ||
ServerTimeEvent event = new ServerTimeEvent(); | ||
SkriptEventHandler.logEventStart(event); | ||
SkriptEventHandler.logTriggerStart(trigger); | ||
trigger.execute(event); | ||
SkriptEventHandler.logTriggerEnd(trigger); | ||
SkriptEventHandler.logEventEnd(); | ||
} | ||
|
||
private void preExecute(ServerTimeInfo info) { | ||
// Safety check, ensure this 'EvtServerTime' was not unloaded | ||
if (unloaded) | ||
return; | ||
// Bump the next execution time by the appropriate amount | ||
info.executionTime += HOUR_24_MILLISECONDS; | ||
// Reschedule task for new executionTime | ||
createNewTask(info); | ||
// Activate trigger | ||
execute(); | ||
} | ||
|
||
private void createNewTask(ServerTimeInfo info) { | ||
TimerTask task = new TimerTask() { | ||
@Override | ||
public void run() { | ||
preExecute(info); | ||
} | ||
}; | ||
info.task = task; | ||
timer.schedule(task, new Date(info.executionTime)); | ||
} | ||
|
||
private static class ServerTimeInfo { | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private long executionTime; | ||
private final Time time; | ||
private TimerTask task; | ||
|
||
public ServerTimeInfo(Time time, long executionTime) { | ||
this.time = time; | ||
this.executionTime = executionTime; | ||
} | ||
|
||
} | ||
|
||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.