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

EvtRealTime #7332

Open
wants to merge 18 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
146 changes: 146 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,146 @@
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.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);
}
RealTimeInfo info = new RealTimeInfo(time, 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();
}

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

private static class RealTimeInfo {
private long executionTime;
private final Time time;
private TimerTask task;

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

}

}
59 changes: 44 additions & 15 deletions src/main/java/ch/njol/skript/util/Time.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
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> {

public enum TimeFormat {
AM, PM, TWENTY_FOUR_HOURS
}

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 @@ -24,16 +28,26 @@ public class Time implements YggdrasilSerializable, Cyclical<Integer> {
private final static int HOUR_ZERO = 6 * TICKS_PER_HOUR;

private final int time;
private int hour;
private int minute;
private TimeFormat timeFormat;

private static final Pattern DAY_TIME_PATTERN = Pattern.compile("(\\d?\\d)(:(\\d\\d))? ?(am|pm)", Pattern.CASE_INSENSITIVE);
private static final Pattern TIME_PATTERN = Pattern.compile("\\d?\\d:\\d\\d", Pattern.CASE_INSENSITIVE);

public Time() {
time = 0;
}

public Time(int time) {
this.time = time;
}

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

/**
Expand All @@ -49,10 +63,23 @@ public int getTicks() {
public int getTime() {
return (time + HOUR_ZERO) % TICKS_PER_DAY;
}


public int getHour() {
return hour;
}

public int getMinute() {
return minute;
}

public TimeFormat getTimeState() {
return timeFormat;
}

@Override
public String toString() {
return toString(time);
String string = toString(time);
return string + ((timeFormat == null || timeFormat == TimeFormat.TWENTY_FOUR_HOURS ? "" : timeFormat.name().replace('_', ' ')));
}

public static String toString(final int ticks) {
Expand Down Expand Up @@ -94,7 +121,7 @@ public static Time parse(final String s) {
Skript.error("" + m_error_60_minutes);
return null;
}
return new Time((int) Math.round(hours * TICKS_PER_HOUR - HOUR_ZERO + minutes * TICKS_PER_MINUTE));
return new Time((int) Math.round(hours * TICKS_PER_HOUR - HOUR_ZERO + minutes * TICKS_PER_MINUTE), hours, minutes, TimeFormat.TWENTY_FOUR_HOURS);
} else {
final Matcher m = DAY_TIME_PATTERN.matcher(s);
if (m.matches()) {
Expand All @@ -112,17 +139,20 @@ public static Time parse(final String s) {
Skript.error("" + m_error_60_minutes);
return null;
}
if (m.group(4).equalsIgnoreCase("pm"))
TimeFormat state = TimeFormat.AM;
if (m.group(4).equalsIgnoreCase("pm")) {
hours += 12;
return new Time((int) Math.round(hours * TICKS_PER_HOUR - HOUR_ZERO + minutes * TICKS_PER_MINUTE));
state = TimeFormat.PM;
}
return new Time((int) Math.round(hours * TICKS_PER_HOUR - HOUR_ZERO + minutes * TICKS_PER_MINUTE), hours, minutes, state);
}
}
return null;
}

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

@Override
Expand All @@ -131,10 +161,9 @@ 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;
return time == other.time && timeFormat == other.timeFormat;
}

@Override
Expand Down
Loading