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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
package de.hysky.skyblocker.skyblock.auction;
import com.google.gson.JsonElement;
import de.hysky.skyblocker.SkyblockerMod;
import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.skyblock.auction.widgets.AuctionTypeWidget;
import de.hysky.skyblocker.skyblock.auction.widgets.CategoryTabWidget;
import de.hysky.skyblocker.skyblock.auction.widgets.RarityWidget;
import de.hysky.skyblocker.skyblock.auction.widgets.SortWidget;
import de.hysky.skyblocker.skyblock.item.tooltip.ItemTooltip;
import de.hysky.skyblocker.skyblock.item.tooltip.TooltipInfoType;
import de.hysky.skyblocker.utils.ItemUtils;
import de.hysky.skyblocker.utils.render.gui.AbstractCustomHypixelGUI;
import it.unimi.dsi.fastutil.ints.Int2BooleanOpenHashMap;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.tooltip.Tooltip;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.texture.Sprite;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.screen.slot.Slot;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Colors;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;
import org.lwjgl.glfw.GLFW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
public class AuctionBrowserScreen extends AbstractCustomHypixelGUI<AuctionHouseScreenHandler> {
private static final Logger LOGGER = LoggerFactory.getLogger(AuctionBrowserScreen.class);
private static final Identifier TEXTURE = new Identifier(SkyblockerMod.NAMESPACE, "textures/gui/auctions_gui/browser/background.png");
private static final Identifier SCROLLER_TEXTURE = new Identifier("container/creative_inventory/scroller");
private static final Identifier up_arrow_tex = new Identifier(SkyblockerMod.NAMESPACE, "up_arrow_even"); // Put them in their own fields to avoid object allocation on each frame
private static final Identifier down_arrow_tex = new Identifier(SkyblockerMod.NAMESPACE, "down_arrow_even");
public static final Supplier<Sprite> UP_ARROW = () -> MinecraftClient.getInstance().getGuiAtlasManager().getSprite(up_arrow_tex);
public static final Supplier<Sprite> DOWN_ARROW = () -> MinecraftClient.getInstance().getGuiAtlasManager().getSprite(down_arrow_tex);
// SLOTS
public static final int RESET_BUTTON_SLOT = 47;
public static final int SEARCH_BUTTON_SLOT = 48;
public static final int BACK_BUTTON_SLOT = 49;
public static final int SORT_BUTTON_SLOT = 50;
public static final int RARITY_BUTTON_SLOT = 51;
public static final int AUCTION_TYPE_BUTTON_SLOT = 52;
public static final int PREV_PAGE_BUTTON = 46;
public static final int NEXT_PAGE_BUTTON = 53;
private final Int2BooleanOpenHashMap isSlotHighlighted = new Int2BooleanOpenHashMap(24);
// WIDGETS
private SortWidget sortWidget;
private AuctionTypeWidget auctionTypeWidget;
private RarityWidget rarityWidget;
private ButtonWidget resetFiltersButton;
private final List<CategoryTabWidget> categoryTabWidgets = new ArrayList<>(6);
private String search = "";
public AuctionBrowserScreen(AuctionHouseScreenHandler handler, PlayerInventory inventory) {
super(handler, inventory, Text.literal("Auctions Browser"));
this.backgroundHeight = 187;
this.playerInventoryTitleY = 92;
this.titleX = 999;
}
@Override
protected void init() {
super.init();
sortWidget = new SortWidget(x + 25, y + 81, this::clickSlot);
sortWidget.setSlotId(SORT_BUTTON_SLOT);
addDrawableChild(sortWidget);
auctionTypeWidget = new AuctionTypeWidget(x + 134, y + 77, this::clickSlot);
auctionTypeWidget.setSlotId(AUCTION_TYPE_BUTTON_SLOT);
addDrawableChild(auctionTypeWidget);
rarityWidget = new RarityWidget(x + 73, y + 80, this::clickSlot);
rarityWidget.setSlotId(RARITY_BUTTON_SLOT);
addDrawableChild(rarityWidget);
resetFiltersButton = new ScaledTextButtonWidget(x + 10, y + 77, 12, 12, Text.literal("↻"), this::onResetPressed);
addDrawableChild(resetFiltersButton);
resetFiltersButton.setTooltip(Tooltip.of(Text.literal("Reset Filters")));
resetFiltersButton.setTooltipDelay(500);
addDrawableChild(new ButtonWidget.Builder(Text.literal("<"), button -> this.clickSlot(BACK_BUTTON_SLOT))
.position(x + 98, y + 4)
.size(12, 12)
.build());
if (categoryTabWidgets.isEmpty())
for (int i = 0; i < 6; i++) {
CategoryTabWidget categoryTabWidget = new CategoryTabWidget(new ItemStack(Items.SPONGE), this::clickSlot);
categoryTabWidgets.add(categoryTabWidget);
addSelectableChild(categoryTabWidget); // This method only makes it clickable, does not add it to the drawables list
// manually rendered in the render method to have it not render behind the durability bars
categoryTabWidget.setPosition(x - 30, y + 3 + i * 28);
}
else
for (int i = 0; i < categoryTabWidgets.size(); i++) {
CategoryTabWidget categoryTabWidget = categoryTabWidgets.get(i);
categoryTabWidget.setPosition(x - 30, y + 3 + i * 28);
}
}
private void onResetPressed(ButtonWidget buttonWidget) {
buttonWidget.setFocused(false); // Annoying.
this.clickSlot(RESET_BUTTON_SLOT, 0);
}
@Override
protected void drawBackground(DrawContext context, float delta, int mouseX, int mouseY) {
context.drawTexture(TEXTURE, this.x, this.y, 0, 0, this.backgroundWidth, this.backgroundHeight);
}
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
for (CategoryTabWidget categoryTabWidget : categoryTabWidgets) {
categoryTabWidget.render(context, mouseX, mouseY, delta);
}
if (isWaitingForServer) {
String waiting = "Waiting for server...";
context.drawText(textRenderer, waiting, this.width - textRenderer.getWidth(waiting) - 5, this.height - textRenderer.fontHeight - 2, Colors.WHITE, true);
}
MatrixStack matrices = context.getMatrices();
matrices.push();
matrices.translate(x, y, 0);
// Search
context.enableScissor(x + 7, y + 4, x + 97, y + 16);
context.drawText(textRenderer, Text.literal(search).fillStyle(Style.EMPTY.withUnderline(onSearchField(mouseX, mouseY))), 9, 6, Colors.WHITE, true);
context.disableScissor();
// Scrollbar
if (prevPageVisible) {
if (onScrollbarTop(mouseX, mouseY))
context.drawSprite(159, 13, 0, 6, 3, UP_ARROW.get());
else context.drawSprite(159, 13, 0, 6, 3, UP_ARROW.get(), 0.54f, 0.54f, 0.54f, 1);
}
if (nextPageVisible) {
if (onScrollbarBottom(mouseX, mouseY))
context.drawSprite(159, 72, 0, 6, 3, DOWN_ARROW.get());
else context.drawSprite(159, 72, 0, 6, 3, DOWN_ARROW.get(), 0.54f, 0.54f, 0.54f, 1);
}
context.drawText(textRenderer, String.format("%d/%d", currentPage, totalPages), 111, 6, Colors.GRAY, false);
if (totalPages <= 1)
context.drawGuiTexture(SCROLLER_TEXTURE, 156, 18, 12, 15);
else
context.drawGuiTexture(SCROLLER_TEXTURE, 156, (int) (18 + (float) (Math.min(currentPage, totalPages) - 1) / (totalPages - 1) * 37), 12, 15);
matrices.pop();
this.drawMouseoverTooltip(context, mouseX, mouseY);
}
@Override
protected void drawSlot(DrawContext context, Slot slot) {
if (SkyblockerConfigManager.get().general.fancyAuctionHouse.highlightCheapBIN && isSlotHighlighted.getOrDefault(slot.id, false)) {
context.drawBorder(slot.x, slot.y, 16, 16, new Color(0, 255, 0, 100).getRGB());
}
super.drawSlot(context, slot);
}
@Override
protected void onMouseClick(Slot slot, int slotId, int button, SlotActionType actionType) {
if (slotId >= handler.getRows() * 9) return;
super.onMouseClick(slot, slotId, button, actionType);
}
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (isWaitingForServer) return super.mouseClicked(mouseX, mouseY, button);
if (onScrollbarTop((int) mouseX, (int) mouseY) && prevPageVisible) {
clickSlot(PREV_PAGE_BUTTON);
return true;
}
if (onScrollbarBottom((int) mouseX, (int) mouseY) && nextPageVisible) {
clickSlot(NEXT_PAGE_BUTTON);
return true;
}
if (onSearchField((int) mouseX, (int) mouseY)) {
clickSlot(SEARCH_BUTTON_SLOT);
return true;
}
return super.mouseClicked(mouseX, mouseY, button);
}
private boolean onScrollbarTop(int mouseX, int mouseY) {
int localX = mouseX - x;
int localY = mouseY - y;
return localX > 154 && localX < 169 && localY > 6 && localY < 44;
}
private boolean onScrollbarBottom(int mouseX, int mouseY) {
int localX = mouseX - x;
int localY = mouseY - y;
return localX > 154 && localX < 169 && localY > 43 && localY < 80;
}
private boolean onSearchField(int mouseX, int mouseY) {
int localX = mouseX - x;
int localY = mouseY - y;
return localX > 6 && localX < 97 && localY > 3 && localY < 16;
}
@Override
public void onSlotChange(AuctionHouseScreenHandler handler, int slotId, ItemStack stack) {
if (client == null || stack.isEmpty()) return;
isWaitingForServer = false;
switch (slotId) {
case PREV_PAGE_BUTTON -> {
prevPageVisible = false;
if (stack.isOf(Items.ARROW)) {
prevPageVisible = true;
parsePage(stack);
}
}
case NEXT_PAGE_BUTTON -> {
nextPageVisible = false;
if (stack.isOf(Items.ARROW)) {
nextPageVisible = true;
parsePage(stack);
}
}
case SORT_BUTTON_SLOT ->
sortWidget.setCurrent(SortWidget.Option.get(getOrdinal(ItemUtils.getNbtTooltips(stack))));
case AUCTION_TYPE_BUTTON_SLOT ->
auctionTypeWidget.setCurrent(AuctionTypeWidget.Option.get(getOrdinal(ItemUtils.getNbtTooltips(stack))));
case RARITY_BUTTON_SLOT -> {
List<Text> tooltip = ItemUtils.getNbtTooltips(stack);
int ordinal = getOrdinal(tooltip);
String split = tooltip.get(ordinal + 1).getString().substring(2);
rarityWidget.setText(tooltip.subList(1, tooltip.size() - 3), split);
}
case RESET_BUTTON_SLOT -> {
if (resetFiltersButton != null)
resetFiltersButton.active = handler.getSlot(slotId).getStack().isOf(Items.ANVIL);
}
case SEARCH_BUTTON_SLOT -> {
List<Text> tooltipSearch = ItemUtils.getNbtTooltips(stack);
for (Text text : tooltipSearch) {
String string = text.getString();
if (string.contains("Filtered:")) {
String[] splitSearch = string.split(":");
if (splitSearch.length < 2) {
search = "";
} else search = splitSearch[1].trim();
break;
}
}
}
default -> {
if (slotId < this.handler.getRows() * 9 && slotId % 9 == 0) {
CategoryTabWidget categoryTabWidget = categoryTabWidgets.get(slotId / 9);
categoryTabWidget.setSlotId(slotId);
categoryTabWidget.setIcon(handler.getSlot(slotId).getStack());
List<Text> tooltipDefault = ItemUtils.getNbtTooltips(handler.getSlot(slotId).getStack());
for (int j = tooltipDefault.size() - 1; j >= 0; j--) {
String lowerCase = tooltipDefault.get(j).getString().toLowerCase();
if (lowerCase.contains("currently")) {
categoryTabWidget.setToggled(true);
break;
} else if (lowerCase.contains("click")) {
categoryTabWidget.setToggled(false);
break;
} else categoryTabWidget.setToggled(false);
}
} else if (slotId > 9 && slotId < (handler.getRows() - 1) * 9 && slotId % 9 > 1 && slotId % 9 < 8) {
if (!SkyblockerConfigManager.get().general.fancyAuctionHouse.highlightCheapBIN) return;
List<Text> tooltip = ItemUtils.getNbtTooltips(stack);
for (int k = tooltip.size() - 1; k >= 0; k--) {
Text text = tooltip.get(k);
String string = text.getString();
if (string.toLowerCase().contains("buy it now:")) {
String[] split = string.split(":");
if (split.length < 2) continue;
String coins = split[1].replace(",", "").replace("coins", "").trim();
try {
long parsed = Long.parseLong(coins);
String name = ItemTooltip.getInternalNameFromNBT(stack, false);
String internalID = ItemTooltip.getInternalNameFromNBT(stack, true);
String neuName = name;
if (name == null || internalID == null) break;
if (name.startsWith("ISSHINY_")) {
neuName = internalID;
}
JsonElement jsonElement = TooltipInfoType.THREE_DAY_AVERAGE.getData().get(ItemTooltip.getNeuName(internalID, neuName));
if (jsonElement == null) break;
else {
isSlotHighlighted.put(slotId, jsonElement.getAsDouble() > parsed);
}
} catch (Exception e) {
LOGGER.error("[Skyblocker Fancy Auction House] Failed to parse BIN price", e);
}
}
}
}
}
}
}
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (keyCode == GLFW.GLFW_KEY_UP && prevPageVisible) {
clickSlot(PREV_PAGE_BUTTON);
return true;
}
if (keyCode == GLFW.GLFW_KEY_DOWN && nextPageVisible) {
clickSlot(NEXT_PAGE_BUTTON);
return true;
}
return super.keyPressed(keyCode, scanCode, modifiers);
}
private static int getOrdinal(List<Text> tooltip) {
int ordinal = 0;
for (int j = 0; j < tooltip.size() - 4; j++) {
if (j + 1 >= tooltip.size()) break;
if (tooltip.get(j + 1).getString().contains("▶")) {
ordinal = j;
break;
}
}
return ordinal;
}
int currentPage = 0;
int totalPages = 1;
private boolean prevPageVisible = false;
private boolean nextPageVisible = false;
private void parsePage(ItemStack stack) {
assert client != null;
try {
List<Text> tooltip = ItemUtils.getNbtTooltips(stack);
String str = tooltip.get(0).getString().trim();
str = str.substring(1, str.length() - 1); // remove parentheses
String[] parts = str.split("/"); // split the string
currentPage = Integer.parseInt(parts[0].replace(",", "")); // parse current page
totalPages = Integer.parseInt(parts[1].replace(",", "")); // parse total
} catch (Exception e) {
LOGGER.error("[Skyblocker Fancy Auction House] Failed to parse page arrow", e);
}
}
@Override
protected boolean isClickOutsideBounds(double mouseX, double mouseY, int left, int top, int button) {
return mouseX < (double) left - 32 || mouseY < (double) top || mouseX >= (double) (left + this.backgroundWidth) || mouseY >= (double) (top + this.backgroundHeight);
}
private static class ScaledTextButtonWidget extends ButtonWidget {
protected ScaledTextButtonWidget(int x, int y, int width, int height, Text message, PressAction onPress) {
super(x, y, width, height, message, onPress, Supplier::get);
}
// Code taken mostly from YACL by isxander. Love you <3
@Override
public void drawMessage(DrawContext graphics, TextRenderer textRenderer, int color) {
TextRenderer font = MinecraftClient.getInstance().textRenderer;
MatrixStack pose = graphics.getMatrices();
float textScale = 2.f;
pose.push();
pose.translate(((this.getX() + this.width / 2f) - font.getWidth(getMessage()) * textScale / 2) + 1, (float) this.getY() + (this.height - font.fontHeight * textScale) / 2f - 1, 0);
pose.scale(textScale, textScale, 1);
graphics.drawText(font, getMessage(), 0, 0, color | MathHelper.ceil(this.alpha * 255.0F) << 24, true);
pose.pop();
}
}
}
|