Fix cable shape cache not working

There are different BlockState objects for each color of cable :S
This commit is contained in:
Shadowfacts 2021-03-15 21:44:50 -04:00
parent 369dcebe1b
commit 47ff975449
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 14 additions and 9 deletions

View File

@ -2,7 +2,6 @@ package net.shadowfacts.phycon.block.cable
import net.fabricmc.fabric.api.`object`.builder.v1.block.FabricBlockSettings import net.fabricmc.fabric.api.`object`.builder.v1.block.FabricBlockSettings
import net.minecraft.block.* import net.minecraft.block.*
import net.minecraft.block.piston.PistonBehavior
import net.minecraft.entity.player.PlayerEntity import net.minecraft.entity.player.PlayerEntity
import net.minecraft.item.ItemPlacementContext import net.minecraft.item.ItemPlacementContext
import net.minecraft.state.StateManager import net.minecraft.state.StateManager
@ -53,19 +52,25 @@ class CableBlock(
Direction.WEST to createCuboidShape(0.0, 6.0, 6.0, 6.0, 10.0, 10.0), Direction.WEST to createCuboidShape(0.0, 6.0, 6.0, 6.0, 10.0, 10.0),
Direction.EAST to createCuboidShape(10.0, 6.0, 6.0, 16.0, 10.0, 10.0) Direction.EAST to createCuboidShape(10.0, 6.0, 6.0, 16.0, 10.0, 10.0)
) )
private val SHAPE_CACHE = mutableMapOf<BlockState, VoxelShape>() private val SHAPE_CACHE = Array<VoxelShape>(64) { key ->
val connectedSides = Direction.values().filterIndexed { index, _ ->
((key shr index) and 1) == 1
}
connectedSides.fold(CENTER_SHAPE) { acc, side ->
VoxelShapes.union(acc, SIDE_SHAPES[side])
}
}
val CONNECTIONS: Map<Direction, EnumProperty<CableConnection>> = Direction.values().associate { it to EnumProperty.of(it.name.toLowerCase(), CableConnection::class.java) } val CONNECTIONS: Map<Direction, EnumProperty<CableConnection>> = Direction.values().associate { it to EnumProperty.of(it.name.toLowerCase(), CableConnection::class.java) }
fun getShape(state: BlockState): VoxelShape { fun getShape(state: BlockState): VoxelShape {
return SHAPE_CACHE.getOrPut(state) { val key = Direction.values().foldIndexed(0) { i, acc, dir ->
var shape = CENTER_SHAPE if (state[CONNECTIONS[dir]] == CableConnection.ON) {
for ((side, prop) in CONNECTIONS) { acc or (1 shl i)
if (state[prop] == CableConnection.ON) { } else {
shape = VoxelShapes.union(shape, SIDE_SHAPES[side]) acc
} }
} }
return shape return SHAPE_CACHE[key]
}
} }
} }