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 10 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
147 changes: 147 additions & 0 deletions src/main/java/ch/njol/skript/events/EvtServerTime.java
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 {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
@Override
public @NotNull HandlerList getHandlers() {
throw new IllegalStateException();
}
}

private static final long HOUR_12_MILLISECONDS = 43200000;
TheAbsolutionism 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,
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
"(server|real) time (of|at) %times%")
TheAbsolutionism 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 {
TheAbsolutionism 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;
}

}

}
58 changes: 43 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,24 @@
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.regex.Matcher;
import java.util.regex.Pattern;

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

public enum TimeState {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
AM, PM, TWENTY_FOUR_HOUR
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

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 +27,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 TimeState timeState;

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, TimeState timeState) {
this.time = Math2.mod(time, TICKS_PER_DAY);
this.hour = hour;
this.minute = minute;
this.timeState = timeState;
}

/**
Expand All @@ -49,10 +62,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 TimeState getTimeState() {
return timeState;
}

@Override
public String toString() {
return toString(time);
String string = toString(time);
return string + ((timeState == null || timeState == TimeState.TWENTY_FOUR_HOUR ? "" : timeState));
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

public static String toString(final int ticks) {
Expand Down Expand Up @@ -94,7 +120,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, TimeState.TWENTY_FOUR_HOUR);
} else {
final Matcher m = DAY_TIME_PATTERN.matcher(s);
if (m.matches()) {
Expand All @@ -112,17 +138,20 @@ public static Time parse(final String s) {
Skript.error("" + m_error_60_minutes);
return null;
}
if (m.group(4).equalsIgnoreCase("pm"))
TimeState state = TimeState.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 = TimeState.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 time * (timeState.ordinal() + 1);
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand All @@ -131,10 +160,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 && timeState == other.timeState;
}

@Override
Expand Down
Loading