-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMinecraftItemsRegistry.kt
More file actions
87 lines (71 loc) · 3.05 KB
/
MinecraftItemsRegistry.kt
File metadata and controls
87 lines (71 loc) · 3.05 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
/*
* 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/>.
*/
package ru.endlesscode.mimic.impl.vanilla
import org.bukkit.Material
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.Damageable
import org.bukkit.inventory.meta.ItemMeta
import ru.endlesscode.mimic.internal.Log
import ru.endlesscode.mimic.items.BukkitItemsRegistry
/**
* Items service implementation using material name as itemId.
* Supports [ItemMetaPayload] as a payload in [getItem].
*/
public class MinecraftItemsRegistry : BukkitItemsRegistry {
override val id: String = ID
override val knownIds: List<String> by lazy {
Material.entries.asSequence()
.filter { it.isItem }
.map { it.name.lowercase() }
.toList()
}
override fun isItemExists(itemId: String): Boolean = getMaterial(itemId) != null
override fun getItemId(item: ItemStack): String = item.type.name.lowercase()
override fun getItem(itemId: String, payload: Any?, amount: Int): ItemStack? {
val material = getMaterial(itemId) ?: return null
val realAmount = amount.coerceIn(1, material.maxStackSize)
val minecraftPayload = ItemMetaPayload.of(payload)
if (payload != null && minecraftPayload == null) {
Log.w("[${javaClass.simpleName}] Ignoring unsupported payload for item $itemId:\n$payload")
}
return ItemStack(material, realAmount).apply {
if (minecraftPayload != null) itemMeta = itemMeta?.applyPayload(minecraftPayload)
}
}
private fun getMaterial(name: String): Material? {
return Material.getMaterial(name.uppercase())
?.takeIf { it.isItem }
}
private fun ItemMeta.applyPayload(payload: ItemMetaPayload): ItemMeta {
// Apply text options
displayName(payload.name)
lore(payload.lore)
// Apply damage and custom model data
isUnbreakable = payload.isUnbreakable
(this as? Damageable)?.damage = payload.damage
if (payload.customModelData != null) setCustomModelData(payload.customModelData)
// Apply enchants and item flags
payload.enchantments.forEach { (enchant, level) -> addEnchant(enchant, level, true) }
addItemFlags(*payload.flags.toTypedArray())
return this
}
public companion object {
public const val ID: String = "minecraft"
}
}