Skip to content
Merged
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
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ java {
}

group = 'me.playbosswar.com'
version = '8.16.1'
version = '8.16.2'
description = 'CommandTimer'

repositories {
Expand Down Expand Up @@ -74,7 +74,7 @@ publishing {
maven(MavenPublication) {
groupId = 'me.playbosswar.com'
artifactId = 'commandtimer'
version = '8.16.1'
version = '8.16.2'

from components.java
}
Expand Down
4 changes: 2 additions & 2 deletions java17-build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ java {


group = 'me.playbosswar.com'
version = '8.16.1'
version = '8.16.2'
description = 'CommandTimer'

repositories {
Expand Down Expand Up @@ -63,7 +63,7 @@ publishing {
maven(MavenPublication) {
groupId = 'me.playbosswar.com'
artifactId = 'commandtimer-java17'
version = '8.16.1'
version = '8.16.2'

from components.java
}
Expand Down
4 changes: 2 additions & 2 deletions java21-build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ java {


group = 'me.playbosswar.com'
version = '8.16.1'
version = '8.16.2'
description = 'CommandTimer'

repositories {
Expand Down Expand Up @@ -67,7 +67,7 @@ publishing {
maven(MavenPublication) {
groupId = 'me.playbosswar.com'
artifactId = 'commandtimer-java21'
version = '8.16.1'
version = '8.16.2'
from components.java
}
}
Expand Down
184 changes: 102 additions & 82 deletions src/main/java/me/playbosswar/com/tasks/TasksManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,90 +221,79 @@ public void populateScheduleForTask(Task task) {
}

if (!task.getTimes().isEmpty()) {
List<TaskTime> rangeTimes = new ArrayList<>();
List<TaskTime> nonRangeTimes = new ArrayList<>();

for (TaskTime taskTime : task.getTimes()) {
if (taskTime.isRange()) {
rangeTimes.add(taskTime);
} else {
nonRangeTimes.add(taskTime);
}
}

for (TaskTime taskTime : rangeTimes) {
if (taskTime.isMinecraftTime()) {
World world = Bukkit.getWorld(taskTime.getWorld() == null ? "world" : taskTime.getWorld());
if (world == null) {
continue;
}

if (taskTime.isRange()) {
LocalTime startRange = taskTime.getTime1();
LocalTime endRange = taskTime.getTime2();

LocalTime currentMcTime = Tools.getMinecraftTimeAt(world, ZonedDateTime.now());
boolean currentlyInWindow = isTimeInRange(currentMcTime, startRange, endRange);

int mcDay = 0;
boolean firstIteration = true;
while (maxToSchedule > 0) {
ZonedDateTime windowStart;
ZonedDateTime windowEnd;

if (firstIteration && currentlyInWindow) {
windowStart = ZonedDateTime.now();
windowEnd = Tools.getNextMinecraftTime(world, endRange, 0);
if (windowEnd.isBefore(windowStart)) {
windowEnd = Tools.getNextMinecraftTime(world, endRange, 1);
}
} else {
int dayOffset = (firstIteration && currentlyInWindow) ? 1 : mcDay;
if (firstIteration && !currentlyInWindow) {
dayOffset = 0;
}
windowStart = Tools.getNextMinecraftTime(world, startRange, dayOffset);
windowEnd = Tools.getNextMinecraftTime(world, endRange, dayOffset);
if (windowEnd.isBefore(windowStart)) {
windowEnd = Tools.getNextMinecraftTime(world, endRange, dayOffset + 1);
}
}
firstIteration = false;
LocalTime startRange = taskTime.getTime1();
LocalTime endRange = taskTime.getTime2();

if (!task.getDays().contains(windowStart.toLocalDate().getDayOfWeek())) {
mcDay++;
continue;
}
LocalTime currentMcTime = Tools.getMinecraftTimeAt(world, ZonedDateTime.now());
boolean currentlyInWindow = isTimeInRange(currentMcTime, startRange, endRange);

if (windowEnd.isBefore(latestScheduledDate)) {
mcDay++;
continue;
int mcDay = 0;
boolean firstIteration = true;
while (maxToSchedule > 0) {
ZonedDateTime windowStart;
ZonedDateTime windowEnd;

if (firstIteration && currentlyInWindow) {
windowStart = ZonedDateTime.now();
windowEnd = Tools.getNextMinecraftTime(world, endRange, 0);
if (windowEnd.isBefore(windowStart)) {
windowEnd = Tools.getNextMinecraftTime(world, endRange, 1);
}
} else {
int dayOffset = (firstIteration && currentlyInWindow) ? 1 : mcDay;
if (firstIteration && !currentlyInWindow) {
dayOffset = 0;
}
windowStart = Tools.getNextMinecraftTime(world, startRange, dayOffset);
windowEnd = Tools.getNextMinecraftTime(world, endRange, dayOffset);
if (windowEnd.isBefore(windowStart)) {
windowEnd = Tools.getNextMinecraftTime(world, endRange, dayOffset + 1);
}
}
firstIteration = false;

ZonedDateTime execTime = windowStart.isBefore(latestScheduledDate) ? latestScheduledDate : windowStart;
long intervalSeconds = task.getInterval().toSeconds();
if (intervalSeconds <= 0) intervalSeconds = 1;
if (!task.getDays().contains(windowStart.toLocalDate().getDayOfWeek())) {
mcDay++;
continue;
}

while (maxToSchedule > 0 && !execTime.isAfter(windowEnd)) {
if (!execTime.isBefore(latestScheduledDate)) {
scheduledTasks.add(new ScheduledTask(task, execTime));
maxToSchedule--;
}
execTime = execTime.plusSeconds(intervalSeconds);
}
if (windowEnd.isBefore(latestScheduledDate)) {
mcDay++;
continue;
}
} else {
LocalTime time = taskTime.getTime1();

int i = 0;
while (maxToSchedule > 0) {
ZonedDateTime nextMinecraftTime = Tools.getNextMinecraftTime(world, time, i);
if (!task.getDays().contains(nextMinecraftTime.getDayOfWeek())) {
i++;
continue;
}
ZonedDateTime execTime = windowStart.isBefore(latestScheduledDate) ? latestScheduledDate : windowStart;
long intervalSeconds = task.getInterval().toSeconds();
if (intervalSeconds <= 0) intervalSeconds = 1;

if (nextMinecraftTime.isBefore(latestScheduledDate)) {
i++;
continue;
while (maxToSchedule > 0 && !execTime.isAfter(windowEnd)) {
if (!execTime.isBefore(latestScheduledDate)) {
scheduledTasks.add(new ScheduledTask(task, execTime));
maxToSchedule--;
}

scheduledTasks.add(new ScheduledTask(task, nextMinecraftTime));
maxToSchedule--;
i++;
execTime = execTime.plusSeconds(intervalSeconds);
}
mcDay++;
}
} else if (taskTime.isRange()) {
} else {
LocalTime startRange = taskTime.getTime1();
LocalTime endRange = taskTime.getTime2();

Expand All @@ -317,7 +306,8 @@ public void populateScheduleForTask(Task task) {
continue;
}

if (!(date.toLocalTime().isAfter(startRange) && date.toLocalTime().isBefore(endRange))) {
boolean isInRange = Tools.isTimeInRange(date.toLocalTime(), startRange, endRange);
if (!isInRange) {
i++;
continue;
}
Expand All @@ -331,29 +321,59 @@ public void populateScheduleForTask(Task task) {
maxToSchedule--;
i++;
}
} else {
LocalTime time = taskTime.getTime1();

int i = 0;
while (maxToSchedule > 0) {
ZonedDateTime date = ZonedDateTime.of(LocalDate.now(), time, ZoneId.systemDefault())
.plusDays(i);
if (!task.getDays().contains(date.getDayOfWeek())) {
i++;
}
}

if (!nonRangeTimes.isEmpty()) {
List<ZonedDateTime> allOccurrences = new ArrayList<>();

for (TaskTime taskTime : nonRangeTimes) {
if (taskTime.isMinecraftTime()) {
World world = Bukkit.getWorld(taskTime.getWorld() == null ? "world" : taskTime.getWorld());
if (world == null) {
continue;
}

if (date.isBefore(latestScheduledDate)) {
LocalTime time = taskTime.getTime1();
int i = 0;
int collected = 0;
while (collected < maxToSchedule * 2) {
ZonedDateTime nextMinecraftTime = Tools.getNextMinecraftTime(world, time, i);
if (task.getDays().contains(nextMinecraftTime.getDayOfWeek())
&& !nextMinecraftTime.isBefore(latestScheduledDate)) {
allOccurrences.add(nextMinecraftTime);
collected++;
}
i++;
continue;
}

scheduledTasks.add(new ScheduledTask(task, date));
maxToSchedule--;
i++;
} else {
LocalTime time = taskTime.getTime1();
int i = 0;
int collected = 0;
while (collected < maxToSchedule * 2) {
ZonedDateTime date = ZonedDateTime.of(LocalDate.now(), time, ZoneId.systemDefault())
.plusDays(i);
if (task.getDays().contains(date.getDayOfWeek())
&& !date.isBefore(latestScheduledDate)) {
allOccurrences.add(date);
collected++;
}
i++;
}
}
}

allOccurrences.sort(ZonedDateTime::compareTo);

for (ZonedDateTime date : allOccurrences) {
if (maxToSchedule <= 0) {
break;
}
scheduledTasks.add(new ScheduledTask(task, date));
maxToSchedule--;
}
}

return;
}

Expand All @@ -363,7 +383,7 @@ public void populateScheduleForTask(Task task) {
return;
}

int i = 0;
int i = 1;
while (maxToSchedule > 0) {
ZonedDateTime date = latestScheduledDate.plusSeconds(i * task.getInterval().toSeconds());
if (!task.getDays().contains(date.getDayOfWeek())) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/me/playbosswar/com/utils/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,12 @@ public static long minecraftTimeToTicks(LocalTime mcTime) {
long ticks = (hours - 6) * 1000L + (minutes * 1000L / 60);
return ticks % 24000;
}

public static boolean isTimeInRange(LocalTime time, LocalTime startRange, LocalTime endRange) {
if (startRange.isBefore(endRange) || startRange.equals(endRange)) {
return !time.isBefore(startRange) && !time.isAfter(endRange);
} else {
return !time.isBefore(startRange) || !time.isAfter(endRange);
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
main: me.playbosswar.com.CommandTimerPlugin
name: "CommandTimer"
version: "8.16.1"
version: "8.16.2"
description: "Schedule commands like you want"
author: PlayBossWar
api-version: 1.13
Expand Down