Don't wrap blockstate and blockentity in lazy

This commit is contained in:
DBotThePony 2024-10-30 20:59:58 +07:00
parent 35ae7ed88a
commit 604999ebe3
Signed by: DBot
GPG Key ID: DCC23B5715498507
4 changed files with 24 additions and 18 deletions

View File

@ -39,6 +39,7 @@ import net.minecraft.world.level.Level
import net.minecraft.world.level.LevelAccessor import net.minecraft.world.level.LevelAccessor
import net.minecraft.world.level.block.Block import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.Blocks import net.minecraft.world.level.block.Blocks
import net.minecraft.world.level.block.entity.BlockEntity
import net.minecraft.world.level.block.state.BlockState import net.minecraft.world.level.block.state.BlockState
import net.minecraft.world.level.block.state.StateHolder import net.minecraft.world.level.block.state.StateHolder
import net.minecraft.world.level.block.state.properties.Property import net.minecraft.world.level.block.state.properties.Property
@ -116,6 +117,10 @@ fun LevelAccessor.getBlockStateNow(pos: BlockPos): BlockState {
return chunkSource.getChunkNow(SectionPos.blockToSectionCoord(pos.x), SectionPos.blockToSectionCoord(pos.z))?.getBlockState(pos) ?: Blocks.AIR.defaultBlockState() return chunkSource.getChunkNow(SectionPos.blockToSectionCoord(pos.x), SectionPos.blockToSectionCoord(pos.z))?.getBlockState(pos) ?: Blocks.AIR.defaultBlockState()
} }
fun LevelAccessor.getBlockEntityNow(pos: BlockPos): BlockEntity? {
return chunkSource.getChunkNow(SectionPos.blockToSectionCoord(pos.x), SectionPos.blockToSectionCoord(pos.z))?.getBlockEntity(pos)
}
inline val FluidStack.isNotEmpty get() = !isEmpty inline val FluidStack.isNotEmpty get() = !isEmpty
inline val ItemStack.isNotEmpty get() = !isEmpty inline val ItemStack.isNotEmpty get() = !isEmpty

View File

@ -11,7 +11,7 @@ import ru.dbotthepony.mc.otm.core.getBlockStateNow
import ru.dbotthepony.mc.otm.core.math.plus import ru.dbotthepony.mc.otm.core.math.plus
fun interface BlockPredicate { fun interface BlockPredicate {
fun test(pos: BlockPos, access: LevelAccessor, blockState: Lazy<BlockState>, blockEntity: Lazy<BlockEntity?>): Boolean fun test(pos: BlockPos, access: LevelAccessor, blockState: BlockState, blockEntity: BlockEntity?): Boolean
fun rotate(rotation: Rotation): BlockPredicate { fun rotate(rotation: Rotation): BlockPredicate {
return this return this
@ -42,7 +42,7 @@ fun interface BlockPredicate {
} }
data class Positioned(val offset: BlockPos, val parent: BlockPredicate) : BlockPredicate { data class Positioned(val offset: BlockPos, val parent: BlockPredicate) : BlockPredicate {
override fun test(pos: BlockPos, access: LevelAccessor, blockState: Lazy<BlockState>, blockEntity: Lazy<BlockEntity?>): Boolean { override fun test(pos: BlockPos, access: LevelAccessor, blockState: BlockState, blockEntity: BlockEntity?): Boolean {
return parent.test(offset + pos, access, blockState, blockEntity) return parent.test(offset + pos, access, blockState, blockEntity)
} }
@ -55,7 +55,7 @@ fun interface BlockPredicate {
constructor(vararg nodes: BlockPredicate) : this(ImmutableSet.copyOf(nodes)) constructor(vararg nodes: BlockPredicate) : this(ImmutableSet.copyOf(nodes))
constructor(nodes: Set<BlockPredicate>) : this(ImmutableSet.copyOf(nodes)) constructor(nodes: Set<BlockPredicate>) : this(ImmutableSet.copyOf(nodes))
override fun test(pos: BlockPos, access: LevelAccessor, blockState: Lazy<BlockState>, blockEntity: Lazy<BlockEntity?>): Boolean { override fun test(pos: BlockPos, access: LevelAccessor, blockState: BlockState, blockEntity: BlockEntity?): Boolean {
return nodes.all { it.test(pos, access, blockState, blockEntity) } return nodes.all { it.test(pos, access, blockState, blockEntity) }
} }
} }
@ -64,20 +64,20 @@ fun interface BlockPredicate {
constructor(vararg nodes: BlockPredicate) : this(ImmutableSet.copyOf(nodes)) constructor(vararg nodes: BlockPredicate) : this(ImmutableSet.copyOf(nodes))
constructor(nodes: Set<BlockPredicate>) : this(ImmutableSet.copyOf(nodes)) constructor(nodes: Set<BlockPredicate>) : this(ImmutableSet.copyOf(nodes))
override fun test(pos: BlockPos, access: LevelAccessor, blockState: Lazy<BlockState>, blockEntity: Lazy<BlockEntity?>): Boolean { override fun test(pos: BlockPos, access: LevelAccessor, blockState: BlockState, blockEntity: BlockEntity?): Boolean {
return nodes.any { it.test(pos, access, blockState, blockEntity) } return nodes.any { it.test(pos, access, blockState, blockEntity) }
} }
} }
object Air : BlockPredicate { object Air : BlockPredicate {
override fun test(pos: BlockPos, access: LevelAccessor, blockState: Lazy<BlockState>, blockEntity: Lazy<BlockEntity?>): Boolean { override fun test(pos: BlockPos, access: LevelAccessor, blockState: BlockState, blockEntity: BlockEntity?): Boolean {
return blockState.value.isAir return blockState.isAir
} }
} }
object NotAir : BlockPredicate { object NotAir : BlockPredicate {
override fun test(pos: BlockPos, access: LevelAccessor, blockState: Lazy<BlockState>, blockEntity: Lazy<BlockEntity?>): Boolean { override fun test(pos: BlockPos, access: LevelAccessor, blockState: BlockState, blockEntity: BlockEntity?): Boolean {
return !blockState.value.isAir return !blockState.isAir
} }
} }
} }

View File

@ -17,6 +17,7 @@ import net.minecraft.world.level.block.state.BlockState
import ru.dbotthepony.mc.otm.core.addAll import ru.dbotthepony.mc.otm.core.addAll
import ru.dbotthepony.mc.otm.core.collect.collect import ru.dbotthepony.mc.otm.core.collect.collect
import ru.dbotthepony.mc.otm.core.collect.map import ru.dbotthepony.mc.otm.core.collect.map
import ru.dbotthepony.mc.otm.core.getBlockEntityNow
import ru.dbotthepony.mc.otm.core.getBlockStateNow import ru.dbotthepony.mc.otm.core.getBlockStateNow
import ru.dbotthepony.mc.otm.core.math.plus import ru.dbotthepony.mc.otm.core.math.plus
import ru.dbotthepony.mc.otm.core.registryName import ru.dbotthepony.mc.otm.core.registryName
@ -92,7 +93,7 @@ class ShapedMultiblock(pos: BlockPos, factory: ShapedMultiblockFactory) : IMulti
stream.writeByte(status.ordinal) stream.writeByte(status.ordinal)
} }
private fun orPredicates(levelAccessor: LevelAccessor, blockState: Lazy<BlockState>, blockEntity: Lazy<BlockEntity?>): Boolean { private fun orPredicates(levelAccessor: LevelAccessor, blockState: BlockState, blockEntity: BlockEntity?): Boolean {
if (prototype.predicate.isNotEmpty()) { if (prototype.predicate.isNotEmpty()) {
if (lastSuccessfulPathPredicate != -1 && !prototype.predicate[lastSuccessfulPathPredicate].test(pos, levelAccessor, blockState, blockEntity)) { if (lastSuccessfulPathPredicate != -1 && !prototype.predicate[lastSuccessfulPathPredicate].test(pos, levelAccessor, blockState, blockEntity)) {
lastSuccessfulPathPredicate = -1 lastSuccessfulPathPredicate = -1
@ -107,7 +108,7 @@ class ShapedMultiblock(pos: BlockPos, factory: ShapedMultiblockFactory) : IMulti
return true return true
} }
private fun orChildren(levelAccessor: LevelAccessor, blockState: Lazy<BlockState>, blockEntity: Lazy<BlockEntity?>): Boolean { private fun orChildren(levelAccessor: LevelAccessor, blockState: BlockState, blockEntity: BlockEntity?): Boolean {
if (children.isNotEmpty()) { if (children.isNotEmpty()) {
if (lastSuccessfulPathChildren != -1 && !children[lastSuccessfulPathChildren].test0(levelAccessor, blockState, blockEntity)) { if (lastSuccessfulPathChildren != -1 && !children[lastSuccessfulPathChildren].test0(levelAccessor, blockState, blockEntity)) {
lastSuccessfulPathChildren = -1 lastSuccessfulPathChildren = -1
@ -122,7 +123,7 @@ class ShapedMultiblock(pos: BlockPos, factory: ShapedMultiblockFactory) : IMulti
return true return true
} }
private fun test0(levelAccessor: LevelAccessor, blockState: Lazy<BlockState>, blockEntity: Lazy<BlockEntity?>): Boolean { private fun test0(levelAccessor: LevelAccessor, blockState: BlockState, blockEntity: BlockEntity?): Boolean {
val test = when (prototype.strategy) { val test = when (prototype.strategy) {
Strategy.OR_BOTH -> orPredicates(levelAccessor, blockState, blockEntity) && orChildren(levelAccessor, blockState, blockEntity) Strategy.OR_BOTH -> orPredicates(levelAccessor, blockState, blockEntity) && orChildren(levelAccessor, blockState, blockEntity)
@ -137,7 +138,7 @@ class ShapedMultiblock(pos: BlockPos, factory: ShapedMultiblockFactory) : IMulti
if (test) { if (test) {
if (assignedBlockEntityLists.isNotEmpty()) { if (assignedBlockEntityLists.isNotEmpty()) {
val be1 = this.blockEntity val be1 = this.blockEntity
val be2 = blockEntity.value val be2 = blockEntity
if (be1 != be2) { if (be1 != be2) {
if (be1 != null) { if (be1 != null) {
@ -153,7 +154,7 @@ class ShapedMultiblock(pos: BlockPos, factory: ShapedMultiblockFactory) : IMulti
} }
if (assignedBlockStateLists.isNotEmpty() || assignedBlockLists.isNotEmpty()) { if (assignedBlockStateLists.isNotEmpty() || assignedBlockLists.isNotEmpty()) {
val state = blockState.value val state = blockState
val old = this.blockState val old = this.blockState
if (state != old) { if (state != old) {
@ -188,8 +189,8 @@ class ShapedMultiblock(pos: BlockPos, factory: ShapedMultiblockFactory) : IMulti
} }
fun test(levelAccessor: LevelAccessor): Boolean { fun test(levelAccessor: LevelAccessor): Boolean {
val blockEntity = lazy(LazyThreadSafetyMode.NONE) { levelAccessor.getBlockEntity(pos) } val blockEntity = levelAccessor.getBlockEntityNow(pos)
val blockState = lazy(LazyThreadSafetyMode.NONE) { levelAccessor.getBlockStateNow(pos) } val blockState = levelAccessor.getBlockStateNow(pos)
val status = test0(levelAccessor, blockState, blockEntity) val status = test0(levelAccessor, blockState, blockEntity)
val previous = this.status val previous = this.status

View File

@ -119,7 +119,7 @@ class ShapedMultiblockBuilder {
*/ */
fun block(block: Block): T { fun block(block: Block): T {
predicates.add { pos, access, blockState, blockEntity -> predicates.add { pos, access, blockState, blockEntity ->
blockState.value.block === block blockState.block === block
} }
return this as T return this as T
@ -130,7 +130,7 @@ class ShapedMultiblockBuilder {
*/ */
fun block(block: TagKey<Block>): T { fun block(block: TagKey<Block>): T {
predicates.add { pos, access, blockState, blockEntity -> predicates.add { pos, access, blockState, blockEntity ->
blockState.value.`is`(block) blockState.`is`(block)
} }
return this as T return this as T
@ -149,7 +149,7 @@ class ShapedMultiblockBuilder {
*/ */
fun block(state: BlockState): T { fun block(state: BlockState): T {
predicates.add { pos, access, blockState, blockEntity -> predicates.add { pos, access, blockState, blockEntity ->
blockState.value === state blockState === state
} }
return this as T return this as T