Skip to content

Commit

Permalink
Refactor book synchronization and add custom book key handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
1lent committed Dec 17, 2024
1 parent 71f7ff7 commit 108e9a9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
11 changes: 8 additions & 3 deletions src/main/kotlin/com/lent/snapchat/Item.kt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
}
}
8 changes: 8 additions & 0 deletions src/main/kotlin/com/lent/snapchat/Keys.kt
Original file line number Diff line number Diff line change
@@ -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")
}
15 changes: 6 additions & 9 deletions src/main/kotlin/com/lent/snapchat/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,27 @@ 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()
getPluginManager().registerEvents(snapChatEvents, this)
foundation.registerCommands(this)

}



@Command("snapchat")
@CommandPermission("snapchat.conversation")
fun onCommand(sender: CommandSender) {
val player = sender as Player
player.inventory.addItem(item.snapbook)
}
}

}
}
26 changes: 14 additions & 12 deletions src/main/kotlin/com/lent/snapchat/SnapChatEvents.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}

}
}
}

0 comments on commit 108e9a9

Please sign in to comment.