aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/CookieWarning.java
blob: ab8c5e5777a81590bb8509d1b5c56eb6f852a02c (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
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * 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.miscfeatures;

import com.google.common.collect.Lists;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.mixins.AccessorGuiPlayerTabOverlay;
import io.github.moulberry.notenoughupdates.util.NotificationHandler;
import io.github.moulberry.notenoughupdates.util.Utils;
import net.minecraft.client.Minecraft;
import net.minecraft.util.EnumChatFormatting;

public class CookieWarning {

	private static boolean hasNotified;

	public static void resetNotification() {
		hasNotified = false;
		NotificationHandler.cancelNotification();
	}

	/**
	 * Checks the tab list for a cookie timer, and sends a notification if the timer is within the tolerance
	 */
	public static void checkCookie() {
		if (NotEnoughUpdates.INSTANCE.config.notifications.doBoosterNotif &&
			NotEnoughUpdates.INSTANCE.hasSkyblockScoreboard()) {
			String[] lines;
			try {
				lines = ((AccessorGuiPlayerTabOverlay) Minecraft.getMinecraft().ingameGUI.getTabList())
					.getFooter()
					.getUnformattedText()
					.split("\n");
			} catch (NullPointerException e) {
				return; // if the footer is null or somehow doesn't exist, stop
			}
			boolean hasCookie = true;
			String timeLine = null; // the line that contains the cookie timer
			for (int i = 0; i < lines.length; i++) {
				if (lines[i].startsWith("Cookie Buff")) {
					timeLine = lines[i + 1]; // the line after the "Cookie Buff" line
				}
				if (lines[i].startsWith("Not active! Obtain booster cookies from the")) {
					hasCookie = false;
				}
			}
			if (!hasCookie) {
				if (!hasNotified) {
					NotificationHandler.displayNotification(Lists.newArrayList(
						"\u00a7cBooster Cookie Ran Out!",
						"\u00a77Your Booster Cookie expired!",
						"\u00a77",
						"\u00a77Press X on your keyboard to close this notification"
					), true, true);
					hasNotified = true;
				}
				return;
			}
			if (timeLine != null) {
				String[] digits = timeLine.replaceAll("(\u00a7.)", "").split(" ");
				int minutes = 0;
				try {
					for (int i = 0; i < digits.length; i++) {
						if (i % 2 == 1) continue;

						String number = digits[i];
						String unit = digits[i + 1];
						long val = Integer.parseInt(number);
						switch (unit) {
							case "Years":
							case "Year":
								minutes += val * 525600;
								break;
							case "Months":
							case "Month":
								minutes += val * 43200;
								break;
							case "Days":
							case "Day":
								minutes += val * 1440;
								break;
							case "Hours":
							case "Hour":
							case "h":
								minutes += val * 60;
								break;
							case "Minutes":
							case "Minute":
							case "m":
								minutes += val;
								break;
						} // ignore seconds
					}
				} catch (NumberFormatException e) {
					e.printStackTrace();
					Utils.addChatMessage(EnumChatFormatting.RED +
						"NEU ran into an issue when retrieving the Booster Cookie Timer. Check the logs for details.");
					hasNotified = true;
				}
				if (minutes < NotEnoughUpdates.INSTANCE.config.notifications.boosterCookieWarningMins && !hasNotified) {
					NotificationHandler.displayNotification(Lists.newArrayList(
						"\u00a7cBooster Cookie Running Low!",
						"\u00a77Your Booster Cookie will expire in " + timeLine,
						"\u00a77",
						"\u00a77Press X on your keyboard to close this notification"
					), true, true);
					hasNotified = true;
				}
			}
		}

	}
}