From f80b913d611ad34fb3d843427e83ef1c7e6785fd Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 9 Sep 2022 13:23:47 +0700 Subject: [PATCH] streamline tickers --- .../dbotthepony/mc/otm/GlobalEventHandler.kt | 148 ++++++++---------- .../mc/otm/block/BatteryBankBlock.kt | 6 +- .../mc/otm/block/ChemicalGeneratorBlock.kt | 4 +- .../otm/block/GravitationStabilizerBlock.kt | 10 +- .../mc/otm/block/entity/MatteryBlockEntity.kt | 23 ++- .../mc/otm/block/storage/StorageBusBlock.kt | 4 +- .../mc/otm/block/storage/StorageInterfaces.kt | 6 +- .../ru/dbotthepony/mc/otm/core/TickList.kt | 18 +-- .../mc/otm/graph/Abstract6Graph.kt | 4 +- .../otm/graph/storage/StorageNetworkGraph.kt | 4 +- 10 files changed, 101 insertions(+), 126 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/GlobalEventHandler.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/GlobalEventHandler.kt index 080ccab71..845bda2e8 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/GlobalEventHandler.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/GlobalEventHandler.kt @@ -115,42 +115,6 @@ fun onServerTick(event: ServerTickEvent) { } } -fun tickServerPre(ticker: IConditionalTickable) { - if (SERVER_IS_DYING) { - LOGGER.error("Refusing to add ticker $ticker while server is dying", IllegalStateException("Server is stopping")) - return - } - - preServerTick.add(ticker) -} - -fun tickServer(ticker: IConditionalTickable) { - if (SERVER_IS_DYING) { - LOGGER.error("Refusing to add ticker $ticker while server is dying", IllegalStateException("Server is stopping")) - return - } - - postServerTick.add(ticker) -} - -fun onceServerPre(ticker: ITickable) { - if (SERVER_IS_DYING) { - LOGGER.error("Refusing to add ticker $ticker while server is dying", IllegalStateException("Server is stopping")) - return - } - - preServerTick.add(ticker) -} - -fun onceServer(ticker: ITickable) { - if (SERVER_IS_DYING) { - LOGGER.error("Refusing to add ticker $ticker while server is dying", IllegalStateException("Server is stopping")) - return - } - - postServerTick.add(ticker) -} - fun onWorldTick(event: LevelTickEvent) { if (event.phase === TickEvent.Phase.START) { preWorldTick[event.level]?.tick() @@ -159,70 +123,88 @@ fun onWorldTick(event: LevelTickEvent) { } } -fun addPreWorldTicker(level: Level, ticker: IConditionalTickable) { - if (SERVER_IS_DYING) { - LOGGER.error("Refusing to add ticker $ticker while server is dying", IllegalStateException("Server is stopping")) - return - } - - preWorldTick.computeIfAbsent(level) { TickList() }.add(ticker) +fun onceServerPre(ticker: ITickable) { + preServerTick.add(ticker, SERVER_IS_DYING, "Server is stopping") } -fun addPostWorldTicker(level: Level, ticker: IConditionalTickable) { - if (SERVER_IS_DYING) { - LOGGER.error("Refusing to add ticker $ticker while server is dying", IllegalStateException("Server is stopping")) - return - } - - postWorldTick.computeIfAbsent(level) { TickList() }.add(ticker) +fun onceServer(ticker: ITickable) { + postServerTick.add(ticker, SERVER_IS_DYING, "Server is stopping") } -fun addPreWorldTickerOnce(level: Level, ticker: ITickable) { - if (SERVER_IS_DYING) { - LOGGER.error("Refusing to add ticker $ticker while server is dying", IllegalStateException("Server is stopping")) - return - } - - preWorldTick.computeIfAbsent(level) { TickList() }.add(ticker) +fun tickServerPre(ticker: IConditionalTickable) { + preServerTick.add(ticker, SERVER_IS_DYING, "Server is stopping") } -fun addPostWorldTickerOnce(level: Level, ticker: ITickable) { - if (SERVER_IS_DYING) { - LOGGER.error("Refusing to add ticker $ticker while server is dying", IllegalStateException("Server is stopping")) - return - } - - postWorldTick.computeIfAbsent(level) { TickList() }.add(ticker) +fun tickServer(ticker: IConditionalTickable) { + postServerTick.add(ticker, SERVER_IS_DYING, "Server is stopping") } -fun Level.until(ticker: () -> Boolean) { - addPostWorldTicker(this, object : IConditionalTickable { - override var canTick: Boolean = true - private set - - override fun tick() { - canTick = !ticker.invoke() - } - }) +fun tickUntilServerPre(ticker: () -> Boolean) { + preServerTick.until(ticker, SERVER_IS_DYING, "Server is stopping") } -fun Level.untilPre(ticker: () -> Boolean) { - addPreWorldTicker(this, object : IConditionalTickable { - override var canTick: Boolean = true - private set +fun tickUntilServer(ticker: () -> Boolean) { + postServerTick.until(ticker, SERVER_IS_DYING, "Server is stopping") +} - override fun tick() { - canTick = !ticker.invoke() - } - }) +fun tickWhileServerPre(condition: () -> Boolean, ticker: () -> Unit) { + preServerTick.`while`(condition, ticker, SERVER_IS_DYING, "Server is stopping") +} + +fun tickWhileServer(condition: () -> Boolean, ticker: () -> Unit) { + postServerTick.`while`(condition, ticker, SERVER_IS_DYING, "Server is stopping") } fun Level.once(ticker: ITickable) { - addPostWorldTickerOnce(this, ticker) + if (SERVER_IS_DYING) { + LOGGER.error("Refusing to add ticker $ticker while server is dying", IllegalStateException("Server is stopping")) + return + } + + postWorldTick.computeIfAbsent(this) { TickList() }.add(ticker) } fun Level.oncePre(ticker: ITickable) { - addPreWorldTickerOnce(this, ticker) + if (SERVER_IS_DYING) { + LOGGER.error("Refusing to add ticker $ticker while server is dying", IllegalStateException("Server is stopping")) + return + } + + preWorldTick.computeIfAbsent(this) { TickList() }.add(ticker) +} + +fun Level.addTicker(ticker: IConditionalTickable) { + if (SERVER_IS_DYING) { + LOGGER.error("Refusing to add ticker $ticker while server is dying", IllegalStateException("Server is stopping")) + return + } + + postWorldTick.computeIfAbsent(this) { TickList() }.add(ticker) +} + +fun Level.addTickerPre(ticker: IConditionalTickable) { + if (SERVER_IS_DYING) { + LOGGER.error("Refusing to add ticker $ticker while server is dying", IllegalStateException("Server is stopping")) + return + } + + preWorldTick.computeIfAbsent(this) { TickList() }.add(ticker) +} + +fun Level.until(ticker: () -> Boolean) { + addTicker(IConditionalTickable.wrap(ticker)) +} + +fun Level.untilPre(ticker: () -> Boolean) { + addTickerPre(IConditionalTickable.wrap(ticker)) +} + +fun Level.`while`(condition: () -> Boolean, ticker: () -> Unit) { + addTicker(IConditionalTickable.wrap(condition, ticker)) +} + +fun Level.whilePre(condition: () -> Boolean, ticker: () -> Unit) { + addTickerPre(IConditionalTickable.wrap(condition, ticker)) } private fun clear() { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/BatteryBankBlock.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/BatteryBankBlock.kt index 4922db754..bb2a49852 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/BatteryBankBlock.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/BatteryBankBlock.kt @@ -18,7 +18,7 @@ import net.minecraft.world.level.Level import net.minecraft.world.level.block.Block import net.minecraft.world.phys.shapes.CollisionContext import net.minecraft.world.phys.shapes.VoxelShape -import ru.dbotthepony.mc.otm.addPostWorldTickerOnce +import ru.dbotthepony.mc.otm.once import ru.dbotthepony.mc.otm.registry.MBlockEntities import ru.dbotthepony.mc.otm.shapes.BlockShapes @@ -76,10 +76,8 @@ class BatteryBankBlock : RotatableMatteryBlock(), EntityBlock { movedByPiston: Boolean ) { super.neighborChanged(state, level, pos, neighbour, neighbourPos, movedByPiston) - val blockEntity = level.getBlockEntity(pos) as? BatteryBankBlockEntity ?: return - - addPostWorldTickerOnce(level) { blockEntity.checkSurroundings(level) } + level.once { blockEntity.checkSurroundings(level) } } companion object { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/ChemicalGeneratorBlock.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/ChemicalGeneratorBlock.kt index 308fd2918..bd8ab52a1 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/ChemicalGeneratorBlock.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/ChemicalGeneratorBlock.kt @@ -13,9 +13,9 @@ import net.minecraft.world.level.block.state.BlockState import net.minecraft.world.level.block.state.StateDefinition import net.minecraft.world.phys.shapes.CollisionContext import net.minecraft.world.phys.shapes.VoxelShape -import ru.dbotthepony.mc.otm.addPreWorldTickerOnce import ru.dbotthepony.mc.otm.block.entity.ChemicalGeneratorBlockEntity import ru.dbotthepony.mc.otm.block.entity.WorkerState +import ru.dbotthepony.mc.otm.oncePre import ru.dbotthepony.mc.otm.registry.MBlockEntities import ru.dbotthepony.mc.otm.shapes.BlockShapes @@ -54,7 +54,7 @@ class ChemicalGeneratorBlock : RotatableMatteryBlock(), EntityBlock { val tile = level.getBlockEntity(pos) if (tile is ChemicalGeneratorBlockEntity) { - addPreWorldTickerOnce(level) { + level.oncePre { tile.checkSurroundings() } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/GravitationStabilizerBlock.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/GravitationStabilizerBlock.kt index f36191971..09fa3929d 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/GravitationStabilizerBlock.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/GravitationStabilizerBlock.kt @@ -22,12 +22,12 @@ import net.minecraft.world.level.material.MaterialColor import net.minecraft.world.level.material.PushReaction import net.minecraft.world.phys.shapes.CollisionContext import net.minecraft.world.phys.shapes.VoxelShape -import ru.dbotthepony.mc.otm.addPreWorldTickerOnce import ru.dbotthepony.mc.otm.block.entity.GravitationStabilizerBlockEntity import ru.dbotthepony.mc.otm.block.entity.blackhole.BlackHoleBlockEntity import ru.dbotthepony.mc.otm.block.entity.WorkerState import ru.dbotthepony.mc.otm.core.plus import ru.dbotthepony.mc.otm.core.times +import ru.dbotthepony.mc.otm.oncePre import ru.dbotthepony.mc.otm.registry.MBlockEntities import ru.dbotthepony.mc.otm.registry.MBlocks import ru.dbotthepony.mc.otm.shapes.BlockShapes @@ -120,8 +120,8 @@ class BlockGravitationStabilizer : RotatableMatteryBlock(props), EntityBlock { ) { super.neighborChanged(state, level, pos, neighbour, neighbourPos, movedByPiston) - addPreWorldTickerOnce(level) { - if (level.getBlockState(pos).block !is BlockGravitationStabilizer) return@addPreWorldTickerOnce + level.oncePre { + if (level.getBlockState(pos).block !is BlockGravitationStabilizer) return@oncePre val bb = getBoundingBlock(level, state, pos) if (bb.block !is BlockGravitationStabilizerLens) { @@ -181,8 +181,8 @@ class BlockGravitationStabilizerLens : RotatableMatteryBlock(props) { ) { super.neighborChanged(state, level, pos, neighbour, neighbourPos, movedByPiston) - addPreWorldTickerOnce(level) { - if (level.getBlockState(pos).block !is BlockGravitationStabilizerLens) return@addPreWorldTickerOnce + level.oncePre { + if (level.getBlockState(pos).block !is BlockGravitationStabilizerLens) return@oncePre val bb = getBoundingBlock(level, state, pos) if (bb.block !is BlockGravitationStabilizer) { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryBlockEntity.kt index c3c933dfc..d253b9aae 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryBlockEntity.kt @@ -21,9 +21,9 @@ import net.minecraft.nbt.StringTag import net.minecraft.network.chat.Component import net.minecraft.world.level.Level import net.minecraftforge.common.capabilities.Capability -import ru.dbotthepony.mc.otm.addPreWorldTickerOnce import ru.dbotthepony.mc.otm.core.ifHas import ru.dbotthepony.mc.otm.core.set +import ru.dbotthepony.mc.otm.oncePre abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: BlockPos, p_155230_: BlockState) : BlockEntity(p_155228_, p_155229_, p_155230_), MenuProvider { var customDisplayName: Component? = null @@ -69,33 +69,30 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc abstract override fun createMenu(containerID: Int, inventory: Inventory, ply: Player): AbstractContainerMenu? protected fun tickOnce(func: Runnable) { - val level = level - if (level != null) addPreWorldTickerOnce(level) { if (!isRemoved) func.run() } - } + level?.oncePre { if (!isRemoved) func.run() } + } protected fun tickOnceServer(func: Runnable) { - val level = level - if (level is ServerLevel) addPreWorldTickerOnce(level) { if (!isRemoved) func.run() } + (level as? ServerLevel)?.oncePre { if (!isRemoved) func.run() } } protected fun tickOnceClient(func: Runnable) { - val level = level - if (level is ClientLevel) addPreWorldTickerOnce(level) { if (!isRemoved) func.run() } + (level as? ClientLevel)?.oncePre { if (!isRemoved) func.run() } } protected fun tickOnce(func: (Level) -> Unit) { val level = level - if (level != null) addPreWorldTickerOnce(level) { if (!isRemoved) func(level) } + level?.oncePre { if (!isRemoved) func.invoke(level) } } protected fun tickOnceServer(func: (ServerLevel) -> Unit) { - val level = level - if (level is ServerLevel) addPreWorldTickerOnce(level) { if (!isRemoved) func(level) } + val level = level as? ServerLevel ?: return + level.oncePre { if (!isRemoved) func.invoke(level) } } protected fun tickOnceClient(func: (ClientLevel) -> Unit) { - val level = level - if (level is ClientLevel) addPreWorldTickerOnce(level) { if (!isRemoved) func(level) } + val level = level as? ClientLevel ?: return + level.oncePre { if (!isRemoved) func.invoke(level) } } protected fun getAndBind( diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/storage/StorageBusBlock.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/storage/StorageBusBlock.kt index d18042d68..2eee2bd7d 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/storage/StorageBusBlock.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/storage/StorageBusBlock.kt @@ -15,7 +15,6 @@ import net.minecraft.world.phys.shapes.BooleanOp import net.minecraft.world.phys.shapes.CollisionContext import net.minecraft.world.phys.shapes.Shapes import net.minecraft.world.phys.shapes.VoxelShape -import ru.dbotthepony.mc.otm.addPreWorldTickerOnce import ru.dbotthepony.mc.otm.block.CableBlock import ru.dbotthepony.mc.otm.block.RotatableMatteryBlock import ru.dbotthepony.mc.otm.block.StorageCableBlock @@ -23,6 +22,7 @@ import ru.dbotthepony.mc.otm.block.entity.storage.StorageBusBlockEntity import ru.dbotthepony.mc.otm.registry.MBlockEntities import ru.dbotthepony.mc.otm.shapes.BlockShapes import ru.dbotthepony.mc.otm.core.unaryMinus +import ru.dbotthepony.mc.otm.oncePre class StorageBusBlock : RotatableMatteryBlock(), EntityBlock { override val hasFreeRotation: Boolean get() = true @@ -107,7 +107,7 @@ class StorageBusBlock : RotatableMatteryBlock(), EntityBlock { val tile = level.getBlockEntity(pos) if (tile is StorageBusBlockEntity) { - addPreWorldTickerOnce(level) { + level.oncePre { tile.checkSurroundings() } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/storage/StorageInterfaces.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/storage/StorageInterfaces.kt index 23604cfd1..d4cf60322 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/storage/StorageInterfaces.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/storage/StorageInterfaces.kt @@ -15,7 +15,6 @@ import net.minecraft.world.phys.shapes.BooleanOp import net.minecraft.world.phys.shapes.CollisionContext import net.minecraft.world.phys.shapes.Shapes import net.minecraft.world.phys.shapes.VoxelShape -import ru.dbotthepony.mc.otm.addPreWorldTickerOnce import ru.dbotthepony.mc.otm.block.CableBlock import ru.dbotthepony.mc.otm.block.RotatableMatteryBlock import ru.dbotthepony.mc.otm.block.StorageCableBlock @@ -24,6 +23,7 @@ import ru.dbotthepony.mc.otm.block.entity.storage.StorageImporterBlockEntity import ru.dbotthepony.mc.otm.registry.MBlockEntities import ru.dbotthepony.mc.otm.shapes.BlockShapes import ru.dbotthepony.mc.otm.core.unaryMinus +import ru.dbotthepony.mc.otm.oncePre class StorageImporterBlock : RotatableMatteryBlock(), EntityBlock { override val hasFreeRotation: Boolean get() = true @@ -108,7 +108,7 @@ class StorageImporterBlock : RotatableMatteryBlock(), EntityBlock { val tile = level.getBlockEntity(pos) if (tile is StorageImporterBlockEntity) { - addPreWorldTickerOnce(level) { + level.oncePre { tile.checkSurroundings() } } @@ -199,7 +199,7 @@ class StorageExporterBlock : RotatableMatteryBlock(), EntityBlock { val tile = level.getBlockEntity(pos) if (tile is StorageExporterBlockEntity) { - addPreWorldTickerOnce(level) { + level.oncePre { tile.checkSurroundings() } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/TickList.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/TickList.kt index 076b0b138..d50029f83 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/TickList.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/TickList.kt @@ -35,11 +35,7 @@ class TickList { return } - if (inTicker) { - conditionalValveTime.add(ticker) - } else { - conditional.addFirst(ticker) - } + return add(ticker) } fun add(ticker: ITickable, condition: Boolean, reason: String) { @@ -48,13 +44,15 @@ class TickList { return } - if (inTicker) { - onceValveTime.add(ticker) - } else { - once.addFirst(ticker) - } + return add(ticker) } + fun until(ticker: () -> Boolean) = add(IConditionalTickable.wrap(ticker)) + fun `while`(tickerCondition: () -> Boolean, ticker: () -> Unit) = add(IConditionalTickable.wrap(tickerCondition, ticker)) + + fun until(ticker: () -> Boolean, condition: Boolean, reason: String) = add(IConditionalTickable.wrap(ticker), condition, reason) + fun `while`(tickerCondition: () -> Boolean, ticker: () -> Unit, condition: Boolean, reason: String) = add(IConditionalTickable.wrap(tickerCondition, ticker), condition, reason) + fun tick() { if (inTicker) { throw ConcurrentModificationException("Already ticking") 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 795bd9d41..915eb16d9 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/graph/Abstract6Graph.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/graph/Abstract6Graph.kt @@ -6,8 +6,8 @@ import net.minecraft.core.SectionPos import net.minecraft.server.level.ServerLevel import net.minecraft.world.level.block.entity.BlockEntity import ru.dbotthepony.mc.otm.core.IConditionalTickable -import ru.dbotthepony.mc.otm.addPreWorldTicker import ru.dbotthepony.mc.otm.core.plus +import ru.dbotthepony.mc.otm.addTicker import java.util.* import kotlin.collections.ArrayList import kotlin.collections.HashMap @@ -92,7 +92,7 @@ abstract class Abstract6Graph : IConditionalTickable { nodeGetter: (BlockEntity) -> Graph6Node?, factory: () -> Abstract6Graph ) { - addPreWorldTicker(level, object : IConditionalTickable { + level.addTicker(object : IConditionalTickable { override fun tick() { discovered = discover(level, blockPos, node, nodeGetter, factory) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/graph/storage/StorageNetworkGraph.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/graph/storage/StorageNetworkGraph.kt index 87422a09a..5c4f94710 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/graph/storage/StorageNetworkGraph.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/graph/storage/StorageNetworkGraph.kt @@ -5,7 +5,7 @@ import it.unimi.dsi.fastutil.objects.Object2ObjectFunction import net.minecraft.server.level.ServerLevel import net.minecraft.world.level.Level import net.minecraft.world.level.block.entity.BlockEntity -import ru.dbotthepony.mc.otm.addPreWorldTicker +import ru.dbotthepony.mc.otm.addTickerPre import ru.dbotthepony.mc.otm.capability.IMatteryEnergyStorage import ru.dbotthepony.mc.otm.capability.MatteryCapability import ru.dbotthepony.mc.otm.graph.Abstract6Graph @@ -51,7 +51,7 @@ class StorageNetworkGraph(private val level: Level) : Abstract6Graph