Instantly share code, notes, and snippets.
Last active
January 23, 2020 12:01
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save DevSrSouza/cb4a689d9482b33c89b0b1ff576e7dd4 to your computer and use it in GitHub Desktop.
KotlinBukkitAPI Menu Pagination sample
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
private val items = Array<ItemStack>(64) { | |
item(Material.GOLD_INGOT, it).displayName("$it") | |
}.toMutableList() | |
private var menuCache: MenuDSL? = null | |
val goldsMenu: MenuDSL | |
get() { | |
if(menuCache == null) | |
menuCache = generateMenUConfiguration() | |
} | |
private fun generateMenuConfiguration() = menu("Golds Menu", 6) { | |
val previousPage = slot(6, 1, item(Material.ARROW).displayName("Previous Page")) | |
val nextPage = slot(6, 9, item(Material.ARROW).displayName("Next Page")) | |
val pagination = pagination( | |
ObservableList(items), | |
nextPage, previousPage | |
) { | |
slot { | |
onRender { showingItem = it } | |
} | |
onPageChange { | |
updateSlot(previousPageSlot, player) | |
updateSlot(nextPageSlot, player) | |
} | |
} | |
fun MenuPlayerInventorySlot.showItemWhenHasPage(hasPageLogic: () -> Boolean) { | |
if(hasPageLogic()) { | |
showingItem = nextPage.item | |
} else { | |
showingItem = null | |
} | |
} | |
nextPage.onRender { showItemWhenHasPage { pagination.hasNextPage(player) } } | |
nextPage.onUpdate { showItemWhenHasPage { pagination.hasNextPage(player) } } | |
previousPage.onRender { showItemWhenHasPage { pagination.hasPreviousPage(player) } } | |
previousPage.onUpdate { showItemWhenHasPage { pagination.hasPreviousPage(player) } } | |
} |
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
private val MINECRAFT_MATERIALS = ObservableList( | |
Material.values().toMutableList() | |
) | |
private var menuCache: MenuDSL? = null | |
val materialMenu: MenuDSL | |
get() { | |
if(menuCache == null) | |
menuCache = generateMenuConfiguration() | |
return menuCache!! | |
} | |
private fun generateMenuConfiguration() = menu("Mercado", 6, true) { | |
val previousPage = slot(6, 1, item(Material.ARROW).displayName("Previous Page")) | |
val nextPage = slot(6, 9, item(Material.ARROW).displayName("Next Page")) | |
val itemsAdapter: ItemsAdapter<Material> = { items -> | |
val filter = getPlayerData(PICKAXE_FILTER_KEY) as Boolean? ?: false | |
if(filter) items.filter { | |
it.name.contains("PICKAXE") | |
} | |
else items | |
} | |
val pagination = pagination( | |
MINECRAFT_MATERIALS, | |
nextPage, previousPage, | |
startLine = 2, endLine = 4, | |
startSlot = 2, endSlot = 4, | |
itemsAdapterOnOpen = itemsAdapter, | |
itemsAdapterOnUpdate = itemsAdapter | |
) { | |
slot { | |
onRender { currentItem -> | |
showingItem = currentItem?.asItemStack() | |
} | |
} | |
onPageChange { | |
updateSlot(nextPageSlot, player) | |
updateSlot(previousPageSlot, player) | |
} | |
} | |
fun MenuPlayerInventorySlot.showItemWhenHasPage(hasPageLogic: () -> Boolean) { | |
if(hasPageLogic()) { | |
showingItem = nextPage.item | |
} else { | |
showingItem = null | |
} | |
} | |
nextPage.onRender { showItemWhenHasPage { pagination.hasNextPage(player) } } | |
nextPage.onUpdate { showItemWhenHasPage { pagination.hasNextPage(player) } } | |
previousPage.onRender { showItemWhenHasPage { pagination.hasPreviousPage(player) } } | |
previousPage.onUpdate { showItemWhenHasPage { pagination.hasPreviousPage(player) } } | |
slot(6, 5, item(Material.DIAMOND_PICKAXE).displayName("FILTER BY PICKAXE")) { | |
onClick { | |
canceled = true | |
putPlayerData(PICKAXE_FILTER_KEY, true) | |
pagination.updateItemsToPlayer(this) | |
} | |
} | |
slot(6, 6, item(Material.STONE).displayName("REMOVE FILTER")) { | |
onClick { | |
putPlayerData(PICKAXE_FILTER_KEY, false) | |
pagination.updateItemsToPlayer(this) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment