Skip to content

Commit 6268cc4

Browse files
committed
feat: Add Mimic PlayerInventory API hook
1 parent 625adb4 commit 6268cc4

5 files changed

Lines changed: 66 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Added
44

55
- [Documentation](https://endlesscodegroup.github.io/ECInventory/docs/intro)
6+
- Support of Mimic PlayerInventory API to integrate with other plugins
67

78
### Changed
89

core/src/main/kotlin/ECInventoryPlugin.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.bukkit.plugin.java.JavaPlugin
2424
import ru.endlesscode.inventory.internal.compat.BukkitVersion
2525
import ru.endlesscode.inventory.internal.di.DI
2626
import ru.endlesscode.inventory.internal.di.DataModule
27+
import ru.endlesscode.inventory.internal.hooks.EcInventoryPlayerInventory
2728
import ru.endlesscode.inventory.internal.hooks.PlaceholderApiPlaceholders
2829
import ru.endlesscode.inventory.internal.listener.InventoryClicksRouter
2930
import ru.endlesscode.inventory.internal.listener.PlayerInventoriesLoader
@@ -108,6 +109,7 @@ public class ECInventoryPlugin : JavaPlugin() {
108109
Please download latest version: https://www.spigotmc.org/resources/mimic.82515/
109110
""".trimIndent()
110111
}
112+
EcInventoryPlayerInventory.hook(this)
111113
}
112114

113115
private fun makeSure(action: () -> Boolean): Boolean {

core/src/main/kotlin/internal/data/repository/InventoriesRepository.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ internal class InventoriesRepository(
4242

4343
private val cache = InventoriesCache()
4444

45+
fun getInventories(player: Player): Sequence<CustomInventory> = cache.getInventories(player.uniqueId)
46+
4547
fun getInventory(player: Player, type: String): CustomInventory {
4648
val inventory = cache.getInventories(player.uniqueId).find { it.type == type }
4749
return inventory ?: createInventory(player, type)

core/src/main/kotlin/internal/di/DI.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal object DI {
3434
val scheduler: TaskScheduler by lazy { PluginTaskScheduler(plugin) }
3535

3636
// Hooks
37-
private val mimic: Mimic by lazy { Mimic.getInstance() }
37+
val mimic: Mimic by lazy { Mimic.getInstance() }
3838
val itemsRegistry: BukkitItemsRegistry by lazy { mimic.getItemsRegistry() }
3939
var placeholders: Placeholders = DisabledPlaceholders()
4040

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* This file is part of ECInventory
3+
* <https://github.com/EndlessCodeGroup/ECInventory>.
4+
* Copyright (c) 2022 EndlessCode Group and contributors
5+
*
6+
* ECInventory is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* ECInventory is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with ECInventory. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
package ru.endlesscode.inventory.internal.hooks
21+
22+
import org.bukkit.entity.Player
23+
import org.bukkit.inventory.ItemStack
24+
import org.bukkit.plugin.Plugin
25+
import org.bukkit.plugin.ServicePriority.High
26+
import ru.endlesscode.inventory.internal.data.repository.InventoriesRepository
27+
import ru.endlesscode.inventory.internal.di.DI
28+
import ru.endlesscode.inventory.slot.SlotContentType
29+
import ru.endlesscode.mimic.ExperimentalMimicApi
30+
import ru.endlesscode.mimic.MimicApiLevel.VERSION_0_8
31+
import ru.endlesscode.mimic.inventory.BukkitPlayerInventory
32+
33+
@Suppress("UnstableApiUsage")
34+
@OptIn(ExperimentalMimicApi::class)
35+
internal class EcInventoryPlayerInventory(
36+
player: Player,
37+
private val inventoriesRepository: InventoriesRepository,
38+
) : BukkitPlayerInventory(player) {
39+
40+
constructor(player: Player) : this(player, DI.data.inventoriesRepository)
41+
42+
override val equippedItems: List<ItemStack>
43+
get() = collectEquippedItems(collectItems(SlotContentType.EQUIPMENT))
44+
45+
override val storedItems: List<ItemStack>
46+
get() = collectStoredItems(collectItems(SlotContentType.GENERIC))
47+
48+
private fun collectItems(type: SlotContentType): List<ItemStack> {
49+
return inventoriesRepository.getInventories(player)
50+
.flatMap { it.getContainerSlots(type) }
51+
.map { it.content }
52+
.toList()
53+
}
54+
55+
companion object {
56+
fun hook(plugin: Plugin) {
57+
DI.mimic.registerPlayerInventoryProvider(::EcInventoryPlayerInventory, VERSION_0_8, plugin, priority = High)
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)