|
| 1 | +package com.y54895.matrixlib.api.action |
| 2 | + |
| 3 | +import com.y54895.matrixlib.api.text.MatrixText |
| 4 | +import org.bukkit.Sound |
| 5 | +import org.bukkit.entity.Player |
| 6 | + |
| 7 | +data class ActionContext( |
| 8 | + val player: Player, |
| 9 | + val placeholders: Map<String, String>, |
| 10 | + val backAction: Runnable? = null |
| 11 | +) |
| 12 | + |
| 13 | +object ActionExecutor { |
| 14 | + |
| 15 | + fun execute(context: ActionContext, actions: List<String>) { |
| 16 | + actions.forEach { executeSingle(context, it) } |
| 17 | + } |
| 18 | + |
| 19 | + private fun executeSingle(context: ActionContext, rawAction: String) { |
| 20 | + val action = MatrixText.apply(rawAction, context.placeholders) |
| 21 | + when { |
| 22 | + action.equals("close", true) -> context.player.closeInventory() |
| 23 | + action.equals("back", true) -> context.backAction?.run() ?: context.player.closeInventory() |
| 24 | + action.startsWith("tell:", true) -> context.player.sendMessage(MatrixText.color(action.substringAfter(':').trim())) |
| 25 | + action.startsWith("sound:", true) -> playSound(context.player, action.substringAfter(':').trim()) |
| 26 | + action.isNotBlank() -> context.player.performCommand(action.removePrefix("/")) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + private fun playSound(player: Player, raw: String) { |
| 31 | + val split = raw.split('-') |
| 32 | + val soundName = split.getOrNull(0)?.trim()?.uppercase() ?: return |
| 33 | + val volume = split.getOrNull(1)?.toFloatOrNull() ?: 1f |
| 34 | + val pitch = split.getOrNull(2)?.toFloatOrNull() ?: 1f |
| 35 | + val sound = runCatching { Sound.valueOf(soundName) }.getOrNull() ?: return |
| 36 | + player.playSound(player.location, sound, volume, pitch) |
| 37 | + } |
| 38 | +} |
0 commit comments