Skip to content

Commit

Permalink
feat: SuspendedTile
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Nov 12, 2024
1 parent 2b0c110 commit e6e960d
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 4 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//file:noinspection all

plugins {
id "fabric-loom" version "1.8-SNAPSHOT"
id "org.jetbrains.kotlin.jvm"
Expand Down Expand Up @@ -76,6 +78,10 @@ tasks.withType(JavaCompile).configureEach {
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
jvmTarget = 21
freeCompilerArgs += [
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
"-opt-in=kotlinx.coroutines.DelicateCoroutinesApi",
]
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ kotlin_version=2.0.21
fabric_kotlin_version=1.12.3

# mod properties
mod_version=1.5.1
mod_version=1.5.2
maven_group=net.mcbrawls
mod_id=slate
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ enum class ClickType {
* Parses a click type from the given data.
*/
fun parse(button: Int, actionType: SlotActionType): ClickType {
return when(actionType) {
return when (actionType) {
SlotActionType.SWAP -> if (button == PlayerInventory.OFF_HAND_SLOT) OFFHAND else NUMBER_KEY
SlotActionType.CLONE -> MIDDLE
SlotActionType.THROW -> THROW
Expand Down
84 changes: 84 additions & 0 deletions src/main/kotlin/net/mcbrawls/slate/tile/SuspendedTile.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package net.mcbrawls.slate.tile

import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import net.mcbrawls.slate.Slate
import net.minecraft.item.ItemStack
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.util.Util
import org.apache.commons.lang3.mutable.MutableLong

/**
* A tile provided by a suspended function.
*/
class SuspendedTile(
/**
* The tile to display when no tile has been calculated.
*/
val baseTile: Tile,

/**
* The suspended function factory.
*/
val tileFactory: ChildFactory,
) : Tile() {
/**
* The created tile.
*/
private var tile: Tile? = null
set(value) {
field = value
value?.also(::setMetadataFrom)
}

private var latestCallTimestamp = MutableLong(0L)
private var factoryState: FactoryState = FactoryState.EMPTY

fun updateTile(slate: Slate, player: ServerPlayerEntity): Tile {
if (factoryState == FactoryState.EMPTY) {
factoryState = FactoryState.SUSPENDED

val timestamp = Util.getMeasuringTimeMs()
latestCallTimestamp.setValue(timestamp)

GlobalScope.async {
val newTile = tileFactory.create(slate, player)

// only update for latest function call
synchronized(latestCallTimestamp) {
if (timestamp == latestCallTimestamp.value) {
tile = newTile
factoryState = FactoryState.FINISHED
}
}
}
}

return tile ?: baseTile
}

fun refreshTile() {
factoryState = FactoryState.EMPTY
}

override fun createDisplayedStack(slate: Slate, player: ServerPlayerEntity): ItemStack {
val trueTile = updateTile(slate, player)
return trueTile.createDisplayedStack(slate, player)
}

fun interface ChildFactory {
suspend fun create(slate: Slate, player: ServerPlayerEntity): Tile?
}

enum class FactoryState {
EMPTY,
SUSPENDED,
FINISHED
}

companion object {
fun tile(baseTile: Tile, tileFactory: ChildFactory): SuspendedTile {
return SuspendedTile(baseTile, tileFactory)
}
}
}
10 changes: 10 additions & 0 deletions src/main/kotlin/net/mcbrawls/slate/tile/Tile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ open class Tile {
}
}

fun setMetadataFrom(tile: Tile) {
tooltip.clear()
tooltip.addAll(tile.tooltip)

immovable = tile.immovable

clickCallbacks.clear()
clickCallbacks.addAll(tile.clickCallbacks)
}

override fun toString(): String {
return "Tile"
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/net/mcbrawls/slate/tile/TileGrid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ open class TileGrid(val width: Int, val height: Int) {
* The width of this screen handler type.
*/
val ScreenHandlerType<*>.width: Int get() {
return when(this) {
return when (this) {
ScreenHandlerType.CRAFTING -> 2
ScreenHandlerType.SMITHING -> 4
ScreenHandlerType.GENERIC_3X3 -> 3
Expand All @@ -172,7 +172,7 @@ open class TileGrid(val width: Int, val height: Int) {
* The height of this screen handler type.
*/
val ScreenHandlerType<*>.height: Int get() {
return when(this) {
return when (this) {
ScreenHandlerType.GENERIC_9X6 -> 6
ScreenHandlerType.CRAFTING -> 6
ScreenHandlerType.GENERIC_9X5 -> 5
Expand Down
16 changes: 16 additions & 0 deletions src/test/kotlin/net/mcbrawls/slate/test/SlateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import net.mcbrawls.slate.Slate.Companion.slate
import net.mcbrawls.slate.SlatePlayer
import net.mcbrawls.slate.screen.slot.ClickType
import net.mcbrawls.slate.tile.StackTile
import net.mcbrawls.slate.tile.SuspendedTile.Companion.tile
import net.mcbrawls.slate.tile.Tile.Companion.tile
import net.mcbrawls.slate.tile.TileGrid
import net.mcbrawls.slate.tooltip.TooltipChunk.Companion.tooltipChunk
Expand All @@ -20,6 +21,7 @@ import net.minecraft.text.Text
import net.minecraft.util.ActionResult
import net.minecraft.util.Formatting
import net.minecraft.util.math.random.Random
import kotlin.concurrent.thread

class SlateTest : ModInitializer {
override fun onInitialize() {
Expand Down Expand Up @@ -61,6 +63,20 @@ class SlateTest : ModInitializer {
tiles[0, 0] = tile()
tiles[1, 0] = tile(Items.WHITE_WOOL)

val suspendedTile = tile(tile()) { slate, player ->
tile(ItemStack(Items.CHORUS_FRUIT)) {
tooltip(Text.literal("${slate.key}, ${player.age}"))
}
}
tiles[0, 1] = suspendedTile

thread {
for (i in 0 until 5) {
Thread.sleep(3000L)
suspendedTile.refreshTile()
}
}

// bound tests
tiles.setInventory(0, 0, tile(Items.ORANGE_WOOL))
tiles[tiles.width - 1, 0] = tile(Items.RED_WOOL)
Expand Down

0 comments on commit e6e960d

Please sign in to comment.