-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathItemMetaPayload.kt
More file actions
103 lines (96 loc) · 3.78 KB
/
ItemMetaPayload.kt
File metadata and controls
103 lines (96 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* This file is part of BukkitMimic.
* Copyright (C) 2021 Osip Fatkullin
* Copyright (C) 2021 EndlessCode Group and contributors
*
* BukkitMimic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BukkitMimic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BukkitMimic. If not, see <http://www.gnu.org/licenses/>.
*/
@file:UseSerializers(EnchantmentSerializer::class, ItemFlagsSerializer::class, MiniMessageComponentSerializer::class)
package ru.endlesscode.mimic.impl.vanilla
import com.typesafe.config.ConfigFactory
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import kotlinx.serialization.hocon.decodeFromConfig
import net.kyori.adventure.text.Component
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemFlag
import ru.endlesscode.mimic.internal.*
/**
* Payload to configure item's [ItemMeta][org.bukkit.inventory.meta.ItemMeta].
*
* @property name Item name. Supports MiniMessage formatting.
* @property lore Item lore. Supports MiniMessage formatting.
* @property isUnbreakable Is item unbreakable. Affects only items that have durability (like weapons or tools).
* @property damage Damage to item durability. Affects only items that have durability (like weapons or tools).
* @property customModelData A value used to override item model.
* @property enchantments Item's enchantments. See [Enchantment].
* @property flags Item flags used to hide information about item from lore. See [ItemFlag].
* @see org.bukkit.inventory.meta.ItemMeta
* @see ItemMetaPayload.parse
*/
@Serializable
public data class ItemMetaPayload(
val name: Component? = null,
val lore: List<Component>? = null,
@SerialName("unbreakable")
val isUnbreakable: Boolean = false,
val damage: Int = 0,
val customModelData: Int? = null,
val enchantments: Map<Enchantment, Int> = emptyMap(),
val flags: Set<ItemFlag> = emptySet(),
) {
public companion object {
/**
* Returns [ItemMetaPayload] if it can be retrieved from the given [payload] value.
*
* Supported types are:
* - [ItemMetaPayload] - returns itself
* - [String] - tries to parse payload using [ItemMetaPayload.parse]
*/
@JvmStatic
public fun of(payload: Any?): ItemMetaPayload? = when (payload) {
is ItemMetaPayload -> payload
is String -> parse(payload)
else -> null
}
/**
* Tries to parse [ItemMetaPayload] from the given [input].
* Returns `null` if parsing failed or [input] is empty.
*
* Input should be formatted in [HOCON](https://github.com/lightbend/config/blob/main/HOCON.md).
* Unknown fields are ignored.
* ```
* {
* name = "&6Item Name"
* lore = [Line1, Line2]
* unbreakable = true
* damage = 42
* custom-model-data = 1
* flags = [hide_attributes, hide_dye]
* enchantments = {unbreaking: 3}
* }
* ```
*/
@JvmStatic
public fun parse(input: String): ItemMetaPayload? {
return try {
DI.hocon.decodeFromConfig(ConfigFactory.parseString(input))
} catch (e: Throwable) {
Log.w(e)
null
}
}
}
}