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 248
Expand file tree
/
Copy pathTexture.kt
More file actions
171 lines (146 loc) · 6.17 KB
/
Copy pathTexture.kt
File metadata and controls
171 lines (146 loc) · 6.17 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package trplugins.menu.module.display.texture
import org.bukkit.Material
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.LeatherArmorMeta
import org.bukkit.inventory.meta.SkullMeta
import taboolib.common.util.Strings.similarDegree
import taboolib.library.xseries.XMaterial
import taboolib.module.nms.MinecraftVersion
import taboolib.platform.util.buildItem
import trplugins.menu.api.menu.ITexture
import trplugins.menu.module.display.MenuSession
import trplugins.menu.module.internal.hook.HookPlugin
import trplugins.menu.module.internal.hook.impl.HookSkulls
import trplugins.menu.module.internal.item.ItemRepository
import trplugins.menu.module.internal.item.ItemSource
import trplugins.menu.util.Regexs
import trplugins.menu.util.bukkit.Heads
import trplugins.menu.util.bukkit.ItemHelper
/**
* @author Arasple
* @date 2021/1/24 11:50
*/
class Texture(
val raw: String,
val type: TextureType,
val texture: String,
val dynamic: Boolean,
var static: ItemStack?,
val meta: Map<TextureMeta, String>
) : ITexture {
override fun generate(session: MenuSession): ItemStack {
if (static != null) return static!!
val temp = if (dynamic) session.parse(texture) else texture
var itemStack = when (type) {
TextureType.NORMAL -> parseMaterial(temp)
TextureType.HEAD -> Heads.getHead(temp)
TextureType.REPO -> ItemRepository.getItem(temp)
TextureType.SOURCE -> ItemSource.fromSource(session, temp)
TextureType.RAW -> ItemHelper.fromJson(temp)
}
if (itemStack != null) {
if (itemStack.type == Material.AIR || itemStack.type.name.endsWith("_AIR")) {
return itemStack
} else itemStack = buildItem(itemStack) {
meta.forEach { (meta, metaValue) ->
val value = session.parse(metaValue)
when (meta) {
TextureMeta.DATA_VALUE -> damage = value.toIntOrNull() ?: 0
TextureMeta.MODEL_DATA -> customModelData = value.toInt()
TextureMeta.LEATHER_DYE -> color = ItemHelper.serializeColor(value)
TextureMeta.BANNER_PATTERN -> ItemHelper.deserializePattern(this, value)
}
}
}
if (type == TextureType.NORMAL && !dynamic) {
static = itemStack
}
}
return itemStack ?: FALL_BACK
}
override fun toString(): String {
return "{type=$type, texture=$texture, dynamic=$dynamic, static=$static, meta=$meta}"
}
companion object {
val FALL_BACK = ItemStack(Material.BEDROCK)
fun createTexture(itemStack: ItemStack): String {
val material = itemStack.type.name.lowercase().replace("_", " ")
val itemMeta = itemStack.itemMeta
// Head Meta
if (itemMeta is SkullMeta) {
val hdb =
if (HookPlugin.getHeadDatabase().isHooked) {
HookPlugin.getHeadDatabase().getId(itemStack)
} else null
val skulls =
if (hdb != null) null
else if (HookPlugin[HookSkulls::class.java].isHooked) {
HookPlugin[HookSkulls::class.java].getId(itemStack)
} else null
return when {
hdb != null -> "source:HDB:$hdb"
skulls != null -> "source:SKULLS:$skulls"
itemMeta.hasOwner() -> "head:${itemMeta.owningPlayer?.name}"
else -> "head:${Heads.seekTexture(itemStack)}"
}
}
// Model Data
if (MinecraftVersion.majorLegacy >= 11400 && itemMeta != null && itemMeta.hasCustomModelData()) {
return "$material{model-data:${itemMeta.customModelData}}"
}
// Leather
if (itemMeta is LeatherArmorMeta) {
return "$material{dye:${ItemHelper.deserializeColor(itemMeta.color)}}"
}
// Banner
return material
}
/**
* Create a texture from string
*/
fun createTexture(raw: String): Texture {
var type = TextureType.NORMAL
var static: ItemStack? = null
var texture = raw
val meta = mutableMapOf<TextureMeta, String>()
TextureMeta.values().forEach {
it.regex.find(raw)?.groupValues?.get(1)?.also { value ->
meta[it] = value
texture = texture.replace(it.regex, "")
}
}
TextureType.values().filter { it.group != -1 }.forEach {
it.regex.find(texture)?.groupValues?.get(it.group)?.also { value ->
type = it
texture = value
}
}
val dynamic = Regexs.containsPlaceholder(texture)
if (type == TextureType.NORMAL) {
if (Regexs.JSON_TEXTURE.find(texture) != null) {
type = TextureType.RAW
if (!dynamic) static = ItemHelper.fromJson(texture)!!
}
}
return Texture(raw, type, texture, dynamic, static, meta)
}
private fun parseMaterial(material: String): ItemStack {
val split = material.split(":", limit = 2)
val id = split[0].toIntOrNull() ?: split[0].uppercase().replace("[ _]".toRegex(), "_")
val item = try {
buildItem(XMaterial.matchXMaterial(FALL_BACK)) {
val name = id.toString()
this.material = Material.getMaterial(name)!!
}
} catch (e: Throwable) {
runCatching {
XMaterial.values().find { it.name.equals(id.toString(), true) }
?: XMaterial.values().find { it -> it.legacy.any { it == id.toString() } }
?: XMaterial.values().maxByOrNull { similarDegree(id.toString(), it.name) }
}.getOrNull()?.parseItem()
?: FALL_BACK
}
return item
}
}
}