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 fd2b92b48..1e8dd0ef1 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 @@ -7,7 +7,6 @@ import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap import it.unimi.dsi.fastutil.objects.ObjectArraySet import it.unimi.dsi.fastutil.objects.Reference2ObjectArrayMap import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap -import net.minecraft.client.multiplayer.ClientLevel import net.minecraft.core.BlockPos import net.minecraft.core.Direction import net.minecraft.core.SectionPos @@ -57,7 +56,6 @@ import ru.dbotthepony.mc.otm.network.BlockEntitySyncPacket import ru.dbotthepony.mc.otm.network.FieldSynchronizer import ru.dbotthepony.mc.otm.network.WorldNetworkChannel import ru.dbotthepony.mc.otm.once -import ru.dbotthepony.mc.otm.oncePre import ru.dbotthepony.mc.otm.onceServer import java.lang.ref.WeakReference import java.util.* @@ -435,31 +433,11 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc _sides[blockRotation.dir2Side(dir)]!!.updateTracked() } - protected fun tickOnce(func: Runnable) { - level?.oncePre { if (!isRemoved) func.run() } - } - - protected fun tickOnceServer(func: Runnable) { - (level as? ServerLevel)?.oncePre { if (!isRemoved) func.run() } - } - - protected fun tickOnceClient(func: Runnable) { - (level as? ClientLevel)?.oncePre { if (!isRemoved) func.run() } - } - - protected fun tickOnce(func: (Level) -> Unit) { + protected fun onceServer(action: (ServerLevel) -> Unit) { val level = level - level?.oncePre { if (!isRemoved) func.invoke(level) } - } - - protected fun tickOnceServer(func: (ServerLevel) -> Unit) { - val level = level as? ServerLevel ?: return - level.oncePre { if (!isRemoved) func.invoke(level) } - } - - protected fun tickOnceClient(func: (ClientLevel) -> Unit) { - val level = level as? ClientLevel ?: return - level.oncePre { if (!isRemoved) func.invoke(level) } + if (level is ServerLevel) { + tickList.once { action.invoke(level) } + } } // Just to mark chunk unsaved @@ -477,7 +455,7 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc return@FieldSynchronizer if (!isRemoved && level?.isClientSide == false && (_subCache == null || (_subCache ?: throw ConcurrentModificationException()).players.isNotEmpty())) { - onceServer { + ru.dbotthepony.mc.otm.onceServer { synchronizeToPlayers(true) } } else { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/DriveViewerBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/DriveViewerBlockEntity.kt index 9e8f3b45c..d442b950c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/DriveViewerBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/DriveViewerBlockEntity.kt @@ -1,6 +1,7 @@ package ru.dbotthepony.mc.otm.block.entity.storage import net.minecraft.core.BlockPos +import net.minecraft.server.level.ServerLevel import net.minecraft.world.entity.player.Inventory import net.minecraft.world.entity.player.Player import net.minecraft.world.inventory.AbstractContainerMenu @@ -22,19 +23,21 @@ class DriveViewerBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte override fun setChanged() { super.setChanged() - tickOnceServer { - var state = blockState + val level = level + if (level is ServerLevel) + tickList.once { + var state = blockState - if (container.getItem(0).getCapability(MatteryCapability.DRIVE).isPresent && energy.batteryLevel >= OverdriveThatMatters.INSTANCE.ITEM_STORAGE().energyPerOperation) { - state = state.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.WORKING) - } else { - state = state.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.IDLE) - } + if (container.getItem(0).getCapability(MatteryCapability.DRIVE).isPresent && energy.batteryLevel >= OverdriveThatMatters.INSTANCE.ITEM_STORAGE().energyPerOperation) { + state = state.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.WORKING) + } else { + state = state.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.IDLE) + } - if (state !== blockState) { - it.setBlock(blockPos, state, Block.UPDATE_CLIENTS) + if (state !== blockState) { + level.setBlock(blockPos, state, Block.UPDATE_CLIENTS) + } } - } } val energy = WorkerEnergyStorage(this, MachinesConfig.DRIVE_VIEWER) @@ -43,21 +46,24 @@ class DriveViewerBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte override fun setChanged(slot: Int, new: ItemStack, old: ItemStack) { super.setChanged(slot, new, old) - tickOnceServer { - if (!isRemoved) { - var state = blockState + val level = level - if (new.getCapability(MatteryCapability.DRIVE).isPresent) { - state = state.setValue(DriveViewerBlock.DRIVE_PRESENT, true) - } else { - state = state.setValue(DriveViewerBlock.DRIVE_PRESENT, false) - } + if (level is ServerLevel) + tickList.once { + if (!isRemoved) { + var state = blockState - if (state !== blockState) { - it.setBlock(blockPos, state, Block.UPDATE_CLIENTS) + if (new.getCapability(MatteryCapability.DRIVE).isPresent) { + state = state.setValue(DriveViewerBlock.DRIVE_PRESENT, true) + } else { + state = state.setValue(DriveViewerBlock.DRIVE_PRESENT, false) + } + + if (state !== blockState) { + level.setBlock(blockPos, state, Block.UPDATE_CLIENTS) + } } } - } } }.also(::addDroppableContainer) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/StorageBusBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/StorageBusBlockEntity.kt index af2a6edbe..c11ce88fa 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/StorageBusBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/StorageBusBlockEntity.kt @@ -114,7 +114,7 @@ class StorageBusBlockEntity(blockPos: BlockPos, blockState: BlockState) : Matter StorageNetworkGraph.discoverFull(this, cell.storageNode) } - tickOnceServer(this::checkSurroundings) + tickList.once(this::checkSurroundings) } private var neighbour: LazyOptional? = null