Skip to content

Commit

Permalink
feat: tile redirect rework
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Nov 11, 2024
1 parent e13ca84 commit 7e5f624
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 deletions.
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.4.2
mod_version=1.5
maven_group=net.mcbrawls
mod_id=slate
8 changes: 8 additions & 0 deletions src/main/kotlin/net/mcbrawls/slate/Slate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ open class Slate {
return slate.apply(builder)
}

/**
* Modifies the given slate to be a subslate of this slate.
*/
fun subslate(slate: Slate): Slate {
slate.parent = this
return slate
}

operator fun get(tileIndex: Int): Tile? {
val x = tileIndex % tiles.width
val y = tileIndex / tiles.width
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/net/mcbrawls/slate/tile/RedirectType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.mcbrawls.slate.tile

enum class RedirectType {
/**
* Redirects the entire tile.
*/
NORMAL,

/**
* Redirects the tile, hiding the stack at the new location.
*/
INVISIBLE
}
17 changes: 17 additions & 0 deletions src/main/kotlin/net/mcbrawls/slate/tile/RedirectedTile.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.mcbrawls.slate.tile

import net.mcbrawls.slate.Slate
import net.minecraft.item.ItemStack
import net.minecraft.server.network.ServerPlayerEntity

class RedirectedTile(
val parent: Tile,
val type: RedirectType,
) : Tile() {
override fun createDisplayedStack(slate: Slate, player: ServerPlayerEntity): ItemStack {
return when (type) {
RedirectType.NORMAL -> parent.createDisplayedStack(slate, player)
RedirectType.INVISIBLE -> ItemStack.EMPTY
}
}
}
6 changes: 4 additions & 2 deletions src/main/kotlin/net/mcbrawls/slate/tile/Tile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import net.minecraft.util.Identifier
/**
* A slot within a slate.
*/
abstract class Tile {
open class Tile {
/**
* The complete tooltip of the tile stack.
* The first element is the name, and the rest is flushed to the tooltip.
Expand Down Expand Up @@ -128,7 +128,9 @@ abstract class Tile {
/**
* The base item stack to be displayed.
*/
abstract fun createBaseStack(slate: Slate, player: ServerPlayerEntity): ItemStack
open fun createBaseStack(slate: Slate, player: ServerPlayerEntity): ItemStack {
return ItemStack.EMPTY
}

/**
* Creates the final displayed stack for this tile.
Expand Down
15 changes: 10 additions & 5 deletions src/main/kotlin/net/mcbrawls/slate/tile/TileGrid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ open class TileGrid(val width: Int, val height: Int) {
/**
* Redirects a tile index to another tile index.
*/
val redirects: MutableMap<Int, Int> = mutableMapOf()
val redirects: MutableMap<Int, Pair<Int, RedirectType>> = mutableMapOf()

/**
* The total size of all tiles.
Expand Down Expand Up @@ -71,8 +71,13 @@ open class TileGrid(val width: Int, val height: Int) {
* Gets a slot tile from the given index.
*/
operator fun get(index: Int): Tile? {
val trueIndex = redirects[index] ?: index
return tiles.getOrNull(trueIndex)
redirects[index]?.also { (trueIndex, type) ->
tiles.getOrNull(trueIndex)?.also { tile ->
return RedirectedTile(tile, type)
}
}

return tiles.getOrNull(index)
}

/**
Expand Down Expand Up @@ -108,8 +113,8 @@ open class TileGrid(val width: Int, val height: Int) {
/**
* Sets a redirect on this tile grid.
*/
fun redirect(index: Int, otherIndex: Int) {
redirects[index] = otherIndex
fun redirect(index: Int, otherIndex: Int, type: RedirectType = RedirectType.NORMAL) {
redirects[index] = otherIndex to type
}

fun forEach(action: (Int, Tile?) -> Unit) {
Expand Down

0 comments on commit 7e5f624

Please sign in to comment.