Skip to content

Commit 283b809

Browse files
committed
Fixed ItemHolderFinder bug, optimised NumericSlotType
1 parent d3c42ee commit 283b809

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

Diff for: core/src/main/kotlin/com/willfp/libreforge/slot/ItemHolderFinder.kt

+12-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ import com.willfp.libreforge.HolderProvider
88
import com.willfp.libreforge.TypedHolderProvider
99
import com.willfp.libreforge.TypedProvidedHolder
1010
import com.willfp.libreforge.get
11+
import com.willfp.libreforge.ifType
12+
import com.willfp.libreforge.isType
1113
import com.willfp.libreforge.registerRefreshFunction
14+
import com.willfp.libreforge.slot.impl.NumericSlotType
1215
import org.bukkit.entity.LivingEntity
16+
import org.bukkit.entity.Player
1317
import org.bukkit.inventory.ItemStack
1418
import java.util.UUID
1519
import java.util.concurrent.TimeUnit
@@ -37,10 +41,6 @@ abstract class ItemHolderFinder<T : Holder> {
3741
* Find holders on an [entity] for a given [slot].
3842
*/
3943
fun findHolders(entity: LivingEntity, slot: SlotType): List<TypedProvidedHolder<T>> {
40-
return doFindHolders(entity, slot)
41-
}
42-
43-
private fun doFindHolders(entity: LivingEntity, slot: SlotType): List<TypedProvidedHolder<T>> {
4444
val items = slot.getItems(entity)
4545

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

77+
val slots = SlotTypes.baseTypes.toMutableSet()
78+
79+
// Prevents double scanning of held item slot
80+
dispatcher.ifType<Player> {
81+
slots.remove(NumericSlotType(it.inventory.heldItemSlot))
82+
}
83+
7784
// Only check for non-combined slot types
78-
SlotTypes.baseTypes.flatMap { slot -> findHolders(entity, slot) }
85+
slots.flatMap { slot -> findHolders(entity, slot) }
7986
}
8087
}
8188
}

Diff for: core/src/main/kotlin/com/willfp/libreforge/slot/impl/NumericSlotType.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import org.bukkit.inventory.ItemStack
1010
class NumericSlotType(
1111
private val slot: Int
1212
) : SlotType(slot.toString()) {
13+
private val slotList = slot.toSingletonList()
14+
1315
override fun addToSlot(player: Player, item: ItemStack): Boolean {
1416
if (!player.inventory.getItem(slot).isEcoEmpty) {
1517
return false
@@ -28,6 +30,6 @@ class NumericSlotType(
2830
}
2931

3032
override fun getItemSlots(player: Player): List<Int> {
31-
return slot.toSingletonList()
33+
return slotList
3234
}
3335
}

0 commit comments

Comments
 (0)