Skip to content

Commit

Permalink
LevelComponent now supports infinite levelling
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed May 15, 2023
1 parent 2a31d8d commit a705129
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ allprojects {
}

dependencies {
compileOnly 'com.willfp:eco:6.46.0'
compileOnly 'com.willfp:eco:6.59.1'
compileOnly 'org.jetbrains:annotations:23.0.0'
compileOnly 'org.jetbrains.kotlin:kotlin-stdlib:1.7.20'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.willfp.ecomponent.components
import com.willfp.eco.core.gui.menu.Menu
import com.willfp.eco.core.gui.slot
import com.willfp.eco.core.gui.slot.Slot
import com.willfp.eco.core.map.nestedMap
import com.willfp.ecomponent.AutofillComponent
import com.willfp.ecomponent.GUIPosition
import org.bukkit.entity.Player
Expand Down Expand Up @@ -30,20 +31,46 @@ enum class LevelState(
/** Component to display level progression, for Skills/Jobs/etc. */
@Suppress("MemberVisibilityCanBePrivate")
abstract class LevelComponent : AutofillComponent() {
private val slots = mutableMapOf<Int, MutableMap<GUIPosition, Slot>>()
private val slots = nestedMap<GUIPosition, Int, Slot?>()

private val progressionSlots = mutableMapOf<GUIPosition, Int>()

private var isBuilt = false

abstract val pattern: List<String>

abstract val maxLevel: Int

private fun buildSlot(level: Int) = slot { player, menu ->
getLevelItem(
player,
menu,
level,
getLevelState(
player,
level
)
)
}

override fun getSlotAt(row: Int, column: Int, player: Player, menu: Menu): Slot? {
if (!isBuilt) {
build()
}

return slots[menu.getPage(player)]?.get(GUIPosition(row, column))
val position = GUIPosition(row, column)

return slots[position].getOrPut(menu.getPage(player)) {
val offset = progressionSlots[position] ?: return null
val base = (menu.getPage(player) - 1) * levelsPerPage
val level = offset + base

if (level > maxLevel) {
null
} else {
buildSlot(level)
}
}
}

private var _levelsPerPage by Delegates.notNull<Int>()
Expand Down Expand Up @@ -71,7 +98,6 @@ abstract class LevelComponent : AutofillComponent() {
private fun build() {
isBuilt = true

val progressionSlots = mutableMapOf<Int, GUIPosition>()

var x = 0
for (row in pattern) {
Expand All @@ -89,38 +115,12 @@ abstract class LevelComponent : AutofillComponent() {
continue
}

progressionSlots[pos + 1] = GUIPosition(x, y)
progressionSlots[GUIPosition(x, y)] = pos + 1
}
}

_levelsPerPage = progressionSlots.size
_pages = ceil(maxLevel.toDouble() / levelsPerPage).toInt()

for (page in 1..pages) {
for ((levelOffset, position) in progressionSlots) {
val level = ((page - 1) * levelsPerPage) + levelOffset

if (level > maxLevel) {
continue
}

val pageSlots = slots[page] ?: mutableMapOf()

pageSlots[position] = slot { player, menu ->
getLevelItem(
player,
menu,
level,
getLevelState(
player,
level
)
)
}

slots[page] = pageSlots
}
}
}

fun getPageOf(level: Int): Int {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = 1.4.0
version = 1.4.1
kotlin.code.style = official

0 comments on commit a705129

Please sign in to comment.