|
| 1 | +package com.y54895.matrixstorage.api |
| 2 | + |
| 3 | +import com.y54895.matrixstorage.api.model.MatrixStorageBlockWarehouseSnapshot |
| 4 | +import com.y54895.matrixstorage.api.model.MatrixStorageDefinitionSnapshot |
| 5 | +import com.y54895.matrixstorage.api.model.MatrixStorageMailboxMessageSnapshot |
| 6 | +import com.y54895.matrixstorage.api.model.MatrixStorageProfileSnapshot |
| 7 | +import com.y54895.matrixstorage.api.model.MatrixStorageStatusSnapshot |
| 8 | +import com.y54895.matrixstorage.bridge.MatrixShopBridge |
| 9 | +import com.y54895.matrixstorage.service.StorageConfig |
| 10 | +import com.y54895.matrixstorage.service.StoragePersistenceService |
| 11 | +import com.y54895.matrixstorage.service.StorageStateService |
| 12 | +import org.bukkit.Bukkit |
| 13 | +import org.bukkit.Location |
| 14 | +import java.util.UUID |
| 15 | + |
| 16 | +/** |
| 17 | + * Stable MatrixStorage facade for other Matrix plugins and third-party integrations. |
| 18 | + */ |
| 19 | +/** |
| 20 | + * Stable MatrixStorage facade for other Matrix plugins and third-party integrations. |
| 21 | + */ |
| 22 | +object MatrixStorageApi { |
| 23 | + |
| 24 | + /** |
| 25 | + * Whether MatrixStorage is currently enabled on this server. |
| 26 | + */ |
| 27 | + fun isAvailable(): Boolean { |
| 28 | + return Bukkit.getPluginManager().getPlugin("MatrixStorage")?.isEnabled == true |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Return the current MatrixStorage runtime summary snapshot. |
| 33 | + */ |
| 34 | + fun status(): MatrixStorageStatusSnapshot { |
| 35 | + return MatrixStorageStatusSnapshot( |
| 36 | + backendDescription = StoragePersistenceService.backendDescription(), |
| 37 | + mailboxCount = StorageStateService.mailboxCount(), |
| 38 | + blockWarehouseCount = StorageStateService.blockWarehouseCount(), |
| 39 | + definitionCount = StorageConfig.allDefinitions().size, |
| 40 | + matrixShopBridge = MatrixShopBridge.statusSummary() |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Return all configured warehouse definition snapshots. |
| 46 | + */ |
| 47 | + fun definitions(): List<MatrixStorageDefinitionSnapshot> { |
| 48 | + return StorageConfig.allDefinitions().map { definition -> |
| 49 | + MatrixStorageDefinitionSnapshot( |
| 50 | + key = definition.key, |
| 51 | + source = definition.source, |
| 52 | + type = definition.type, |
| 53 | + mode = definition.mode, |
| 54 | + id = definition.id, |
| 55 | + displayName = definition.displayName, |
| 56 | + inventorySize = definition.inventorySize |
| 57 | + ) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Return current mailbox message snapshots for an owner. |
| 63 | + */ |
| 64 | + fun mailbox(ownerId: UUID): List<MatrixStorageMailboxMessageSnapshot> { |
| 65 | + return StorageStateService.mailbox(ownerId).map { message -> |
| 66 | + MatrixStorageMailboxMessageSnapshot( |
| 67 | + id = message.id, |
| 68 | + ownerId = message.ownerId, |
| 69 | + ownerName = message.ownerName, |
| 70 | + senderId = message.senderId, |
| 71 | + senderName = message.senderName, |
| 72 | + sourceModule = message.sourceModule, |
| 73 | + sourceId = message.sourceId, |
| 74 | + text = message.text, |
| 75 | + hasAttachedItem = message.attachedItem != null, |
| 76 | + money = message.money, |
| 77 | + currencyKey = message.currencyKey, |
| 78 | + createdAt = message.createdAt, |
| 79 | + claimed = message.claimed, |
| 80 | + archived = message.archived |
| 81 | + ) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Return the storage profile snapshot for an owner. |
| 87 | + */ |
| 88 | + fun profile(ownerId: UUID, ownerName: String = ownerId.toString()): MatrixStorageProfileSnapshot { |
| 89 | + val profile = StorageStateService.profile(ownerId, ownerName) |
| 90 | + return MatrixStorageProfileSnapshot( |
| 91 | + ownerId = profile.ownerId, |
| 92 | + ownerName = profile.ownerName, |
| 93 | + warehouseSize = profile.warehouseContents.size, |
| 94 | + pendingSize = profile.pendingContents.size, |
| 95 | + warehouseUsedSlots = profile.warehouseContents.count { it != null && it.type.name != "AIR" }, |
| 96 | + pendingUsedSlots = profile.pendingContents.count { it != null && it.type.name != "AIR" }, |
| 97 | + unlockedWarehouseSlots = profile.unlockedWarehouseSlots, |
| 98 | + unlockedMailboxSlots = profile.unlockedMailboxSlots |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Resolve a block warehouse snapshot by id. |
| 104 | + */ |
| 105 | + fun blockWarehouse(id: String): MatrixStorageBlockWarehouseSnapshot? { |
| 106 | + return StorageStateService.blockWarehouse(id)?.let(::snapshot) |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Resolve a block warehouse snapshot by location. |
| 111 | + */ |
| 112 | + fun blockWarehouse(location: Location): MatrixStorageBlockWarehouseSnapshot? { |
| 113 | + return StorageStateService.blockWarehouse(location)?.let(::snapshot) |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * List all block warehouse snapshots owned by a player. |
| 118 | + */ |
| 119 | + fun ownedBlockWarehouses(ownerId: UUID): List<MatrixStorageBlockWarehouseSnapshot> { |
| 120 | + return StorageStateService.ownedBlockWarehouses(ownerId).map(::snapshot) |
| 121 | + } |
| 122 | + |
| 123 | + private fun snapshot(record: com.y54895.matrixstorage.model.BlockWarehouseRecord): MatrixStorageBlockWarehouseSnapshot { |
| 124 | + return MatrixStorageBlockWarehouseSnapshot( |
| 125 | + id = record.id, |
| 126 | + definitionKey = record.definitionKey, |
| 127 | + ownerId = record.ownerId, |
| 128 | + ownerName = record.ownerName, |
| 129 | + worldName = record.worldName, |
| 130 | + x = record.x, |
| 131 | + y = record.y, |
| 132 | + z = record.z, |
| 133 | + source = record.source, |
| 134 | + type = record.type, |
| 135 | + displayId = record.displayId, |
| 136 | + inventorySize = record.inventorySize, |
| 137 | + displayName = record.displayName, |
| 138 | + trustedPlayers = record.trust.toSet(), |
| 139 | + allowHopperInsert = record.allowHopperInsert, |
| 140 | + allowHopperExtract = record.allowHopperExtract, |
| 141 | + comparatorEnabled = record.comparatorEnabled, |
| 142 | + usesNativeInventory = record.usesNativeInventory |
| 143 | + ) |
| 144 | + } |
| 145 | +} |
0 commit comments