From 7e5f624f79ec8e726ec477af6df3ff6db7561b59 Mon Sep 17 00:00:00 2001 From: Daniel Orr Date: Mon, 11 Nov 2024 20:28:31 +0000 Subject: [PATCH] feat: tile redirect rework --- gradle.properties | 2 +- src/main/kotlin/net/mcbrawls/slate/Slate.kt | 8 ++++++++ .../net/mcbrawls/slate/tile/RedirectType.kt | 13 +++++++++++++ .../net/mcbrawls/slate/tile/RedirectedTile.kt | 17 +++++++++++++++++ src/main/kotlin/net/mcbrawls/slate/tile/Tile.kt | 6 ++++-- .../kotlin/net/mcbrawls/slate/tile/TileGrid.kt | 15 ++++++++++----- 6 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 src/main/kotlin/net/mcbrawls/slate/tile/RedirectType.kt create mode 100644 src/main/kotlin/net/mcbrawls/slate/tile/RedirectedTile.kt diff --git a/gradle.properties b/gradle.properties index 19e1944..759a9fe 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/src/main/kotlin/net/mcbrawls/slate/Slate.kt b/src/main/kotlin/net/mcbrawls/slate/Slate.kt index b9bfd04..a151644 100644 --- a/src/main/kotlin/net/mcbrawls/slate/Slate.kt +++ b/src/main/kotlin/net/mcbrawls/slate/Slate.kt @@ -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 diff --git a/src/main/kotlin/net/mcbrawls/slate/tile/RedirectType.kt b/src/main/kotlin/net/mcbrawls/slate/tile/RedirectType.kt new file mode 100644 index 0000000..f7d65ea --- /dev/null +++ b/src/main/kotlin/net/mcbrawls/slate/tile/RedirectType.kt @@ -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 +} diff --git a/src/main/kotlin/net/mcbrawls/slate/tile/RedirectedTile.kt b/src/main/kotlin/net/mcbrawls/slate/tile/RedirectedTile.kt new file mode 100644 index 0000000..5021a4e --- /dev/null +++ b/src/main/kotlin/net/mcbrawls/slate/tile/RedirectedTile.kt @@ -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 + } + } +} diff --git a/src/main/kotlin/net/mcbrawls/slate/tile/Tile.kt b/src/main/kotlin/net/mcbrawls/slate/tile/Tile.kt index df2c562..4b1e9ef 100644 --- a/src/main/kotlin/net/mcbrawls/slate/tile/Tile.kt +++ b/src/main/kotlin/net/mcbrawls/slate/tile/Tile.kt @@ -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. @@ -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. diff --git a/src/main/kotlin/net/mcbrawls/slate/tile/TileGrid.kt b/src/main/kotlin/net/mcbrawls/slate/tile/TileGrid.kt index 9ad45f5..ffa9e69 100644 --- a/src/main/kotlin/net/mcbrawls/slate/tile/TileGrid.kt +++ b/src/main/kotlin/net/mcbrawls/slate/tile/TileGrid.kt @@ -16,7 +16,7 @@ open class TileGrid(val width: Int, val height: Int) { /** * Redirects a tile index to another tile index. */ - val redirects: MutableMap = mutableMapOf() + val redirects: MutableMap> = mutableMapOf() /** * The total size of all tiles. @@ -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) } /** @@ -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) {