diff --git a/src/main/java/codes/biscuit/skyblockaddons/SkyblockAddons.java b/src/main/java/codes/biscuit/skyblockaddons/SkyblockAddons.java index 4337e9c0..330c078b 100644 --- a/src/main/java/codes/biscuit/skyblockaddons/SkyblockAddons.java +++ b/src/main/java/codes/biscuit/skyblockaddons/SkyblockAddons.java @@ -52,6 +52,8 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.time.ZoneId; +import java.time.ZonedDateTime; import java.util.*; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; @@ -81,6 +83,7 @@ public class SkyblockAddons { @Getter private static SkyblockAddons instance; @Getter private boolean fullyInitialized = false; + @Getter private static final ZoneId hypixelZoneId = ZoneId.of("America/New_York"); @SuppressWarnings({"rawtypes", "unchecked"}) private static final Gson GSON = new GsonBuilder() @@ -314,6 +317,10 @@ public static Logger getLogger() { return LogManager.getLogger(fullClassName, new SkyblockAddonsMessageFactory(simpleClassName)); } + public static ZonedDateTime getHypixelZonedDateTime() { + return ZonedDateTime.now(hypixelZoneId); + } + public static void runAsync(Runnable runnable) { THREAD_EXECUTOR.execute(runnable); } diff --git a/src/main/java/codes/biscuit/skyblockaddons/features/FetchurManager.java b/src/main/java/codes/biscuit/skyblockaddons/features/FetchurManager.java index 9e9c3400..0bb6e0ee 100644 --- a/src/main/java/codes/biscuit/skyblockaddons/features/FetchurManager.java +++ b/src/main/java/codes/biscuit/skyblockaddons/features/FetchurManager.java @@ -10,9 +10,7 @@ import net.minecraft.init.Items; import net.minecraft.item.ItemStack; -import java.util.Calendar; -import java.util.GregorianCalendar; -import java.util.TimeZone; +import java.time.Instant; /** * Manages the Fetchur Feature, Pointing out which item Fetchur wants next @@ -24,11 +22,6 @@ public class FetchurManager { @Getter private static final FetchurManager instance = new FetchurManager(); private static final long MILLISECONDS_IN_A_DAY = 24 * 60 * 60 * 1000; - // Hypixel timezone - // Currently using new york timezone, gotta check november 7th to see if this still works - @Getter - private static final TimeZone fetchurZone = TimeZone.getTimeZone("America/New_York"); - private static final Calendar fetchurCalendar = new GregorianCalendar(TimeZone.getTimeZone("America/New_York")); @Getter private final String fetchurTaskCompletedPhrase = "thanks thats probably what i needed"; @@ -85,14 +78,12 @@ public boolean hasFetchedToday() { } /** - * Returns the day of the month in the fetchur calendar (EST time zone) - * - * @param currTimeMilis Epoch UTC miliseconds (e.g. from {@link System#currentTimeMillis()}) - * @return the 1-indexed day of the month in the fetchur time zone + * Returns the day of the month in the Fetchur calendar (EST time zone) + * @param currTimeMillis Epoch UTC milliseconds (e.g. from {@link System#currentTimeMillis()}) + * @return the 1-indexed day of the month in the Fetchur time zone */ - private int getFetchurDayOfMonth(long currTimeMilis) { - fetchurCalendar.setTimeInMillis(currTimeMilis); - return fetchurCalendar.get(Calendar.DAY_OF_MONTH); + private int getFetchurDayOfMonth(long currTimeMillis) { + return Instant.ofEpochMilli(currTimeMillis).atZone(SkyblockAddons.getHypixelZoneId()).getDayOfMonth(); } /** @@ -116,8 +107,7 @@ public void recalculateFetchurItem() { * Triggered if the player has just given the correct item, or has already given the correct item, to Fetchur. */ public void saveLastTimeFetched() { - SkyblockAddons main = SkyblockAddons.getInstance(); - main.getPersistentValuesManager().setLastTimeFetchur(System.currentTimeMillis()); + SkyblockAddons.getInstance().getPersistentValuesManager().setLastTimeFetchur(System.currentTimeMillis()); } /** diff --git a/src/main/java/codes/biscuit/skyblockaddons/gui/IslandWarpGui.java b/src/main/java/codes/biscuit/skyblockaddons/gui/IslandWarpGui.java index 3e447b88..95fc3175 100644 --- a/src/main/java/codes/biscuit/skyblockaddons/gui/IslandWarpGui.java +++ b/src/main/java/codes/biscuit/skyblockaddons/gui/IslandWarpGui.java @@ -20,6 +20,7 @@ import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; +import java.time.Month; import java.util.Locale; import static codes.biscuit.skyblockaddons.core.Translations.*; @@ -48,7 +49,8 @@ public void initGui() { for (Island island : Island.values()) { if (island == Island.JERRYS_WORKSHOP - && main.getUtils().getCurrentDate().getMonth() != SkyblockDate.SkyblockMonth.LATE_WINTER) { + && main.getUtils().getCurrentDate().getMonth() != SkyblockDate.SkyblockMonth.LATE_WINTER + && SkyblockAddons.getHypixelZonedDateTime().getMonth() != Month.DECEMBER) { continue; } this.buttonList.add(new IslandButton(island)); diff --git a/src/main/java/codes/biscuit/skyblockaddons/listeners/RenderListener.java b/src/main/java/codes/biscuit/skyblockaddons/listeners/RenderListener.java index 254e58e0..04d0c61b 100644 --- a/src/main/java/codes/biscuit/skyblockaddons/listeners/RenderListener.java +++ b/src/main/java/codes/biscuit/skyblockaddons/listeners/RenderListener.java @@ -71,6 +71,8 @@ import java.math.BigDecimal; import java.math.RoundingMode; import java.text.ParseException; +import java.time.Duration; +import java.time.ZonedDateTime; import java.util.List; import java.util.*; import java.util.regex.Pattern; @@ -949,61 +951,28 @@ public void drawText(Feature feature, float scale, ButtonLocation buttonLocation case DARK_AUCTION_TIMER: // The timezone of the server, to avoid problems with like timezones that are 30 minutes ahead or whatnot. - Calendar nextDarkAuction = Calendar.getInstance(TimeZone.getTimeZone("EST")); - if (nextDarkAuction.get(Calendar.MINUTE) >= 55) { - nextDarkAuction.add(Calendar.HOUR_OF_DAY, 1); + ZonedDateTime nowDA = SkyblockAddons.getHypixelZonedDateTime(); + ZonedDateTime nextDarkAuction = nowDA.withMinute(55).withSecond(0); + if (nowDA.getMinute() >= 55) { + nextDarkAuction = nextDarkAuction.plusHours(1); } - nextDarkAuction.set(Calendar.MINUTE, 55); - nextDarkAuction.set(Calendar.SECOND, 0); - int differenceDAH = (int) (nextDarkAuction.getTimeInMillis() - System.currentTimeMillis()); - int minutesDAH = differenceDAH / 60000; - int secondsDAH = (int) Math.round((double) (differenceDAH % 60000) / 1000); - StringBuilder timestampDAH = new StringBuilder(); - if (minutesDAH < 10) { - timestampDAH.append("0"); - } - timestampDAH.append(minutesDAH).append(":"); - if (secondsDAH < 10) { - timestampDAH.append("0"); - } - timestampDAH.append(secondsDAH); - text = timestampDAH.toString(); + Duration diffDA = Duration.between(nowDA, nextDarkAuction); + text = String.format("%02d:%02d", diffDA.toMinutes(), diffDA.getSeconds() % 60); break; case FARM_EVENT_TIMER: // The timezone of the server, to avoid problems with like timezones that are 30 minutes ahead or whatnot. - Calendar nextFarmEvent = Calendar.getInstance(TimeZone.getTimeZone("EST")); - if (nextFarmEvent.get(Calendar.MINUTE) >= 15) { - nextFarmEvent.add(Calendar.HOUR_OF_DAY, 1); - } - nextFarmEvent.set(Calendar.MINUTE, 15); - nextFarmEvent.set(Calendar.SECOND, 0); - int differenceFE = (int) (nextFarmEvent.getTimeInMillis() - System.currentTimeMillis()); - int minutesFE = differenceFE / 60000; - int secondsFE = (int) Math.round((double) (differenceFE % 60000) / 1000); + ZonedDateTime nowFE = SkyblockAddons.getHypixelZonedDateTime(); + ZonedDateTime nextFarmEvent = nowFE.withMinute(15).withSecond(0); + if (nowFE.getMinute() >= 15) { + nextFarmEvent = nextFarmEvent.plusHours(1); + } + Duration diffFE = Duration.between(nowFE, nextFarmEvent); + long minutesFE = diffFE.toMinutes(); if (minutesFE < 40) { - StringBuilder timestampFE = new StringBuilder(); - if (minutesFE < 10) { - timestampFE.append("0"); - } - timestampFE.append(minutesFE).append(":"); - if (secondsFE < 10) { - timestampFE.append("0"); - } - timestampFE.append(secondsFE); - text = timestampFE.toString(); + text = String.format("%02d:%02d", minutesFE, diffFE.getSeconds() % 60); } else { - StringBuilder timestampActive = new StringBuilder(); - timestampActive.append("Active: "); - if (minutesFE - 40 < 10) { - timestampActive.append("0"); - } - timestampActive.append(minutesFE - 40).append(":"); - if (secondsFE < 10) { - timestampActive.append("0"); - } - timestampActive.append(secondsFE); - text = timestampActive.toString(); + text = String.format("Active: %02d:%02d", minutesFE - 40, diffFE.getSeconds() % 60); } break; diff --git a/src/main/java/codes/biscuit/skyblockaddons/utils/Utils.java b/src/main/java/codes/biscuit/skyblockaddons/utils/Utils.java index 48ca3858..72858d5e 100644 --- a/src/main/java/codes/biscuit/skyblockaddons/utils/Utils.java +++ b/src/main/java/codes/biscuit/skyblockaddons/utils/Utils.java @@ -42,7 +42,8 @@ import java.io.IOException; import java.nio.FloatBuffer; import java.text.ParseException; -import java.util.Calendar; +import java.time.Month; +import java.time.ZonedDateTime; import java.util.LinkedList; import java.util.List; import java.util.Locale; @@ -662,13 +663,12 @@ public File getSBAFolder() { } /** - * Checks if it is currently Halloween according to the system calendar. - * + * Checks if it is currently Halloween according to the Hypixel time zone. * @return {@code true} if it is Halloween, {@code false} otherwise */ public boolean isHalloween() { - Calendar calendar = Calendar.getInstance(); - return calendar.get(Calendar.MONTH) == Calendar.OCTOBER && calendar.get(Calendar.DAY_OF_MONTH) == 31; + ZonedDateTime zonedDateTime = ZonedDateTime.now(SkyblockAddons.getHypixelZoneId()); + return zonedDateTime.getMonth() == Month.OCTOBER && zonedDateTime.getDayOfMonth() == 31; } public int getDefaultBlue(int alpha) {