Skip to content

Commit

Permalink
Fixed ItemHolderFinder bug, optimised NumericSlotType
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed May 11, 2024
1 parent d3c42ee commit 283b809
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import com.willfp.libreforge.HolderProvider
import com.willfp.libreforge.TypedHolderProvider
import com.willfp.libreforge.TypedProvidedHolder
import com.willfp.libreforge.get
import com.willfp.libreforge.ifType
import com.willfp.libreforge.isType
import com.willfp.libreforge.registerRefreshFunction
import com.willfp.libreforge.slot.impl.NumericSlotType
import org.bukkit.entity.LivingEntity
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import java.util.UUID
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -37,10 +41,6 @@ abstract class ItemHolderFinder<T : Holder> {
* Find holders on an [entity] for a given [slot].
*/
fun findHolders(entity: LivingEntity, slot: SlotType): List<TypedProvidedHolder<T>> {
return doFindHolders(entity, slot)
}

private fun doFindHolders(entity: LivingEntity, slot: SlotType): List<TypedProvidedHolder<T>> {
val items = slot.getItems(entity)

val holders = items.map { item ->
Expand Down Expand Up @@ -74,8 +74,15 @@ abstract class ItemHolderFinder<T : Holder> {
return cache.get(dispatcher.uuid) {
val entity = dispatcher.get<LivingEntity>() ?: return@get emptyList()

val slots = SlotTypes.baseTypes.toMutableSet()

// Prevents double scanning of held item slot
dispatcher.ifType<Player> {
slots.remove(NumericSlotType(it.inventory.heldItemSlot))
}

// Only check for non-combined slot types
SlotTypes.baseTypes.flatMap { slot -> findHolders(entity, slot) }
slots.flatMap { slot -> findHolders(entity, slot) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import org.bukkit.inventory.ItemStack
class NumericSlotType(
private val slot: Int
) : SlotType(slot.toString()) {
private val slotList = slot.toSingletonList()

override fun addToSlot(player: Player, item: ItemStack): Boolean {
if (!player.inventory.getItem(slot).isEcoEmpty) {
return false
Expand All @@ -28,6 +30,6 @@ class NumericSlotType(
}

override fun getItemSlots(player: Player): List<Int> {
return slot.toSingletonList()
return slotList
}
}

0 comments on commit 283b809

Please sign in to comment.