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
|
package de.hysky.skyblocker.skyblock.auction;
import de.hysky.skyblocker.SkyblockerMod;
import de.hysky.skyblocker.utils.ItemUtils;
import de.hysky.skyblocker.utils.render.gui.AbstractCustomHypixelGUI;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.PopupScreen;
import net.minecraft.client.gui.tooltip.Tooltip;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.DirectionalLayoutWidget;
import net.minecraft.client.gui.widget.SimplePositioningWidget;
import net.minecraft.client.gui.widget.TextWidget;
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.SlotActionType;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.text.TextColor;
import net.minecraft.util.Colors;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;
import java.time.Duration;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
public class AuctionViewScreen extends AbstractCustomHypixelGUI<AuctionHouseScreenHandler> {
protected static final Identifier BACKGROUND_TEXTURE = new Identifier(SkyblockerMod.NAMESPACE, "textures/gui/auctions_gui/browser/background_view.png");
public static final int BACK_BUTTON_SLOT = 49;
DirectionalLayoutWidget verticalLayout = DirectionalLayoutWidget.vertical();
public final boolean isBinAuction;
private TextWidget priceWidget;
private final Text clickToEditBidText = Text.translatable("skyblocker.fancyAuctionHouse.editBid").setStyle(Style.EMPTY.withUnderline(true));
private TextWidget infoTextWidget;
public String minBid = "";
private BuyState buyState = null;
private MutableText priceText = Text.literal("?");
private ButtonWidget buyButton;
private TextWidget priceTextWidget;
public AuctionViewScreen(AuctionHouseScreenHandler handler, PlayerInventory inventory, Text title) {
super(handler, inventory, title);
backgroundHeight = 187;
isBinAuction = this.getTitle().getString().toLowerCase().contains("bin");
playerInventoryTitleY = 93;
titleX = 5;
titleY = 4;
}
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (keyCode == GLFW.GLFW_KEY_ESCAPE) {
clickSlot(BACK_BUTTON_SLOT);
return true;
}
return super.keyPressed(keyCode, scanCode, modifiers);
}
@Override
protected void init() {
super.init();
verticalLayout.spacing(2).getMainPositioner().alignHorizontalCenter();
priceTextWidget = new TextWidget(isBinAuction ? Text.translatable("skyblocker.fancyAuctionHouse.price") : Text.translatable("skyblocker.fancyAuctionHouse.newBid"), textRenderer).alignCenter();
verticalLayout.add(priceTextWidget);
priceWidget = new TextWidget(Text.literal("?"), textRenderer).alignCenter();
priceWidget.setWidth(textRenderer.getWidth(clickToEditBidText));
priceWidget.active = true;
verticalLayout.add(priceWidget);
infoTextWidget = new TextWidget(Text.literal("Can't Afford"), textRenderer).alignCenter();
verticalLayout.add(infoTextWidget);
buyButton = ButtonWidget.builder(isBinAuction ? Text.translatable("skyblocker.fancyAuctionHouse.buy") : Text.translatable("skyblocker.fancyAuctionHouse.bid"), button -> {
if (buySlotID == -1) return;
clickSlot(buySlotID);
}).size(60, 15).build();
verticalLayout.add(buyButton);
verticalLayout.forEachChild(this::addDrawableChild);
updateLayout();
ButtonWidget backButton = new ButtonWidget.Builder(Text.literal("<"), button -> this.clickSlot(BACK_BUTTON_SLOT))
.position(x + backgroundWidth - 16, y + 4)
.size(12, 12)
.tooltip(Tooltip.of(Text.literal("or press ESC!")))
.build();
backButton.setTooltipDelay(Duration.ofSeconds(1));
addDrawableChild(backButton);
}
private void changeState(BuyState newState) {
if (newState == buyState) return;
buyState = newState;
switch (buyState) {
case CANT_AFFORD -> {
infoTextWidget.setMessage(Text.translatable("skyblocker.fancyAuctionHouse.cantAfford").withColor(Colors.RED));
buyButton.active = false;
}
case TOP_BID ->
infoTextWidget.setMessage(Text.translatable("skyblocker.fancyAuctionHouse.alreadyTopBid").withColor(Colors.LIGHT_YELLOW));
case AFFORD -> infoTextWidget.setMessage(Text.empty());
case COLLECT_AUCTION -> {
infoTextWidget.setMessage(changeProfile ? Text.translatable("skyblocker.fancyAuctionHouse.differentProfile") : wonAuction ? Text.empty() : Text.translatable("skyblocker.fancyAuctionHouse.didntWin"));
//priceWidget.setMessage(Text.empty());
priceWidget.active = false;
if (changeProfile) {
buyButton.setMessage(Text.translatable("skyblocker.fancyAuctionHouse.changeProfile").setStyle(Style.EMPTY.withColor(Formatting.AQUA)));
} else if (wonAuction) {
buyButton.setMessage(Text.translatable("skyblocker.fancyAuctionHouse.collectAuction"));
} else {
buyButton.setMessage(Text.translatable("skyblocker.fancyAuctionHouse.collectBid"));
}
buyButton.setWidth(textRenderer.getWidth(buyButton.getMessage()) + 4);
priceTextWidget.setMessage(Text.translatable("skyblocker.fancyAuctionHouse.auctionEnded"));
priceTextWidget.setWidth(textRenderer.getWidth(priceTextWidget.getMessage()));
}
case CANCELLABLE_AUCTION -> {
buyButton.setMessage(Text.translatable("skyblocker.fancyAuctionHouse.cancelAuction").setStyle(Style.EMPTY.withColor(Formatting.RED)));
buyButton.setWidth(textRenderer.getWidth(buyButton.getMessage()) + 4);
buyButton.active = true;
buyButton.visible = true;
}
case OWN_AUCTION -> {
buyButton.visible = false;
priceWidget.active = false;
infoTextWidget.setMessage(Text.translatable("skyblocker.fancyAuctionHouse.yourAuction"));
}
}
infoTextWidget.setWidth(textRenderer.getWidth(infoTextWidget.getMessage()));
updateLayout();
}
private void updateLayout() {
verticalLayout.refreshPositions();
SimplePositioningWidget.setPos(verticalLayout, x, y + 36, backgroundWidth, 60);
}
@Override
protected void drawBackground(DrawContext context, float delta, int mouseX, int mouseY) {
context.drawTexture(BACKGROUND_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);
if (isWaitingForServer) context.drawText(textRenderer, "Waiting...", 0, 0, Colors.WHITE, true);
MatrixStack matrices = context.getMatrices();
matrices.push();
matrices.translate(x + 77, y + 14, 0);
matrices.scale(1.375f, 1.375f, 1.375f);
//matrices.translate(0, 0, 100f);
ItemStack stack = handler.getSlot(13).getStack();
context.drawItem(stack, 0, 0);
context.drawItemInSlot(textRenderer, stack, 0, 0);
matrices.pop();
if (!isBinAuction && buyState != BuyState.COLLECT_AUCTION) {
if (priceWidget.isMouseOver(mouseX, mouseY) && buyState != BuyState.CANT_AFFORD) {
priceWidget.setMessage(clickToEditBidText);
} else {
priceWidget.setMessage(priceText);
}
}
drawMouseoverTooltip(context, mouseX, mouseY);
}
@Override
protected void drawMouseoverTooltip(DrawContext context, int x, int y) {
super.drawMouseoverTooltip(context, x, y);
if (x > this.x + 75 && x < this.x + 75 + 26 && y > this.y + 13 && y < this.y + 13 + 26) {
context.drawTooltip(this.textRenderer, this.getTooltipFromItem(handler.getSlot(13).getStack()), x, y);
}
}
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (!isBinAuction && priceWidget.isMouseOver(mouseX, mouseY)) {
clickSlot(31);
return true;
}
return super.mouseClicked(mouseX, mouseY, button);
}
@Override
public void onSlotChange(AuctionHouseScreenHandler handler, int slotId, ItemStack stack) {
if (stack.isOf(Items.BLACK_STAINED_GLASS_PANE) || slotId == 13 || slotId >= handler.getRows() * 9) return;
assert client != null;
if (stack.isOf(Items.RED_TERRACOTTA)) { // Red terracotta shows up when you can cancel it
changeState(BuyState.CANCELLABLE_AUCTION);
buySlotID = slotId;
}
if (priceParsed) return;
if (stack.isOf(Items.POISONOUS_POTATO)) {
changeState(BuyState.CANT_AFFORD);
getPriceFromTooltip(ItemUtils.getLore(stack));
buySlotID = slotId;
} else if (stack.isOf(Items.GOLD_NUGGET)) {
changeState(BuyState.AFFORD);
getPriceFromTooltip(ItemUtils.getLore(stack));
buySlotID = slotId;
} else if (stack.isOf(Items.GOLD_BLOCK)) {
changeState(BuyState.TOP_BID);
getPriceFromTooltip(ItemUtils.getLore(stack));
buySlotID = slotId;
} else if (stack.isOf(Items.NAME_TAG)) {
getPriceFromTooltip(ItemUtils.getLore(stack));
changeProfile = true;
buySlotID = slotId;
}
String lowerCase = stack.getName().getString().toLowerCase();
if (priceParsed && lowerCase.contains("collect auction")) {
changeState(BuyState.COLLECT_AUCTION);
}
}
private int buySlotID = -1;
private boolean priceParsed = false;
private boolean wonAuction = true;
private boolean changeProfile = false;
private void getPriceFromTooltip(List<Text> tooltip) {
if (priceParsed) return;
String minBid = null;
String priceString = null;
AtomicReference<String> stringAtomicReference = new AtomicReference<>("");
for (Text text : tooltip) {
String string = text.getString();
String thingToLookFor = (isBinAuction) ? "price:" : "new bid:";
String lowerCase = string.toLowerCase();
if (lowerCase.contains(thingToLookFor)) {
String[] split = string.split(":");
if (split.length < 2) continue;
priceString = split[1].trim();
} else if (lowerCase.contains("minimum bid:") && !isBinAuction) {
String[] split = string.split(":");
if (split.length < 2) continue;
minBid = split[1].replace("coins", "").replace(",", "").trim();
} else if (lowerCase.contains("you pay:")) {
String[] split = string.split(":");
if (split.length < 2) continue;
if (buyState != BuyState.CANT_AFFORD && !isBinAuction) {
infoTextWidget.setMessage(Text.translatable("skyblocker.fancyAuctionHouse.youPay", split[1].trim()));
infoTextWidget.setWidth(textRenderer.getWidth(infoTextWidget.getMessage()));
}
} else if (lowerCase.contains("top bid:")) { // Shows up when an auction ended and you lost
wonAuction = false;
} else if (lowerCase.contains("correct profile")) { // When an auction ended but on a different profile
changeProfile = true;
priceWidget.setMessage(Text.empty());
} else if (lowerCase.contains("own auction")) { // it's yours
changeState(BuyState.OWN_AUCTION);
}
text.visit((style, asString) -> {
// The regex removes [, ] and +. To ignore mvp++ rank and orange + in mvp+
String res = Objects.equals(style.getColor(), TextColor.fromFormatting(Formatting.GOLD)) && !asString.matches(".*[]\\[+].*") && !asString.contains("Collect") ? asString : null;
return Optional.ofNullable(res);
}, Style.EMPTY).ifPresent(s -> stringAtomicReference.set(stringAtomicReference.get() + s));
}
if (priceString == null) priceString = stringAtomicReference.get();
if (minBid != null) this.minBid = minBid;
else this.minBid = priceString;
priceText = Text.literal(priceString).setStyle(Style.EMPTY.withFormatting(Formatting.BOLD).withColor(Formatting.GOLD));
priceWidget.setMessage(priceText);
int width = textRenderer.getWidth(priceText);
if (width > priceWidget.getWidth()) priceWidget.setWidth(width);
priceParsed = true;
updateLayout();
}
public PopupScreen getConfirmPurchasePopup(Text title) {
// This really shouldn't be possible to be null in its ACTUAL use case.
//noinspection DataFlowIssue
return new PopupScreen.Builder(this, title)
.button(Text.translatable("text.skyblocker.confirm"), popupScreen -> this.client.interactionManager.clickSlot(this.client.player.currentScreenHandler.syncId, 11, 0, SlotActionType.PICKUP, client.player))
.button(Text.translatable("gui.cancel"), popupScreen -> this.client.interactionManager.clickSlot(this.client.player.currentScreenHandler.syncId, 15, 0, SlotActionType.PICKUP, client.player))
.message((isBinAuction ? Text.translatable("skyblocker.fancyAuctionHouse.price") : Text.translatable("skyblocker.fancyAuctionHouse.newBid")).append(" ").append(priceText)).build();
}
private enum BuyState {
CANT_AFFORD,
AFFORD,
TOP_BID,
COLLECT_AUCTION,
CANCELLABLE_AUCTION,
OWN_AUCTION
}
}
|