|
| 1 | +package com.mineinabyss.packy.menus.picker |
| 2 | + |
| 3 | +import com.github.shynixn.mccoroutine.bukkit.launch |
| 4 | +import com.mineinabyss.guiy.viewmodel.GuiyViewModel |
| 5 | +import com.mineinabyss.idofront.messaging.info |
| 6 | +import com.mineinabyss.packy.PackyServer |
| 7 | +import com.mineinabyss.packy.components.packyData |
| 8 | +import com.mineinabyss.packy.config.PackyMenu |
| 9 | +import com.mineinabyss.packy.config.PackyTemplate |
| 10 | +import com.mineinabyss.packy.config.packy |
| 11 | +import kotlinx.coroutines.flow.* |
| 12 | +import org.bukkit.entity.Player |
| 13 | +import org.bukkit.inventory.ItemStack |
| 14 | + |
| 15 | +class PackPickerViewModel( |
| 16 | + val player: Player, |
| 17 | +) : GuiyViewModel() { |
| 18 | + private val originalTemplates = player.packyData.templates.toMap() |
| 19 | + private val _packyData = MutableStateFlow(player.packyData) |
| 20 | + val packyData = _packyData.asStateFlow() |
| 21 | + val subPackList = packy.menu.subMenus.map { it.value to it.value.packs.toList() }.toMap() |
| 22 | + |
| 23 | + fun itemFor( |
| 24 | + templateId: String, |
| 25 | + subMenu: PackyMenu.PackySubMenu, |
| 26 | + ): StateFlow<ItemStack> = packyData.map { |
| 27 | + subMenu.buttonFor(state = it.enabledPackIds.contains(templateId)) |
| 28 | + }.stateIn(viewModelScope, SharingStarted.Eagerly, ItemStack.empty()) |
| 29 | + |
| 30 | + /** |
| 31 | + * Resends the resourcepack with any changes made by the player in the menu. |
| 32 | + */ |
| 33 | + fun sendPackChanges() { |
| 34 | + val packyData = _packyData.value |
| 35 | + if (originalTemplates != packyData.templates) { |
| 36 | + // todo send message listing packs that were enabled/disabled |
| 37 | + val enabledPacks = packyData.templates.filter { it.value } |
| 38 | + .keys.filter { !originalTemplates[it]!! } |
| 39 | + val disabledPacks = packyData.templates.filter { !it.value } |
| 40 | + .keys.filter { originalTemplates[it]!! } |
| 41 | + |
| 42 | + player.info(buildString { |
| 43 | + appendLine("<gray>Changed resourcepacks:") |
| 44 | + if (enabledPacks.isNotEmpty()) |
| 45 | + append(" • Enabled <green>${enabledPacks.joinToString(", ")}</green>") |
| 46 | + if (disabledPacks.isNotEmpty()) { |
| 47 | + if (enabledPacks.isNotEmpty()) appendLine() |
| 48 | + append(" • Disabled <red>${disabledPacks.joinToString(", ")}</red>") |
| 49 | + } |
| 50 | + }) |
| 51 | + |
| 52 | + packy.plugin.launch { |
| 53 | + player.packyData = packyData |
| 54 | + PackyServer.sendPack(player) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + fun togglePack(templateId: String) { |
| 60 | + val isEnabled = _packyData.value.templates[templateId] == true |
| 61 | + if (isEnabled) disablePack(templateId) |
| 62 | + else enablePack(templateId) |
| 63 | + } |
| 64 | + |
| 65 | + fun enablePack(pack: String) { |
| 66 | + packy.templates[pack]?.let { template -> |
| 67 | + disableConflictingPacks(template) |
| 68 | + if (_packyData.value.templates[template.id] == true) return // Don't do anything if already enabled |
| 69 | + _packyData.update { |
| 70 | + it.copy( |
| 71 | + //TODO make packyData immutable |
| 72 | + templates = it.templates.plus(template.id to true).toMutableMap() |
| 73 | + ) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + fun disablePack(pack: String) { |
| 79 | + packy.templates[pack]?.let { template -> |
| 80 | + if (_packyData.value.templates[template.id] == false) return // Don't do anything if already disabled |
| 81 | + _packyData.update { |
| 82 | + it.copy( |
| 83 | + //TODO immutable, as above |
| 84 | + templates = it.templates.plus(template.id to false).toMutableMap() |
| 85 | + ) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private fun disableConflictingPacks(template: PackyTemplate) { |
| 91 | + _packyData.update { |
| 92 | + val conflictingDisabled = it.templates.keys |
| 93 | + .mapNotNull(packy.templates::get) |
| 94 | + .filter(template::conflictsWith) |
| 95 | + .associate { it.id to false } |
| 96 | + |
| 97 | + it.copy( |
| 98 | + templates = it.templates.plus(conflictingDisabled).toMutableMap() |
| 99 | + ) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments