-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBukkitPlayerInventory.kt
59 lines (52 loc) · 2.15 KB
/
BukkitPlayerInventory.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package ru.endlesscode.mimic.inventory
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import org.jetbrains.annotations.ApiStatus
import ru.endlesscode.mimic.ExperimentalMimicApi
import ru.endlesscode.mimic.PlayerSystemProviderService
import ru.endlesscode.mimic.internal.filterNotNullOrEmpty
import ru.endlesscode.mimic.util.ExistingWeakReference
/**
* [PlayerInventory] for Bukkit.
* @since 0.8.0
*/
@ExperimentalMimicApi
@ApiStatus.Experimental
public abstract class BukkitPlayerInventory(player: Player) : PlayerInventory<ItemStack> {
/** The player owning this inventory. */
public val player: Player get() = playerRef.get()
private val playerRef = ExistingWeakReference(player)
/**
* Helper method to get player's inventory contents that is not `null` or AIR.
*
* Appends the given [additionalItems] to this list. Be careful, to avoid items duplicates.
* Elements in the returned list doesn't contain item held in the main hand.
*/
@JvmOverloads
protected fun collectStoredItems(additionalItems: List<ItemStack?> = emptyList()): List<ItemStack> {
val playerInventory = player.inventory
return playerInventory.storageContents
.asSequence()
.plus(additionalItems)
.minus(playerInventory.itemInMainHand)
.filterNotNullOrEmpty()
.toList()
}
/**
* Helper method to get list of player's equipped items. Includes armor and items in hands.
* Doesn't contain `null` or AIR.
*
* Appends the given [additionalItems] to this list. Be careful, to avoid items duplicates.
*/
@JvmOverloads
protected fun collectEquippedItems(additionalItems: List<ItemStack?> = emptyList()): List<ItemStack> {
val playerInventory = player.inventory
return sequenceOf(playerInventory.itemInMainHand, playerInventory.itemInOffHand)
.plus(playerInventory.armorContents)
.plus(additionalItems)
.filterNotNullOrEmpty()
.toList()
}
/** Provider for [BukkitPlayerInventory]. */
public fun interface Provider : PlayerSystemProviderService<BukkitPlayerInventory>
}