-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
4 changed files
with
36 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
} |