Use tick list for adding "tick once" stuff
This commit is contained in:
parent
bc99bc0c41
commit
f3b824b484
@ -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 {
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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<IItemHandler>? = null
|
||||
|
Loading…
Reference in New Issue
Block a user