PhysicalConnectivity/src/main/kotlin/net/shadowfacts/phycon/block/netswitch/SwitchBlock.kt

62 lines
2.0 KiB
Kotlin
Raw Normal View History

2021-02-28 18:48:39 +00:00
package net.shadowfacts.phycon.block.netswitch
2019-10-27 03:13:26 +00:00
import alexiil.mc.lib.attributes.AttributeList
import alexiil.mc.lib.attributes.AttributeProvider
import net.minecraft.block.BlockState
import net.minecraft.block.Material
2021-12-22 23:59:51 +00:00
import net.minecraft.block.entity.BlockEntity
import net.minecraft.block.entity.BlockEntityTicker
import net.minecraft.block.entity.BlockEntityType
2021-02-28 23:56:04 +00:00
import net.minecraft.sound.BlockSoundGroup
2019-10-27 03:13:26 +00:00
import net.minecraft.util.Identifier
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Direction
2019-10-27 03:13:26 +00:00
import net.minecraft.world.BlockView
import net.minecraft.world.World
import net.minecraft.world.WorldAccess
2019-10-27 03:13:26 +00:00
import net.shadowfacts.phycon.PhysicalConnectivity
2021-02-13 23:24:36 +00:00
import net.shadowfacts.phycon.api.Interface
import net.shadowfacts.phycon.api.NetworkComponentBlock
2019-10-27 03:13:26 +00:00
import net.shadowfacts.phycon.block.BlockWithEntity
import java.util.*
2019-10-27 03:13:26 +00:00
/**
* @author shadowfacts
*/
2021-02-28 23:56:04 +00:00
class SwitchBlock: BlockWithEntity<SwitchBlockEntity>(
Settings.of(Material.METAL)
.strength(1.5f)
.sounds(BlockSoundGroup.METAL)
),
2021-02-13 23:24:36 +00:00
NetworkComponentBlock,
AttributeProvider {
2019-10-27 03:13:26 +00:00
companion object {
val ID = Identifier(PhysicalConnectivity.MODID, "switch")
}
override fun getNetworkConnectedSides(state: BlockState, world: WorldAccess, pos: BlockPos): Collection<Direction> {
return EnumSet.allOf(Direction::class.java)
}
override fun getNetworkInterfaceForSide(side: Direction, state: BlockState, world: WorldAccess, pos: BlockPos): Interface? {
2021-02-13 23:24:36 +00:00
return getBlockEntity(world, pos)?.interfaces?.find { it.side == side }
}
2021-12-22 23:59:51 +00:00
override fun createBlockEntity(pos: BlockPos, state: BlockState) = SwitchBlockEntity(pos, state)
override fun <T: BlockEntity> getTicker(world: World, state: BlockState, type: BlockEntityType<T>): BlockEntityTicker<T>? {
return if (world.isClient) {
null
} else {
BlockEntityTicker { world, blockPos, blockState, blockEntity ->
(blockEntity as SwitchBlockEntity).tick()
}
}
}
2019-10-27 03:13:26 +00:00
override fun addAllAttributes(world: World, pos: BlockPos, state: BlockState, to: AttributeList<*>) {
to.offer(getBlockEntity(world, pos))
}
}