aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/AbiphoneFavourites.java
blob: 7ca79d08ca436668a1a24fb130b6b02d167a6efc (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * 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 io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe;
import io.github.moulberry.notenoughupdates.core.config.KeybindHelper;
import io.github.moulberry.notenoughupdates.core.util.StringUtils;
import io.github.moulberry.notenoughupdates.core.util.render.RenderUtils;
import io.github.moulberry.notenoughupdates.events.GuiContainerBackgroundDrawnEvent;
import io.github.moulberry.notenoughupdates.events.ReplaceItemEvent;
import io.github.moulberry.notenoughupdates.events.SlotClickEvent;
import io.github.moulberry.notenoughupdates.options.NEUConfig;
import io.github.moulberry.notenoughupdates.util.ItemUtils;
import io.github.moulberry.notenoughupdates.util.Utils;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IChatComponent;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

@NEUAutoSubscribe
public class AbiphoneFavourites {

	private static final AbiphoneFavourites INSTANCE = new AbiphoneFavourites();
	private long lastClick = 0L;
	private boolean isInShowMenu = false;

	public static AbiphoneFavourites getInstance() {
		return INSTANCE;
	}

	private final ItemStack ITEM_STACK_FAVOURITE_ONLY = Utils.createItemStack(
		Items.diamond,
		"§6Show only favourite contacts",
		"§7Non favourite contacts are hidden.",
		"§7Only favourite contacts can be called.",
		"§8This is a NEU feature and not made by hypixel.",
		" ",
		"§eClick to show all contacts!"
	);
	private final ItemStack ITEM_STACK_ALL = Utils.createItemStack(
		Items.emerald,
		"§aShow all contacts",
		"§7Favourite contacts are marked §6orange§7.",
		"§7All contacts can be called.",
		"§8This is a NEU feature and not made by hypixel.",
		" ",
		"§eClick to show only favourite contacts!"
	);

	@SubscribeEvent
	public void onItemTooltip(ItemTooltipEvent event) {
		if (isWrongInventory()) return;

		List<String> list = event.toolTip;
		if (list == null) return;
		if (list.isEmpty()) return;

		ItemStack stack = event.itemStack;
		if (!isContact(stack)) return;
		String rawName = stack.getDisplayName();
		String name = StringUtils.cleanColour(rawName);

		if (isAbiphoneShowOnlyFavourites()) {
			if (!getFavouriteContacts().contains(name)) {
				list.clear();
				return;
			}
		}

		if (isAbiphoneShowOnlyFavourites()) {
			list.removeIf(s -> s.contains("§8Right-click to remove!"));
			return;
		}

		int index = list.size() - 1;
		if (getFavouriteContacts().contains(name)) {
			list.set(0, rawName + " §f- §6Favourite");
			list.add(index, "§eShift-click to remove from the favourites!");
		} else {
			list.add(index, "§eShift-click to add to the favourites!");
		}

		if (KeybindHelper.isKeyPressed(NotEnoughUpdates.INSTANCE.manager.keybindFavourite.getKeyCode())) {
			if (System.currentTimeMillis() > lastClick + 500) {
				toggleFavouriteContact(rawName, name);
				lastClick = System.currentTimeMillis();
			}
		}
	}

	@SubscribeEvent(priority = EventPriority.HIGH)
	public void onStackClick(SlotClickEvent event) {
		if (isWrongInventory()) return;

		ItemStack stack = event.slot.getStack();
		if (stack == null || stack.getDisplayName() == null) return;

		if ((stack == ITEM_STACK_FAVOURITE_ONLY || stack == ITEM_STACK_ALL)) {
			if (System.currentTimeMillis() > lastClick + 200) {
				NEUConfig.HiddenProfileSpecific profileSpecific = NotEnoughUpdates.INSTANCE.config.getProfileSpecific();
				if (profileSpecific != null) {
					profileSpecific.abiphoneShowOnlyFavourites =
						!isAbiphoneShowOnlyFavourites();
					lastClick = System.currentTimeMillis();
				}
			}
			event.setCanceled(true);
			return;
		}

		if (!isContact(stack)) return;

		int clickType = event.clickType;
		int clickedButton = event.clickedButton;

		//allows removing the contact
		if (clickType == 0 && clickedButton == 1) {
			if (!isAbiphoneShowOnlyFavourites()) {
				return;
			}
		}
		String rawName = stack.getDisplayName();
		String name = StringUtils.cleanColour(rawName);

		//allows calling
		if (clickType == 0 && clickedButton == 0) {
			if (!isAbiphoneShowOnlyFavourites() || getFavouriteContacts().contains(name)) {
				return;
			}
		}

		//toggle favourite contact
		if (clickType == 1) {
			if (!isAbiphoneShowOnlyFavourites()) {
				toggleFavouriteContact(rawName, name);
			}
		}

		event.setCanceled(true);
	}

	@SubscribeEvent
	public void replaceItem(ReplaceItemEvent event) {
		IChatComponent chatComponent = event.getInventory().getDisplayName();
		if (chatComponent == null || isWrongInventory()) return;
		ItemStack original = event.getOriginal();
		if (original == null) return;
		if (original.getItem() != Item.getItemFromBlock(Blocks.stained_glass_pane)) return;

		if (event.getSlotNumber() > 2 && event.getSlotNumber() < 6) {
			event.replaceWith(isAbiphoneShowOnlyFavourites() ? ITEM_STACK_FAVOURITE_ONLY : ITEM_STACK_ALL);
		}
	}

	private void toggleFavouriteContact(String rawName, String name) {
		if (getFavouriteContacts().contains(name)) {
			getFavouriteContacts().remove(name);
			Utils.addChatMessage("§e[NEU] Removed §r" + rawName + " §efrom your favourite contacts!");
		} else {
			getFavouriteContacts().add(name);
			Utils.addChatMessage("§e[NEU] Added §r" + rawName + " §eto your favourite contacts!");
		}
	}

	public boolean onRenderStack(ItemStack stack) {
		if (isWrongInventory()) return false;

		if (stack == null || stack.getDisplayName() == null) return false;

		if (!isContact(stack)) return false;

		String rawName = stack.getDisplayName();
		String name = StringUtils.cleanColour(rawName);

		return isAbiphoneShowOnlyFavourites() && !getFavouriteContacts().contains(name);
	}

	@SubscribeEvent
	public void onDrawBackground(GuiContainerBackgroundDrawnEvent event) {
		if (!NotEnoughUpdates.INSTANCE.hasSkyblockScoreboard()
			|| !NotEnoughUpdates.INSTANCE.config.misc.abiphoneFavourites
			|| Utils.getOpenChestName().equals("Abiphone Shop")
			|| !Utils.getOpenChestName().startsWith("Abiphone ")) return;

		GuiContainer container = event.getContainer();

		ItemStack checkForShowMenu = container.inventorySlots.getSlot(1*9 + 4).getStack();
		isInShowMenu = checkForShowMenu != null && checkForShowMenu.getDisplayName().contains("Abiphone ");

		for (Slot slot : container.inventorySlots.inventorySlots) {
			if (slot == null) continue;
			ItemStack stack = slot.getStack();
			if (stack == null) continue;

			if (!isContact(stack)) continue;

			String rawName = stack.getDisplayName();
			String name = StringUtils.cleanColour(rawName);

			if (!isAbiphoneShowOnlyFavourites()) {
				if (getFavouriteContacts().contains(name)) {
					RenderUtils.highlightSlot(slot, Color.ORANGE);
				}
			}
		}
	}

	private boolean isWrongInventory() {
		return !NotEnoughUpdates.INSTANCE.hasSkyblockScoreboard()
			|| !NotEnoughUpdates.INSTANCE.config.misc.abiphoneFavourites
			|| Utils.getOpenChestName().equals("Abiphone Shop")
			|| !Utils.getOpenChestName().startsWith("Abiphone ")
			|| isInShowMenu;
	}

	private boolean isContact(ItemStack stack) {
		for (String line : ItemUtils.getLore(stack)) {
			if (line.equals("§eLeft-click to call!") || line.equals("§eClick to call!")) {
				return true;
			}
		}

		return false;
	}

	private List<String> getFavouriteContacts() {
		NEUConfig.HiddenProfileSpecific profileSpecific = NotEnoughUpdates.INSTANCE.config.getProfileSpecific();
		if (profileSpecific !=