Skip to content

Commit 68a71c7

Browse files
committed
feat: Duration instead of a long for CalendarEvents
1 parent 20876fb commit 68a71c7

4 files changed

Lines changed: 15 additions & 9 deletions

File tree

type.skyblockgeneric/src/main/java/net/swofty/type/skyblockgeneric/calendar/CalendarEvent.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import net.swofty.proxyapi.ProxyService;
1010
import org.tinylog.Logger;
1111

12+
import java.time.Duration;
1213
import java.util.ArrayList;
1314
import java.util.HashMap;
1415
import java.util.List;
@@ -22,7 +23,7 @@ public record CalendarEvent(
2223
Function<Integer, String> displayName,
2324
List<String> description,
2425
List<Long> times,
25-
long duration,
26+
Duration duration,
2627
boolean tracksYear,
2728
BiConsumer<Long, Integer> action
2829
) {
@@ -40,7 +41,7 @@ public record CalendarEvent(
4041
"§7the Baker is giving out free Cake!"
4142
),
4243
List.of(10L),
43-
20 * 60 * 60L, // 1 hour
44+
Duration.ofHours(1),
4445
true,
4546
(time, year) -> {
4647
// New Year's actions
@@ -58,7 +59,7 @@ public record CalendarEvent(
5859
"§7special items are sold."
5960
),
6061
calculateDarkAuctionTimes(),
61-
20 * 5 * 60L,
62+
Duration.ofMinutes(5),
6263
false,
6364
(time, year) -> {
6465
ProxyService darkAuctionService = new ProxyService(ServiceType.DARK_AUCTION);
@@ -108,7 +109,11 @@ public static List<CalendarEvent> getCurrentEvents(long time) {
108109
List<CalendarEvent> activeEvents = new ArrayList<>();
109110
for (CalendarEvent event : allEvents) {
110111
for (Long eventStartTime : event.times()) {
111-
if (time >= eventStartTime && time < eventStartTime + event.duration()) {
112+
// convert ticks to milliseconds
113+
Duration timeSinceEventStart = Duration.ofMillis((time - eventStartTime) * 50);
114+
115+
// checks if the event is currently active
116+
if (!timeSinceEventStart.isNegative() && timeSinceEventStart.compareTo(event.duration()) < 0) {
112117
activeEvents.add(event);
113118
break;
114119
}

type.skyblockgeneric/src/main/java/net/swofty/type/skyblockgeneric/calendar/SkyBlockCalendar.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.minestom.server.timer.TaskSchedule;
77
import net.swofty.type.skyblockgeneric.commands.MinionGenerationCommand;
88

9+
import java.time.Duration;
910
import java.util.Arrays;
1011
import java.util.LinkedHashMap;
1112
import java.util.List;
@@ -97,10 +98,10 @@ public static List<CalendarEvent> getCurrentEvents() {
9798
/**
9899
* A record to hold information about upcoming events.
99100
* @param timeUntilBegin <b>ticks</b> until the event begins
100-
* @param duration <b>ticks</b> duration of the event
101+
* @param duration duration of the event
101102
* @param year the year the event will occur
102103
*/
103-
public record EventInfo(long timeUntilBegin, long duration, int year) {}
104+
public record EventInfo(long timeUntilBegin, Duration duration, int year) {}
104105

105106
public static Map<EventInfo, CalendarEvent> getEventsWithDurationUntil(int amount) {
106107
Map<EventInfo, CalendarEvent> result = new LinkedHashMap<>();

type.skyblockgeneric/src/main/java/net/swofty/type/skyblockgeneric/gui/inventories/sbmenu/GUISkyBlockMenu.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ private static long getTicksRemaining(CalendarEvent currentEvent) {
283283
long currentElapsedInYear = SkyBlockCalendar.getElapsed() % SkyBlockCalendar.YEAR;
284284
long eventEndTime = 0;
285285
for (Long eventStartTime : currentEvent.times()) {
286-
if (currentElapsedInYear >= eventStartTime && currentElapsedInYear < eventStartTime + currentEvent.duration()) {
287-
eventEndTime = eventStartTime + currentEvent.duration();
286+
if (currentElapsedInYear >= eventStartTime && currentElapsedInYear < eventStartTime + currentEvent.duration().toMillis() / 50) {
287+
eventEndTime = eventStartTime + currentEvent.duration().toMillis() / 50;
288288
break;
289289
}
290290
}

type.skyblockgeneric/src/main/java/net/swofty/type/skyblockgeneric/gui/inventories/sbmenu/calendar/GUICalendar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void onOpen(InventoryGUIOpenEvent e) {
4141
public ItemStack.Builder getItem(HypixelPlayer player) {
4242
List<Component> loreHeader = List.of(
4343
Component.text("§7Starts in: §e" + StringUtility.formatTimeLeft(info.timeUntilBegin() * 50L)),
44-
Component.text("§7Event lasts for §e" + StringUtility.formatTimeLeft(info.duration() * 50L) + "§7!"),
44+
Component.text("§7Event lasts for §e" + StringUtility.formatTimeLeft(info.duration().toMillis()) + "§7!"),
4545
Component.text(" ")
4646
);
4747
List<Component> loreFooter = event.description().stream()

0 commit comments

Comments
 (0)