aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/BazaarSacksProfit.java
blob: c323cd2e8c9073258e662430e15592cb9959b73c (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
/*
 * 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.gson.JsonObject;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.core.util.StringUtils;
import io.github.moulberry.notenoughupdates.miscgui.TrophyRewardOverlay;
import io.github.moulberry.notenoughupdates.util.ItemUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ContainerChest;
import net.minecraft.item.ItemStack;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import org.lwjgl.input.Keyboard;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class BazaarSacksProfit {

	private static BazaarSacksProfit INSTANCE = null;
	private boolean showSellOrderPrice = false;
	private boolean pressedShiftLast = false;

	public static BazaarSacksProfit getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new BazaarSacksProfit();
		}
		return INSTANCE;
	}

	private final Map<String, Integer> prices = new HashMap<>();
	private final Map<String, String> names = new HashMap<>();
	private final List<String> invalidNames = new ArrayList<>();

	@SubscribeEvent
	public void onGuiOpen(GuiOpenEvent event) {
		showSellOrderPrice = false;
		prices.clear();
		names.clear();
		invalidNames.clear();
	}

	@SubscribeEvent(priority = EventPriority.LOW)
	public void onItemTooltipLow(ItemTooltipEvent event) {
		if (!NotEnoughUpdates.INSTANCE.config.tooltipTweaks.bazaarSacksProfit) return;
		if (!inBazaar()) return;

		ItemStack itemStack = event.itemStack;
		String displayName = itemStack.getDisplayName();
		if (!displayName.equals("§bSell Sacks Now")) return;

		boolean shift = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT);
		if (!pressedShiftLast && shift) {
			showSellOrderPrice = !showSellOrderPrice;
		}
		pressedShiftLast = shift;

		if (prices.isEmpty()) {
			out:
			for (String line : ItemUtils.getLore(itemStack)) {
				if (line.contains("§7x ")) {
					String[] split = line.split("§7x ");
					String rawAmount = StringUtils.cleanColour(split[0]).replace(",", "").substring(1);
					int amount = Integer.parseInt(rawAmount);
					String bazaarName = split[1].split(" §7for")[0];
					for (Map.Entry<String, JsonObject> entry : NotEnoughUpdates.INSTANCE.manager
						.getItemInformation()
						.entrySet()) {
						String internalName = entry.getKey();
						JsonObject object = entry.getValue();
						if (object.has("displayname")) {
							String name = object.get("displayname").getAsString();
							if (name.equals(bazaarName)) {
								prices.put(internalName, amount);
								names.put(internalName, bazaarName);
								continue out;
							}
						}
					}
					System.out.println("no bazaar item in repo found for '" + bazaarName + "'");
					invalidNames.add(bazaarName);
				}
			}
		}

		event.toolTip.removeIf(line -> line.contains("§7x ") || line.contains("You earn:"));

		Map<String, Float> map = new HashMap<>();
		DecimalFormat formatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.ENGLISH);
		formatter.applyPattern("#,##0");
		double totalPrice = 0;
		for (Map.Entry<String, Integer> entry : prices.entrySet()) {
			String internalName = entry.getKey();
			int amount = entry.getValue();
			String name = names.get(internalName);

			JsonObject bazaarInfo = NotEnoughUpdates.INSTANCE.manager.auctionManager.getBazaarInfo(internalName);

			float price = 0;
			if (bazaarInfo != null) {

				if (showSellOrderPrice) {
					if (bazaarInfo.has("curr_buy")) {
						price = bazaarInfo.get("curr_buy").getAsFloat();
					} else {
						System.err.println("curr_sell does not exist for '" + internalName + "'");
					}
				} else {
					if (bazaarInfo.has("curr_sell")) {
						price = bazaarInfo.get("curr_sell").getAsFloat();
					} else {
						System.err.println("curr_sell does not exist for '" + internalName + "'");
					}
				}
			}
			float extraPrice = price * amount;
			String priceFormat = formatter.format(extraPrice);
			totalPrice += extraPrice;
			map.put("§a" + formatter.format(amount) + "§7x §f" + name + " §7for §6" + priceFormat + " coins", extraPrice);
		}

		if (showSellOrderPrice) {
			event.toolTip.add(4, "§7Sell order price: §6" + formatter.format(totalPrice));
		} else {
			event.toolTip.add(4, "§7Instant sell price: §6" + formatter.format(totalPrice));
		}

		event.toolTip.add(4, "");
		for (String name : invalidNames) {
			event.toolTip.add(4, name + " §cMissing repo data!");
		}
		for (String text : TrophyRewardOverlay.sortByValue(map).keySet()) {
			event.toolTip.add(4, text);
		}

		event.toolTip.add("");
		if (!showSellOrderPrice) {
			event.toolTip.add("§8[Press SHIFT to show sell order price]");
		} else {
			event.toolTip.add("§8[Press SHIFT to show instant sell price]");
		}
	}

	public static boolean inBazaar() {
		if (!NotEnoughUpdates.INSTANCE.isOnSkyblock()) return false;

		Minecraft minecraft = Minecraft.getMinecraft();
		if (minecraft == null || minecraft.thePlayer == null) return false;

		Container inventoryContainer = minecraft.thePlayer.openContainer;
		if (!(inventoryContainer instanceof ContainerChest)) return false;
		ContainerChest containerChest = (ContainerChest) inventoryContainer;
		return containerChest.getLowerChestInventory().getDisplayName().getUnformattedText().startsWith("Bazaar ");
	}
}