aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/overlays/BazaarSearchOverlay.java
blob: 343ba156745229245db3c7c9daf3c39baa0239f9 (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
/*
 * 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.overlays;

import com.google.gson.JsonObject;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe;
import io.github.moulberry.notenoughupdates.events.SlotClickEvent;
import io.github.moulberry.notenoughupdates.miscfeatures.CookieWarning;
import io.github.moulberry.notenoughupdates.mixins.AccessorGuiEditSign;
import io.github.moulberry.notenoughupdates.util.Utils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiEditSign;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntitySign;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.util.ArrayList;
import java.util.Comparator;

@NEUAutoSubscribe
public class BazaarSearchOverlay extends SearchOverlayScreen {

	private static final Comparator<String> salesComparator = (o1, o2) -> {
		JsonObject bazaarInfo1 = NotEnoughUpdates.INSTANCE.manager.auctionManager.getBazaarInfo(o1);
		JsonObject bazaarInfo2 = NotEnoughUpdates.INSTANCE.manager.auctionManager.getBazaarInfo(o2);

		boolean auc1Invalid = bazaarInfo1 == null || !bazaarInfo1.has("curr_sell");
		boolean auc2Invalid = bazaarInfo2 == null || !bazaarInfo2.has("curr_sell");

		if (auc1Invalid && auc2Invalid) return o1.compareTo(o2);
		if (auc1Invalid) return 1;
		if (auc2Invalid) return -1;

		int sales1 = bazaarInfo1.get("curr_sell").getAsInt();
		int sales2 = bazaarInfo2.get("curr_sell").getAsInt();

		if (sales1 == sales2) return o1.compareTo(o2);
		if (sales1 > sales2) return -1;
		return 1;
	};

	public BazaarSearchOverlay() {
		super(new TileEntitySign());
	}

	public BazaarSearchOverlay(TileEntitySign tes) {
		super(tes);
		this.tileSign = tes;
		this.guiType = GuiType.BAZAAR;
	}

	public static boolean shouldReplace() {
		return Minecraft.getMinecraft().currentScreen instanceof BazaarSearchOverlay;
	}

	public static boolean isinBzSign() {
		if (!NotEnoughUpdates.INSTANCE.hasSkyblockScoreboard()) return false;
		if (!NotEnoughUpdates.INSTANCE.config.bazaarTweaks.enableSearchOverlay) return false;

		if (!(Minecraft.getMinecraft().currentScreen instanceof GuiEditSign)) {
			if (!NotEnoughUpdates.INSTANCE.config.bazaarTweaks.keepPreviousSearch) searchString = "";
			return false;
		}

		String lastContainer = Utils.getLastOpenChestName();
		if (!lastContainer.startsWith("Bazaar ➜ ")) return false;

		TileEntitySign tes = ((AccessorGuiEditSign) Minecraft.getMinecraft().currentScreen).getTileSign();

		if (tes == null) return false;
		if (tes.getPos().getY() != 0) return false;
		if (!tes.signText[2].getUnformattedText().equals("^^^^^^^^^^^^^^^")) return false;
		return tes.signText[3].getUnformattedText().equals("Enter query");
	}

	@SubscribeEvent
	public void onSlotClick(SlotClickEvent event) {
		if (!NotEnoughUpdates.INSTANCE.config.bazaarTweaks.enableSearchOverlay) return;
		if (!CookieWarning.hasActiveBoosterCookie()) return;
		if (!Utils.getOpenChestName().startsWith("Bazaar ➜")) return;
		ItemStack stack = event.slot.getStack();
		if (event.slot.slotNumber == 45 && stack != null && stack.hasDisplayName() && stack.getItem() == Items.sign && stack.getDisplayName().equals("§aSearch")) {
			event.setCanceled(true);
			NotEnoughUpdates.INSTANCE.openGui = new BazaarSearchOverlay();
		}
	}

	@SubscribeEvent
	public void onSignDrawn(GuiScreenEvent.DrawScreenEvent.Post event) {
		if (!isinBzSign() || !(event.gui instanceof GuiEditSign) || event.gui instanceof SearchOverlayScreen)
			return;
		GuiEditSign guiEditSign = (GuiEditSign) event.gui;
		TileEntitySign tileSign = ((AccessorGuiEditSign) guiEditSign).getTileSign();
		if (tileSign != null) {
			Minecraft.getMinecraft().displayGuiScreen(new BazaarSearchOverlay(tileSign));
		}
	}

	@Override
	public Comparator<String> getSearchComparator() {
		return salesComparator;
	}

	@Override
	public boolean enableSearchOverlay() {
		return NotEnoughUpdates.INSTANCE.config.bazaarTweaks.enableSearchOverlay;
	}

	@Override
	public ArrayList<String> previousSearches() {
		return NotEnoughUpdates.INSTANCE.config.hidden.previousBazaarSearches;
	}

	@Override
	public int searchHistorySize() {
		return NotEnoughUpdates.INSTANCE.config.bazaarTweaks.bzSearchHistorySize;
	}

	@Override
	public boolean showPastSearches() {
		return NotEnoughUpdates.INSTANCE.config.bazaarTweaks.showPastSearches;
	}

	@Override
	public boolean escFullClose() {
		return NotEnoughUpdates.INSTANCE.config.bazaarTweaks.escFullClose;
	}

	@Override
	public boolean keepPreviousSearch() {
		return NotEnoughUpdates.INSTANCE.config.bazaarTweaks.keepPreviousSearch;
	}

	@Override
	public GuiType currentGuiType() {
		return GuiType.BAZAAR;
	}
}