From a7051291e78fdd9ba2cf1c8459356bee8a16e283 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Mon, 15 May 2023 19:05:40 +0100 Subject: [PATCH] LevelComponent now supports infinite levelling --- build.gradle | 2 +- .../ecomponent/components/LevelComponent.kt | 60 +++++++++---------- gradle.properties | 2 +- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/build.gradle b/build.gradle index 1487910..53749fd 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } diff --git a/eco-api/src/main/kotlin/com/willfp/ecomponent/components/LevelComponent.kt b/eco-api/src/main/kotlin/com/willfp/ecomponent/components/LevelComponent.kt index d2e154d..5982b90 100644 --- a/eco-api/src/main/kotlin/com/willfp/ecomponent/components/LevelComponent.kt +++ b/eco-api/src/main/kotlin/com/willfp/ecomponent/components/LevelComponent.kt @@ -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 @@ -30,7 +31,9 @@ enum class LevelState( /** Component to display level progression, for Skills/Jobs/etc. */ @Suppress("MemberVisibilityCanBePrivate") abstract class LevelComponent : AutofillComponent() { - private val slots = mutableMapOf>() + private val slots = nestedMap() + + private val progressionSlots = mutableMapOf() private var isBuilt = false @@ -38,12 +41,36 @@ abstract class LevelComponent : AutofillComponent() { 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() @@ -71,7 +98,6 @@ abstract class LevelComponent : AutofillComponent() { private fun build() { isBuilt = true - val progressionSlots = mutableMapOf() var x = 0 for (row in pattern) { @@ -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 { diff --git a/gradle.properties b/gradle.properties index 27a7cb5..598d952 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ -version = 1.4.0 +version = 1.4.1 kotlin.code.style = official \ No newline at end of file