Eliminated plugin and kSpigot parameters

This commit is contained in:
bluefireoly
2020-10-12 17:01:01 +02:00
parent 6b2f95c7fa
commit 29bd82803d
11 changed files with 63 additions and 69 deletions

View File

@@ -3,7 +3,7 @@
package net.axay.kspigot.inventory
import net.axay.kspigot.event.listen
import net.axay.kspigot.main.KSpigot
import net.axay.kspigot.main.KSpigotMainInstance
import org.bukkit.entity.HumanEntity
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.inventory.Inventory
@@ -25,7 +25,7 @@ fun HumanEntity.openGUI(gui: InventoryGUI<*>, page: Int? = null): InventoryView?
// GUI HOLDER
class InventoryGUIHolder(kSpigot: KSpigot) : AutoCloseable {
class InventoryGUIHolder : AutoCloseable {
private val registered = HashSet<InventoryGUI<ForInventory>>()
@@ -39,7 +39,7 @@ class InventoryGUIHolder(kSpigot: KSpigot) : AutoCloseable {
init {
kSpigot.listen<InventoryClickEvent> {
listen<InventoryClickEvent> {
val clickedInv = it.clickedInventory ?: return@listen
@@ -76,7 +76,6 @@ class InventoryGUIClickEvent<T : ForInventory>(
private const val DEFAULT_PAGE = 1
class InventoryGUIData<T : ForInventory>(
val plugin: KSpigot,
val inventoryType: InventoryType<T>,
val title: String?,
internal val pages: Map<Int, InventoryGUIPage<T>>,
@@ -108,14 +107,14 @@ abstract class InventoryGUI<T : ForInventory>(
* (KSpigot will listen for actions in the inventory.)
*/
@Suppress("UNCHECKED_CAST")
fun register() = data.plugin.inventoryGUIHolder.register(this as InventoryGUI<ForInventory>)
fun register() = KSpigotMainInstance.inventoryGUIHolder.register(this as InventoryGUI<ForInventory>)
/**
* Stops KSpigot from listening to actions in this
* InventoryGUI anymore.
*/
@Suppress("UNCHECKED_CAST")
fun unregister() = data.plugin.inventoryGUIHolder.unregister(this as InventoryGUI<ForInventory>)
fun unregister() = KSpigotMainInstance.inventoryGUIHolder.unregister(this as InventoryGUI<ForInventory>)
/**
* Loads the specified page in order to display it in the GUI.

View File

@@ -2,16 +2,14 @@
package net.axay.kspigot.inventory
import net.axay.kspigot.main.KSpigot
import org.bukkit.inventory.ItemStack
fun <T : ForInventory> KSpigot.inventoryGUI(
fun <T : ForInventory> inventoryGUI(
type: InventoryType<T>,
builder: InventoryGUIBuilder<T>.() -> Unit,
) = InventoryGUIBuilder(this, type).apply(builder).build()
) = InventoryGUIBuilder(type).apply(builder).build()
class InventoryGUIBuilder<T : ForInventory>(
private val kSpigot: KSpigot,
val type: InventoryType<T>
) {
@@ -38,7 +36,7 @@ class InventoryGUIBuilder<T : ForInventory>(
}
internal fun build() = InventoryGUIShared(
InventoryGUIData(kSpigot, type, title, guiSlots, transitionTo, transitionFrom, onClickElement)
InventoryGUIData(type, title, guiSlots, transitionTo, transitionFrom, onClickElement)
).apply { register() }
}

View File

@@ -1,7 +1,6 @@
package net.axay.kspigot.inventory
import net.axay.kspigot.main.KSpigot
import net.axay.kspigot.runnables.task
import net.axay.kspigot.main.KSpigotMainInstance
abstract class InventoryGUIPageChangeCalculator {
@@ -54,7 +53,7 @@ internal fun InventoryGUI<*>.changePage(
val width = data.inventoryType.dimensions.width
changePageEffect(data.plugin, fromPageInt, toPageInt, width) { currentOffset, ifInverted ->
changePageEffect(fromPageInt, toPageInt, width) { currentOffset, ifInverted ->
if (ifInverted) {
loadPageUnsafe(fromPage, offsetHorizontally = currentOffset)
loadPageUnsafe(toPage, offsetHorizontally = -(width - currentOffset))
@@ -70,7 +69,7 @@ internal fun InventoryGUI<*>.changePage(
val height = data.inventoryType.dimensions.heigth
changePageEffect(data.plugin, fromPageInt, toPageInt, height) { currentOffset, ifInverted ->
changePageEffect(fromPageInt, toPageInt, height) { currentOffset, ifInverted ->
if (ifInverted) {
loadPageUnsafe(fromPage, offsetVertically = currentOffset)
loadPageUnsafe(toPage, offsetVertically = -(height - currentOffset))
@@ -86,7 +85,7 @@ internal fun InventoryGUI<*>.changePage(
val width = data.inventoryType.dimensions.width
changePageEffect(data.plugin, fromPageInt, toPageInt, width) { currentOffset, ifInverted ->
changePageEffect(fromPageInt, toPageInt, width) { currentOffset, ifInverted ->
if (ifInverted) {
loadPageUnsafe(toPage, offsetHorizontally = -(width - currentOffset))
} else {
@@ -100,7 +99,7 @@ internal fun InventoryGUI<*>.changePage(
val height = data.inventoryType.dimensions.heigth
changePageEffect(data.plugin, fromPageInt, toPageInt, height) { currentOffset, ifInverted ->
changePageEffect(fromPageInt, toPageInt, height) { currentOffset, ifInverted ->
if (ifInverted) {
loadPageUnsafe(toPage, offsetVertically = -(height - currentOffset))
} else {
@@ -120,7 +119,6 @@ internal fun InventoryGUI<*>.changeGUI(
) = changePage(effect.effect, fromPage, toPage)
private inline fun changePageEffect(
kSpigot: KSpigot,
fromPage: Int,
toPage: Int,
doFor: Int,
@@ -130,7 +128,7 @@ private inline fun changePageEffect(
val ifInverted = fromPage >= toPage
var currentOffset = 1
kSpigot.task(
KSpigotMainInstance.task(
sync = true,
period = 1,
howOften = doFor.toLong()