From 108e9a9fa7f7fea62eff3d72921d68c7a54c3818 Mon Sep 17 00:00:00 2001 From: 1lent Date: Tue, 17 Dec 2024 21:22:51 +0800 Subject: [PATCH] Refactor book synchronization and add custom book key handling Revised logic for updating writable books to use a data-driven approach with persistent keys. Added `Keys` class for managing namespaced keys and improved item setup with custom enchantments and metadata flags. This enhances maintainability and ensures consistent behavior across inventories. --- src/main/kotlin/com/lent/snapchat/Item.kt | 11 +++++--- src/main/kotlin/com/lent/snapchat/Keys.kt | 8 ++++++ src/main/kotlin/com/lent/snapchat/Main.kt | 15 +++++------ .../com/lent/snapchat/SnapChatEvents.kt | 26 ++++++++++--------- 4 files changed, 36 insertions(+), 24 deletions(-) create mode 100644 src/main/kotlin/com/lent/snapchat/Keys.kt diff --git a/src/main/kotlin/com/lent/snapchat/Item.kt b/src/main/kotlin/com/lent/snapchat/Item.kt index 1f2a1a1..43251fb 100644 --- a/src/main/kotlin/com/lent/snapchat/Item.kt +++ b/src/main/kotlin/com/lent/snapchat/Item.kt @@ -1,8 +1,12 @@ package com.lent.snapchat import com.github.supergluelib.foundation.util.ItemBuilder +import org.bukkit.Bukkit import org.bukkit.Material +import org.bukkit.enchantments.Enchantment +import org.bukkit.inventory.ItemFlag import org.bukkit.inventory.meta.BookMeta +import org.bukkit.persistence.PersistentDataType class Item { var snapbook = ItemBuilder(Material.WRITABLE_BOOK, "Snapchat App", 1) @@ -12,8 +16,9 @@ class Item { meta.title = "Snapchat App" meta.author = "System" meta.pages = listOf("Welcome to Snapchat! Start writing your messages here!") + meta.addItemFlags(ItemFlag.HIDE_ENCHANTS) + meta.addEnchant(Enchantment.ARROW_DAMAGE, 1, true) + meta.persistentDataContainer.set(Main.Keys.CUSTOM_BOOK, PersistentDataType.BOOLEAN, true) itemMeta = meta } - - val bookMeta = snapbook.itemMeta as BookMeta -} +} \ No newline at end of file diff --git a/src/main/kotlin/com/lent/snapchat/Keys.kt b/src/main/kotlin/com/lent/snapchat/Keys.kt new file mode 100644 index 0000000..e0ef49e --- /dev/null +++ b/src/main/kotlin/com/lent/snapchat/Keys.kt @@ -0,0 +1,8 @@ +package com.lent.snapchat + +import com.lent.snapchat.Main.Companion.plugin +import org.bukkit.NamespacedKey + +public class Keys { + public final val CUSTOM_BOOK: NamespacedKey = NamespacedKey(plugin,"CustomBook") +} \ No newline at end of file diff --git a/src/main/kotlin/com/lent/snapchat/Main.kt b/src/main/kotlin/com/lent/snapchat/Main.kt index 344f567..c64af1b 100644 --- a/src/main/kotlin/com/lent/snapchat/Main.kt +++ b/src/main/kotlin/com/lent/snapchat/Main.kt @@ -13,15 +13,15 @@ import revxrsal.commands.annotation.Command import revxrsal.commands.bukkit.annotation.CommandPermission class Main : JavaPlugin() { - companion object { lateinit var plugin: Main private set - val item = Item() + lateinit var Keys: Keys + lateinit var item: Item } - - override fun onEnable() { plugin = this + Keys = Keys() + item = Item() val foundation = Foundations.setup(this) logger.info("Snapchat plugin has been enabled successfully!") val snapChatEvents = SnapChatEvents() @@ -29,14 +29,11 @@ class Main : JavaPlugin() { foundation.registerCommands(this) } - - - @Command("snapchat") @CommandPermission("snapchat.conversation") fun onCommand(sender: CommandSender) { val player = sender as Player player.inventory.addItem(item.snapbook) - } + } -} +} \ No newline at end of file diff --git a/src/main/kotlin/com/lent/snapchat/SnapChatEvents.kt b/src/main/kotlin/com/lent/snapchat/SnapChatEvents.kt index 43c5298..0c94415 100644 --- a/src/main/kotlin/com/lent/snapchat/SnapChatEvents.kt +++ b/src/main/kotlin/com/lent/snapchat/SnapChatEvents.kt @@ -1,28 +1,30 @@ package com.lent.snapchat +import com.lent.snapchat.Main.Companion.item import org.bukkit.Bukkit +import org.bukkit.Material import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.player.PlayerEditBookEvent +import org.bukkit.inventory.meta.BookMeta class SnapChatEvents : Listener { + @EventHandler fun onPlayerWrite(bookEvent: PlayerEditBookEvent) { - val player = bookEvent.player val updatedMeta = bookEvent.newBookMeta - for (onlinePlayer in Bukkit.getOnlinePlayers()) { - if (onlinePlayer.inventory.contains(Main.item.snapbook)) { - val snapbook = Main.item.snapbook - val updatedBook = snapbook.clone() - val updatedBookMeta = updatedBook.itemMeta as org.bukkit.inventory.meta.BookMeta - updatedBookMeta.pages = updatedMeta.pages - updatedBook.itemMeta = updatedBookMeta - - onlinePlayer.inventory.removeItem(snapbook) - onlinePlayer.inventory.addItem(updatedBook) + for (item in onlinePlayer.inventory.contents) { + val meta = item.itemMeta + if (item != null && item.type == Material.WRITABLE_BOOK) { + if (meta is BookMeta && meta.persistentDataContainer.has(Main.Keys.CUSTOM_BOOK)) { + val updatedBookMeta = item.itemMeta as BookMeta + updatedBookMeta.pages = updatedMeta.pages + item.itemMeta = updatedBookMeta + } + } } } } -} +} \ No newline at end of file