From c6d807ee3e3c682deb5ac113d010887756da50d0 Mon Sep 17 00:00:00 2001 From: Esoteric Enderman Date: Mon, 28 Oct 2024 22:57:58 +0000 Subject: [PATCH] Add custom item features (Not tested) --- .../plugins/library/item/CustomItem.kt | 55 +++++++++++++++++++ .../plugins/library/item/CustomItemManager.kt | 28 ++++++++++ .../plugins/library/item/CustomItemPlugin.kt | 7 +++ 3 files changed, 90 insertions(+) create mode 100644 lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/item/CustomItem.kt create mode 100644 lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/item/CustomItemManager.kt create mode 100644 lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/item/CustomItemPlugin.kt diff --git a/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/item/CustomItem.kt b/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/item/CustomItem.kt new file mode 100644 index 0000000..3370694 --- /dev/null +++ b/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/item/CustomItem.kt @@ -0,0 +1,55 @@ +package foundation.esoteric.minecraft.plugins.library.item + +import org.bukkit.Bukkit +import org.bukkit.Material +import org.bukkit.entity.Player +import org.bukkit.event.Listener +import org.bukkit.inventory.ItemStack +import org.bukkit.inventory.meta.ItemMeta +import org.bukkit.persistence.PersistentDataType + +abstract class CustomItem(private val plugin: CustomItemPlugin, private val itemId: String, private val material: Material) : Listener { + init { + Bukkit.getPluginManager().registerEvents(this, plugin) + + plugin.customItemManager.addCustomItem(itemId, this) + } + + protected abstract fun generateCustomItem(baseCustomItem: ItemStack?, player: Player?): ItemStack + + fun getCustomItem(player: Player?): ItemStack { + val item = ItemStack(material) + item.editMeta { meta: ItemMeta -> + meta.persistentDataContainer.set( + plugin.customItemManager.customItemIdKey, + PersistentDataType.STRING, + itemId + ) + } + return generateCustomItem(item, player) + } + + fun isItem(itemStack: ItemStack?): Boolean { + if (itemStack == null) { + return false + } + + if (!itemStack.hasItemMeta()) { + return false + } + + val dataContainerItemIdValue = + itemStack.itemMeta.persistentDataContainer.get(plugin.customItemManager.customItemIdKey, PersistentDataType.STRING) + ?: return false + + return try { + itemId === dataContainerItemIdValue + } catch (exception: IllegalArgumentException) { + false + } + } + + fun give(player: Player) { + player.inventory.addItem(getCustomItem(player)) + } +} diff --git a/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/item/CustomItemManager.kt b/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/item/CustomItemManager.kt new file mode 100644 index 0000000..e0dfdcf --- /dev/null +++ b/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/item/CustomItemManager.kt @@ -0,0 +1,28 @@ +package foundation.esoteric.minecraft.plugins.library.item + +import org.bukkit.NamespacedKey +import org.bukkit.entity.Player +import org.bukkit.plugin.java.JavaPlugin + +class CustomItemManager(private val plugin: JavaPlugin) { + + private val customItemMap: MutableMap = HashMap() + + val customItemIdKey: NamespacedKey = NamespacedKey(plugin, "custom_item_id") + + fun getPlugin(): JavaPlugin { + return plugin + } + + fun addCustomItem(itemId: String, customItem: CustomItem) { + customItemMap[itemId] = customItem + } + + fun getAbstractCustomItem(itemId: String): CustomItem? { + return customItemMap[itemId] + } + + fun giveCustomItem(itemId: String, player: Player?) { + customItemMap[itemId]!!.give(player!!) + } +} diff --git a/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/item/CustomItemPlugin.kt b/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/item/CustomItemPlugin.kt new file mode 100644 index 0000000..98ff3d0 --- /dev/null +++ b/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/item/CustomItemPlugin.kt @@ -0,0 +1,7 @@ +package foundation.esoteric.minecraft.plugins.library.item + +import org.bukkit.plugin.Plugin + +interface CustomItemPlugin : Plugin { + val customItemManager: CustomItemManager +}