Skip to content

Commit cfcbd23

Browse files
committed
Working on pagination
1 parent 7fe5862 commit cfcbd23

File tree

5 files changed

+95
-33
lines changed

5 files changed

+95
-33
lines changed

src/main/java/com/greatorator/tolkienmobs/containers/handlers/ToggleButtonFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,13 @@ public static NumberButton ITEM_PAYMENT_METHOD(int x, int y, int value, Button.O
169169

170170
public static NumberButton ITEM_PAYMENT_METHOD_CLIENT(int x, int y, int value, Button.OnPress onPress) {
171171

172-
return new NumberButton(x, y, 16, 16, value, value, value, itemPaymentButtonLocalization, onPress);
172+
return new NumberButton(x, y, 16, 15, value, value, value, itemPaymentButtonLocalization, onPress);
173173
}
174174
public static NumberButton EXPERIENCE_PAYMENT_METHOD(int x, int y, int value, Button.OnPress onPress) {
175175
return new NumberButton(x, y, 16, 16, value, 1, 1, experiencePaymentButtonLocalization, onPress);
176176
}
177177
public static NumberButton EXPERIENCE_PAYMENT_METHOD_CLIENT(int x, int y, int value, Button.OnPress onPress) {
178-
return new NumberButton(x, y, 16, 16, value, 1, 1, experiencePaymentButtonLocalization, onPress);
178+
return new NumberButton(x, y, 16, 15, value, 1, 1, experiencePaymentButtonLocalization, onPress);
179179
}
180180

181181
/** Set Tick Delay Amount **/

src/main/java/com/greatorator/tolkienmobs/containers/screens/MilestoneScreen.java

Lines changed: 83 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.greatorator.tolkienmobs.containers.screens;
22

33
import com.greatorator.tolkienmobs.TolkienMobsConfig;
4-
import com.greatorator.tolkienmobs.TolkienMobsMain;
54
import com.greatorator.tolkienmobs.block.custom.entity.MilestoneBlockEntity;
65
import com.greatorator.tolkienmobs.containers.MilestoneContainer;
76
import com.greatorator.tolkienmobs.containers.handlers.ToggleButtonFactory;
@@ -30,6 +29,7 @@
3029
import org.lwjgl.glfw.GLFW;
3130

3231
import java.util.ArrayList;
32+
import java.util.Collections;
3333
import java.util.List;
3434
import java.util.Objects;
3535

@@ -44,8 +44,11 @@ public class MilestoneScreen extends AbstractContainerScreen<MilestoneContainer>
4444
private EditBox milestoneName;
4545
protected List<AbstractWidget> widgetsToRemove = new ArrayList<>();
4646
protected List<AbstractWidget> widgetsToAdd = new ArrayList<>();
47-
private final List<AbstractWidget> locationButtons = new ArrayList<>();
47+
private final List<String> locationList = new ArrayList<>();
48+
private static final List<AbstractWidget> locationButtons = new ArrayList<>();
4849
protected boolean renderablesChanged = false;
50+
public static int maxItemsPerPage = 7;
51+
public static int currentPage = 1;
4952

5053
public MilestoneScreen(MilestoneContainer container, Inventory inv, Component title) {
5154
super(container, inv, title);
@@ -79,6 +82,37 @@ public void init() {
7982
addPaymentMethod();
8083
}
8184
addLocations();
85+
86+
int relX = (this.width - this.imageWidth) / 2;
87+
int relY = (this.height - this.imageHeight) / 2;
88+
89+
Button prevButton = Button.builder(Component.literal("<"), button -> {
90+
// Decrement page and rebuild the screen
91+
if (currentPage > 0) {
92+
currentPage--;
93+
this.clearWidgets(); // Clear old widgets
94+
this.init(); // Reinitialize with new page
95+
}
96+
})
97+
.pos(relX + 7, relY + 173)
98+
.size(8, 16)
99+
.build();
100+
prevButton.active = currentPage > 0;
101+
this.addRenderableWidget(prevButton);
102+
103+
Button nextButton = Button.builder(Component.literal(">"), button -> {
104+
// Increment page and rebuild the screen
105+
if (currentPage <= getPageCount() - 1) {
106+
currentPage++;
107+
this.clearWidgets();
108+
this.init();
109+
}
110+
})
111+
.pos(relX + 161, relY + 173)
112+
.size(8, 16)
113+
.build();
114+
nextButton.active = currentPage < getPageCount();
115+
this.addRenderableWidget(nextButton);
82116
tileEntity.updateClientState();
83117
}
84118

@@ -124,43 +158,31 @@ public void addNameField() {
124158
}
125159

126160
public void addLocations() {
127-
int i = -1;
161+
List<String> locationOnPage = getItemsForPage();
162+
int yOffset = 0;
128163

129-
for (MilestoneHandler.MilestoneData data : MilestoneHandler.getKnownByPlayer(Objects.requireNonNull(getMinecraft().player))) {
130-
if (data.getUuid().equals(tileEntity.getUUID())) continue;
131-
i++;
164+
for(String locations : locationOnPage){
132165
int relX = (this.width - this.imageWidth) / 2;
133166
int relY = (this.height - this.imageHeight) / 2;
167+
int y = 66 + (yOffset * 15);
134168

135-
int y = 66 + (i * 16);
136-
this.locationButtons.add(
137-
this.addRenderableWidget(Button.builder(Component.literal(data.getName()),
138-
button -> {
139-
TolkienMobsMain.LOGGER.warn("You teleported!");
140-
TolkienMobsMain.LOGGER.warn(String.valueOf(getMinecraft().player));
141-
TolkienMobsMain.LOGGER.warn(String.valueOf(data.getUuid()));
142-
TolkienMobsMain.LOGGER.warn(String.valueOf(data.getPos()));
143-
TolkienMobsMain.LOGGER.warn(String.valueOf(data.getWorldKey()));
144-
TolkienMobsMain.LOGGER.warn(data.getName());
145-
this.tileEntity.sendPacketToServer(output -> output.writeUUID(data.getUuid()), 3);
146-
// PacketDistributor.sendToServer(new MilestoneTeleportUpdateManager(true));
147-
})
148-
.pos(relX + 5, relY + y)
149-
.size(150, 16)
150-
.build()
151-
)
152-
);
169+
Button itemButton = Button.builder(Component.literal(locations), button -> {
170+
// this.tileEntity.sendPacketToServer(output -> output.writeUUID(data.getUuid()), 3);
171+
})
172+
.pos(relX + 5, relY + y)
173+
.size(150, 15)
174+
.build();
175+
this.addRenderableWidget(itemButton);
153176
if (TolkienMobsConfig.PAYMENT_TYPE.get()) {
154-
addRenderableWidget(ToggleButtonFactory.ITEM_PAYMENT_METHOD_CLIENT(relX + 155, relY + y, this.tileEntity.getItemTravelCost(data),
177+
addRenderableWidget(ToggleButtonFactory.ITEM_PAYMENT_METHOD_CLIENT(relX + 155, relY + y, 1,
155178
(button) -> {
156-
157179
}));
158180
} else {
159-
addRenderableWidget(ToggleButtonFactory.EXPERIENCE_PAYMENT_METHOD_CLIENT(relX + 155, relY + y, this.tileEntity.getExperienceTravelCost(data),
181+
addRenderableWidget(ToggleButtonFactory.EXPERIENCE_PAYMENT_METHOD_CLIENT(relX + 155, relY + y, 1,
160182
(button) -> {
161-
162183
}));
163184
}
185+
yOffset +=1; // Adjust for next button
164186
}
165187
}
166188

@@ -191,6 +213,33 @@ public int adjustNumberButton(int value, int change, int min, int max) {
191213
return value;
192214
}
193215

216+
public int getPageCount(){
217+
int i = -1;
218+
for (MilestoneHandler.MilestoneData data : MilestoneHandler.getKnownByPlayer(Objects.requireNonNull(getMinecraft().player))) {
219+
if (data.getUuid().equals(tileEntity.getUUID())) continue;
220+
i++;
221+
}
222+
return Math.max((int) Math.ceil((double) i / maxItemsPerPage) - 1, 0);
223+
}
224+
225+
private List<String> getItemsForPage() {
226+
int start = currentPage * maxItemsPerPage;
227+
int i = 0;
228+
229+
for (MilestoneHandler.MilestoneData data : MilestoneHandler.getKnownByPlayer(Objects.requireNonNull(getMinecraft().player))) {
230+
if (data.getUuid().equals(tileEntity.getUUID())) continue;
231+
i++;
232+
locationList.add(String.valueOf(data.getName()));
233+
}
234+
235+
int end = Math.min(start + maxItemsPerPage, i);
236+
237+
if (start >= end) {
238+
return Collections.emptyList();
239+
}
240+
return locationList.subList(start, end);
241+
}
242+
194243
@Override
195244
protected void renderTooltip(GuiGraphics pGuiGraphics, int pX, int pY) {
196245
super.renderTooltip(pGuiGraphics, pX, pY);
@@ -203,9 +252,6 @@ protected void renderTooltip(GuiGraphics pGuiGraphics, int pX, int pY) {
203252
if (getMinecraft().player.isCreative()) {
204253
this.milestoneName.setTooltip(Tooltip.create(tooltip));
205254
}
206-
// if (){
207-
// Component.literal("hello");
208-
// }
209255
}
210256

211257
@Override
@@ -214,6 +260,12 @@ public void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, flo
214260
if (getMinecraft().player.isCreative()) {
215261
this.milestoneName.render(guiGraphics, mouseX, mouseY, partialTicks);
216262
}
263+
int relX = (this.width - this.imageWidth) / 2;
264+
int relY = (this.height - this.imageHeight) / 2;
265+
266+
String pagesLabel = GeneralUtility.withSuffix(currentPage) + " / " + GeneralUtility.withSuffix(getPageCount());
267+
guiGraphics.drawString(font, pagesLabel, (relX + 100 - font.width(pagesLabel)), relY + 179, ColorUtility.DARKGRAY.getColor(), false);
268+
217269
this.renderTooltip(guiGraphics, mouseX, mouseY);
218270
}
219271

src/main/java/com/greatorator/tolkienmobs/containers/widget/ButtonTexture.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public enum ButtonTexture implements TexturePosition {
66
SMALL_BUTTON(0, 168, 15, 200),
77
UP_BUTTON(176, 0, 8, 16),
88
DOWN_BUTTON(172, 8, 8, 16),
9+
PREVIOUS_BUTTON(176, 16, 16, 8),
10+
NEXT_BUTTON(184, 16, 16, 8),
911
DEPLOY_BED_BUTTON(208, 0, 16, 16),
1012
PICKUP_BED_BUTTON(208, 16, 16, 16),
1113
DEPLOY_CAMPFIRE_BUTTON(224, 0, 16, 16),

src/main/java/com/greatorator/tolkienmobs/util/GeneralUtility.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ public RedstoneMode next() {
7878
}
7979
}
8080

81+
public static String withSuffix(int count) {
82+
if (count < 1000) return "" + count;
83+
int exp = (int) (Math.log(count) / Math.log(1000));
84+
return String.format("%.1f%c",
85+
count / Math.pow(1000, exp),
86+
"kMGTPE".charAt(exp - 1));
87+
}
88+
8189
public static void unsafeRunWhenOn(Dist dist, Supplier<Runnable> toRun) {
8290
if (dist == FMLEnvironment.dist) {
8391
toRun.get().run();
-8.06 KB
Loading

0 commit comments

Comments
 (0)