2014-03-23 10:19:47 +01:00
package resonantinduction.archaic.blocks
2014-05-14 07:32:00 +02:00
import codechicken.multipart.TileMultipart
2014-07-22 08:32:42 +02:00
import cpw.mods.fml.relauncher. { Side , SideOnly }
2014-07-19 01:48:40 +02:00
import net.minecraft.block.Block
2014-03-23 10:19:47 +01:00
import net.minecraft.block.material.Material
2014-07-14 20:46:49 +02:00
import net.minecraft.client.renderer.texture.IIconRegister
import net.minecraft.util.IIcon
2014-07-22 08:32:42 +02:00
import net.minecraft.world. { IBlockAccess , World }
2014-07-14 20:46:49 +02:00
import net.minecraftforge.common.util.ForgeDirection
2014-05-14 07:32:00 +02:00
import resonant.api.IRotatable
2014-07-14 20:46:49 +02:00
import resonant.content.prefab.RenderRotatedTexture
import resonant.content.spatial.block.SpatialBlock
import resonant.lib.content.prefab.TRotatable
2014-03-23 10:19:47 +01:00
import resonantinduction.core.Reference
2014-07-14 20:46:49 +02:00
import universalelectricity.core.transform.vector.Vector3
2014-03-23 10:19:47 +01:00
2014-07-22 08:32:42 +02:00
object TileTurntable {
var top : IIcon = null
2014-07-14 20:46:49 +02:00
}
2014-07-22 08:32:42 +02:00
class TileTurntable extends SpatialBlock ( Material . piston ) with TRotatable with RenderRotatedTexture {
textureName = "turntable_side"
tickRandomly = true
rotationMask = Integer . parseInt ( "111111" , 2 ) . toByte
2014-03-23 10:19:47 +01:00
2014-07-22 08:32:42 +02:00
override def tickRate ( par1World : World ) : Int = 5
2014-03-23 10:19:47 +01:00
2014-07-22 08:32:42 +02:00
@SideOnly ( Side . CLIENT )
override def registerIcons ( iconReg : IIconRegister ) {
super . registerIcons ( iconReg )
TileTurntable . top = iconReg . registerIcon ( Reference . prefix + "turntable" )
2014-05-14 07:32:00 +02:00
}
2014-03-23 10:19:47 +01:00
2014-07-22 08:32:42 +02:00
override def update ( ) {
updateTurntableState ( world , x , y , z )
2014-03-23 10:19:47 +01:00
}
2014-07-22 08:32:42 +02:00
@SideOnly ( Side . CLIENT )
override def getIcon ( access : IBlockAccess , side : Int ) : IIcon =
2014-07-14 20:46:49 +02:00
{
2014-07-22 08:32:42 +02:00
if ( side == metadata ) {
return TileTurntable . top
}
2014-07-14 20:46:49 +02:00
2014-07-22 08:32:42 +02:00
return getIcon
2014-03-23 10:19:47 +01:00
}
2014-07-22 08:32:42 +02:00
@SideOnly ( Side . CLIENT )
override def getIcon ( side : Int , meta : Int ) : IIcon =
2014-07-14 20:46:49 +02:00
{
2014-07-22 08:32:42 +02:00
if ( side == 1 ) {
return TileTurntable . top
}
return getIcon
}
override def onNeighborChanged ( block : Block ) {
scheduleTick ( 10 )
}
private def updateTurntableState ( world : World , x : Int , y : Int , z : Int ) {
if ( world . isBlockIndirectlyGettingPowered ( x , y , z ) ) {
try {
val facing : ForgeDirection = ForgeDirection . getOrientation ( world . getBlockMetadata ( x , y , z ) )
val position = new Vector3 ( x , y , z ) + facing
val tileEntity = position . getTileEntity ( world )
val block = position . getBlock ( world )
if ( ! ( tileEntity . isInstanceOf [ TileMultipart ] ) ) {
if ( tileEntity . isInstanceOf [ IRotatable ] ) {
val blockRotation = tileEntity . asInstanceOf [ IRotatable ] . getDirection
tileEntity . asInstanceOf [ IRotatable ] . setDirection ( blockRotation . getRotation ( facing . getOpposite ) )
}
else if ( block != null ) {
block . rotateBlock ( world , position . xi , position . yi , position . zi , facing . getOpposite )
}
world . markBlockForUpdate ( position . xi , position . yi , position . zi )
world . playSoundEffect ( x + 0.5D , y + 0.5D , z + 0.5D , "tile.piston.in" , 0.5F , world . rand . nextFloat * 0.15F + 0.6F )
}
}
catch {
case e : Exception =>
{
System . out . println ( "Error while rotating a block near " + x + "x " + y + "y " + z + "z " + ( if ( world != null && world . provider != null ) world . provider . dimensionId + "d" else "null:world" ) )
e . printStackTrace
}
}
2014-07-14 20:46:49 +02:00
}
2014-03-23 10:19:47 +01:00
}
2014-03-24 18:46:33 +01:00
}