Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move runSync function into InterfacesListeners #7

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.noxcrew.interfaces.click.ClickHandler
import com.noxcrew.interfaces.click.CompletableClickHandler
import com.noxcrew.interfaces.grid.GridPoint
import com.noxcrew.interfaces.pane.PlayerPane
import com.noxcrew.interfaces.utilities.runSync
import com.noxcrew.interfaces.view.AbstractInterfaceView
import com.noxcrew.interfaces.view.ChestInterfaceView
import com.noxcrew.interfaces.view.InterfaceView
Expand Down Expand Up @@ -505,4 +504,14 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
(query.view as AbstractInterfaceView<*, *, *>).markClosed(Reason.PLAYER)
}
}

/** Runs [function] on the main thread. */
internal fun runSync(function: () -> Unit) {
if (Bukkit.isPrimaryThread()) {
function()
return
}

Bukkit.getScheduler().callSyncMethod(plugin, function)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import com.noxcrew.interfaces.properties.Trigger
import com.noxcrew.interfaces.transform.AppliedTransform
import com.noxcrew.interfaces.utilities.CollapsablePaneMap
import com.noxcrew.interfaces.utilities.forEachInGrid
import com.noxcrew.interfaces.utilities.runSync
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.withTimeout
Expand Down Expand Up @@ -176,7 +175,7 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, T : Interfa
// Ensure we always close on the main thread! Don't close if we are
// changing views though.
if (!changingView && isOpen()) {
runSync {
InterfacesListeners.INSTANCE.runSync {
if (!player.isConnected) return@runSync
player.closeInventory()
}
Expand Down Expand Up @@ -394,7 +393,7 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, T : Interfa
// Draw the contents of the inventory synchronously because
// we don't want it to happen in between ticks and show
// a half-finished inventory.
runSync {
InterfacesListeners.INSTANCE.runSync {
// If the menu has since been requested to close we ignore all this
if (!shouldBeOpened.get()) return@runSync

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.noxcrew.interfaces.InterfacesListeners
import com.noxcrew.interfaces.interfaces.PlayerInterface
import com.noxcrew.interfaces.inventory.PlayerInterfacesInventory
import com.noxcrew.interfaces.pane.PlayerPane
import com.noxcrew.interfaces.utilities.runSync
import net.kyori.adventure.text.Component
import org.bukkit.entity.Player
import org.bukkit.event.inventory.InventoryCloseEvent
Expand Down Expand Up @@ -65,7 +64,7 @@ public class PlayerInterfaceView internal constructor(

// Ensure we update the interface state in the main thread!
// Even if the menu is not currently on the screen.
runSync {
InterfacesListeners.INSTANCE.runSync {
InterfacesListeners.INSTANCE.setOpenInterface(player.uniqueId, null)
}
}
Expand Down
Loading