diff --git a/buildSrc/src/main/kotlin/BuildConstants.kt b/buildSrc/src/main/kotlin/BuildConstants.kt index 7d33ded0..5e644b60 100644 --- a/buildSrc/src/main/kotlin/BuildConstants.kt +++ b/buildSrc/src/main/kotlin/BuildConstants.kt @@ -1,5 +1,5 @@ object BuildConstants { - const val fabrikVersion = "0.4.0" + const val fabrikVersion = "0.4.1" const val projectState = "beta" const val curseforgeId = "447425" @@ -9,6 +9,6 @@ object BuildConstants { const val minecraftVersion = "1.16.5" const val yarnMappingsVersion = "1.16.5+build.9:v2" const val fabricLoaderVersion = "0.11.3" - const val fabricApiVersion = "0.34.1+1.16" + const val fabricApiVersion = "0.34.2+1.16" const val fabricLanguageKotlinVersion = "1.6.0+kotlin.1.5.0" } diff --git a/fabrikmc-core/src/main/kotlin/net/axay/fabrik/core/task/CoroutineTask.kt b/fabrikmc-core/src/main/kotlin/net/axay/fabrik/core/task/CoroutineTask.kt index 42f4fcec..1a879d36 100644 --- a/fabrikmc-core/src/main/kotlin/net/axay/fabrik/core/task/CoroutineTask.kt +++ b/fabrikmc-core/src/main/kotlin/net/axay/fabrik/core/task/CoroutineTask.kt @@ -1,16 +1,6 @@ package net.axay.fabrik.core.task -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch - -/** - * A CoroutineScope using the current MinecraftServer - * as the Dispatcher. - */ -lateinit var mcCoroutineScope: CoroutineScope - internal set +import kotlinx.coroutines.* /** * A CoroutineScope using the IO Dispatcher diff --git a/fabrikmc-core/src/main/kotlin/net/axay/fabrik/core/task/FabrikCoroutineManager.kt b/fabrikmc-core/src/main/kotlin/net/axay/fabrik/core/task/FabrikCoroutineManager.kt index 9a508276..a61b4f15 100644 --- a/fabrikmc-core/src/main/kotlin/net/axay/fabrik/core/task/FabrikCoroutineManager.kt +++ b/fabrikmc-core/src/main/kotlin/net/axay/fabrik/core/task/FabrikCoroutineManager.kt @@ -8,7 +8,8 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents object FabrikCoroutineManager { fun init() { ServerLifecycleEvents.SERVER_STARTING.register { - mcCoroutineScope = CoroutineScope(it.asCoroutineDispatcher()) + mcCoroutineDispatcher = it.asCoroutineDispatcher() + mcCoroutineScope = CoroutineScope(mcCoroutineDispatcher) logInfo("Initialized mcCoroutineScope") } } diff --git a/fabrikmc-core/src/main/kotlin/net/axay/fabrik/core/task/MinecraftSync.kt b/fabrikmc-core/src/main/kotlin/net/axay/fabrik/core/task/MinecraftSync.kt new file mode 100644 index 00000000..5ec804e3 --- /dev/null +++ b/fabrikmc-core/src/main/kotlin/net/axay/fabrik/core/task/MinecraftSync.kt @@ -0,0 +1,36 @@ +package net.axay.fabrik.core.task + +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.launch + +/** + * A [CoroutineDispatcher] which executes code synchronously to the + * MinecraftServer main thread. + */ +lateinit var mcCoroutineDispatcher: CoroutineDispatcher + internal set + +/** + * A [CoroutineScope] using the current MinecraftServer + * as the Dispatcher. + */ +lateinit var mcCoroutineScope: CoroutineScope + internal set + +/** + * Does the same as [launch], but the dispatcher defaults to [mcCoroutineDispatcher]. + * + * This way, you can execute code synchronously (to the MinecraftServer main thread) + * very easily. + * + * ```kotlin + * coroutineScope { + * mcSyncLaunch { + * // suspending and sync now + * } + * } + * ``` + */ +fun CoroutineScope.mcSyncLaunch(block: suspend CoroutineScope.() -> Unit) = + launch(mcCoroutineDispatcher, block = block) diff --git a/fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/Gui.kt b/fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/Gui.kt index e2deb729..88c13b8e 100644 --- a/fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/Gui.kt +++ b/fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/Gui.kt @@ -63,6 +63,8 @@ class Gui( page.startUsing(this) currentPage = page + } else if (!page.inUse) { + page.startUsing(this) } } diff --git a/fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/GuiPage.kt b/fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/GuiPage.kt index d088c03d..9cc39795 100644 --- a/fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/GuiPage.kt +++ b/fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/GuiPage.kt @@ -6,7 +6,7 @@ class GuiPage( val content: Map, val effectTo: ChangeEffect?, val effectFrom: ChangeEffect?, -) { +) : GuiUseable() { enum class ChangeEffect { INSTANT, SLIDE_HORIZONTALLY, @@ -15,7 +15,13 @@ class GuiPage( SWIPE_VERTICALLY, } - internal fun startUsing(gui: Gui) = content.values.toHashSet().forEach { it.startUsing(gui) } + override fun startUsing(gui: Gui) { + content.values.toHashSet().forEach { it.startUsing(gui) } + super.startUsing(gui) + } - internal fun stopUsing(gui: Gui) = content.values.toHashSet().forEach { it.stopUsing(gui) } + override fun stopUsing(gui: Gui) { + content.values.toHashSet().forEach { it.stopUsing(gui) } + super.stopUsing(gui) + } } diff --git a/fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/GuiScreenHandler.kt b/fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/GuiScreenHandler.kt index bb7ff142..d8248184 100644 --- a/fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/GuiScreenHandler.kt +++ b/fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/GuiScreenHandler.kt @@ -59,7 +59,7 @@ class GuiScreenHandler( ): ItemStack { if (gui.isOffset) return ItemStack.EMPTY - var shouldCancel = false + var shouldCancel = true val element = gui.currentPage.content[slotIndex] if (element != null) { diff --git a/fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/GuiUseable.kt b/fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/GuiUseable.kt index f8420309..4a604040 100644 --- a/fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/GuiUseable.kt +++ b/fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/GuiUseable.kt @@ -3,17 +3,24 @@ package net.axay.fabrik.igui abstract class GuiUseable { protected val registeredGuis = HashSet() + var inUse = false + private set + protected open fun onChangeUseStatus(inUse: Boolean) { } internal open fun startUsing(gui: Gui) { - if (registeredGuis.isEmpty()) + if (registeredGuis.isEmpty()) { + inUse = true onChangeUseStatus(true) + } registeredGuis += gui } internal open fun stopUsing(gui: Gui) { registeredGuis -= gui - if (registeredGuis.isEmpty()) + if (registeredGuis.isEmpty()) { + inUse = false onChangeUseStatus(false) + } } }