Skip to content

Commit 72edc68

Browse files
author
54895y
committed
Add shared menu and compatibility APIs
1 parent 88df3f3 commit 72edc68

4 files changed

Lines changed: 917 additions & 2 deletions

File tree

src/main/kotlin/com/y54895/matrixlib/MatrixLib.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object MatrixLib : Plugin() {
1919
branding = branding,
2020
headline = "Loading shared Matrix runtime",
2121
details = listOf(
22-
MatrixConsoleFact("Exports", "branding / text / yaml / console"),
22+
MatrixConsoleFact("Exports", "action / menu / compat / text / yaml / console"),
2323
MatrixConsoleFact("Usage", "Required by MatrixShop, MatrixAuth, MatrixCook")
2424
)
2525
)
@@ -30,7 +30,7 @@ object MatrixLib : Plugin() {
3030
branding = branding,
3131
version = pluginVersion,
3232
details = listOf(
33-
MatrixConsoleFact("API", "MatrixText, MatrixYamlBundle"),
33+
MatrixConsoleFact("API", "Action, menu, compat, text, yaml"),
3434
MatrixConsoleFact("Runtime", "Console visuals and resource bridge")
3535
)
3636
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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

Comments
 (0)