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 2febc6b60..fb7176745 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 @@ -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, SidelessCap<*>>() + protected val tickList = TickList() + protected val blockStateChangesCounter = IntCounter() + protected val dirtyListeners = ISubscriptable.Impl() + private val waitForServerLevel = ArrayList<() -> Unit>() private val _droppableContainers = ObjectArraySet() private val _neighbourChangeListeners = ObjectArraySet() val droppableContainers: Set = Collections.unmodifiableSet(_droppableContainers) val neighbourChangeListeners: Set = Collections.unmodifiableSet(_neighbourChangeListeners) + private val _sides = EnumMap(RelativeSide::class.java) + val sides: Map = 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::class.java) - val sides: Map = Collections.unmodifiableMap(_sides) - fun side(side: RelativeSide) = sides[side]!! private data class SidelessCap(val cap: T, var optional: LazyOptional) - private val sidelessCaps = Reference2ObjectOpenHashMap, SidelessCap<*>>() - protected val tickList = TickList() - protected val blockStateChangesCounter = IntCounter() - protected val dirtyListeners = ISubscriptable.Impl() - 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 diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/EnergyCounterBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/EnergyCounterBlockEntity.kt index 445f2ae6d..3fec7ce7e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/EnergyCounterBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/EnergyCounterBlockEntity.kt @@ -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 }