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

Imrove Gui api #5

Merged
merged 8 commits into from
May 20, 2021
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
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/BuildConstants.kt
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/Gui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class Gui(
page.startUsing(this)

currentPage = page
} else if (!page.inUse) {
page.startUsing(this)
}
}

Expand Down
12 changes: 9 additions & 3 deletions fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/GuiPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class GuiPage(
val content: Map<Int, GuiElement>,
val effectTo: ChangeEffect?,
val effectFrom: ChangeEffect?,
) {
) : GuiUseable() {
enum class ChangeEffect {
INSTANT,
SLIDE_HORIZONTALLY,
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 9 additions & 2 deletions fabrikmc-igui/src/main/kotlin/net/axay/fabrik/igui/GuiUseable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ package net.axay.fabrik.igui
abstract class GuiUseable {
protected val registeredGuis = HashSet<Gui>()

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)
}
}
}