Don't wrap blockstate and blockentity in lazy
This commit is contained in:
parent
35ae7ed88a
commit
604999ebe3
@ -39,6 +39,7 @@ import net.minecraft.world.level.Level
|
||||
import net.minecraft.world.level.LevelAccessor
|
||||
import net.minecraft.world.level.block.Block
|
||||
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.StateHolder
|
||||
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()
|
||||
}
|
||||
|
||||
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 ItemStack.isNotEmpty get() = !isEmpty
|
||||
|
||||
|
@ -11,7 +11,7 @@ import ru.dbotthepony.mc.otm.core.getBlockStateNow
|
||||
import ru.dbotthepony.mc.otm.core.math.plus
|
||||
|
||||
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 {
|
||||
return this
|
||||
@ -42,7 +42,7 @@ fun interface 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)
|
||||
}
|
||||
|
||||
@ -55,7 +55,7 @@ fun interface BlockPredicate {
|
||||
constructor(vararg nodes: 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) }
|
||||
}
|
||||
}
|
||||
@ -64,20 +64,20 @@ fun interface BlockPredicate {
|
||||
constructor(vararg nodes: 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) }
|
||||
}
|
||||
}
|
||||
|
||||
object Air : BlockPredicate {
|
||||
override fun test(pos: BlockPos, access: LevelAccessor, blockState: Lazy<BlockState>, blockEntity: Lazy<BlockEntity?>): Boolean {
|
||||
return blockState.value.isAir
|
||||
override fun test(pos: BlockPos, access: LevelAccessor, blockState: BlockState, blockEntity: BlockEntity?): Boolean {
|
||||
return blockState.isAir
|
||||
}
|
||||
}
|
||||
|
||||
object NotAir : BlockPredicate {
|
||||
override fun test(pos: BlockPos, access: LevelAccessor, blockState: Lazy<BlockState>, blockEntity: Lazy<BlockEntity?>): Boolean {
|
||||
return !blockState.value.isAir
|
||||
override fun test(pos: BlockPos, access: LevelAccessor, blockState: BlockState, blockEntity: BlockEntity?): Boolean {
|
||||
return !blockState.isAir
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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.collect.collect
|
||||
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.math.plus
|
||||
import ru.dbotthepony.mc.otm.core.registryName
|
||||
@ -92,7 +93,7 @@ class ShapedMultiblock(pos: BlockPos, factory: ShapedMultiblockFactory) : IMulti
|
||||
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 (lastSuccessfulPathPredicate != -1 && !prototype.predicate[lastSuccessfulPathPredicate].test(pos, levelAccessor, blockState, blockEntity)) {
|
||||
lastSuccessfulPathPredicate = -1
|
||||
@ -107,7 +108,7 @@ class ShapedMultiblock(pos: BlockPos, factory: ShapedMultiblockFactory) : IMulti
|
||||
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 (lastSuccessfulPathChildren != -1 && !children[lastSuccessfulPathChildren].test0(levelAccessor, blockState, blockEntity)) {
|
||||
lastSuccessfulPathChildren = -1
|
||||
@ -122,7 +123,7 @@ class ShapedMultiblock(pos: BlockPos, factory: ShapedMultiblockFactory) : IMulti
|
||||
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) {
|
||||
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 (assignedBlockEntityLists.isNotEmpty()) {
|
||||
val be1 = this.blockEntity
|
||||
val be2 = blockEntity.value
|
||||
val be2 = blockEntity
|
||||
|
||||
if (be1 != be2) {
|
||||
if (be1 != null) {
|
||||
@ -153,7 +154,7 @@ class ShapedMultiblock(pos: BlockPos, factory: ShapedMultiblockFactory) : IMulti
|
||||
}
|
||||
|
||||
if (assignedBlockStateLists.isNotEmpty() || assignedBlockLists.isNotEmpty()) {
|
||||
val state = blockState.value
|
||||
val state = blockState
|
||||
val old = this.blockState
|
||||
|
||||
if (state != old) {
|
||||
@ -188,8 +189,8 @@ class ShapedMultiblock(pos: BlockPos, factory: ShapedMultiblockFactory) : IMulti
|
||||
}
|
||||
|
||||
fun test(levelAccessor: LevelAccessor): Boolean {
|
||||
val blockEntity = lazy(LazyThreadSafetyMode.NONE) { levelAccessor.getBlockEntity(pos) }
|
||||
val blockState = lazy(LazyThreadSafetyMode.NONE) { levelAccessor.getBlockStateNow(pos) }
|
||||
val blockEntity = levelAccessor.getBlockEntityNow(pos)
|
||||
val blockState = levelAccessor.getBlockStateNow(pos)
|
||||
val status = test0(levelAccessor, blockState, blockEntity)
|
||||
|
||||
val previous = this.status
|
||||
|
@ -119,7 +119,7 @@ class ShapedMultiblockBuilder {
|
||||
*/
|
||||
fun block(block: Block): T {
|
||||
predicates.add { pos, access, blockState, blockEntity ->
|
||||
blockState.value.block === block
|
||||
blockState.block === block
|
||||
}
|
||||
|
||||
return this as T
|
||||
@ -130,7 +130,7 @@ class ShapedMultiblockBuilder {
|
||||
*/
|
||||
fun block(block: TagKey<Block>): T {
|
||||
predicates.add { pos, access, blockState, blockEntity ->
|
||||
blockState.value.`is`(block)
|
||||
blockState.`is`(block)
|
||||
}
|
||||
|
||||
return this as T
|
||||
@ -149,7 +149,7 @@ class ShapedMultiblockBuilder {
|
||||
*/
|
||||
fun block(state: BlockState): T {
|
||||
predicates.add { pos, access, blockState, blockEntity ->
|
||||
blockState.value === state
|
||||
blockState === state
|
||||
}
|
||||
|
||||
return this as T
|
||||
|
Loading…
Reference in New Issue
Block a user