Skip to content

Commit

Permalink
Make the coroutine scope available to other plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeltumn committed Jul 31, 2024
1 parent ee9cf51 commit df74314
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 61 deletions.
39 changes: 0 additions & 39 deletions interfaces/src/main/kotlin/com/noxcrew/interfaces/Constants.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.noxcrew.interfaces

import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.asCoroutineDispatcher
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicInteger

/** Holds the shared scope used for any interfaces coroutines. */
public object InterfacesConstants {

/** The [CoroutineScope] for any suspending operations performed by interfaces. */
public val SCOPE: CoroutineScope = CoroutineScope(
CoroutineName("interfaces") +
SupervisorJob() +
run {
val threadNumber = AtomicInteger()
val factory = { runnable: Runnable ->
Thread("interfaces-${threadNumber.getAndIncrement()}").apply {
isDaemon = true
}
}

System.getProperty("com.noxcrew.interfaces.fixedPoolSize")
?.toIntOrNull()
?.coerceAtLeast(2)
?.let { size ->
if (System.getProperty("com.noxcrew.interfaces.useScheduledPool").toBoolean()) {
Executors.newScheduledThreadPool(size, factory)
} else {
Executors.newFixedThreadPool(size, factory)
}
}
?.asCoroutineDispatcher()
?: Dispatchers.Default
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.destroystokyo.paper.MaterialSetTag
import com.destroystokyo.paper.MaterialTags
import com.github.benmanes.caffeine.cache.Cache
import com.github.benmanes.caffeine.cache.Caffeine
import com.noxcrew.interfaces.Constants.SCOPE
import com.noxcrew.interfaces.InterfacesConstants.SCOPE
import com.noxcrew.interfaces.click.ClickContext
import com.noxcrew.interfaces.click.ClickHandler
import com.noxcrew.interfaces.click.CompletableClickHandler
Expand Down Expand Up @@ -155,6 +155,15 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
.weakValues()
.build()

/** Re-opens the current open interface of [player]. */
public fun reopenInventory(player: Player) {
getOpenInterface(player.uniqueId)?.also {
SCOPE.launch {
it.open()
}
}
}

/** Returns the currently open interface for [playerId]. */
public fun getOpenInterface(playerId: UUID): PlayerInterfaceView? {
// Check if the menu is definitely still meant to be open
Expand Down Expand Up @@ -285,25 +294,25 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
// Don't check top inventory if we're in the player inventory!
if (
(
!isInPlayerInventory && topInventory.withIndex().any { (index, it) ->
// Check if any item is being collected that cannot be moved!
it != null && it.isSimilar(clickedItem) && !canFreelyMove(
view,
requireNotNull(GridPoint.fromBukkitChestSlot(index)),
false
)
}
) ||
!isInPlayerInventory && topInventory.withIndex().any { (index, it) ->
// Check if any item is being collected that cannot be moved!
it != null && it.isSimilar(clickedItem) && !canFreelyMove(
view,
requireNotNull(GridPoint.fromBukkitChestSlot(index)),
false
)
}
) ||
bottomInventory.withIndex().any { (index, it) ->
it != null && it.isSimilar(clickedItem) &&
// These slots are always in the player inventory and always need to be relativized!
!canFreelyMove(
view,
view.backing.relativizePlayerInventorySlot(
requireNotNull(GridPoint.fromBukkitPlayerSlot(index))
),
true
)
// These slots are always in the player inventory and always need to be relativized!
!canFreelyMove(
view,
view.backing.relativizePlayerInventorySlot(
requireNotNull(GridPoint.fromBukkitPlayerSlot(index))
),
true
)
}
) {
event.isCancelled = true
Expand Down Expand Up @@ -477,9 +486,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)

@EventHandler
public fun onRespawn(event: PlayerRespawnEvent) {
SCOPE.launch {
getOpenInterface(event.player.uniqueId)?.open()
}
reopenInventory(event.player)
}

@EventHandler(priority = EventPriority.LOWEST)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.noxcrew.interfaces.view

import com.google.common.collect.HashMultimap
import com.noxcrew.interfaces.Constants.SCOPE
import com.noxcrew.interfaces.InterfacesConstants.SCOPE
import com.noxcrew.interfaces.InterfacesListeners
import com.noxcrew.interfaces.event.DrawPaneEvent
import com.noxcrew.interfaces.grid.GridPoint
Expand Down

0 comments on commit df74314

Please sign in to comment.