aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/util/StarCultCalculator.java
blob: fadfef40281727483158ddd793b49ba94f58a5fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright (C) 2022 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.util;

import java.time.Instant;

// cryptic helped me a "little" with the calculation
public class StarCultCalculator {

	public static final String[] SEASONS = new String[]{
		"Early Spring",
		"Spring",
		"Late Spring",
		"Early Summer",
		"Summer",
		"Late Summer",
		"Early Autumn",
		"Autumn",
		"Late Autumn",
		"Early Winter",
		"Winter",
		"Late Winter",
	};

	public static final long HOUR_MS = 50000;
	public static final long DAY_MS = 24 * HOUR_MS;
	public static final long MONTH_MS = 31 * DAY_MS;
	public static final long YEAR_MS = SEASONS.length * MONTH_MS;
	public static final long YEAR_0 = 1560275700000L;

	public static int getSkyblockYear() {
		long now = Instant.now().toEpochMilli();
		long currentYear = Math.floorDiv((now - YEAR_0), YEAR_MS);
		return (int) (currentYear + 1);
	}

	private static boolean active = false;
	private static long activeTill = 0;

	public static String getNextStarCult() {
		Instant cultStart = getNextStarCultTime();

		long l = System.currentTimeMillis();
		if (cultStart.toEpochMilli() - l <= 1000) {
			active = true;
			activeTill = l + 300000;
		}

		if (l > activeTill) {
			active = false;
			activeTill = 0;
		}

		if (active && activeTill != 0) {
			return "Active! (" + Utils.prettyTime(activeTill - System.currentTimeMillis()) + ")";
		}

		return Utils.prettyTime(cultStart.toEpochMilli() - l);
	}

	public static Instant getNextStarCultTime() {
		return getNextStarCultTime(Instant.now());
	}

	public static Instant getNextStarCultTime(Instant after) {
		long nowEpoch = after.toEpochMilli();
		long currentOffset = (nowEpoch - YEAR_0) % YEAR_MS;

		int currentMonth = (int) Math.floorDiv(currentOffset, MONTH_MS);
		int currentDay = (int) Math.floorDiv((currentOffset - (long) currentMonth * MONTH_MS) % MONTH_MS, DAY_MS) + 1;

		int out = 7;
		if (currentDay > 21) {
			out = 28;
		} else if (currentDay > 14) {
			out = 21;
		} else if (currentDay > 7) {
			out = 14;
		}
		Instant cultStart = Instant.ofEpochMilli(
			YEAR_0 + (getSkyblockYear() - 1) * YEAR_MS + currentMonth * MONTH_MS + (out - 1) * DAY_MS);
		if (cultStart.isBefore(after)) {
			int curYearCult = getSkyblockYear() - 1;
			if (out == 28) {
				out = 7;
				if (currentMonth == 12) {
					currentMonth = 1;
					curYearCult++;
				} else {
					currentMonth++;
				}
			} else {
				out += 7;
			}
			out--;
			cultStart = Instant.ofEpochMilli(YEAR_0 + (curYearCult) * YEAR_MS + currentMonth * MONTH_MS + out * DAY_MS);
		}
		return cultStart;
	}
}