aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/AuctionProfit.java
blob: c0c45db0dda6f546da4768ff7999ee812f9b6131 (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
/*
 * 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.util.StringUtils;
import io.github.moulberry.notenoughupdates.events.ButtonExclusionZoneEvent;
import io.github.moulberry.notenoughupdates.mixins.AccessorGuiContainer;
import io.github.moulberry.notenoughupdates.util.Rectangle;
import io.github.moulberry.notenoughupdates.util.Utils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.inventory.GuiChest;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ContainerChest;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import org.lwjgl.opengl.GL11;

@NEUAutoSubscribe
public class AuctionProfit {

	public static final ResourceLocation auctionProfitImage =
		new ResourceLocation("notenoughupdates:auction_profit.png");

	@SubscribeEvent
	public void onButtonExclusionZones(ButtonExclusionZoneEvent event) {
		if (inAuctionPage()) {
			event.blockArea(
				new Rectangle(
					event.getGuiBaseRect().getRight(),
					event.getGuiBaseRect().getTop(),
					128 /*width*/ + 4 /*space*/, 56
				),
				ButtonExclusionZoneEvent.PushDirection.TOWARDS_RIGHT
			);
		}
	}

	@SubscribeEvent
	public void onDrawBackground(GuiScreenEvent.BackgroundDrawnEvent event) {
		if (!inAuctionPage()) return;

		Minecraft minecraft = Minecraft.getMinecraft();
		Container inventoryContainer = minecraft.thePlayer.openContainer;

		if (!(Minecraft.getMinecraft().currentScreen instanceof GuiChest)) return;
		Gui gui = event.gui;
		int xSize = ((AccessorGuiContainer) gui).getXSize();
		int guiLeft = ((AccessorGuiContainer) gui).getGuiLeft();
		int guiTop = ((AccessorGuiContainer) gui).getGuiTop();
		minecraft.getTextureManager().bindTexture(auctionProfitImage);
		GL11.glColor4f(1, 1, 1, 1);
		GlStateManager.disableLighting();
		Utils.drawTexturedRect(guiLeft + xSize + 4, guiTop, 180, 101, 0, 180 / 256f, 0, 101 / 256f, GL11.GL_NEAREST);

		double coinsToCollect = 0;
		double coinsIfAllSold = 0;
		int expiredAuctions = 0;
		int unclaimedAuctions = 0;
		for (ItemStack itemStack : inventoryContainer.getInventory()) {
			boolean isBin = false;
			if (itemStack == null || !itemStack.hasTagCompound()) continue;

			NBTTagCompound tag = itemStack.getTagCompound();
			if (tag == null) continue;
			NBTTagCompound display = tag.getCompoundTag("display");
			if (!display.hasKey("Lore", 9)) continue;
			NBTTagList lore = itemStack.getTagCompound().getCompoundTag("display").getTagList("Lore", 8);

			double coinsToCheck = 0;
			for (int i = 0; i < lore.tagCount(); i++) {
				String line = lore.getStringTagAt(i);
				if (line.contains("§7Buy it now")) {
					isBin = true;
					String s = line.split("§7Buy it now: ")[1];
					String coinsString = s.split("coins")[0];
					double coins = tryParse(EnumChatFormatting.getTextWithoutFormattingCodes(coinsString.trim()));
					if (coins != 0) {
						coinsToCheck += coins;
					}
				}

				if (line.contains("§7Top bid: ")) {
					String s = line.split("§7Top bid: ")[1];
					String coinsString = s.split("coins")[0];
					String textWithoutFormattingCodes = EnumChatFormatting.getTextWithoutFormattingCodes(coinsString.trim());
					double coins = tryParse(textWithoutFormattingCodes);
					if (coins != 0) {
						coinsToCheck += coins;
					}
				}

				if (line.contains("§7Sold for: ")) {
					String s = line.split("§7Sold for: ")[1];
					String coinsString = s.split("coins")[0];
					double coins = tryParse(EnumChatFormatting.getTextWithoutFormattingCodes(coinsString.trim()));
					if (coins != 0) {
						coins = removeTax(coins);
						coinsToCollect += coins;
					}
				}

				if (line.contains("§7Status: §aSold!") || line.contains("§7Status: §aEnded!")) {
					if (coinsToCheck != 0) {
						coinsToCheck = removeTax(coinsToCheck);
						coinsToCollect += coinsToCheck;
						coinsToCheck = 0;
					}
					unclaimedAuctions++;
				} else if (line.contains("§7Status: §cExpired!")) {
					expiredAuctions++;
				}

				if (isBin && line.contains("§7Ends in") && coinsToCheck != 0) {
					coinsIfAllSold += coinsToCheck;
					coinsToCheck = 0;
				}

			}

		}
		int a = guiLeft + xSize + 4;
		String unclaimedAuctionsStr = EnumChatFormatting.DARK_GREEN.toString()
			+ unclaimedAuctions + EnumChatFormatting.BOLD + EnumChatFormatting.DARK_GRAY + " Unclaimed auctions";
		String expiredAuctionsStr =
			EnumChatFormatting.RED.toString() + expiredAuctions + EnumChatFormatting.BOLD + EnumChatFormatting.DARK_GRAY +
				" Expired auctions";

		FontRenderer fontRendererObj = minecraft.fontRendererObj;
		fontRendererObj.drawString(unclaimedAuctionsStr, a + 6, guiTop + 6, -1, false);
		fontRendererObj.drawString(expiredAuctionsStr, a + 6, guiTop + 16, -1, false);

		String coinsToCollectStr =
			EnumChatFormatting.BOLD + EnumChatFormatting.DARK_GRAY.toString() + "Coins to collect: " +
				EnumChatFormatting.RESET + EnumChatFormatting.DARK_GREEN + "" +
				StringUtils.shortNumberFormat(coinsToCollect);
		String valueIfSoldStr = EnumChatFormatting.BOLD + EnumChatFormatting.DARK_GRAY.toString() + "Value if all sold: " +
			EnumChatFormatting.RESET + EnumChatFormatting.DARK_GREEN + "" +
			StringUtils.shortNumberFormat(coinsIfAllSold);

		fontRendererObj.drawString(coinsToCollectStr, a + 6, guiTop + 32, -1, false);
		fontRendererObj.drawString(valueIfSoldStr, a + 6, guiTop + 42, -1, false);
	}

	private double removeTax(double coins) {
		if (coins < 10_000_000) {
			return coins / 1.01;
		}
		if (coins < 100_000_000) {
			return coins / 1.02;
		}
		return coins / 1.025;
	}

	public static Double tryParse(String s) {
		try {
			return Double.parseDouble(s.replace(",", ""));
		} catch (NumberFormatException exception) {
			return 0.0;
		}
	}

	public static boolean inAuctionPage() {
		if (!NotEnoughUpdates.INSTANCE.config.ahTweaks.enableAhSellValue
			|| !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().equalsIgnoreCase("Manage Auctions");
	}
}