Skip to content

Commit 41dcbf4

Browse files
CopilotTrilleo
andauthored
Improvement: Extract duplicated totalPages calculation into single method
Agent-Logs-Url: https://github.com/Trilleo/PaperExamplePlugin/sessions/c2e302db-0e71-4358-9043-640145c129bb Co-authored-by: Trilleo <133519132+Trilleo@users.noreply.github.com>
1 parent f1e41dc commit 41dcbf4

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

‎src/main/kotlin/com/example/exampleplugin/registration/PagedPluginGUI.kt‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ abstract class PagedPluginGUI(
110110
if (page > 0) openPage(player, event.inventory, page - 1)
111111
}
112112
navRowStart + NEXT_OFFSET -> {
113-
val totalPages = totalPages(player)
113+
val totalPages = totalPages(getItems(player).size)
114114
if (page < totalPages - 1) openPage(player, event.inventory, page + 1)
115115
}
116116
else -> {
@@ -127,11 +127,9 @@ abstract class PagedPluginGUI(
127127
// ----- Internal helpers ---------------------------------------------------
128128

129129
/**
130-
* Calculates the total number of pages for the given player based on
131-
* the item count returned by [getItems].
130+
* Calculates the total number of pages based on the given [itemCount].
132131
*/
133-
private fun totalPages(player: Player): Int {
134-
val itemCount = getItems(player).size
132+
private fun totalPages(itemCount: Int): Int {
135133
if (itemCount == 0) return 1
136134
return (itemCount + contentSlots - 1) / contentSlots
137135
}
@@ -147,7 +145,7 @@ abstract class PagedPluginGUI(
147145
inventory.clear()
148146

149147
val items = getItems(player)
150-
val totalPages = if (items.isEmpty()) 1 else (items.size + contentSlots - 1) / contentSlots
148+
val totalPages = totalPages(items.size)
151149
val start = page * contentSlots
152150
val end = minOf(start + contentSlots, items.size)
153151

0 commit comments

Comments
 (0)