Improved how books are handled

Implemented admin model viewer ::models
This commit is contained in:
Oven Bread
2023-07-30 08:33:40 +00:00
committed by Ryan
parent 3cf73ba09a
commit d76b99c77a
30 changed files with 214 additions and 95 deletions
@@ -14,10 +14,9 @@ import core.game.node.entity.player.Player
* This will handle component(26), component(27) and component(49) globally.
* DO NOT extend this class or override on(26), on(27), on(49) for defineInterfaceListeners.
*
* Instead, simply call BookInterface.pageSetup(...) and pass two attributes to open a book.
* bookInterfaceCallback - function to callback (player: Player, pageNum: Int, buttonID: Int) : Boolean
* bookInterfaceCurrentPage - 0 for first page.
* Recommend creating a display() function both to be called when opening an item and passing it to the callback.
* Instead, simply call BookInterface.openBook(...) in the listener and set the following attributes:
* bookInterfaceCallback - callback function for the page to display(player: Player, pageNum: Int, buttonID: Int) : Boolean
* You must create that display() function so that can be passed in the callback and display contents of each page.
*
* You may at any time after pageSetup,
* - call any functions below
@@ -32,6 +31,9 @@ import core.game.node.entity.player.Player
class BookInterface : InterfaceListener {
companion object {
const val CALLBACK_ATTRIBUTE = "bookInterfaceCallback";
const val CURRENT_PAGE_ATTRIBUTE = "bookInterfaceCurrentPage";
/* These should be in org.rs09.consts.Components but currently are not. */
const val FANCY_BOOK_26 = 26 // This is a 15-Lines per page book.
const val FANCY_BOOK_2_27 = 27 // This is a 15-Lines per page book with index and row clickable listeners.
@@ -47,30 +49,51 @@ class BookInterface : InterfaceListener {
val FANCY_BOOK_2_27_BUTTON_IDS = arrayOf(1, 3, 159, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158)
val FANCY_BOOK_3_49_BUTTON_IDS = arrayOf(51, 53);
/** Sets up standard pagination and page numbering. Call this for default setup of book components. */
fun pageSetup(player: Player, bookComponent: Int, title: String, contents: Array<PageSet>) {
val currentPage = getAttribute(player, "bookInterfaceCurrentPage", 0)
closeInterface(player) // Important: Close previous interfaces.
/* Image IDs. [...lines 1 to X] */
val FANCY_BOOK_2_27_IMAGE_ENABLE_DRAW_IDS = arrayOf(9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97);
val FANCY_BOOK_2_27_IMAGE_DRAW_IDS = arrayOf(10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98);
/** Opens the book interface. Call this only once in the defineListeners() at the start of opening a book. */
fun openBook(player: Player, bookComponent: Int, displayCallback: (player: Player, pageNum: Int, buttonId: Int) -> Boolean) {
closeInterface(player) // Important: Closes the previous interface.
setAttribute(player, CURRENT_PAGE_ATTRIBUTE, 0) // Resets the book to the first page.
setAttribute(player, CALLBACK_ATTRIBUTE, displayCallback) // Sets the display callback
if (bookComponent == FANCY_BOOK_26) {
openInterface(player, FANCY_BOOK_26) // Important: Opens the current interface.
} else if (bookComponent == FANCY_BOOK_2_27) {
openInterface(player, FANCY_BOOK_2_27) // Important: Opens the current interface.
} else if (bookComponent == FANCY_BOOK_3_49) {
openInterface(player, FANCY_BOOK_3_49) // Important: Opens the current interface.
}
displayCallback.invoke(player, 0, 0)
}
/** Sets up title, pagination and content. Call this in the display callback every time the page changes. */
fun pageSetup(player: Player, bookComponent: Int, title: String, contents: Array<PageSet>, hasPagination: Boolean = true) {
val currentPage = getAttribute(player, CURRENT_PAGE_ATTRIBUTE, 0)
if (bookComponent == FANCY_BOOK_26) {
clearBookLines(player, FANCY_BOOK_26, FANCY_BOOK_26_LINE_IDS);
clearButtons(player, FANCY_BOOK_26, FANCY_BOOK_26_BUTTON_IDS);
setTitle(player, FANCY_BOOK_26, FANCY_BOOK_26_LINE_IDS, title);
setPagination(player, FANCY_BOOK_26, FANCY_BOOK_26_LINE_IDS, FANCY_BOOK_26_BUTTON_IDS, currentPage, contents.size, contents[currentPage].pages.size == 1)
if (hasPagination) {
setPagination(player, FANCY_BOOK_26, FANCY_BOOK_26_LINE_IDS, FANCY_BOOK_26_BUTTON_IDS, currentPage, contents.size, contents[currentPage].pages.size == 1)
}
setPageContent(player, FANCY_BOOK_26, FANCY_BOOK_26_LINE_IDS, FANCY_BOOK_26_BUTTON_IDS, currentPage, contents);
} else if (bookComponent == FANCY_BOOK_2_27) {
openInterface(player, FANCY_BOOK_2_27) // Important: Opens the current interface.
clearBookLines(player, FANCY_BOOK_2_27, FANCY_BOOK_2_27_LINE_IDS);
clearButtons(player, FANCY_BOOK_2_27, FANCY_BOOK_2_27_BUTTON_IDS);
setTitle(player, FANCY_BOOK_2_27, FANCY_BOOK_2_27_LINE_IDS, title);
setPagination(player, FANCY_BOOK_2_27, FANCY_BOOK_2_27_LINE_IDS, FANCY_BOOK_2_27_BUTTON_IDS, currentPage, contents.size, contents[currentPage].pages.size == 1)
if (hasPagination) {
setPagination(player, FANCY_BOOK_2_27, FANCY_BOOK_2_27_LINE_IDS, FANCY_BOOK_2_27_BUTTON_IDS, currentPage, contents.size, contents[currentPage].pages.size == 1)
}
setPageContent(player, FANCY_BOOK_2_27, FANCY_BOOK_2_27_LINE_IDS, FANCY_BOOK_2_27_BUTTON_IDS, currentPage, contents);
} else if (bookComponent == FANCY_BOOK_3_49) {
openInterface(player, FANCY_BOOK_3_49) // Important: Opens the current interface.
clearBookLines(player, FANCY_BOOK_3_49, FANCY_BOOK_3_49_LINE_IDS);
clearButtons(player, FANCY_BOOK_3_49, FANCY_BOOK_3_49_BUTTON_IDS);
setTitle(player, FANCY_BOOK_3_49, FANCY_BOOK_3_49_LINE_IDS, title);
setPagination(player, FANCY_BOOK_3_49, FANCY_BOOK_3_49_LINE_IDS, FANCY_BOOK_3_49_BUTTON_IDS, currentPage, contents.size, contents[currentPage].pages.size == 1)
if (hasPagination) {
setPagination(player, FANCY_BOOK_3_49, FANCY_BOOK_3_49_LINE_IDS, FANCY_BOOK_3_49_BUTTON_IDS, currentPage, contents.size, contents[currentPage].pages.size == 1)
}
setPageContent(player, FANCY_BOOK_3_49, FANCY_BOOK_3_49_LINE_IDS, FANCY_BOOK_3_49_BUTTON_IDS, currentPage, contents);
}
}
@@ -102,8 +125,14 @@ class BookInterface : InterfaceListener {
player.packetDispatch.sendString("" + (currentPage * 2 + 1), componentId, bookLineIds[1])
player.packetDispatch.sendString("" + (currentPage * 2 + 2), componentId, bookLineIds[2])
if (hasRightPage) {
// If there's no right side page, remove the page number. Usually for odd page books.
player.packetDispatch.sendString("", componentId, BookInterface.FANCY_BOOK_26_LINE_IDS[2])
// If there's no right side page, remove the page number. Usually for odd paged books.
if (componentId == FANCY_BOOK_26) {
player.packetDispatch.sendString("", componentId, FANCY_BOOK_26_LINE_IDS[2])
} else if (componentId == FANCY_BOOK_2_27) {
player.packetDispatch.sendString("", componentId, FANCY_BOOK_2_27_LINE_IDS[2])
} else if (componentId == FANCY_BOOK_3_49) {
player.packetDispatch.sendString("", componentId, FANCY_BOOK_3_49_LINE_IDS[2])
}
}
}
@@ -124,6 +153,17 @@ class BookInterface : InterfaceListener {
}
}
/** Sets models(pictures) on lineId of pageSet (0 index). Call this in the display function after pageSetup. */
fun setModelOnPage(player: Player, pageSet: Int, modelId: Int, componentId: Int, enableLineId: Int, drawLineId: Int, zoom: Int, pitch: Int, yaw: Int) {
if (pageSet == getAttribute(player, CURRENT_PAGE_ATTRIBUTE, 0)) {
player.packetDispatch.sendInterfaceConfig(componentId, enableLineId, false)
player.packetDispatch.sendModelOnInterface(modelId, componentId, drawLineId, 0)
player.packetDispatch.sendAngleOnInterface(componentId, drawLineId, zoom, pitch, yaw)
} else {
player.packetDispatch.sendInterfaceConfig(componentId, enableLineId, true)
}
}
/** Function to check if player read to the last page. For quest triggers. */
fun isLastPage(pageNum: Int, totalPages: Int): Boolean {
return pageNum == totalPages - 1;
@@ -132,9 +172,9 @@ class BookInterface : InterfaceListener {
/** PRIVATE: Increments the current page and invokes the callback function. */
private fun changePageAndCallback(player: Player, increment: Int, buttonId: Int) {
val callback: ((player: Player, pageNum: Int, buttonId: Int) -> Boolean)? =
getAttribute(player, "bookInterfaceCallback", null)
val currentPage = getAttribute(player, "bookInterfaceCurrentPage", 0)
setAttribute(player, "bookInterfaceCurrentPage", currentPage + increment)
getAttribute(player, CALLBACK_ATTRIBUTE, null)
val currentPage = getAttribute(player, CURRENT_PAGE_ATTRIBUTE, 0)
setAttribute(player, CURRENT_PAGE_ATTRIBUTE, currentPage + increment)
callback?.invoke(player, currentPage + increment, buttonId)
}
@@ -4,8 +4,7 @@ import content.global.handlers.iface.BookInterface
import content.global.handlers.iface.BookLine
import content.global.handlers.iface.Page
import content.global.handlers.iface.PageSet
import core.api.getAttribute
import core.api.setAttribute
import core.api.*
import core.game.interaction.IntType
import core.game.interaction.InteractionListener
import core.game.node.entity.player.Player
@@ -204,9 +203,7 @@ class GeneralRuleBook {
/** Since the Town Crier shows you the book, there is no item here. */
fun openBook(player: Player) {
setAttribute(player, "bookInterfaceCallback", ::display)
setAttribute(player, "bookInterfaceCurrentPage", 0)
display(player, 0, 0)
BookInterface.openBook(player, BookInterface.FANCY_BOOK_2_27, ::display)
}
}
}
@@ -132,9 +132,7 @@ class GrimDiary : InteractionListener {
override fun defineListeners() {
on(Items.THE_GRIM_REAPERS_DIARY_11780, IntType.ITEM, "read") { player, _ ->
setAttribute(player, "bookInterfaceCallback", ::display)
setAttribute(player, "bookInterfaceCurrentPage", 0)
display(player, 0, 0)
BookInterface.openBook(player, BookInterface.FANCY_BOOK_26, ::display)
return@on true
}
}
@@ -127,9 +127,7 @@ class SecurityBookPlugin : InteractionListener {
override fun defineListeners() {
on(Items.SECURITY_BOOK_9003, IntType.ITEM, "read") { player, _ ->
setAttribute(player, "bookInterfaceCallback", ::display)
setAttribute(player, "bookInterfaceCurrentPage", 0)
display(player, 0, 0)
BookInterface.openBook(player, BookInterface.FANCY_BOOK_2_27, ::display)
return@on true
}
}
@@ -129,9 +129,7 @@ class StrongholdNotes : InteractionListener {
override fun defineListeners() {
on(Items.STRONGHOLD_NOTES_9004, IntType.ITEM, "read") { player, _ ->
setAttribute(player, "bookInterfaceCallback", ::display)
setAttribute(player, "bookInterfaceCurrentPage", 0)
display(player, 0, 0)
BookInterface.openBook(player, BookInterface.FANCY_BOOK_2_27, ::display)
return@on true
}
}