-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: SlateLayer extendability, callbacks, and dsl
- Loading branch information
Showing
8 changed files
with
137 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
7 changes: 6 additions & 1 deletion
7
...ls/slate/callback/SlateCallbackHandler.kt → .../callback/handler/SlateCallbackHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package net.mcbrawls.slate.layer | ||
|
||
import net.mcbrawls.slate.Slate | ||
import net.mcbrawls.slate.layer.callback.SlateLayerCallbackHandler | ||
import net.mcbrawls.slate.layer.callback.SlateLayerTickCallback | ||
import net.mcbrawls.slate.tile.TileGrid | ||
import net.minecraft.server.network.ServerPlayerEntity | ||
|
||
/** | ||
* An instance of a layer on a slate. | ||
*/ | ||
open class SlateLayer( | ||
val width: Int, | ||
val height: Int, | ||
) { | ||
val tiles: TileGrid = TileGrid(width, height) | ||
|
||
/** | ||
* Handles all callbacks for this layer. | ||
*/ | ||
open var callbackHandler: SlateLayerCallbackHandler = SlateLayerCallbackHandler() | ||
|
||
/** | ||
* Provides a factory for setting up callbacks of this layer. | ||
*/ | ||
inline fun callbacks(factory: SlateLayerCallbackHandler.() -> Unit) { | ||
callbackHandler = callbackHandler.apply(factory) | ||
} | ||
|
||
internal fun onTick(slate: Slate, player: ServerPlayerEntity) { | ||
// invoke callbacks | ||
callbackHandler.collectCallbacks<SlateLayerTickCallback>().invoke(slate, this, player) | ||
} | ||
|
||
fun interface Factory { | ||
fun create(width: Int, height: Int): SlateLayer | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/net/mcbrawls/slate/layer/callback/SlateLayerCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package net.mcbrawls.slate.layer.callback | ||
|
||
import net.mcbrawls.slate.Slate | ||
import net.mcbrawls.slate.layer.SlateLayer | ||
import net.minecraft.server.network.ServerPlayerEntity | ||
|
||
fun interface SlateLayerCallback { | ||
operator fun invoke(slate: Slate, layer: SlateLayer, player: ServerPlayerEntity) | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/net/mcbrawls/slate/layer/callback/SlateLayerCallbackHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package net.mcbrawls.slate.layer.callback | ||
|
||
import net.mcbrawls.slate.layer.SlateLayer | ||
|
||
/** | ||
* Handles callbacks for instances of [SlateLayer]. | ||
*/ | ||
open class SlateLayerCallbackHandler { | ||
val callbacks: MutableList<SlateLayerCallback> = mutableListOf() | ||
|
||
/** | ||
* Adds a callback invoked on the layer every game tick. | ||
*/ | ||
fun onTick(callback: SlateLayerTickCallback) { | ||
callbacks.add(callback) | ||
} | ||
|
||
/** | ||
* Combines all callbacks for the given type into one callable object. | ||
*/ | ||
inline fun <reified T : SlateLayerCallback> collectCallbacks(): SlateLayerCallback { | ||
return SlateLayerCallback { slate, layer, player -> | ||
callbacks | ||
.filterIsInstance<T>() | ||
.forEach { callback -> callback.invoke(slate, layer, player) } | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/main/kotlin/net/mcbrawls/slate/layer/callback/SlateLayerTickCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package net.mcbrawls.slate.layer.callback | ||
|
||
fun interface SlateLayerTickCallback : SlateLayerCallback |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters