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 1fac08b2b..460a3b3b0 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 @@ -65,6 +65,7 @@ import ru.dbotthepony.mc.otm.onceServer import java.lang.ref.WeakReference import java.util.* import java.util.function.Consumer +import java.util.function.Predicate import java.util.function.Supplier import java.util.stream.Stream import kotlin.NoSuchElementException @@ -134,43 +135,23 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc setChanged() } - protected fun exposeAllSidesExcept(side: RelativeSide, capability: Capability, value: T) { - for ((k, v) in _sides) { - if (k != side) { - v.Cap(capability, value) - } - } - } - - protected fun isSidelessExposed(capability: Capability<*>): Boolean { - return sidelessCaps.containsKey(capability) - } - - protected fun removeSideless(capability: Capability<*>): Boolean { - if (sidelessCaps.remove(capability) != null) { - setChanged() - return true - } - - return false - } - /** * Exposes capability unconditionally, on all sides and sideless */ - protected fun exposeGlobally(capability: Capability, value: T) { + protected fun exposeGlobally(capability: Capability, value: T, predicate: Predicate = Predicate { true }) { exposeSideless(capability, value) for (side in _sides.values) - side.Cap(capability, value) + if (predicate.test(side.side)) + side.Cap(capability, value) } - protected fun exposeEnergyGlobally(value: IMatteryEnergyStorage) { - exposeGlobally(ForgeCapabilities.ENERGY, value) - exposeGlobally(MatteryCapability.ENERGY, value) + protected fun exposeEnergyGlobally(value: IMatteryEnergyStorage, predicate: Predicate = Predicate { true }) { + exposeGlobally(ForgeCapabilities.ENERGY, value, predicate) + exposeGlobally(MatteryCapability.ENERGY, value, predicate) if (isMekanismLoaded) { - exposeGlobally(MatteryCapability.MEKANISM_ENERGY, Mattery2MekanismEnergyWrapper(value)) + exposeGlobally(MatteryCapability.MEKANISM_ENERGY, Mattery2MekanismEnergyWrapper(value), predicate) } } 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 2e5635e39..a352411dc 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 @@ -62,12 +62,6 @@ class StorageBusBlockEntity(blockPos: BlockPos, blockState: BlockState) : Matter } val energy = WorkerEnergyStorage(this::setChangedLight, MachinesConfig.STORAGE_INTERFACES) - - init { - exposeEnergyGlobally(energy) - savetable(::energy, ENERGY_KEY) - } - val cell: StorageNode = object : StorageNode(energy) { override fun onNeighbour(link: Link) { if (link is DirectionLink) { @@ -88,9 +82,21 @@ class StorageBusBlockEntity(blockPos: BlockPos, blockState: BlockState) : Matter } } + private var component: ItemHandlerComponent? = null + init { - exposeSideless(MatteryCapability.STORAGE_NODE, cell) - exposeAllSidesExcept(RelativeSide.FRONT, MatteryCapability.STORAGE_NODE, cell) + exposeEnergyGlobally(energy) + savetable(::energy, ENERGY_KEY) + exposeGlobally(MatteryCapability.STORAGE_NODE, cell) { it != RelativeSide.FRONT } + + side(RelativeSide.FRONT).track(ForgeCapabilities.ITEM_HANDLER).addListener { + component?.let(cell::removeStorageComponent) + component = if (it.isPresent) { + ItemHandlerComponent(it.orThrow()).also(cell::addStorageComponent) + } else { + null + } + } } val filter = ItemFilter(MAX_FILTERS) { _, _, _ -> @@ -105,12 +111,8 @@ class StorageBusBlockEntity(blockPos: BlockPos, blockState: BlockState) : Matter override fun setLevel(level: Level) { super.setLevel(level) cell.discover(this) - tickList.once(this::checkSurroundings) } - private var neighbour: LazyOptional? = null - private var component: ItemHandlerComponent? = null - override fun tick() { super.tick() component?.scan() @@ -122,37 +124,6 @@ class StorageBusBlockEntity(blockPos: BlockPos, blockState: BlockState) : Matter cell.isValid = false } - fun checkSurroundings() { - if (isRemoved) - return - - val front = blockPos + blockState.getValue(BlockRotationFreedom.XZ_XY.property) - val storage = level?.getBlockEntity(front)?.getCapability(ForgeCapabilities.ITEM_HANDLER, -blockState.getValue(BlockRotationFreedom.XZ_XY.property))?.let { if (it.isPresent) it else null } - - if (neighbour != storage) { - neighbour = storage - - if (storage != null) { - val ref = WeakReference(this) - - storage.addListener { - val self = ref.get() ?: return@addListener - - if (self.neighbour === it && SERVER_IS_LIVE) { - self.checkSurroundings() - } - } - - component?.let(cell::removeStorageComponent) - component = ItemHandlerComponent(storage.orThrow()) - component!!.let(cell::addStorageComponent) - } else { - component?.let(cell::removeStorageComponent) - component = null - } - } - } - companion object { const val MAX_FILTERS = 6 * 3 } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/StorageInterfaces.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/StorageInterfaces.kt index b6b548c32..a2dc766ca 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/StorageInterfaces.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/StorageInterfaces.kt @@ -51,12 +51,6 @@ abstract class AbstractStorageImportExport( energyValues: EnergyBalanceValues = MachinesConfig.STORAGE_INTERFACES ) : MatteryPoweredBlockEntity(blockType, blockPos, blockState) { val energy = WorkerEnergyStorage(::setChangedLight, energyValues) - - init { - exposeEnergyGlobally(energy) - savetable(::energy, ENERGY_KEY) - } - val cell: StorageNode = object : StorageNode(energy) { override fun onNeighbour(link: Link) { if (link is DirectionLink) { @@ -86,8 +80,9 @@ abstract class AbstractStorageImportExport( } init { - exposeSideless(MatteryCapability.STORAGE_NODE, cell) - exposeAllSidesExcept(RelativeSide.FRONT, MatteryCapability.STORAGE_NODE, cell) + exposeEnergyGlobally(energy) + savetable(::energy, ENERGY_KEY) + exposeGlobally(MatteryCapability.STORAGE_NODE, cell) { it != RelativeSide.FRONT } } override fun setRemoved() { 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 ff7937407..1894edeca 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 @@ -112,26 +112,4 @@ class StorageBusBlock : RotatableMatteryBlock(), EntityBlock { ): VoxelShape { return shapes[p_60555_]!! } - - @Suppress("OVERRIDE_DEPRECATION") - override fun neighborChanged( - state: BlockState, - level: Level, - pos: BlockPos, - neighbour: Block, - neighbourPos: BlockPos, - movedByPiston: Boolean - ) { - super.neighborChanged(state, level, pos, neighbour, neighbourPos, movedByPiston) - - if (!level.isClientSide) { - val tile = level.getBlockEntity(pos) - - if (tile is StorageBusBlockEntity) { - level.oncePre { - tile.checkSurroundings() - } - } - } - } }