From fdae27844afdb68b56056a04f4434c50407a3efa Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sat, 18 Mar 2023 08:32:28 +0700 Subject: [PATCH] Reimagine IConditionalTickable --- .../mc/otm/core/util/IConditionalTickable.kt | 37 +++++++++---------- .../dbotthepony/mc/otm/core/util/ITickable.kt | 5 ++- .../dbotthepony/mc/otm/core/util/TickList.kt | 3 +- .../mc/otm/graph/Abstract6Graph.kt | 34 +++++------------ .../ru/dbotthepony/mc/otm/graph/Graph6Node.kt | 18 +++------ 5 files changed, 38 insertions(+), 59 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/IConditionalTickable.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/IConditionalTickable.kt index 7a9491798..f100b383f 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/IConditionalTickable.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/IConditionalTickable.kt @@ -1,35 +1,34 @@ package ru.dbotthepony.mc.otm.core.util +import java.util.function.BooleanSupplier + /** - * Represents tick callback with condition, once + * Represents something that can tick without any additional callback, + * with sign whenever can it tick more */ -interface IConditionalTickable : ITickable { +fun interface IConditionalTickable { /** - * Once this returns false, it should stay false. + * Function to be called by tick executors * - * If it suddenly turns true after being false, result is undefined. + * Once this returns false, ticker is considered finished, and should be removed from ticking list */ - val canTick: Boolean + fun tick(): Boolean companion object { - fun wrap(ticker: () -> Boolean): IConditionalTickable { - return object : IConditionalTickable { - override var canTick: Boolean = true - private set + fun wrap(ticker: () -> Boolean): IConditionalTickable = IConditionalTickable { ticker.invoke() } + fun wrap(ticker: BooleanSupplier): IConditionalTickable = IConditionalTickable { ticker.asBoolean } - override fun tick() { - canTick = !ticker.invoke() - } + fun wrap(condition: () -> Boolean, ticker: () -> Unit): IConditionalTickable { + return IConditionalTickable { + ticker.invoke() + condition.invoke() } } - fun wrap(condition: () -> Boolean, ticker: () -> Unit): IConditionalTickable { - return object : IConditionalTickable { - override val canTick: Boolean get() = condition.invoke() - - override fun tick() { - ticker.invoke() - } + fun wrap(condition: BooleanSupplier, ticker: () -> Unit): IConditionalTickable { + return IConditionalTickable { + ticker.invoke() + condition.asBoolean } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/ITickable.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/ITickable.kt index 80c55d765..97e828be0 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/ITickable.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/ITickable.kt @@ -1,8 +1,11 @@ package ru.dbotthepony.mc.otm.core.util /** - * Represents tick callback + * Represents something that can tick without any additional context */ fun interface ITickable { + /** + * Function to be called by tick executors + */ fun tick() } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/TickList.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/TickList.kt index 492a1d766..e7a95b0da 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/TickList.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/TickList.kt @@ -163,10 +163,9 @@ class TickList : ITickable { val iterator = conditional.iterator() for (ticker in iterator) { - if (!ticker.canTick) { + if (!ticker.tick()) { iterator.remove() } else { - ticker.tick() nothingToDo = false } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/graph/Abstract6Graph.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/graph/Abstract6Graph.kt index 794e61d1c..4a7b15557 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/graph/Abstract6Graph.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/graph/Abstract6Graph.kt @@ -27,19 +27,16 @@ abstract class Abstract6Graph : IConditionalTickable { abstract fun onNodeRemoved(node: Graph6Node) abstract fun onNodeAdded(node: Graph6Node) - override val canTick: Boolean - get() = nodes.size > 0 - - override fun tick() { + override fun tick(): Boolean { for (i in tickable.size - 1 downTo 0) { val node = tickable[i] - if (node.canTick) { - node.tick() - } else { + if (!node.tick()) { tickable.removeAt(i) } } + + return nodes.size > 0 } fun removeNode(node: Graph6Node) { @@ -48,10 +45,7 @@ abstract class Abstract6Graph : IConditionalTickable { node.graph = null onNodeRemoved(node) - - if (node.canTick) { - tickable.remove(node) - } + tickable.remove(node) } fun addNode(node: Graph6Node) { @@ -61,10 +55,7 @@ abstract class Abstract6Graph : IConditionalTickable { nodes.add(node) node.graph = this onNodeAdded(node) - - if (node.canTick) { - tickable.add(node) - } + tickable.add(node) } fun merge(other: Abstract6Graph): Abstract6Graph { @@ -92,16 +83,9 @@ abstract class Abstract6Graph : IConditionalTickable { nodeGetter: (BlockEntity) -> Graph6Node?, factory: () -> Abstract6Graph ) { - level.addTicker(object : IConditionalTickable { - override fun tick() { - discovered = discover(level, blockPos, node, nodeGetter, factory) - } - - private var discovered = false - - override val canTick: Boolean - get() = !discovered && node.valid - }) + level.addTicker { + !discover(level, blockPos, node, nodeGetter, factory) && node.valid + } } @JvmStatic diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/graph/Graph6Node.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/graph/Graph6Node.kt index c8b12be6b..2ca4c3e02 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/graph/Graph6Node.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/graph/Graph6Node.kt @@ -185,20 +185,14 @@ class Graph6Node(@JvmField val value: T, graph: Abstract6Graph? = null) : } } - override val canTick: Boolean - get() { - return if (value is IConditionalTickable) { - value.canTick - } else { - value is ITickable - } - } - - override fun tick() { - if (value is ITickable) { + override fun tick(): Boolean { + if (value is IConditionalTickable) { + return value.tick() + } else if (value is ITickable) { value.tick() + return true } else { - throw ClassCastException("$value is not tickable") + return false } }