Skip to content

Commit 121a7b1

Browse files
committed
Scrollable Text made updatable by recreating ScrollableTextWidget
1 parent 2a5baf5 commit 121a7b1

File tree

5 files changed

+185
-63
lines changed

5 files changed

+185
-63
lines changed

src/client/java/com/coflnet/CoflModClient.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,17 @@ public void onInitializeClient() {
8080
ScreenEvents.beforeRender(gcs).register((screen1, drawContext, mouseX, mouseY, tickDelta) -> {
8181
GenericContainerScreen gcs1 = (GenericContainerScreen) screen1;
8282
if (CoflCore.config.purchaseOverlay != null
83-
&& gcs.getTitle().getLiteralString().contains("BIN Auction View")
84-
&& gcs.getScreenHandler().getInventory().size() == 9 * 6) {
83+
&& (gcs.getTitle().getLiteralString().contains("BIN Auction View")
84+
&& gcs.getScreenHandler().getInventory().size() == 9 * 6
85+
|| gcs.getTitle().getLiteralString().contains("Confirm Purchase")
86+
&& gcs.getScreenHandler().getInventory().size() == 9 * 3)
87+
) {
8588
if (!(client.currentScreen instanceof CoflBinGUI || client.currentScreen instanceof TfmBinGUI)) {
8689
switch (CoflCore.config.purchaseOverlay) {
87-
case COFL: client.setScreen(new CoflBinGUI(Items.BREAD, gcs1.getScreenHandler()));break;
90+
case COFL: client.setScreen(new CoflBinGUI(Items.BREAD, gcs1));break;
8891
case TFM: client.setScreen(new TfmBinGUI(Items.BREAD));break;
8992
case null: default: break;
9093
}
91-
System.out.println("Empty?: " + gcs.getScreenHandler().getInventory().isEmpty());
9294
}
9395
}
9496
});
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.coflnet.gui;
22

33
public enum AuctionStatus{
4+
INIT,
45
SOLD,
56
WAITING,
6-
BUYING
7+
BUYING,
8+
CONFIRMING
79
}

src/client/java/com/coflnet/gui/RenderUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,4 @@ public static String lorem(){
381381
"Lorem ipsum dolor sit amet,";
382382
}
383383
}
384+

src/client/java/com/coflnet/gui/cofl/CoflBinGUI.java

Lines changed: 91 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,62 @@
33
import com.coflnet.gui.AuctionStatus;
44
import com.coflnet.gui.RenderUtils;
55
import com.coflnet.gui.widget.ItemWidget;
6+
import com.coflnet.gui.widget.ScrollableDynamicTextWidget;
67
import net.minecraft.client.MinecraftClient;
78
import net.minecraft.client.gui.DrawContext;
89
import net.minecraft.client.gui.screen.Screen;
10+
import net.minecraft.client.gui.screen.ingame.GenericContainerScreen;
911
import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder;
10-
import net.minecraft.client.gui.widget.ClickableWidget;
11-
import net.minecraft.client.gui.widget.ScrollableTextWidget;
12-
import net.minecraft.client.gui.widget.TextWidget;
13-
import net.minecraft.inventory.Inventory;
14-
import net.minecraft.inventory.InventoryChangedListener;
12+
import net.minecraft.client.gui.widget.*;
13+
import net.minecraft.client.render.item.ItemRenderer;
14+
import net.minecraft.entity.player.PlayerEntity;
1515
import net.minecraft.item.Item;
1616
import net.minecraft.item.ItemStack;
1717
import net.minecraft.item.Items;
1818
import net.minecraft.screen.GenericContainerScreenHandler;
1919
import net.minecraft.screen.ScreenHandler;
2020
import net.minecraft.screen.ScreenHandlerListener;
21+
import net.minecraft.screen.ScreenHandlerType;
22+
import net.minecraft.screen.slot.SlotActionType;
23+
import net.minecraft.text.HoverEvent;
2124
import net.minecraft.text.Text;
2225

