Skip to content

Commit

Permalink
Breakup event listeners (#21)
Browse files Browse the repository at this point in the history
* refactor: breakup event listeners

* misc: bump version
  • Loading branch information
DebitCardz authored Nov 26, 2023
1 parent c50302d commit 691c54a
Show file tree
Hide file tree
Showing 11 changed files with 411 additions and 345 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repositories {
}

dependencies {
implementation("com.github.DebitCardz:mc-chestui-plus:1.3.1")
implementation("com.github.DebitCardz:mc-chestui-plus:1.3.2")
}
```
### Maven
Expand All @@ -34,7 +34,7 @@ dependencies {
<dependency>
<groupId>com.github.DebitCardz</groupId>
<artifactId>mc-chestui-plus</artifactId>
<version>1.3.1</version>
<version>1.3.2</version>
</dependency>

```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ val githubActor = project.findProperty("gpr.user") as String? ?: System.getenv("
val githubToken = project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN")

group = "me.tech"
version = "1.3.1"
version = "1.3.2"

repositories {
mavenCentral()
Expand Down
24 changes: 19 additions & 5 deletions src/main/kotlin/me/tech/mcchestui/GUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
package me.tech.mcchestui

import me.tech.mcchestui.item.GUIItem
import me.tech.mcchestui.listeners.GUIListener
import me.tech.mcchestui.listeners.*
import me.tech.mcchestui.listeners.item.*
import me.tech.mcchestui.listeners.hotbar.*
import me.tech.mcchestui.utils.GUICloseEvent
import me.tech.mcchestui.utils.GUIDragItemEvent
import me.tech.mcchestui.utils.GUIItemPickupEvent
Expand Down Expand Up @@ -138,7 +140,17 @@ class GUI(
*/
internal var templateSlots = mutableMapOf<Char, Slot>()

private val guiListener = GUIListener(this)
private val eventListeners = listOf(
GUISlotClickListener(this),

GUIItemPickupListener(this),
GUIItemPlaceListener(this),
GUIItemDragListener(this),

GUIHotbarListener(this),

GUICloseListener(this)
)

/**
* Define weather the [GUI] has been unregistered.
Expand All @@ -147,8 +159,10 @@ class GUI(
internal var unregistered = false

init {
plugin.server.pluginManager
.registerEvents(guiListener, plugin)
eventListeners.forEach {
plugin.server.pluginManager
.registerEvents(it, plugin)
}
}

/**
Expand Down Expand Up @@ -308,7 +322,7 @@ class GUI(
* Mark a [GUI] as unregistered and remove its [Listener].
*/
fun unregister() {
HandlerList.unregisterAll(guiListener)
eventListeners.forEach(HandlerList::unregisterAll)
unregistered = true
}
}
28 changes: 28 additions & 0 deletions src/main/kotlin/me/tech/mcchestui/listeners/GUICloseListener.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package me.tech.mcchestui.listeners

import me.tech.mcchestui.GUI
import org.bukkit.event.EventHandler
import org.bukkit.event.inventory.InventoryCloseEvent

internal class GUICloseListener(gui: GUI) : GUIEventListener(gui) {
@EventHandler
internal fun InventoryCloseEvent.onClose() {
if(!gui.isBukkitInventory(inventory)) {
return
}

gui.onCloseInventory?.let { uiEvent ->
uiEvent(this, player)
}

if(gui.singleInstance) {
return
}

if(inventory.viewers.size > 1) {
return
}

gui.unregister()
}
}
45 changes: 45 additions & 0 deletions src/main/kotlin/me/tech/mcchestui/listeners/GUIEventListener.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package me.tech.mcchestui.listeners

import me.tech.mcchestui.GUI
import org.bukkit.event.Listener
import org.bukkit.event.inventory.InventoryAction

internal abstract class GUIEventListener(
protected val gui: GUI
) : Listener {
companion object {
/**
* All actions related to placing an item.
*/
@JvmStatic
protected val PLACE_ACTIONS = listOf(
InventoryAction.PLACE_ONE,
InventoryAction.PLACE_SOME,
InventoryAction.PLACE_ALL,
InventoryAction.SWAP_WITH_CURSOR
)

/**
* All actions related to using hotkeys to place/pickup
* an item.
*/
@JvmStatic
protected val HOTBAR_ACTIONS = listOf(
InventoryAction.HOTBAR_SWAP,
InventoryAction.HOTBAR_MOVE_AND_READD
)

/**
* All actions related to picking up an item.
*/
@JvmStatic
protected val PICKUP_ACTIONS = listOf(
InventoryAction.PICKUP_ONE,
InventoryAction.PICKUP_SOME,
InventoryAction.PICKUP_HALF,
InventoryAction.PICKUP_ALL,
InventoryAction.SWAP_WITH_CURSOR,
InventoryAction.COLLECT_TO_CURSOR
)
}
}
Loading

0 comments on commit 691c54a

Please sign in to comment.