This repository was archived by the owner on Sep 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathItemHelper.kt
More file actions
143 lines (130 loc) · 4.68 KB
/
ItemHelper.kt
File metadata and controls
143 lines (130 loc) · 4.68 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package trplugins.menu.util.bukkit
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import trplugins.menu.module.display.MenuSettings
import org.bukkit.ChatColor
import org.bukkit.Color
import org.bukkit.DyeColor
import org.bukkit.Material
import org.bukkit.block.banner.Pattern
import org.bukkit.block.banner.PatternType
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.PlayerInventory
import org.bukkit.inventory.meta.BannerMeta
import taboolib.library.xseries.XMaterial
import taboolib.module.nms.ItemTag
import taboolib.platform.util.ItemBuilder
import trplugins.menu.util.parseJson
import kotlin.math.min
/**
* @author Arasple
* @date 2021/2/4 9:56
*/
object ItemHelper {
fun serializeColor(color: String): Color {
val rgb = color.split(",")
if (rgb.size == 3) {
val r = min(rgb[0].toIntOrNull() ?: 0, 255)
val g = min(rgb[1].toIntOrNull() ?: 0, 255)
val b = min(rgb[2].toIntOrNull() ?: 0, 255)
return Color.fromRGB(r, g, b)
}
return Color.BLACK
}
fun deserializeColor(color: Color): String {
return "${color.red},${color.green},${color.blue}"
}
fun deserializePattern(builder: ItemBuilder, string: String) {
builder.patterns.clear()
builder.patterns.addAll(string.split(",").let {
val patterns = mutableListOf<Pattern>()
it.forEach { it ->
val type = it.split(" ")
if (type.size == 1) {
builder.finishing = {
try {
(it.itemMeta as? BannerMeta)?.baseColor = DyeColor.valueOf(type[0].uppercase())
} catch (e: Exception) {
(it.itemMeta as? BannerMeta)?.baseColor = DyeColor.BLACK
}
}
} else if (type.size == 2) {
try {
patterns.add(Pattern(
DyeColor.valueOf(type[0].uppercase()), PatternType.valueOf(
type[1].uppercase()
)
))
} catch (e: Exception) {
patterns.add(Pattern(DyeColor.BLACK, PatternType.BASE))
}
}
}
patterns
})
}
fun defColorize(string: String, isLore: Boolean = false): String {
return if (string.isNotBlank() && !string.startsWith(ChatColor.COLOR_CHAR) && !string.startsWith('&')) {
val defColor = if (isLore) MenuSettings.DEFAULT_LORE_COLOR else MenuSettings.DEFAULT_NAME_COLOR
defColor + string
} else string
}
fun isJson(json: String): Boolean {
return try {
json.parseJson()
true
} catch (e: Throwable) {
false
}
}
fun fromJson(json: String): ItemStack? {
try {
val parse = JsonParser().parse(json)
if (parse is JsonObject) {
val itemStack = parse["type"].let {
it ?: return XMaterial.STONE.parseItem()
try {
XMaterial.valueOf(it.asString).parseItem()
} catch (e: IllegalArgumentException) {
ItemStack(Material.valueOf(it.asString))
}
}
parse["data"].let {
it ?: return@let
itemStack?.durability = it.asShort
}
parse["amount"].let {
it ?: return@let
itemStack?.amount = it.asInt
}
val meta = parse["meta"]
return if (meta != null) itemStack.also {
if (it != null) {
ItemTag.fromLegacyJson(meta.toString()).saveTo(it)
}
}
else itemStack
}
return null
} catch (t: Throwable) {
return null
}
}
fun fromPlayerInv(inv: PlayerInventory, item: String): Any? {
return when (item.lowercase()) {
"all", "inv" -> inv.contents
"armor" -> inv.armorContents
"hand", "mainhand" -> inv.itemInHand
"offhand" -> inv.itemInOffHand
"helmet" -> inv.armorContents[3]
"chestplate" -> inv.armorContents[2]
"leggings" -> inv.armorContents[1]
"boots" -> inv.armorContents[0]
else -> try {
inv.getItem(Integer.parseInt(item))
} catch (ignored: NumberFormatException) {
null
}
}
}
}