Caching default blockRotation implementation

This commit is contained in:
DBotThePony 2024-01-09 23:35:22 +07:00
parent 91ccd0af16
commit 8c257db68f
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 32 additions and 29 deletions

View File

@ -51,6 +51,7 @@ import ru.dbotthepony.mc.otm.core.math.plus
import ru.dbotthepony.mc.otm.core.util.IntCounter
import ru.dbotthepony.mc.otm.core.util.Savetables
import ru.dbotthepony.mc.otm.core.util.TickList
import ru.dbotthepony.mc.otm.core.util.countingLazy
import ru.dbotthepony.mc.otm.network.BlockEntitySyncPacket
import ru.dbotthepony.mc.otm.network.GenericNetworkChannel
import ru.dbotthepony.mc.otm.network.synchronizer.FieldSynchronizer
@ -73,20 +74,39 @@ import kotlin.reflect.KProperty
abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: BlockPos, p_155230_: BlockState) : BlockEntity(p_155228_, p_155229_, p_155230_), INeighbourChangeListener {
private var isSynchronizing = false
/**
* "shortcut" for getting [BlockRotation]
*
* if block has no rotation, returns [BlockRotation.NORTH]
*/
open val blockRotation: BlockRotation get() {
return blockState[(blockState.block as? RotatableMatteryBlock ?: return BlockRotation.NORTH).rotationProperty]
}
private val sidelessCaps = Reference2ObjectOpenHashMap<Capability<*>, SidelessCap<*>>()
protected val tickList = TickList()
protected val blockStateChangesCounter = IntCounter()
protected val dirtyListeners = ISubscriptable.Impl<Unit>()
private val waitForServerLevel = ArrayList<() -> Unit>()
private val _droppableContainers = ObjectArraySet<Container>()
private val _neighbourChangeListeners = ObjectArraySet<INeighbourChangeListener>()
val droppableContainers: Set<Container> = Collections.unmodifiableSet(_droppableContainers)
val neighbourChangeListeners: Set<INeighbourChangeListener> = Collections.unmodifiableSet(_neighbourChangeListeners)
private val _sides = EnumMap<RelativeSide, Side>(RelativeSide::class.java)
val sides: Map<RelativeSide, Side> = Collections.unmodifiableMap(_sides)
/**
* Shared savetables, written both to level storage and to item tag
*/
protected val savetables = Savetables()
/**
* Level-only savetables, written only to level storage
*/
protected val savetablesLevel = Savetables()
/**
* "shortcut" for getting [BlockRotation]
*
* if block has no rotation, returns [BlockRotation.NORTH]
*/
open val blockRotation: BlockRotation by countingLazy(blockStateChangesCounter) {
blockState[(blockState.block as? RotatableMatteryBlock ?: return@countingLazy BlockRotation.NORTH).rotationProperty]
}
protected fun addDroppableContainer(container: Container) {
_droppableContainers.add(container)
}
@ -101,27 +121,9 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
open fun beforeDroppingItems(oldBlockState: BlockState, level: Level, blockPos: BlockPos, newBlockState: BlockState, movedByPiston: Boolean) {}
open fun beforeDestroyedByPlayer(level: Level, blockPos: BlockPos, blockState: BlockState, player: Player) {}
private val _sides = EnumMap<RelativeSide, Side>(RelativeSide::class.java)
val sides: Map<RelativeSide, Side> = Collections.unmodifiableMap(_sides)
fun side(side: RelativeSide) = sides[side]!!
private data class SidelessCap<T : Any>(val cap: T, var optional: LazyOptional<T>)
private val sidelessCaps = Reference2ObjectOpenHashMap<Capability<*>, SidelessCap<*>>()
protected val tickList = TickList()
protected val blockStateChangesCounter = IntCounter()
protected val dirtyListeners = ISubscriptable.Impl<Unit>()
private val waitForServerLevel = ArrayList<() -> Unit>()
/**
* Shared savetables, written both to level storage and to item tag
*/
protected val savetables = Savetables()
/**
* Level-only savetables, written only to level storage
*/
protected val savetablesLevel = Savetables()
open fun tick() {
tickList.tick()
@ -441,8 +443,8 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
@Suppress("OVERRIDE_DEPRECATION")
override fun setBlockState(pBlockState: BlockState) {
blockStateChangesCounter.increment()
val old = blockRotation
blockStateChangesCounter.increment()
@Suppress("DEPRECATION")
super.setBlockState(pBlockState)
val new = blockRotation

View File

@ -22,6 +22,7 @@ import ru.dbotthepony.mc.otm.core.nbt.getByteArrayList
import ru.dbotthepony.mc.otm.core.nbt.map
import ru.dbotthepony.mc.otm.core.nbt.mapPresent
import ru.dbotthepony.mc.otm.core.nbt.set
import ru.dbotthepony.mc.otm.core.util.countingLazy
import ru.dbotthepony.mc.otm.menu.tech.EnergyCounterMenu
import ru.dbotthepony.mc.otm.registry.MBlockEntities
import java.util.*
@ -29,8 +30,8 @@ import java.util.*
class EnergyCounterBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : MatteryDeviceBlockEntity(MBlockEntities.ENERGY_COUNTER, p_155229_, p_155230_) {
var passed by synchronizer.decimal()
override val blockRotation: BlockRotation get() {
return BlockRotation.of(blockState[EnergyCounterBlock.INPUT_DIRECTION])
override val blockRotation: BlockRotation by countingLazy(blockStateChangesCounter) {
BlockRotation.of(blockState[EnergyCounterBlock.INPUT_DIRECTION])
}
private val history = Array(10 * 20) { Decimal.ZERO }