|
| 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