|
| 1 | +package de.thepixel3261.momentum.api |
| 2 | + |
| 3 | +import java.util.* |
| 4 | + |
| 5 | +/** |
| 6 | + * Root service exposed by Momentum for other plugins. |
| 7 | + * Acquire via Bukkit ServicesManager or the static accessor [MomentumAPI.get()]. |
| 8 | + */ |
| 9 | +interface MomentumService { |
| 10 | + fun sessions(): SessionService |
| 11 | + fun rewards(): RewardActionRegistry |
| 12 | +} |
| 13 | + |
| 14 | +/** Session read/write access for other plugins. */ |
| 15 | +interface SessionService { |
| 16 | + fun get(uuid: UUID): SessionSnapshot? |
| 17 | + |
| 18 | + /** Modify a player's session atomically; creates a session if missing. */ |
| 19 | + fun modify(uuid: UUID, modify: (MutableSession) -> Unit) |
| 20 | +} |
| 21 | + |
| 22 | +/** Immutable view of a player's session. */ |
| 23 | +data class SessionSnapshot( |
| 24 | + val uuid: UUID, |
| 25 | + val joinTime: Long, |
| 26 | + val lastActivity: Long, |
| 27 | + val totalPlayMinutes: Int, |
| 28 | + val claimedTiers: Set<Int>, |
| 29 | + val unlockedTiers: Set<Int>, |
| 30 | + val isAfk: Boolean, |
| 31 | + val multiplier: Double, |
| 32 | + val lastRecycle: Int, |
| 33 | + val recycles: Int, |
| 34 | +) |
| 35 | + |
| 36 | +/** Mutable façade to edit selected fields of a session safely. */ |
| 37 | +interface MutableSession { |
| 38 | + var totalPlayMinutes: Int |
| 39 | + var claimedTiers: MutableSet<Int> |
| 40 | + var unlockedTiers: MutableSet<Int> |
| 41 | + var isAfk: Boolean |
| 42 | + var multiplier: Double |
| 43 | + var lastRecycle: Int |
| 44 | + var recycles: Int |
| 45 | +} |
| 46 | + |
| 47 | +/** Registry for reward action executors, addressable by config "type" id. */ |
| 48 | +interface RewardActionRegistry { |
| 49 | + /** Register a new custom action id. Fails if id already exists unless [override] is true. */ |
| 50 | + fun register(id: String, executor: RewardActionExecutor, override: Boolean = false) |
| 51 | + |
| 52 | + /** Remove a previously registered action id. */ |
| 53 | + fun unregister(id: String) |
| 54 | + |
| 55 | + /** Fetch the executor for an id if present. */ |
| 56 | + fun executorFor(id: String): RewardActionExecutor? |
| 57 | +} |
| 58 | + |
| 59 | +/** Executes an action for a player; params come from rewards.yml action map. */ |
| 60 | +fun interface RewardActionExecutor { |
| 61 | + fun execute(ctx: RewardActionContext) |
| 62 | +} |
| 63 | + |
| 64 | +/** Context provided to executors. */ |
| 65 | +data class RewardActionContext( |
| 66 | + val uuid: UUID, |
| 67 | + val session: SessionSnapshot, |
| 68 | + /** Raw parameters from config for this action (e.g., amount, command, etc.). */ |
| 69 | + val params: Map<String, Any?>, |
| 70 | + /** Whether the action is visible in UI and optional lore defined in config. */ |
| 71 | + val visible: Boolean, |
| 72 | + val lore: List<String>?, |
| 73 | + /** Multiplier currently applied to the player. */ |
| 74 | + val multiplier: Double, |
| 75 | +) |
| 76 | + |
| 77 | +/** Static accessor utility to fetch the service from Bukkit's ServicesManager. */ |
| 78 | +object MomentumAPI { |
| 79 | + @Volatile |
| 80 | + private var instance: MomentumService? = null |
| 81 | + |
| 82 | + fun get(): MomentumService? = instance |
| 83 | + |
| 84 | + // Core sets/unsets this via reflection-safe calls; methods kept internal to avoid ABI leaks. |
| 85 | + fun internalSet(service: MomentumService?) { |
| 86 | + instance = service |
| 87 | + } |
| 88 | +} |
0 commit comments