23-
public class CoflBinGUI extends Screen implements InventoryChangedListener {
26+
public class CoflBinGUI extends Screen {
2427
private TextWidget titleTextWidget;
2528
private ItemWidget itemWidget;
26-
private ScrollableTextWidget loreScrollableTextWidget;
29+
private ScrollableDynamicTextWidget loreScrollableTextWidget;
2730
private ClickableWidget rightClickableWidget;
2831
private ClickableWidget leftClickableWidget;
2932

3033
private int width;
3134
private int height;
3235
private int p;
3336
private int r;
37+
private final int ITEM_SLOT = 13;
38+
private final int BUY_SLOT = 31;
39+
private final int AUCTION_CANCEL_SLOT = 49;
40+
private final int CONFIRM_SLOT = 11;
41+
private final int CONFIRMATION_CANCEL_SLOT = 15;
42+
private AuctionStatus auctionStatus;
3443
public GenericContainerScreenHandler gcsh;
35-
public Item item = Items.AIR;
44+
public GenericContainerScreen gcs;
3645
public String title = "";
37-
public String lore = RenderUtils.lorem();
46+
public Text lore = Text.of(RenderUtils.lorem());
3847
public String rightButtonText = "";
3948
public int rightButtonCol = 0x00000000;
4049

41-
public CoflBinGUI(Item item, GenericContainerScreenHandler gcsh){
50+
public CoflBinGUI(Item item, GenericContainerScreen gcs){
4251
super(Text.literal("Cofl Bin Gui"));
4352

4453
int screenWidth = MinecraftClient.getInstance().currentScreen.width;
4554
int screenHeight = MinecraftClient.getInstance().currentScreen.height;
4655

47-
this.item = item;
48-
this.gcsh = gcsh;
56+
this.gcs = gcs;
57+
this.gcsh = gcs.getScreenHandler();
58+
59+
if (gcsh.getType() == ScreenHandlerType.GENERIC_9X3){
60+
this.auctionStatus = AuctionStatus.CONFIRMING;
61+
} else this.auctionStatus = AuctionStatus.INIT;
4962

5063
this.width = screenWidth / 2;
5164
if (width < 300) this.width = 300;
@@ -73,15 +86,19 @@ protected void renderWidget(DrawContext context, int mouseX, int mouseY, float d
7386
protected void appendClickableNarrations(NarrationMessageBuilder builder) {}
7487

7588
@Override
76-
public void onClick(double mouseX, double mouseY) {close();}
89+
public void onClick(double mouseX, double mouseY) {
90+
System.out.println(auctionStatus.name());
91+
if (auctionStatus != AuctionStatus.CONFIRMING) clickSlot(AUCTION_CANCEL_SLOT);
92+
else clickSlot(CONFIRMATION_CANCEL_SLOT);
93+
}
7794
};
7895

7996
rightClickableWidget = new ClickableWidget(
8097
screenWidth / 2 - width / 2, //screenWidth / 2 - width / 2 + p + width / 5 * 2,
8198
screenHeight / 2 - height / 2, //screenHeight / 2 + height / 2 - p - (225 - 150 - 12 - p*5) - screenHeight / 15,
8299
width, //width / 5 * 3 - p*2,
83100
height, //225 - 150 - 12 - p*5 + screenHeight / 15,
84-
Text.of(rightButtonText)
101+
Text.of("")
85102
){
86103
@Override
87104
protected void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) {
@@ -90,7 +107,7 @@ protected void renderWidget(DrawContext context, int mouseX, int mouseY, float d
90107
screenHeight / 2 + height / 2 - p - (225 - 150 - 12 - p*5) - screenHeight / 15,
91108
width / 5 * 3 - p*2,
92109
225 - 150 - 12 - p*5 + screenHeight / 15,
93-
r, CoflColConfig.CONFIRM
110+
r, rightButtonCol
94111
);
95112
RenderUtils.drawString(context,
96113
this.getMessage().getString(),
@@ -99,14 +116,18 @@ protected void renderWidget(DrawContext context, int mouseX, int mouseY, float d
99116
0xFFEEEEEE
100117
);
101118
}
119+
102120
@Override
103121
protected void appendClickableNarrations(NarrationMessageBuilder builder) {}
104122

105123
@Override
106124
public void onClick(double mouseX, double mouseY) {
107125
if (leftClickableWidget.isMouseOver(mouseX, mouseY)) {
108126
leftClickableWidget.onClick(mouseX, mouseY);
109-
} else MinecraftClient.getInstance().player.sendMessage(Text.of("Confim clicked"));
127+
} else {
128+
if(auctionStatus != AuctionStatus.CONFIRMING) clickSlot(BUY_SLOT);
129+
else clickSlot(CONFIRM_SLOT);
130+
}
110131
}
111132
};
112133

@@ -118,25 +139,30 @@ public void onClick(double mouseX, double mouseY) {
118139
MinecraftClient.getInstance().textRenderer
119140
).alignLeft();
120141

121-
loreScrollableTextWidget = new ScrollableTextWidget(
142+
loreScrollableTextWidget = new ScrollableDynamicTextWidget(
122143
screenWidth / 2 - width / 2 + p + 20 + p + 4,
123144
screenHeight / 2 - height / 2 + p + 12 + p + 2,
124145
width - 20 - p*4 - 4, height - 75 - 2 - screenHeight / 15,
125-
Text.of(lore), MinecraftClient.getInstance().textRenderer
146+
lore, MinecraftClient.getInstance().textRenderer
126147
){
127148
@Override
128149
protected void drawBox(DrawContext context) {}
129150

130151
@Override
131-
public void onClick(double mouseX, double mouseY) {
132-
super.onClick(mouseX, mouseY);
152+
public boolean mouseReleased(double mouseX, double mouseY, int button) {
153+
if(super.mouseReleased(mouseX, mouseY, button) && isMouseOver(mouseX,mouseY) && button == 0){
154+
rightClickableWidget.onClick(mouseX,mouseY);
155+
}
156+
return super.mouseReleased(mouseX, mouseY, button);
133157
}
134158
};
159+
//loreScrollableTextWidget.updateText(Text.of("AAAAAAAAAAAAAa"));
160+
135161

136162
itemWidget = new ItemWidget(
137163
screenWidth / 2 - width / 2 + p + 2,
138164
screenHeight / 2 - height / 2 + p + 12 + p + 2,
139-
this.item.getDefaultStack()
165+
Items.AIR.getDefaultStack()
140166
);
141167

142168
this.addDrawableChild(titleTextWidget);
@@ -148,88 +174,95 @@ public void onClick(double mouseX, double mouseY) {
148174
gcsh.addListener(new ScreenHandlerListener() {
149175
@Override
150176
public void onSlotUpdate(ScreenHandler handler, int slotId, ItemStack stack) {
151-
if (stack.getItem() != Items.AIR) System.out.println("slotid: "+slotId);
152-
switch (slotId){
153-
case 13:
154-
setItem(stack.getItem());
155-
break;
156-
case 31:
157-
switch (getAuctionStatus(stack.getItem())){
158-
case WAITING -> setRightButtonConfig(AuctionStatus.WAITING);
159-
case BUYING -> setRightButtonConfig(AuctionStatus.BUYING);
160-
case SOLD -> setRightButtonConfig(AuctionStatus.SOLD);
161-
case null, default -> System.out.println("Slot 31 empty");
162-
}
163-
break;
164-
case 41:
165-
break;
166-
}
177+
//if (stack.getItem() != Items.AIR) System.out.println("slotid: "+slotId);
178+
if (slotId == ITEM_SLOT) setItem(stack);
179+
if (auctionStatus != AuctionStatus.CONFIRMING){
180+
if(slotId == BUY_SLOT) setRightButtonConfig(setAuctionStatus(stack.getItem()));
181+
} else if (auctionStatus == AuctionStatus.CONFIRMING && slotId == CONFIRM_SLOT) setRightButtonConfig(AuctionStatus.CONFIRMING);
167182
}
168183

169184
@Override
170185
public void onPropertyUpdate(ScreenHandler handler, int property, int value) {}
171186
});
172187
}
173188

174-
private AuctionStatus getAuctionStatus(Item item){
175-
if (item == Items.GOLD_NUGGET) return AuctionStatus.BUYING;
176-
if (item == Items.RED_BED) return AuctionStatus.WAITING;
177-
if (item == Items.POTATO) return AuctionStatus.SOLD;
178-
return null;
189+
private void clickSlot(int slotId) {
190+
PlayerEntity player = client.player;
191+
192+
client.interactionManager.clickSlot(
193+
gcsh.syncId,
194+
slotId,
195+
0,
196+
SlotActionType.PICKUP,
197+
player
198+
);
199+
}
200+
201+
private AuctionStatus setAuctionStatus(Item item){
202+
if (item == Items.GOLD_NUGGET) auctionStatus = AuctionStatus.BUYING;
203+
if (item == Items.RED_BED) auctionStatus = AuctionStatus.WAITING;
204+
if (item == Items.POTATO) auctionStatus = AuctionStatus.SOLD;
205+
return auctionStatus;
179206
}
180207

181208
private void setRightButtonConfig(AuctionStatus auctionStatus){
182209
switch (auctionStatus){
210+
case INIT:
211+
rightButtonCol = CoflColConfig.CONFIRM;
212+
rightClickableWidget.setMessage(Text.of("INIT"));
213+
break;
183214
case BUYING:
184215
rightButtonCol = CoflColConfig.CONFIRM;
185-
rightButtonText = "BUY ITEM";
216+
rightClickableWidget.setMessage(Text.of("BUY ITEM"));
186217
break;
187218
case SOLD:
188219
rightButtonCol = CoflColConfig.UNAVAILABLE;
189-
rightButtonText = "SOLD";
220+
rightClickableWidget.setMessage(Text.of("SOLD"));
190221
break;
191222
case WAITING:
192223
rightButtonCol = CoflColConfig.UNAVAILABLE;
193-
rightButtonText = "WAITING";
224+
rightClickableWidget.setMessage(Text.of("WAITING"));
225+
break;
226+
case CONFIRMING:
227+
rightButtonCol = CoflColConfig.CONFIRM;
228+
rightClickableWidget.setMessage(Text.of("CONFIRM"));
194229
break;
195-
196230
}
197231
}
198232

199-
public void setItem(Item item) {
200-
this.item = item;
233+
public void setItem(ItemStack item) {
234+
lore = Text.empty();
235+
titleTextWidget.setMessage(item.getName());
236+
Text firstEntry = getTooltipFromItem(MinecraftClient.getInstance(), item).getFirst();
237+
lore = firstEntry.getWithStyle(firstEntry.getStyle()).getLast();
238+
239+
loreScrollableTextWidget.updateText(lore);
240+
this.itemWidget.item = item;
201241
}
202242

203243
@Override
204244
public boolean shouldPause() {
205245
return false;
206246
}
207247

208-
@Override
209-
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
210-
super.render(context, mouseX, mouseY, delta);
211-
}
212-
213248
@Override
214249
public void renderBackground(DrawContext drawContext, int mouseX, int mouseY, float delta) {
215250
int screenWidth = MinecraftClient.getInstance().currentScreen.width;
216251
int screenHeight = MinecraftClient.getInstance().currentScreen.height;
217252

218253
// Background
219254
RenderUtils.drawRoundedRect(drawContext, screenWidth / 2 - width / 2,screenHeight / 2 - height / 2, width, height, r, CoflColConfig.BACKGROUND_PRIMARY);
220-
221255
// Title Background
222256
RenderUtils.drawRoundedRect(drawContext, screenWidth / 2 - width / 2 + p,screenHeight / 2 - height / 2 + p, width - p*2, 12, r, CoflColConfig.BACKGROUND_SECONDARY);
223-
224257
// Item Background
225258
RenderUtils.drawRoundedRect(drawContext, screenWidth / 2 - width / 2 + p,screenHeight / 2 - height / 2 + p + 12 + p, 20, 20, r, CoflColConfig.BACKGROUND_SECONDARY);
226-
227259
// Description Background
228260
RenderUtils.drawRoundedRect(drawContext,screenWidth / 2 - width / 2 + p + 20 + p, screenHeight / 2 - height / 2 + p + 12+ p, width - 20 - p*3, height - 75 - screenHeight / 15, r, CoflColConfig.BACKGROUND_SECONDARY);
229261
}
230262

231263
@Override
232-
public void onInventoryChanged(Inventory sender) {
233-
System.out.println("Inv changed");
264+
public void close() {
265+
gcs.close();
266+
super.close();
234267
}
235268
}

0 commit comments

Comments
 (0)