Skip to content

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
merged 24 commits into from
Mar 16, 2025
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
144 changes: 144 additions & 0 deletions src/main/java/ch/njol/skript/events/EvtRealTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
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 EvtRealTime extends SkriptEvent {

public static class RealTimeEvent extends Event {
@Override
public @NotNull HandlerList getHandlers() {
throw new IllegalStateException();
}
}

private static final long HOUR_24_MILLISECONDS = 1000 * 60 * 60 * 24;
private static final Timer TIMER;

static {
Skript.registerEvent("System Time", EvtRealTime.class, RealTimeEvent.class,
"at %times% [in] real time")
.description("Called when the local time of the system the server is running on reaches the provided real-life time.")
.examples(
"at 14:20 in real time:",
"at 2:30am real time:",
"at 6:10 pm in real time:",
"at 5:00 am and 5:00 pm in real time:",
"at 5:00 and 17:00 in real time:"
)
.since("INSERT VERSION");

TIMER = new Timer("EvtSystemTime-Tasks");
}

private Literal<Time> times;
private boolean unloaded = false;
private final List<RealTimeInfo> 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.MILLISECOND, 0);
expectedCalendar.set(Calendar.SECOND, 0);
expectedCalendar.set(Calendar.MINUTE, time.getMinute());
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);
}
RealTimeInfo info = new RealTimeInfo(expectedCalendar.getTimeInMillis());
infoList.add(info);
createNewTask(info);
}
return true;
}

@Override
public void unload() {
unloaded = true;
for (RealTimeInfo info : infoList) {
if (info.task != null)
info.task.cancel();
}
TIMER.purge();
}

@Override
public boolean check(Event event) {
throw new UnsupportedOperationException();
}

private void execute() {
RealTimeEvent event = new RealTimeEvent();
SkriptEventHandler.logEventStart(event);
SkriptEventHandler.logTriggerStart(trigger);
trigger.execute(event);
SkriptEventHandler.logTriggerEnd(trigger);
SkriptEventHandler.logEventEnd();
}

private void preExecute(RealTimeInfo info) {
// Safety check, ensure this 'EvtRealTime' 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(RealTimeInfo info) {
TimerTask task = new TimerTask() {
@Override
public void run() {
preExecute(info);
}
};
info.task = task;
TIMER.schedule(task, new Date(info.executionTime));
}

@Override
public boolean isEventPrioritySupported() {
return false;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "at " + times.toString(event, debug) + " in real time";
}

private static class RealTimeInfo {
private long executionTime;
private TimerTask task;

public RealTimeInfo(long executionTime) {
this.executionTime = executionTime;
}

}

}
39 changes: 26 additions & 13 deletions src/main/java/ch/njol/skript/util/Time.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package ch.njol.skript.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jetbrains.annotations.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.localization.Message;
import ch.njol.util.Math2;
import ch.njol.yggdrasil.YggdrasilSerializable;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.util.Cyclical;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author Peter Güttinger
*/
public class Time implements YggdrasilSerializable, Cyclical<Integer> {

private final static int TICKS_PER_HOUR = 1000, TICKS_PER_DAY = 24 * TICKS_PER_HOUR;
private final static double TICKS_PER_MINUTE = 1000. / 60;
/**
Expand All @@ -29,10 +29,10 @@ public class Time implements YggdrasilSerializable, Cyclical<Integer> {
private static final Pattern TIME_PATTERN = Pattern.compile("\\d?\\d:\\d\\d", Pattern.CASE_INSENSITIVE);

public Time() {
time = 0;
this(0);
}

public Time(final int time) {
public Time(int time) {
this.time = Math2.mod(time, TICKS_PER_DAY);
}

Expand All @@ -49,13 +49,27 @@ public int getTicks() {
public int getTime() {
return (time + HOUR_ZERO) % TICKS_PER_DAY;
}


public int getHour() {
int thisTime = time;
if (time < 0)
thisTime = Math.abs(time);
int hour = (thisTime + HOUR_ZERO) / TICKS_PER_HOUR;
if (hour >= 24)
hour -= 24;
return hour;
}

public int getMinute() {
return (int) Math2.round(((time + HOUR_ZERO) % TICKS_PER_HOUR) / TICKS_PER_MINUTE);
}

@Override
public String toString() {
return toString(time);
}

public static String toString(final int ticks) {
public static String toString(int ticks) {
assert 0 <= ticks && ticks < TICKS_PER_DAY;
final int t = (ticks + HOUR_ZERO) % TICKS_PER_DAY;
int hours = t / TICKS_PER_HOUR;
Expand Down Expand Up @@ -122,7 +136,7 @@ public static Time parse(final String s) {

@Override
public int hashCode() {
return time;
return Objects.hash(time);
}

@Override
Expand All @@ -131,9 +145,8 @@ public boolean equals(final @Nullable Object obj) {
return true;
if (obj == null)
return false;
if (!(obj instanceof Time))
if (!(obj instanceof Time other))
return false;
final Time other = (Time) obj;
return time == other.time;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.skriptlang.skript.test.tests.utils;

import ch.njol.skript.test.runner.SkriptJUnitTest;
import ch.njol.skript.util.Time;
import org.junit.Test;

public class TimeTest extends SkriptJUnitTest {

public void testTime(String parse, int hour, int minute) {
Time time = Time.parse(parse);
if ((parse.contains("am") && hour == 12)) {
hour -= 12;
} else if (parse.contains("pm")) {
hour += 12;
}
assert time != null;
assert time.getHour() == hour;
assert time.getMinute() == minute;
}

public void testTime(String parse) {
String strip = parse.replace("am", "");
strip = strip.replace("pm", "");
String[] sub = strip.split(":");
int hour = Integer.parseInt(sub[0]);
int minute = 0;
if (sub.length == 2)
minute = Integer.parseInt(sub[1]);
testTime(parse, hour, minute);
}

@Test
public void test() {
for (int i = 1; i < 12; i++) {
testTime(i + "am");
testTime(i + ":30am");
testTime(i + "pm");
testTime(i + ":30pm");
}
for (int i = 0; i < 24; i++) {
testTime(i + ":00");
testTime(i + ":30");
}
}

}